14 using System.Collections.Generic;
20 ScannerData _scannerData;
21 public const int DefaultTabWidth = 8;
22 private int _nextNewLinePosition = -1;
25 _scannerData = scannerData;
29 public void SetText(
string text,
int offset,
bool keepLineNumbering) {
32 var line = keepLineNumbering ? _location.Line + 1 : 0;
34 _nextNewLinePosition = text.IndexOfAny(_scannerData.LineTerminatorsArray, offset);
37 #region ISourceStream Members
38 public string Text {
get;
internal set;}
41 [System.Diagnostics.DebuggerStepThrough]
42 get {
return _location; }
45 PreviewPosition = _location.Position;
49 public int PreviewPosition {
get; set; }
51 public int TabWidth {
get; set; }
53 public char PreviewChar {
54 [System.Diagnostics.DebuggerStepThrough]
56 if (PreviewPosition >=
Text.Length)
return '\0';
57 return Text[PreviewPosition];
61 public char NextPreviewChar {
62 [System.Diagnostics.DebuggerStepThrough]
64 if (PreviewPosition + 1 >=
Text.Length)
return '\0';
65 return Text[PreviewPosition + 1];
70 PreviewPosition = Location.Position;
75 var compType = ignoreCase ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture;
76 int cmp = string.Compare(
Text, PreviewPosition, symbol, 0, symbol.Length, compType);
88 var tokenText = GetPreviewText();
89 return new Token(terminal, this.Location, tokenText, tokenText);
92 var tokenText = GetPreviewText();
93 return new Token(terminal, this.Location, tokenText, value);
96 if (args != null && args.Length > 0)
97 message = string.Format(message, args);
98 return new Token(_scannerData.Language.Grammar.SyntaxError, Location, GetPreviewText(), message);
102 [System.Diagnostics.DebuggerStepThrough]
104 return PreviewPosition >= Text.Length;
109 private string GetPreviewText() {
110 var until = PreviewPosition;
112 if (until >
Text.Length) until = Text.Length;
113 string text = Text.Substring(_location.Position, until - _location.Position);
121 if (Location.Position + 20 <
Text.Length)
131 #region Location calculations
132 private static char[] _tab_arr = {
'\t' };
135 if (_location.Position == PreviewPosition)
return;
136 if (PreviewPosition >
Text.Length)
137 PreviewPosition = Text.Length;
140 if (PreviewPosition <= _nextNewLinePosition || _nextNewLinePosition < 0) {
141 _location.Column += PreviewPosition - _location.Position;
142 _location.Position = PreviewPosition;
147 int lineStart = _nextNewLinePosition;
149 CountCharsInText(
Text, _scannerData.LineTerminatorsArray, lineStart + 1, PreviewPosition - 1, ref nlCount, ref lineStart);
150 _location.Line += nlCount;
156 CountCharsInText(
Text, _tab_arr, lineStart, PreviewPosition - 1, ref tabCount, ref dummy);
159 _location.Position = PreviewPosition;
160 _location.Column = PreviewPosition - lineStart - 1;
162 _location.Column += (TabWidth - 1) * tabCount;
165 _nextNewLinePosition = Text.IndexOfAny(_scannerData.LineTerminatorsArray, PreviewPosition);
168 private static void CountCharsInText(
string text,
char[] chars,
int from,
int until, ref
int count, ref
int lastCharOccurrencePosition) {
170 if (from > until)
return;
171 if (until >= text.Length) until = text.Length - 1;
173 int next = text.IndexOfAny(chars, from, until - from + 1);
174 if (next < 0)
return;
177 bool isCRLF = (text[next] ==
'\n' && next > 0 && text[next - 1] ==
'\r');
180 lastCharOccurrencePosition = next;
static string LabelSrcHaveMore
Looks up a localized string similar to ....
static string LabelEofMark
Looks up a localized string similar to (EOF).
void MoveLocationToPreviewPosition()
A strongly-typed resource class, for looking up localized strings, etc.
void ResetPreviewPosition()
override string ToString()
Token CreateToken(Terminal terminal)
Creates a new token based on current preview position.
void SetText(string text, int offset, bool keepLineNumbering)
Token CreateErrorToken(string message, params object[] args)
Creates error token with custom error message as its Value.
SourceStream(ScannerData scannerData, int tabWidth)
Token CreateToken(Terminal terminal, object value)
Creates a new token based on current preview position and sets its Value field.
Interface for Terminals to access the source stream and produce tokens.
bool MatchSymbol(string symbol, bool ignoreCase)
Tries to match the symbol with the text at current preview position.
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
static string MsgSrcPosToString
Looks up a localized string similar to "[{0}], at {1}.