1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using ICSharpCode.AvalonEdit.Utils;
namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
{
[Serializable]
sealed class XmlHighlightingDefinition : IHighlightingDefinition2
{
public string Name { get; private set; }
public XmlHighlightingDefinition(XshdSyntaxDefinition xshd, IHighlightingDefinitionReferenceResolver resolver)
{
this.Name = xshd.Name;
// Create HighlightingRuleSet instances
var rnev = new RegisterNamedElementsVisitor(this);
xshd.AcceptElements(rnev);
// Assign MainRuleSet so that references can be resolved
foreach (XshdElement element in xshd.Elements) {
XshdRuleSet xrs = element as XshdRuleSet;
if (xrs != null && xrs.Name == null) {
if (MainRuleSet != null)
throw Error(element, "Duplicate main RuleSet. There must be only one nameless RuleSet!");
else
MainRuleSet = rnev.ruleSets[xrs];
}
}
if (MainRuleSet == null)
throw new HighlightingDefinitionInvalidException("Could not find main RuleSet.");
// Translate elements within the rulesets (resolving references and processing imports)
xshd.AcceptElements(new TranslateElementVisitor(this, rnev.ruleSets, resolver));
foreach (var p in xshd.Elements.OfType<XshdProperty>())
propDict.Add(p.Name, p.Value);
}
#region RegisterNamedElements
sealed class RegisterNamedElementsVisitor : IXshdVisitor
{
XmlHighlightingDefinition def;
internal readonly Dictionary<XshdRuleSet, HighlightingRuleSet> ruleSets
= new Dictionary<XshdRuleSet, HighlightingRuleSet>();
public RegisterNamedElementsVisitor(XmlHighlightingDefinition def)
{
this.def = def;
}
public object VisitRuleSet(XshdRuleSet ruleSet)
{
HighlightingRuleSet hrs = new HighlightingRuleSet();
ruleSets.Add(ruleSet, hrs);
if (ruleSet.Name != null) {
if (ruleSet.Name.Length == 0)
throw Error(ruleSet, "Name must not be the empty string");
if (def.ruleSetDict.ContainsKey(ruleSet.Name))
throw Error(ruleSet, "Duplicate rule set name '" + ruleSet.Name + "'.");
def.ruleSetDict.Add(ruleSet.Name, hrs);
}
ruleSet.AcceptElements(this);
return null;
}
public object VisitColor(XshdColor color)
{
if (color.Name != null) {
if (color.Name.Length == 0)
throw Error(color, "Name must not be the empty string");
if (def.colorDict.ContainsKey(color.Name))
throw Error(color, "Duplicate color name '" + color.Name + "'.");
def.colorDict.Add(color.Name, new HighlightingColor());
}
return null;
}
public object VisitKeywords(XshdKeywords keywords)
{
return keywords.ColorReference.AcceptVisitor(this);
}
public object VisitSpan(XshdSpan span)
{
span.BeginColorReference.AcceptVisitor(this);
span.SpanColorReference.AcceptVisitor(this);
span.EndColorReference.AcceptVisitor(this);
return span.RuleSetReference.AcceptVisitor(this);
}
public object VisitImport(XshdImport import)
{
return import.RuleSetReference.AcceptVisitor(this);
}
public object VisitRule(XshdRule rule)
{
return rule.ColorReference.AcceptVisitor(this);
}
}
#endregion
#region TranslateElements
sealed class TranslateElementVisitor : IXshdVisitor
{
readonly XmlHighlightingDefinition def;
readonly Dictionary<XshdRuleSet, HighlightingRuleSet> ruleSetDict;
readonly Dictionary<HighlightingRuleSet, XshdRuleSet> reverseRuleSetDict;
readonly IHighlightingDefinitionReferenceResolver resolver;
HashSet<XshdRuleSet> processingStartedRuleSets = new HashSet<XshdRuleSet>();
HashSet<XshdRuleSet> processedRuleSets = new HashSet<XshdRuleSet>();
bool ignoreCase;
public TranslateElementVisitor(XmlHighlightingDefinition def, Dictionary<XshdRuleSet, HighlightingRuleSet> ruleSetDict, IHighlightingDefinitionReferenceResolver resolver)
{
Debug.Assert(def != null);
Debug.Assert(ruleSetDict != null);
this.def = def;
this.ruleSetDict = ruleSetDict;
this.resolver = resolver;
reverseRuleSetDict = new Dictionary<HighlightingRuleSet, XshdRuleSet>();
foreach (var pair in ruleSetDict) {
reverseRuleSetDict.Add(pair.Value, pair.Key);
}
}
public object VisitRuleSet(XshdRuleSet ruleSet)
{
HighlightingRuleSet rs = ruleSetDict[ruleSet];
if (processedRuleSets.Contains(ruleSet))
return rs;
if (!processingStartedRuleSets.Add(ruleSet))
throw Error(ruleSet, "RuleSet cannot be processed because it contains cyclic <Import>");
bool oldIgnoreCase = ignoreCase;
if (ruleSet.IgnoreCase != null)
ignoreCase = ruleSet.IgnoreCase.Value;
rs.Name = ruleSet.Name;
foreach (XshdElement element in ruleSet.Elements) {
object o = element.AcceptVisitor(this);
HighlightingRuleSet elementRuleSet = o as HighlightingRuleSet;
if (elementRuleSet != null) {
Merge(rs, elementRuleSet);
} else {
HighlightingSpan span = o as HighlightingSpan;
if (span != null) {
rs.Spans.Add(span);
} else {
HighlightingRule elementRule = o as HighlightingRule;
if (elementRule != null) {
rs.Rules.Add(elementRule);
}
}
}
}
ignoreCase = oldIgnoreCase;
processedRuleSets.Add(ruleSet);
return rs;
}
static void Merge(HighlightingRuleSet target, HighlightingRuleSet source)
{
target.Rules.AddRange(source.Rules);
target.Spans.AddRange(source.Spans);
}
public object VisitColor(XshdColor color)
{
HighlightingColor c;
if (color.Name != null)
c = def.colorDict[color.Name];
else if (color.Foreground == null && color.FontStyle == null && color.FontWeight == null)
return null;
else
c = new HighlightingColor();
c.Name = color.Name;
c.Foreground = color.Foreground;
c.Background = color.Background;
c.FontStyle = color.FontStyle;
c.FontWeight = color.FontWeight;
return c;
}
public object VisitKeywords(XshdKeywords keywords)
{
if (keywords.Words.Count == 0)
return Error(keywords, "Keyword group must not be empty.");
foreach (string keyword in keywords.Words) {
if (string.IsNullOrEmpty(keyword))
throw Error(keywords, "Cannot use empty string as keyword");
}
StringBuilder keyWordRegex = new StringBuilder();
// We can use "\b" only where the keyword starts/ends with a letter or digit, otherwise we don't
// highlight correctly. (example: ILAsm-Mode.xshd with ".maxstack" keyword)
if (keywords.Words.All(IsSimpleWord)) {
keyWordRegex.Append(@"\b(?>");
// (?> = atomic group
// atomic groups increase matching performance, but we
// must ensure that the keywords are sorted correctly.
// "\b(?>in|int)\b" does not match "int" because the atomic group captures "in".
// To solve this, we are sorting the keywords by descending length.
int i = 0;
foreach (string keyword in keywords.Words.OrderByDescending(w=>w.Length)) {
if (i++ > 0)
keyWordRegex.Append('|');
keyWordRegex.Append(Regex.Escape(keyword));
}
keyWordRegex.Append(@")\b");
} else {
keyWordRegex.Append('(');
int i = 0;
foreach (string keyword in keywords.Words) {
if (i++ > 0)
keyWordRegex.Append('|');
if (char.IsLetterOrDigit(keyword[0]))
keyWordRegex.Append(@"\b");
keyWordRegex.Append(Regex.Escape(keyword));
if (char.IsLetterOrDigit(keyword[keyword.Length - 1]))
keyWordRegex.Append(@"\b");
}
keyWordRegex.Append(')');
}
return new HighlightingRule {
Color = GetColor(keywords, keywords.ColorReference),
Regex = CreateRegex(keywords, keyWordRegex.ToString(), XshdRegexType.Default)
};
}
static bool IsSimpleWord(string word)
{
return char.IsLetterOrDigit(word[0]) && char.IsLetterOrDigit(word, word.Length - 1);
}
Regex CreateRegex(XshdElement position, string regex, XshdRegexType regexType)
{
if (regex == null)
throw Error(position, "Regex missing");
RegexOptions options = RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture;
if (regexType == XshdRegexType.IgnorePatternWhitespace)
options |= RegexOptions.IgnorePatternWhitespace;
if (ignoreCase)
options |= RegexOptions.IgnoreCase;
try {
return new Regex(regex, options);
} catch (ArgumentException ex) {
throw Error(position, ex.Message);
}
}
HighlightingColor GetColor(XshdElement position, XshdReference<XshdColor> colorReference)
{
if (colorReference.InlineElement != null) {
return (HighlightingColor)colorReference.InlineElement.AcceptVisitor(this);
} else if (colorReference.ReferencedElement != null) {
IHighlightingDefinition definition = GetDefinition(position, colorReference.ReferencedDefinition);
HighlightingColor color = definition.GetNamedColor(colorReference.ReferencedElement);
if (color == null)
throw Error(position, "Could not find color named '" + colorReference.ReferencedElement + "'.");
return color;
} else {
return null;
}
}
IHighlightingDefinition GetDefinition(XshdElement position, string definitionName)
{
if (definitionName == null)
return def;
if (resolver == null)
throw Error(position, "Resolving references to other syntax definitions is not possible because the IHighlightingDefinitionReferenceResolver is null.");
IHighlightingDefinition d = resolver.GetDefinition(definitionName);
if (d == null)
throw Error(position, "Could not find definition with name '" + definitionName + "'.");
return d;
}
HighlightingRuleSet GetRuleSet(XshdElement position, XshdReference<XshdRuleSet> ruleSetReference)
{
if (ruleSetReference.InlineElement != null) {
return (HighlightingRuleSet)ruleSetReference.InlineElement.AcceptVisitor(this);
} else if (ruleSetReference.ReferencedElement != null) {
IHighlightingDefinition definition = GetDefinition(position, ruleSetReference.ReferencedDefinition);
HighlightingRuleSet ruleSet = definition.GetNamedRuleSet(ruleSetReference.ReferencedElement);
if (ruleSet == null)
throw Error(position, "Could not find rule set named '" + ruleSetReference.ReferencedElement + "'.");
return ruleSet;
} else {
return null;
}
}
public object VisitSpan(XshdSpan span)
{
string endRegex = span.EndRegex;
if (string.IsNullOrEmpty(span.BeginRegex) && string.IsNullOrEmpty(span.EndRegex))
throw Error(span, "Span has no start/end regex.");
if (!span.Multiline) {
if (endRegex == null)
endRegex = "$";
else if (span.EndRegexType == XshdRegexType.IgnorePatternWhitespace)
endRegex = "($|" + endRegex + "\n)";
else
endRegex = "($|" + endRegex + ")";
}
HighlightingColor wholeSpanColor = GetColor(span, span.SpanColorReference);
return new HighlightingSpan {
StartExpression = CreateRegex(span, span.BeginRegex, span.BeginRegexType),
EndExpression = CreateRegex(span, endRegex, span.EndRegexType),
RuleSet = GetRuleSet(span, span.RuleSetReference),
StartColor = GetColor(span, span.BeginColorReference),
SpanColor = wholeSpanColor,
EndColor = GetColor(span, span.EndColorReference),
SpanColorIncludesStart = true,
SpanColorIncludesEnd = true
};
}
public object VisitImport(XshdImport import)
{
HighlightingRuleSet hrs = GetRuleSet(import, import.RuleSetReference);
XshdRuleSet inputRuleSet;
if (reverseRuleSetDict.TryGetValue(hrs, out inputRuleSet)) {
// ensure the ruleset is processed before importing its members
if (VisitRuleSet(inputRuleSet) != hrs)
Debug.Fail("this shouldn't happen");
}
return hrs;
}
public object VisitRule(XshdRule rule)
{
return new HighlightingRule {
Color = GetColor(rule, rule.ColorReference),
Regex = CreateRegex(rule, rule.Regex, rule.RegexType)
};
}
}
#endregion
static Exception Error(XshdElement element, string message)
{
if (element.LineNumber > 0)
return new HighlightingDefinitionInvalidException(
"Error at line " + element.LineNumber + ":\n" + message);
else
return new HighlightingDefinitionInvalidException(message);
}
Dictionary<string, HighlightingRuleSet> ruleSetDict = new Dictionary<string, HighlightingRuleSet>();
Dictionary<string, HighlightingColor> colorDict = new Dictionary<string, HighlightingColor>();
[OptionalField]
Dictionary<string, string> propDict = new Dictionary<string, string>();
public HighlightingRuleSet MainRuleSet { get; private set; }
public HighlightingRuleSet GetNamedRuleSet(string name)
{
if (string.IsNullOrEmpty(name))
return MainRuleSet;
HighlightingRuleSet r;
if (ruleSetDict.TryGetValue(name, out r))
return r;
else
return null;
}
public HighlightingColor GetNamedColor(string name)
{
HighlightingColor c;
if (colorDict.TryGetValue(name, out c))
return c;
else
return null;
}
public IEnumerable<HighlightingColor> NamedHighlightingColors {
get {
return colorDict.Values;
}
}
public override string ToString()
{
return this.Name;
}
public IDictionary<string, string> Properties {
get {
return propDict;
}
}
}
}
|