示例#1
0
        public void WhenWeTokenize()
        {
            Token currentToken = _lexer.GetNextToken();

            while (currentToken.Type != TokenType.EOF)
            {
                _tokenList.Add(currentToken);
                currentToken = _lexer.GetNextToken();
            }
            _tokenList.Add(currentToken);
        }
示例#2
0
 public void Parse()
 {
     _currentToken = _lexer.GetNextToken();
     E();
     if (_currentToken.Type != TokenTypes.Eof)
     {
         throw new SyntaxException("Se esperaba EOF");
     }
 }
示例#3
0
 public Parser(Lexer.Lexer lexer)
 {
     _lexer = lexer;
     _token = _lexer.GetNextToken();
 }
示例#4
0
 public void NextToken()
 {
     _token = _lexer.GetNextToken();
 }
示例#5
0
        private SentenceNode Sentence()
        {
            if (currentToken.Type == TokenTypes.Int || currentToken.Type == TokenTypes.String)
            {
                var type = currentToken.Type;
                currentToken = lexer.GetNextToken();

                if (currentToken.Type == TokenTypes.Id)
                {
                    string name = currentToken.Lexeme;

                    currentToken = lexer.GetNextToken();

                    var dimensions = Dimensions();

                    if (currentToken.Type == TokenTypes.Eos)
                    {
                        currentToken = lexer.GetNextToken();
                        return(new DeclarationNode {
                            Type = type, Value = name, Dimensions = dimensions
                        });
                    }
                }
            }
            else if (currentToken.Type == TokenTypes.Read)
            {
                currentToken = lexer.GetNextToken();

                if (currentToken.Type == TokenTypes.Id)
                {
                    string name = currentToken.Lexeme;

                    currentToken = lexer.GetNextToken();

                    if (currentToken.Type == TokenTypes.Eos)
                    {
                        currentToken = lexer.GetNextToken();
                        return(new ReadNode()
                        {
                            Id = name
                        });
                    }
                }
            }
            else if (currentToken.Type == TokenTypes.Print)
            {
                currentToken = lexer.GetNextToken();

                var exp = E();
                if (currentToken.Type == TokenTypes.Eos)
                {
                    currentToken = lexer.GetNextToken();
                    return(new PrintNode {
                        Expression = exp
                    });
                }
            }
            else if (currentToken.Type == TokenTypes.Id)
            {
                var idNode = Id();
                if (currentToken.Type == TokenTypes.Equal)
                {
                    currentToken = lexer.GetNextToken();
                    var exp = E();

                    if (currentToken.Type == TokenTypes.Eos)
                    {
                        currentToken = lexer.GetNextToken();
                        return(new AssignNode {
                            Id = idNode, Expression = exp
                        });
                    }
                }
                else if (currentToken.Type == TokenTypes.Id)
                {
                    var name = currentToken.Lexeme;
                    currentToken = lexer.GetNextToken();
                    if (currentToken.Type == TokenTypes.Eos)
                    {
                        currentToken = lexer.GetNextToken();
                        return(new StructNode {
                            Value = name, StructType = idNode.Value
                        });
                    }
                }
            }
            else if (currentToken.Type == TokenTypes.Struct)
            {
                currentToken = lexer.GetNextToken();
                return(DeclareStruct());
            }
            throw new SyntaxErrorException();
        }