14 using System.Collections.Generic;
17 using System.Threading;
21 namespace Irony.GrammarExplorer {
37 public readonly
int Min, Max;
43 return other.Min == Min && other.Max == Max;
54 if (tree == null)
return;
83 Adapter.AddView(
this);
90 _wantsColorize =
true;
99 if (
string.IsNullOrEmpty(newText))
101 Adapter.SetNewText(newText);
110 if (oldData != null && oldData.Tree != null) {
111 DetectAlreadyColoredTokens(oldData.ColoredTokens, _data.Tree.SourceText.Length - oldData.Tree.SourceText.Length);
113 _wantsColorize =
true;
119 public bool WantsColorize {
120 get {
return _wantsColorize; }
124 if (!_wantsColorize)
return;
125 int colorizing = Interlocked.Exchange(ref _colorizing, 1);
126 if (colorizing != 0)
return;
127 _invoker.InvokeOnUIThread(Colorize);
129 private void Colorize() {
135 tokensToColor = ExtractTokensInRange(data.NotColoredTokens, range.Min, range.Max);
137 if (ColorizeTokens != null && tokensToColor != null && tokensToColor.Count > 0) {
138 data.ColoredTokens.AddRange(tokensToColor);
139 ColorizeEventArgs args =
new ColorizeEventArgs(tokensToColor);
140 ColorizeTokens(
this, args);
143 _wantsColorize =
false;
147 private void DetectAlreadyColoredTokens(
TokenList oldColoredTokens,
int shift) {
148 foreach (
Token oldColored
in oldColoredTokens) {
151 if (FindMatchingToken(_data.NotColoredTokens, oldColored, 0, out index, out newColored) ||
152 FindMatchingToken(_data.NotColoredTokens, oldColored, shift, out index, out newColored)) {
153 _data.NotColoredTokens.RemoveAt(index);
154 _data.ColoredTokens.Add(newColored);
161 #region token utilities
162 private bool FindMatchingToken(
TokenList inTokens,
Token token,
int shift, out
int index, out
Token result) {
165 result = inTokens[index];
166 if (TokensMatch(token, result, shift))
return true;
182 for (
int i = tokens.Count - 1; i >= 0; i--) {
184 if (tkn.Location.Position > until || (tkn.Location.Position + tkn.Length < from))
continue;
193 if (data == null)
return null;
194 return GetTokensInRange(data.
Tree.
Tokens, from, until);
198 int fromIndex = LocateToken(tokens, from);
199 int untilIndex = LocateToken(tokens, until);
200 if (fromIndex < 0) fromIndex = 0;
201 if (untilIndex >= tokens.Count) untilIndex = tokens.Count - 1;
202 for (
int i = fromIndex; i <= untilIndex; i++) {
203 result.Add(tokens[i]);
210 if (tokens == null || tokens.Count == 0)
return -1;
211 var lastToken = tokens[tokens.Count - 1];
212 var lastTokenEnd = lastToken.Location.Position + lastToken.Length;
213 if (position < tokens[0].Location.Position || position > lastTokenEnd)
return -1;
214 return LocateTokenExt(tokens, position, 0, tokens.Count - 1);
216 private int LocateTokenExt(
TokenList tokens,
int position,
int fromIndex,
int untilIndex) {
217 if (fromIndex + 1 >= untilIndex)
return fromIndex;
218 int midIndex = (fromIndex + untilIndex) / 2;
219 Token middleToken = tokens[midIndex];
221 return LocateTokenExt(tokens, position, midIndex, untilIndex);
223 return LocateTokenExt(tokens, position, fromIndex, midIndex);
EventHandler< ColorizeEventArgs > ColorizeTokens
int LocateToken(TokenList tokens, int position)
TokenList ExtractTokensInRange(TokenList tokens, int from, int until)
readonly TokenList Tokens
void UpdateParsedSource(ParseTree newTree)
delegate void ColorizeMethod()
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
EditorViewAdapter(EditorAdapter adapter, IUIThreadInvoker invoker)
string Text
Gets the text associated with this token.
TokenList GetTokensInRange(TokenList tokens, int from, int until)
readonly SourceLocation Location
Location in the source code.
ViewRange(int min, int max)
Terminal Terminal
Gets the terminal.
TokenList GetTokensInRange(int from, int until)
bool TokensMatch(Token x, Token y, int shift)
ColorizeEventArgs(TokenList tokens)
bool Equals(ViewRange other)
readonly EditorAdapter Adapter
void SetNewText(string newText)
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Tokens
Summary Canonical example of MPLEX automaton
readonly TokenList Tokens
void SetViewRange(int min, int max)