📄 scanner.frame
字号:
using System;
using System.Drawing;
using System.IO;
using System.Collections;
using System.Text;
-->namespace
public class Token {
public int kind; // token kind
public int pos; // token position in the source text (starting at 0)
public int col; // token column (starting at 0)
public int line; // token line (starting at 1)
public string val; // token value
public Token next; // AW 2003-03-07 Tokens are kept in linked list
public Point Location {
get {
return new Point(line, col);
}
}
public Token () { }
public Token (int kind) { this.kind = kind; }
}
public class Buffer {
public const int eof = '\uffff';
static byte[] buf;
static int bufLen;
static int pos;
public static void Fill (string fileName) {
FileStream s = null;
try {
s = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Fill(s);
} catch (IOException) {
Console.WriteLine("--- Cannot open file {0}", fileName);
System.Environment.Exit(0);
} finally {
if (s != null) s.Close();
}
}
public static void Fill (Stream s) {
bufLen = (int) s.Length;
buf = new byte[bufLen];
s.Read(buf, 0, bufLen);
pos = 0;
}
public static int Read () {
if (pos < bufLen) return buf[pos++];
else return 0;
}
public static int Peek () {
if (pos < bufLen) return buf[pos];
else return 0;
}
/* AW 2003-03-10 moved this from ParserGen.cs */
public static string GetString (int beg, int end) {
StringBuilder s = new StringBuilder(64);
int oldPos = Buffer.Pos;
Buffer.Pos = beg;
while (beg < end) { s.Append((char)Buffer.Read()); beg++; }
Buffer.Pos = oldPos;
return s.ToString();
}
public static int Pos {
get { return pos; }
set {
if (value < 0) pos = 0;
else if (value >= bufLen) pos = bufLen;
else pos = value;
}
}
}
public class Scanner {
const char EOF = '\0';
const char EOL = '\n';
const char CR = '\n';
-->constants
-->declarations
static Token t; // current token
static char ch; // current input character
static int pos; // column number of current character
static int line; // line number of current character
static int lineStart; // start position of current line
static int oldEols; // EOLs that appeared in a comment;
static BitArray ignore; // set of characters to be ignored by the scanner
/* ML ----- begin */
static Token tokens; // the complete input token stream
static Token pt; // current peek token
static int peekCount = 0;
public static int PeekCount { get { return peekCount; } }
static void Init()
{
pos = -1; line = 1; lineStart = 0;
oldEols = 0;
NextCh();
-->initialization
/* AW 2003-03-07 fill token list */
tokens = new Token(); // first token is a dummy
Token node = tokens;
do {
node.next = NextToken();
node = node.next;
} while (node.kind != 0); /* AW: 0 => EOF */
t = pt = tokens;
}
public static void Init(String fileName) {
Buffer.Fill(fileName);
Init();
}
public static void Init(Stream s) {
Buffer.Fill(s);
Init();
}
static void NextCh() {
if (oldEols > 0) { ch = EOL; oldEols--; }
else {
ch = (char)Buffer.Read(); pos++;
// replace isolated '\r' by '\n' in order to make
// eol handling uniform across Windows, Unix and Mac
if (ch == '\r' && Buffer.Peek() != '\n') ch = EOL;
else if (ch > '\u007f') ch = '?';
if (ch == EOL) { line++; lineStart = pos + 1; }
}
}
-->comment
static void CheckLiteral() {
switch (t.val) {
-->literals
}
}
/* AW Scan() renamed to NextToken() */
static Token NextToken() {
while (ignore[ch]) NextCh();
-->scan1
t = new Token();
t.pos = pos; t.col = pos - lineStart + 1; t.line = line;
int state = start[ch];
StringBuilder buf = new StringBuilder(16);
buf.Append(ch); NextCh();
switch (state) {
case 0: { t.kind = noSym; goto done; } // NextCh already done
-->scan2
}
done:
t.val = buf.ToString();
return t;
}
/* AW 2003-03-07 get the next token, move on and synch peek token with current */
public static Token Scan () {
t = pt = t.next;
return t;
}
/* AW 2003-03-07 get the next token, ignore pragmas */
public static Token Peek () {
do { // skip pragmas while peeking
pt = pt.next;
} while (pt != null && pt.kind > maxT);
return pt;
}
/* AW 2003-03-11 to make sure peek start at current scan position */
public static void StartPeek () { pt = t; }
} // end Scanner
public delegate void ErrorCodeProc (int line, int col, int n);
public delegate void ErrorMsgProc (int line, int col, string msg);
public class Errors {
public static int count = 0; // number of errors detected
public static ErrorCodeProc SynErr = new ErrorCodeProc(DefaultCodeError); // syntactic errors
public static ErrorCodeProc SemErr = new ErrorCodeProc(DefaultCodeError); // semantic errors
public static ErrorMsgProc Error = new ErrorMsgProc(DefaultMsgError); // user defined string based errors
public static StringBuilder errorText = new StringBuilder();
public static void Exception (string s) {
Console.WriteLine(s);
System.Environment.Exit(0);
}
static void DefaultCodeError (int line, int col, int n) {
errorText.Append(String.Format("-- line {0} col {1}: error {2}", line, col, n));
errorText.Append("\n");
count++;
}
static void DefaultMsgError (int line, int col, string s) {
errorText.Append(String.Format("-- line {0} col {1}: {2}", line, col, s));
errorText.Append("\n");
count++;
}
} // Errors
$$$
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -