4
using System.Collections;
10
public int kind; // token kind
11
public int pos; // token position in the source text (starting at 0)
12
public int col; // token column (starting at 0)
13
public int line; // token line (starting at 1)
14
public string val; // token value
15
public Token next; // AW 2003-03-07 Tokens are kept in linked list
17
public Point Location {
19
return new Point(line, col);
24
public Token (int kind) { this.kind = kind; }
28
public const int eof = '\uffff';
34
public static void Fill (string fileName) {
37
s = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
39
} catch (IOException) {
40
Console.WriteLine("--- Cannot open file {0}", fileName);
41
System.Environment.Exit(0);
43
if (s != null) s.Close();
47
public static void Fill (Stream s) {
48
bufLen = (int) s.Length;
49
buf = new byte[bufLen];
50
s.Read(buf, 0, bufLen);
54
public static int Read () {
55
if (pos < bufLen) return buf[pos++];
59
public static int Peek () {
60
if (pos < bufLen) return buf[pos];
64
/* AW 2003-03-10 moved this from ParserGen.cs */
65
public static string GetString (int beg, int end) {
66
StringBuilder s = new StringBuilder(64);
67
int oldPos = Buffer.Pos;
69
while (beg < end) { s.Append((char)Buffer.Read()); beg++; }
74
public static int Pos {
77
if (value < 0) pos = 0;
78
else if (value >= bufLen) pos = bufLen;
84
public class Scanner {
85
const char EOF = '\0';
86
const char EOL = '\n';
93
static Token t; // current token
94
static char ch; // current input character
95
static int pos; // column number of current character
96
static int line; // line number of current character
97
static int lineStart; // start position of current line
98
static int oldEols; // EOLs that appeared in a comment;
99
static BitArray ignore; // set of characters to be ignored by the scanner
102
static Token tokens; // the complete input token stream
103
static Token pt; // current peek token
105
static int peekCount = 0;
107
public static int PeekCount { get { return peekCount; } }
111
pos = -1; line = 1; lineStart = 0;
116
/* AW 2003-03-07 fill token list */
117
tokens = new Token(); // first token is a dummy
120
node.next = NextToken();
122
} while (node.kind != 0); /* AW: 0 => EOF */
126
public static void Init(String fileName) {
127
Buffer.Fill(fileName);
131
public static void Init(Stream s) {
136
static void NextCh() {
137
if (oldEols > 0) { ch = EOL; oldEols--; }
139
ch = (char)Buffer.Read(); pos++;
140
// replace isolated '\r' by '\n' in order to make
141
// eol handling uniform across Windows, Unix and Mac
142
if (ch == '\r' && Buffer.Peek() != '\n') ch = EOL;
143
else if (ch > '\u007f') ch = '?';
144
if (ch == EOL) { line++; lineStart = pos + 1; }
150
static void CheckLiteral() {
156
/* AW Scan() renamed to NextToken() */
157
static Token NextToken() {
158
while (ignore[ch]) NextCh();
161
t.pos = pos; t.col = pos - lineStart + 1; t.line = line;
162
int state = start[ch];
163
StringBuilder buf = new StringBuilder(16);
164
buf.Append(ch); NextCh();
167
case 0: { t.kind = noSym; goto done; } // NextCh already done
171
t.val = buf.ToString();
175
/* AW 2003-03-07 get the next token, move on and synch peek token with current */
176
public static Token Scan () {
181
/* AW 2003-03-07 get the next token, ignore pragmas */
182
public static Token Peek () {
183
do { // skip pragmas while peeking
185
} while (pt != null && pt.kind > maxT);
189
/* AW 2003-03-11 to make sure peek start at current scan position */
190
public static void StartPeek () { pt = t; }
194
public delegate void ErrorCodeProc (int line, int col, int n);
195
public delegate void ErrorMsgProc (int line, int col, string msg);
197
public class Errors {
198
public static int count = 0; // number of errors detected
199
public static ErrorCodeProc SynErr = new ErrorCodeProc(DefaultCodeError); // syntactic errors
200
public static ErrorCodeProc SemErr = new ErrorCodeProc(DefaultCodeError); // semantic errors
201
public static ErrorMsgProc Error = new ErrorMsgProc(DefaultMsgError); // user defined string based errors
202
public static StringBuilder errorText = new StringBuilder();
204
public static void Exception (string s) {
205
Console.WriteLine(s);
206
System.Environment.Exit(0);
209
static void DefaultCodeError (int line, int col, int n) {
210
errorText.Append(String.Format("-- line {0} col {1}: error {2}", line, col, n));
211
errorText.Append("\n");
215
static void DefaultMsgError (int line, int col, string s) {
216
errorText.Append(String.Format("-- line {0} col {1}: {2}", line, col, s));
217
errorText.Append("\n");