~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor.Highlighting/Match.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
//
27
27
 
28
28
using System;
 
29
using System.Linq;
29
30
using System.Text.RegularExpressions;
30
31
using System.Xml;
 
32
using System.Collections.Generic;
31
33
 
32
34
namespace Mono.TextEditor.Highlighting
33
35
{
60
62
                        return String.Format ("[Match: Color={0}, Pattern={1}]", Color, Pattern);
61
63
                }
62
64
 
63
 
                public virtual int TryMatch (string text, int matchOffset)
 
65
                protected readonly static int[] emptyMatch = new int[0];
 
66
                public virtual int[] TryMatch (string text, int matchOffset)
64
67
                {
65
 
                        
66
 
//                      System.Text.RegularExpressions.Match match = regex.Match (text, matchOffset);
67
68
                        string matchStr = text.Substring (matchOffset);
68
 
                        System.Text.RegularExpressions.Match match = regex.Match (matchStr);
69
 
                        
70
 
                        if (match.Success)
71
 
                                return match.Length;
72
 
                        return -1;
 
69
                        var match = regex.Match (matchStr);
 
70
                        if (match.Success) {
 
71
                                var result = new int[match.Groups.Count];
 
72
                                for (int i = 0; i < result.Length; i++) {
 
73
                                        result[i] = match.Groups[i].Length;
 
74
                                }
 
75
                                return result;
 
76
                        }
 
77
                        return emptyMatch;
73
78
                }
74
79
                
75
80
                public const string Node = "Match";
 
81
 
 
82
                public bool IsGroupMatch {
 
83
                        get {
 
84
                                return groups != null && string.IsNullOrEmpty (Color);
 
85
                        }
 
86
                }
 
87
 
 
88
                List<string> groups = null;
 
89
                public List<string> Groups {
 
90
                        get {
 
91
                                if (groups == null)
 
92
                                        groups = new List<string> ();
 
93
                                return groups;
 
94
                        }
 
95
                }
 
96
 
76
97
                public static Match Read (XmlReader reader)
77
98
                {
 
99
                        string expression = reader.GetAttribute ("expression");
 
100
                        if (!string.IsNullOrEmpty (expression)) {
 
101
                                var result = new Match ();
 
102
 
 
103
                                result.Pattern = "^" + expression;
 
104
                                result.regex   = new System.Text.RegularExpressions.Regex (result.Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
 
105
 
 
106
                                XmlReadHelper.ReadList (reader, Node, delegate () {
 
107
                                        switch (reader.LocalName) {
 
108
                                        case "Group":
 
109
                                                result.Groups.Add (reader.GetAttribute ("color"));
 
110
                                                return true;
 
111
                                        }
 
112
                                        return false;
 
113
                                });
 
114
 
 
115
                                return result;
 
116
                        }
 
117
 
78
118
                        string color   = reader.GetAttribute ("color");
79
119
                        string pattern = reader.ReadElementString ();
80
 
                        Match result   = pattern == "CSharpNumber" ? new CSharpNumberMatch () : new Match ();
81
 
                        result.Color   = color;
82
 
                        result.Pattern = "^" + pattern;
83
 
                        result.regex   = new System.Text.RegularExpressions.Regex (result.Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
84
 
                        return result;
 
120
                        Match result2   = pattern == "CSharpNumber" ? new CSharpNumberMatch () : new Match ();
 
121
                        result2.Color   = color;
 
122
                        result2.Pattern = "^" + pattern;
 
123
                        result2.regex   = new System.Text.RegularExpressions.Regex (result2.Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
 
124
                        return result2;
85
125
                }
86
126
        }
87
127
        
123
163
                        return true;
124
164
                }
125
165
                
126
 
                public override int TryMatch (string text, int matchOffset)
 
166
                public override int[] TryMatch (string text, int matchOffset)
127
167
                {
128
168
                        int i = matchOffset;
129
169
                        if (matchOffset + 1 < text.Length && text[matchOffset] == '0' && Char.ToUpper (text[matchOffset + 1]) == 'X') {
135
175
                                        i++;
136
176
                                }
137
177
                                ReadNonFloatEnd (text, ref i);
138
 
                                return i - matchOffset;
 
178
                                return new [] {i - matchOffset};
139
179
                        } else {
140
180
                                if (i >= text.Length || !Char.IsDigit (text[i]))
141
 
                                        return -1;
 
181
                                        return emptyMatch;
142
182
                                i++;
143
183
                                while (i < text.Length && Char.IsDigit (text[i]))
144
184
                                        i++;
145
185
                        }
146
186
                        if (ReadNonFloatEnd (text, ref i))
147
 
                                return i - matchOffset;
 
187
                        return new [] {i - matchOffset};
148
188
                        if (i < text.Length && text[i] == '.') {
149
189
                                i++;
150
 
                                if (i >= text.Length) 
151
 
                                        return (i - 1) - matchOffset;
152
 
                                if (!Char.IsDigit (text[i]))
153
 
                                        return -1;
 
190
                                if (i >= text.Length || !char.IsDigit (text[i])) 
 
191
                                        return new [] { (i - 1) - matchOffset };
154
192
                                i++;
155
193
                                while (i < text.Length && Char.IsDigit (text[i]))
156
194
                                        i++;
163
201
                                        i++;
164
202
                        }
165
203
                        ReadFloatEnd (text, ref i);
166
 
                        return i - matchOffset;
 
204
                        return new [] { (i - matchOffset) };
167
205
                }
168
206
        }
169
207