public virtual void OnStyleNeeded(StyleNeededEventArgs e) { if (StyleNeeded != null) { StyleNeeded(this, e); } }
protected virtual void DoHighlighting() { if (parser == null) { return; } //parse text ParseTree tree; try { tree = parser.Parse(Text); } catch (Exception ex) { Console.WriteLine(ex.Message); //oops... return; } //highlight errors if (tree.Status == ParseTreeStatus.Error) { ClearStyle(GetStyleIndexMask(new[] { WavyStyle })); foreach (var msg in tree.ParserMessages) { var loc = msg.Location; var place = new Place(loc.Column, loc.Line); var r = new Range(this, place, place); var f = r.GetFragment(@"[\S]"); f.SetStyle(WavyStyle); } return; } //highlight syntax ClearStyle(StyleIndex.All); foreach (var t in tree.Tokens) { var arg = new StyleNeededEventArgs(t); OnStyleNeeded(arg); if (arg.Cancel) { continue; } if (arg.Style != null) { GetTokenRange(t).SetStyle(arg.Style); continue; } switch (t.Terminal.GetType().Name) { case "KeyTerm": if ((t.Terminal.Flags & TermFlags.IsKeyword) != 0) //keywords are highlighted only { GetTokenRange(t).SetStyle(SyntaxHighlighter.KeywordStyle); } break; case "NumberLiteral": GetTokenRange(t).SetStyle(SyntaxHighlighter.NumberStyle); break; case "StringLiteral": GetTokenRange(t).SetStyle(SyntaxHighlighter.StringStyle); break; case "CommentTerminal": GetTokenRange(t).SetStyle(SyntaxHighlighter.CommentStyle); break; } } }