void Init() { pos = -1; line = 1; col = 0; oldEols = 0; NextCh(); if (ch == 0xEF) { // check optional byte order mark for UTF-8 NextCh(); int ch1 = ch; NextCh(); int ch2 = ch; if (ch1 != 0xBB || ch2 != 0xBF) { throw new FatalError(String.Format("illegal byte order mark: EF {0,2:X} {1,2:X}", ch1, ch2)); } buffer = new UTF8Buffer(buffer); col = 0; NextCh(); } pt = tokens = new Token(); // first token is a dummy }
Token NextToken() { while (ch == ' ' || ch >= 9 && ch <= 10 || ch == 13 ) NextCh(); if (ch == '/' && Comment0() ||ch == '/' && Comment1()) return NextToken(); int recKind = noSym; int recEnd = pos; t = new Token(); t.pos = pos; t.col = col; t.line = line; int state; if (!start.TryGetValue(ch, out state)) state = 0; tlen = 0; AddCh(); switch (state) { case -1: { t.kind = eofSym; break; } // NextCh already done case 0: { if (recKind != noSym) { tlen = recEnd - t.pos; SetScannerBehindT(); } t.kind = recKind; break; } // NextCh already done case 1: recEnd = pos; recKind = 1; if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z' || ch == '_' || ch >= 'a' && ch <= 'z') {AddCh(); goto case 1;} else {t.kind = 1; t.val = new String(tval, 0, tlen); CheckLiteral(); return t;} case 2: recEnd = pos; recKind = 2; if (ch >= '0' && ch <= '9') {AddCh(); goto case 2;} else {t.kind = 2; break;} case 3: {t.kind = 3; break;} case 4: {t.kind = 4; break;} case 5: if (ch <= 9 || ch >= 11 && ch <= 12 || ch >= 14 && ch <= '&' || ch >= '(' && ch <= '[' || ch >= ']' && ch <= 65535) {AddCh(); goto case 6;} else if (ch == 92) {AddCh(); goto case 7;} else {goto case 0;} case 6: if (ch == 39) {AddCh(); goto case 9;} else {goto case 0;} case 7: if (ch >= ' ' && ch <= '~') {AddCh(); goto case 8;} else {goto case 0;} case 8: if (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 8;} else if (ch == 39) {AddCh(); goto case 9;} else {goto case 0;} case 9: {t.kind = 5; break;} case 10: recEnd = pos; recKind = 50; if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z' || ch == '_' || ch >= 'a' && ch <= 'z') {AddCh(); goto case 10;} else {t.kind = 50; break;} case 11: recEnd = pos; recKind = 51; if (ch >= '-' && ch <= ':' || ch >= 'A' && ch <= 'Z' || ch == 92 || ch == '_' || ch >= 'a' && ch <= 'z') {AddCh(); goto case 11;} else {t.kind = 51; break;} case 12: if (ch <= 9 || ch >= 11 && ch <= 12 || ch >= 14 && ch <= '!' || ch >= '#' && ch <= '[' || ch >= ']' && ch <= 65535) {AddCh(); goto case 12;} else if (ch == 10 || ch == 13) {AddCh(); goto case 4;} else if (ch == '"') {AddCh(); goto case 3;} else if (ch == 92) {AddCh(); goto case 14;} else {goto case 0;} case 13: recEnd = pos; recKind = 50; if (ch >= '0' && ch <= '9') {AddCh(); goto case 10;} else if (ch >= 'A' && ch <= 'Z' || ch == '_' || ch >= 'a' && ch <= 'z') {AddCh(); goto case 15;} else {t.kind = 50; break;} case 14: if (ch >= ' ' && ch <= '~') {AddCh(); goto case 12;} else {goto case 0;} case 15: recEnd = pos; recKind = 50; if (ch >= '0' && ch <= '9') {AddCh(); goto case 10;} else if (ch >= 'A' && ch <= 'Z' || ch == '_' || ch >= 'a' && ch <= 'z') {AddCh(); goto case 15;} else if (ch == '=') {AddCh(); goto case 11;} else {t.kind = 50; break;} case 16: if (ch == 'o') {AddCh(); goto case 17;} else {goto case 0;} case 17: if (ch == 'p') {AddCh(); goto case 18;} else {goto case 0;} case 18: if (ch == 'y') {AddCh(); goto case 19;} else {goto case 0;} case 19: if (ch == ']') {AddCh(); goto case 20;} else {goto case 0;} case 20: {t.kind = 6; break;} case 21: if (ch == 'c') {AddCh(); goto case 22;} else {goto case 0;} case 22: if (ch == 'o') {AddCh(); goto case 23;} else {goto case 0;} case 23: if (ch == 'p') {AddCh(); goto case 24;} else {goto case 0;} case 24: if (ch == 'y') {AddCh(); goto case 25;} else {goto case 0;} case 25: if (ch == ']') {AddCh(); goto case 26;} else {goto case 0;} case 26: {t.kind = 7; break;} case 27: {t.kind = 22; break;} case 28: {t.kind = 25; break;} case 29: {t.kind = 26; break;} case 30: {t.kind = 27; break;} case 31: {t.kind = 30; break;} case 32: {t.kind = 31; break;} case 33: {t.kind = 32; break;} case 34: {t.kind = 33; break;} case 35: {t.kind = 37; break;} case 36: {t.kind = 39; break;} case 37: {t.kind = 40; break;} case 38: {t.kind = 41; break;} case 39: {t.kind = 45; break;} case 40: {t.kind = 47; break;} case 41: {t.kind = 48; break;} case 42: recEnd = pos; recKind = 38; if (ch == 'c') {AddCh(); goto case 16;} else if (ch == '/') {AddCh(); goto case 21;} else {t.kind = 38; break;} case 43: recEnd = pos; recKind = 23; if (ch == '.') {AddCh(); goto case 30;} else if (ch == '>') {AddCh(); goto case 33;} else if (ch == ')') {AddCh(); goto case 41;} else {t.kind = 23; break;} case 44: recEnd = pos; recKind = 29; if (ch == '.') {AddCh(); goto case 32;} else {t.kind = 29; break;} case 45: recEnd = pos; recKind = 36; if (ch == '.') {AddCh(); goto case 40;} else {t.kind = 36; break;} } t.val = new String(tval, 0, tlen); return t; }
// get the next token (possibly a token already seen during peeking) public Token Scan() { if (tokens.next == null) { return NextToken(); } else { pt = tokens = tokens.next; return tokens; } }
// make sure that peeking starts at the current scan position public void ResetPeek() { pt = tokens; }
// peek for the next token, ignore pragmas public Token Peek() { do { if (pt.next == null) { pt.next = NextToken(); } pt = pt.next; } while (pt.kind > maxT); // skip pragmas return pt; }
void Get() { for (;;) { t = la; la = scanner.Scan(); if (la.kind <= maxT) { ++errDist; break; } if (la.kind == 50) { } if (la.kind == 51) { } la = t; } }
public void Parse() { la = new Token(); la.val = ""; Get(); SimpleCoco(); Expect(0); // expect end-of-file automatically added }