Exemple #1
0
 void Init()
 {
     pos = -1; line = 1; col = 0; charPos = -1;
     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; charPos = -1;
     NextCh();
     }*/
     pt = tokens = new Token();  // first token is a dummy
 }
Exemple #2
0
        Token NextToken()
        {
            while (ch == ' ' ||
            ch >= 9 && ch <= 10 || ch == 13
            ) NextCh();
            if (ch == '/' && Comment0() ||ch == '/' && Comment1()) return NextToken();
            int apx = 0;
            int recKind = noSym;
            int recEnd = pos;
            t = new Token();
            t.pos = pos; t.col = col; t.line = line; t.charPos = charPos;
            int state;
            if (start.ContainsKey(ch)) { state = (int) start[ch]; }
            else { 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:
                {t.kind = 2; break;}
            case 2:
                {t.kind = 3; break;}
            case 3:
                {t.kind = 5; break;}
            case 4:
                {t.kind = 6; break;}
            case 5:
                {t.kind = 7; break;}
            case 6:
                {t.kind = 9; break;}
            case 7:
                {t.kind = 10; break;}
            case 8:
                {t.kind = 11; break;}
            case 9:
                {t.kind = 12; break;}
            case 10:
                recEnd = pos; recKind = 14;
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z' || ch == '_' || ch >= 'a' && ch <= 'z') {AddCh(); goto case 10;}
                else {t.kind = 14; t.val = new String(tval, 0, tlen); CheckLiteral(); return t;}
            case 11:
                {t.kind = 15; break;}
            case 12:
                {
                    tlen -= apx;
                    SetScannerBehindT();
                    t.kind = 15; break;}
            case 13:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 15;}
                else if (ch == '+' || ch == '-') {AddCh(); goto case 14;}
                else {goto case 0;}
            case 14:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 15;}
                else {goto case 0;}
            case 15:
                recEnd = pos; recKind = 16;
                if (ch >= '0' && ch <= '9' || ch == '_') {AddCh(); goto case 15;}
                else if (ch == 'F' || ch == 'f') {AddCh(); goto case 20;}
                else {t.kind = 16; break;}
            case 16:
                recEnd = pos; recKind = 16;
                if (ch >= '0' && ch <= '9' || ch == '_') {AddCh(); goto case 16;}
                else if (ch == 'F' || ch == 'f') {AddCh(); goto case 20;}
                else if (ch == 'E' || ch == 'e') {AddCh(); goto case 17;}
                else {t.kind = 16; break;}
            case 17:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 19;}
                else if (ch == '+' || ch == '-') {AddCh(); goto case 18;}
                else {goto case 0;}
            case 18:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 19;}
                else {goto case 0;}
            case 19:
                recEnd = pos; recKind = 16;
                if (ch >= '0' && ch <= '9' || ch == '_') {AddCh(); goto case 19;}
                else if (ch == 'F' || ch == 'f') {AddCh(); goto case 20;}
                else {t.kind = 16; break;}
            case 20:
                {t.kind = 16; break;}
            case 21:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 22;}
                else {goto case 0;}
            case 22:
                recEnd = pos; recKind = 17;
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 22;}
                else {t.kind = 17; break;}
            case 23:
                if (ch == 'U' || ch == 'u') {AddCh(); goto case 24;}
                else {goto case 0;}
            case 24:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 25;}
                else {goto case 0;}
            case 25:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 26;}
                else {goto case 0;}
            case 26:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 27;}
                else {goto case 0;}
            case 27:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 28;}
                else {goto case 0;}
            case 28:
                recEnd = pos; recKind = 18;
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 29;}
                else {t.kind = 18; break;}
            case 29:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 30;}
                else {goto case 0;}
            case 30:
                {t.kind = 18; break;}
            case 31:
                if (ch <= '&' || ch >= '(' && ch <= '[' || ch >= ']' && ch <= 65535) {AddCh(); goto case 32;}
                else if (ch == 92) {AddCh(); goto case 33;}
                else if (ch == 39) {AddCh(); goto case 40;}
                else {goto case 0;}
            case 32:
                if (ch == 39) {AddCh(); goto case 40;}
                else {goto case 0;}
            case 33:
                if (ch == 92 || ch >= 'a' && ch <= 'b' || ch == 'f' || ch == 'n' || ch == 'r' || ch == 't') {AddCh(); goto case 32;}
                else if (ch == 'U' || ch == 'u') {AddCh(); goto case 34;}
                else {goto case 0;}
            case 34:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 35;}
                else {goto case 0;}
            case 35:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 36;}
                else {goto case 0;}
            case 36:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 37;}
                else {goto case 0;}
            case 37:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 38;}
                else {goto case 0;}
            case 38:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 39;}
                else if (ch == 39) {AddCh(); goto case 40;}
                else {goto case 0;}
            case 39:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 32;}
                else {goto case 0;}
            case 40:
                {t.kind = 19; break;}
            case 41:
                if (ch <= '!' || ch >= '#' && ch <= '[' || ch >= ']' && ch <= 65535) {AddCh(); goto case 41;}
                else if (ch == 92) {AddCh(); goto case 42;}
                else if (ch == '"') {AddCh(); goto case 48;}
                else {goto case 0;}
            case 42:
                if (ch == 92 || ch >= 'a' && ch <= 'b' || ch == 'f' || ch == 'n' || ch == 'r' || ch == 't') {AddCh(); goto case 41;}
                else if (ch == 'U' || ch == 'u') {AddCh(); goto case 43;}
                else {goto case 0;}
            case 43:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 44;}
                else {goto case 0;}
            case 44:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 45;}
                else {goto case 0;}
            case 45:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 46;}
                else {goto case 0;}
            case 46:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 47;}
                else {goto case 0;}
            case 47:
                if (ch <= '!' || ch >= '#' && ch <= '/' || ch >= ':' && ch <= '@' || ch >= 'G' && ch <= '[' || ch >= ']' && ch <= '`' || ch >= 'g' && ch <= 65535) {AddCh(); goto case 41;}
                else if (ch == 92) {AddCh(); goto case 42;}
                else if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 58;}
                else if (ch == '"') {AddCh(); goto case 48;}
                else {goto case 0;}
            case 48:
                {t.kind = 20; break;}
            case 49:
                if (ch <= '!' || ch >= '#' && ch <= 65535) {AddCh(); goto case 49;}
                else if (ch == '"') {AddCh(); goto case 52;}
                else {goto case 0;}
            case 50:
                if (ch == '"') {AddCh(); goto case 51;}
                else if (ch == '#') {AddCh(); goto case 50;}
                else {goto case 0;}
            case 51:
                if (ch <= '!' || ch >= '#' && ch <= 65535) {AddCh(); goto case 51;}
                else if (ch == '"') {AddCh(); goto case 59;}
                else {goto case 0;}
            case 52:
                {t.kind = 21; break;}
            case 53:
                recEnd = pos; recKind = 15;
                if (ch >= '0' && ch <= '9' || ch == '_') {AddCh(); goto case 53;}
                else if (ch == '.') {apx++; AddCh(); goto case 60;}
                else if (ch == 'L' || ch == 'U' || ch == 'l' || ch == 'u') {AddCh(); goto case 11;}
                else {t.kind = 15; break;}
            case 54:
                recEnd = pos; recKind = 13;
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 16;}
                else if (ch == '.') {AddCh(); goto case 61;}
                else {t.kind = 13; break;}
            case 55:
                recEnd = pos; recKind = 14;
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z' || ch == '_' || ch >= 'a' && ch <= 'z') {AddCh(); goto case 10;}
                else if (ch == '"') {AddCh(); goto case 49;}
                else if (ch == '#') {AddCh(); goto case 50;}
                else {t.kind = 14; t.val = new String(tval, 0, tlen); CheckLiteral(); return t;}
            case 56:
                recEnd = pos; recKind = 4;
                if (ch == ':') {AddCh(); goto case 3;}
                else {t.kind = 4; break;}
            case 57:
                recEnd = pos; recKind = 15;
                if (ch >= '0' && ch <= '9' || ch == '_') {AddCh(); goto case 53;}
                else if (ch == '.') {apx++; AddCh(); goto case 60;}
                else if (ch == 'L' || ch == 'U' || ch == 'l' || ch == 'u') {AddCh(); goto case 11;}
                else if (ch == 'X' || ch == 'x') {AddCh(); goto case 21;}
                else {t.kind = 15; break;}
            case 58:
                if (ch <= '!' || ch >= '#' && ch <= '[' || ch >= ']' && ch <= 65535) {AddCh(); goto case 41;}
                else if (ch == 92) {AddCh(); goto case 42;}
                else if (ch == '"') {AddCh(); goto case 48;}
                else {goto case 0;}
            case 59:
                if (ch <= '!' || ch >= '$' && ch <= 65535) {AddCh(); goto case 51;}
                else if (ch == '"') {AddCh(); goto case 59;}
                else if (ch == '#') {AddCh(); goto case 62;}
                else {goto case 0;}
            case 60:
                recEnd = pos; recKind = 16;
                if (ch >= '0' && ch <= '9') {apx = 0; AddCh(); goto case 16;}
                else if (ch == '.') {apx++; AddCh(); goto case 12;}
                else if (ch == 'F' || ch == 'f') {apx = 0; AddCh(); goto case 20;}
                else if (ch == 'E' || ch == 'e') {apx = 0; AddCh(); goto case 13;}
                else {t.kind = 16; break;}
            case 61:
                recEnd = pos; recKind = 1;
                if (ch == '.') {AddCh(); goto case 1;}
                else {t.kind = 1; break;}
            case 62:
                recEnd = pos; recKind = 21;
                if (ch <= '!' || ch >= '$' && ch <= 65535) {AddCh(); goto case 51;}
                else if (ch == '"') {AddCh(); goto case 59;}
                else if (ch == '#') {AddCh(); goto case 62;}
                else {t.kind = 21; break;}
            case 63:
                {t.kind = 39; break;}
            case 64:
                {t.kind = 41; break;}
            case 65:
                {t.kind = 64; break;}
            case 66:
                {t.kind = 65; break;}
            case 67:
                {t.kind = 66; break;}
            case 68:
                {t.kind = 67; break;}
            case 69:
                {t.kind = 68; break;}
            case 70:
                {t.kind = 69; break;}
            case 71:
                {t.kind = 70; break;}
            case 72:
                {t.kind = 71; break;}
            case 73:
                {t.kind = 72; break;}
            case 74:
                {t.kind = 73; break;}
            case 75:
                {t.kind = 76; break;}
            case 76:
                {t.kind = 78; break;}
            case 77:
                {t.kind = 79; break;}
            case 78:
                {t.kind = 80; break;}
            case 79:
                {t.kind = 81; break;}
            case 80:
                {t.kind = 82; break;}
            case 81:
                {t.kind = 83; break;}
            case 82:
                {t.kind = 84; break;}
            case 83:
                {t.kind = 85; break;}
            case 84:
                {t.kind = 86; break;}
            case 85:
                recEnd = pos; recKind = 8;
                if (ch == '-') {AddCh(); goto case 64;}
                else {t.kind = 8; break;}
            case 86:
                recEnd = pos; recKind = 90;
                if (ch == '>') {AddCh(); goto case 63;}
                else if (ch == '=') {AddCh(); goto case 66;}
                else {t.kind = 90; break;}
            case 87:
                recEnd = pos; recKind = 40;
                if (ch == '>') {AddCh(); goto case 75;}
                else if (ch == '=') {AddCh(); goto case 80;}
                else {t.kind = 40; break;}
            case 88:
                recEnd = pos; recKind = 42;
                if (ch == '<') {AddCh(); goto case 97;}
                else if (ch == '=') {AddCh(); goto case 82;}
                else {t.kind = 42; break;}
            case 89:
                recEnd = pos; recKind = 43;
                if (ch == '>') {AddCh(); goto case 98;}
                else if (ch == '=') {AddCh(); goto case 83;}
                else {t.kind = 43; break;}
            case 90:
                recEnd = pos; recKind = 44;
                if (ch == '=') {AddCh(); goto case 71;}
                else if (ch == '&') {AddCh(); goto case 79;}
                else {t.kind = 44; break;}
            case 91:
                recEnd = pos; recKind = 89;
                if (ch == '=') {AddCh(); goto case 65;}
                else {t.kind = 89; break;}
            case 92:
                recEnd = pos; recKind = 91;
                if (ch == '=') {AddCh(); goto case 67;}
                else if (ch == '*') {AddCh(); goto case 99;}
                else {t.kind = 91; break;}
            case 93:
                recEnd = pos; recKind = 92;
                if (ch == '=') {AddCh(); goto case 68;}
                else {t.kind = 92; break;}
            case 94:
                recEnd = pos; recKind = 93;
                if (ch == '=') {AddCh(); goto case 70;}
                else {t.kind = 93; break;}
            case 95:
                recEnd = pos; recKind = 75;
                if (ch == '=') {AddCh(); goto case 72;}
                else if (ch == '|') {AddCh(); goto case 78;}
                else {t.kind = 75; break;}
            case 96:
                recEnd = pos; recKind = 95;
                if (ch == '=') {AddCh(); goto case 81;}
                else {t.kind = 95; break;}
            case 97:
                recEnd = pos; recKind = 87;
                if (ch == '=') {AddCh(); goto case 73;}
                else {t.kind = 87; break;}
            case 98:
                recEnd = pos; recKind = 88;
                if (ch == '=') {AddCh(); goto case 74;}
                else {t.kind = 88; break;}
            case 99:
                recEnd = pos; recKind = 94;
                if (ch == '=') {AddCh(); goto case 69;}
                else {t.kind = 94; break;}

            }
            t.val = new String(tval, 0, tlen);
            return t;
        }
Exemple #3
0
 // 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;
     }
 }
Exemple #4
0
 // make sure that peeking starts at the current scan position
 public void ResetPeek()
 {
     pt = tokens;
 }
Exemple #5
0
        // 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;
        }
Exemple #6
0
 public void Parse()
 {
     la = new Token();
     la.val = "";
     Get();
     Expresso();
     Expect(0);
 }
Exemple #7
0
        void Get()
        {
            for (;;) {
            t = la;
            la = scanner.Scan();
            if (la.kind <= maxT) { ++errDist; break; }

            la = t;
            }
        }