示例#1
0
        private void expect(TokenType type)
        {
            if (lookahead.TokenType != type)
                throw new SyntaxException (String.Format ("Expected {0}, got {1} ", type, lookahead.TokenType));

            lookahead = lexer.GetNextToken ();
        }
示例#2
0
        public Token GetNextToken()
        {
            Token nextToken;

            RemoveBlanks ();

            if (sourceString.Length == 0) {
                nextToken = new Token (TokenType.EOF);
            } else {
                sourceString = Matcher.Match (sourceString, out nextToken);
            }

            return nextToken;
        }
示例#3
0
        public static string Match(string str, out Token nextToken)
        {
            nextToken = null;

            for (var i = 0; i < matches.Length; i++) {
                if (matches [i].Match (str)) {
                    nextToken = new Token (matches [i].Type);
                    return str.Substring (matches [i].Length);
                }
            }

            // Controllo se si tratta di un id char (char|digit) ...

            if (Char.IsLetter (str [0])) {
                var i = 1;

                while (i < str.Length && Char.IsLetterOrDigit (str [i])) {
                    i++;
                }

                var word = str.Substring (0, i);

                for (i = 0; i < reservedWords.Length; i++) {
                    if (reservedWords [i].Equals (word))
                        nextToken = new Token (reservedWords [i].Type);
                }

                if (nextToken == null)
                    nextToken = new Token (word, TokenType.ID);

                return str.Substring (word.Length);

            } else if (str [0] == '"') {
                var i = 1;

                while (str [i] != '"') {
                    i++;
                }

                nextToken = new Token (str.Substring (1, i - 1), TokenType.ID);
                return str.Substring (i + 1);
            } else
                throw new InvalidInputString ();
        }
示例#4
0
 public Parser(String str)
 {
     lexer = new Scanner (str);
     lookahead = lexer.GetNextToken ();
 }