public class Scanner
const char EOF = '\u0080';
// return this for end of file const char CR = '\r'; const char LF = '\n'; |
static TextReader input; static TextWriter output; static char ch; // lookahead character (= next (unprocessed) character in input stream) static int line, col; // line and column number of ch in input stream |
/* Initializes the scanner. */ public static void Init (TextReader r) { Init(r, Console.Out); } |
public static void Init (TextReader r, TextWriter w) { input = r; output = w; line = 1; col = 0; NextCh(); // read 1st char into ch, increment col to 1 } |
/* Reads next character from input stream into ch. * Keeps pos, line and col in sync with reading position. */ static void NextCh () { try { ch = (char) input.Read(); switch (ch) { case CR: ch = (char) input.Read(); // skip CR line++; col = 0; break; case LF: line++; col = 0; break; case '\uffff': // read returns -1 at end of file ch = EOF; break; default: col++; break; } } catch (IOException) { ch = EOF; } } |
/* Returns next token. * To be used by parser. */ public static Token Next () { // classe TOKEN /*---------------------------------*/ /*----- insert your code here -----*/ /*---------------------------------*/ } /* Error handling for the scanner. * Prints error message to output. */ static void Error (string msg) { // leave this here for scanner testing if (output != null) output.WriteLine("-- line {0}, col {1}: {2}", line, col, msg); else Parser.Errors.Error(msg); } |