示例#1
0
文件: Lexer.cs 项目: buunguyen/bike
 public Token NextToken()
 {
     while (currentChar != Eof)
     {
         currentLocation = new SourceLocation(filePath, currentLineIndex, currentColumnIndex);
         switch ((char)currentChar)
         {
             case '\r':
             case '\n':
             case ' ':
             case '\t':
                 Whitespace();
                 continue;
             case '#':
                 Comment();
                 continue;
             case '=':
                 return Equal();
             case '<':
                 return LessThan();
             case '>':
                 return GreaterThan();
             case '*':
                 return Star();
             case '/':
                 return Slash();
             case '%':
                 return Percent();
             case '!':
                 return Bang();
             case '+':
                 return Plus();
             case '-':
                 return Minus();
             case '|':
                 return Pipe();
             case '&':
                 return Ampersand();
             case ',':
                 Match(',');
                 return Token(TokenType.Comma, ",");
             case ';':
                 Match(';');
                 return Token(TokenType.SemiColon, ";");
             case '.':
                 Match('.');
                 return Token(TokenType.Dot, ".");
             case '?':
                 Match('?');
                 return Token(TokenType.Question, "?");
             case ':':
                 Match(':');
                 return Token(TokenType.Colon, ":");
             case '[':
                 Match('[');
                 return Token(TokenType.LeftBracket, "[");
             case ']':
                 Match(']');
                 return Token(TokenType.RightBracket, "]");
             case '(':
                 Match('(');
                 return Token(TokenType.LeftParen, "(");
             case ')':
                 Match(')');
                 return Token(TokenType.RightParen, ")");
             case '{':
                 Match('{');
                 return Token(TokenType.LeftBrace, "{");
             case '}':
                 Match('}');
                 return Token(TokenType.RightBrace, "}");
             case '0':
             case '1':
             case '2':
             case '3':
             case '4':
             case '5':
             case '6':
             case '7':
             case '8':
             case '9':
                 return Number();
             case '\'':
             case '"':
                 return String();
             default:
                 if (Char.IsLetter((char) currentChar) ||
                     currentChar == '_' ||
                     currentChar == '$')
                     return Identifier();
                 throw Error("Invalid character: " + currentChar);
         }
     }
     return Token(TokenType.Eof, "<EOF>");
 }
示例#2
0
 public ParseException(string msg, SourceLocation location)
     : base(msg)
 {
     Location = location;
 }
示例#3
0
文件: Token.cs 项目: buunguyen/bike
 public Token(TokenType type, string text, SourceLocation location)
 {
     Type = type;
     Text = text;
     Location = location;
 }
示例#4
0
        public Token NextToken()
        {
            while (currentChar != Eof)
            {
                currentLocation = new SourceLocation(filePath, currentLineIndex, currentColumnIndex);
                switch ((char)currentChar)
                {
                case '\r':
                case '\n':
                case ' ':
                case '\t':
                    Whitespace();
                    continue;

                case '#':
                    Comment();
                    continue;

                case '=':
                    return(Equal());

                case '<':
                    return(LessThan());

                case '>':
                    return(GreaterThan());

                case '*':
                    return(Star());

                case '/':
                    return(Slash());

                case '%':
                    return(Percent());

                case '!':
                    return(Bang());

                case '+':
                    return(Plus());

                case '-':
                    return(Minus());

                case '|':
                    return(Pipe());

                case '&':
                    return(Ampersand());

                case ',':
                    Match(',');
                    return(Token(TokenType.Comma, ","));

                case ';':
                    Match(';');
                    return(Token(TokenType.SemiColon, ";"));

                case '.':
                    Match('.');
                    return(Token(TokenType.Dot, "."));

                case '?':
                    Match('?');
                    return(Token(TokenType.Question, "?"));

                case ':':
                    Match(':');
                    return(Token(TokenType.Colon, ":"));

                case '[':
                    Match('[');
                    return(Token(TokenType.LeftBracket, "["));

                case ']':
                    Match(']');
                    return(Token(TokenType.RightBracket, "]"));

                case '(':
                    Match('(');
                    return(Token(TokenType.LeftParen, "("));

                case ')':
                    Match(')');
                    return(Token(TokenType.RightParen, ")"));

                case '{':
                    Match('{');
                    return(Token(TokenType.LeftBrace, "{"));

                case '}':
                    Match('}');
                    return(Token(TokenType.RightBrace, "}"));

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    return(Number());

                case '\'':
                case '"':
                    return(String());

                default:
                    if (Char.IsLetter((char)currentChar) ||
                        currentChar == '_' ||
                        currentChar == '$')
                    {
                        return(Identifier());
                    }
                    throw Error("Invalid character: " + currentChar);
                }
            }
            return(Token(TokenType.Eof, "<EOF>"));
        }