Example #1
0
        /// <summary>
        /// Parse the specified parser.
        /// </summary>
        /// <param name="parser">Parser.</param>
        public static AstNode Parse(Parser parser)
        {
            CodeBlockNode block = new CodeBlockNode();

            while (!parser.EndOfStream)
                block.Children.Add(StatementNode.Parse(parser));

            return block;
        }
Example #2
0
        private static AstNode parseTerm(Parser parser)
        {
            if (parser.MatchToken(TokenType.Number))
                return new NumberNode(Convert.ToDouble(parser.ExpectToken(TokenType.Number).Value));
            else if (parser.AcceptToken(TokenType.Parentheses, "("))
            {
                AstNode statement = ExpressionNode.Parse(parser);
                parser.ExpectToken(TokenType.Parentheses, ")");
                return statement;
            }
            else if (parser.MatchToken(TokenType.Identifier, "Then"))
            {
                CodeBlockNode block = new CodeBlockNode();
                parser.ExpectToken(TokenType.Identifier, "Then");

                while (!parser.EndOfStream && !parser.MatchToken(TokenType.Identifier, "EndIf") && !parser.MatchToken(TokenType.Identifier, "Else"))
                    block.Children.Add(StatementNode.Parse(parser));

                if (parser.MatchToken(TokenType.Identifier, "Else"))
                    return block;

                parser.ExpectToken(TokenType.Identifier, "EndIf");

                return block;
            }
            else if (parser.MatchToken(TokenType.Identifier, "Else"))
            {
                parser.ExpectToken(TokenType.Identifier, "Else");
                return StatementNode.Parse(parser);
            }
            else if (parser.MatchToken(TokenType.Identifier, "Do"))
            {
                CodeBlockNode block = new CodeBlockNode();
                parser.ExpectToken(TokenType.Identifier, "Do");

                while (!parser.EndOfStream && !parser.MatchToken(TokenType.Identifier, "End"))
                    block.Children.Add(StatementNode.Parse(parser));

                parser.ExpectToken(TokenType.Identifier, "End");

                return block;
            }
            else if (parser.MatchToken(TokenType.String))
                return new StringNode((string)parser.ExpectToken(TokenType.String).Value);
            else if (parser.MatchToken(TokenType.Identifier))
                return new IdentifierNode((string)parser.ExpectToken(TokenType.Identifier).Value);
            else
                throw new Exception("Unexpected " + parser.CurrentToken().TokenType + " in Parser: " + parser.CurrentToken().Value + ".");
        }
Example #3
0
        /// <summary>
        /// Parse this instance.
        /// </summary>
        public AstNode Parse()
        {
            CodeBlockNode tree = new CodeBlockNode();
            while (!EndOfStream)
                tree.Children.Add(StatementNode.Parse(this));

            return tree;
        }