Пример #1
0
            public static Node.Sequence Parse()
            {
                Node.Sequence n;

                if (IsLookahead(1, Tokens.Operator, Lexemes.BraceClose) ||
                    IsLookahead(1, Tokens.Operator, Lexemes.ParenClose))
                {
                    n = null;
                }
                else
                {
                    Node.Expression e = Parser.Expression.Parse();

                    if (IsLookahead(1, Tokens.Operator, Lexemes.Comma))
                    {
                        Match(Tokens.Operator, Lexemes.Comma);
                    }

                    n = new Node.Sequence
                    {
                        E = e,
                        S = Parser.Sequence.Parse()
                    };
                }

                return(n);
            }
Пример #2
0
            public static Node.Expression Parse()
            {
                Node.Expression n;

                if (IsLookahead(1, Tokens.Keyword, Lexemes.If))
                {
                    Match(Tokens.Keyword, Lexemes.If);
                    Node.Expression test = Parser.Expression.Parse();
                    Match(Tokens.Keyword, Lexemes.Then);
                    Node.Expression expIf = Parser.Expression.Parse();

                    if (IsLookahead(1, Tokens.Keyword, Lexemes.Else))
                    {
                        Match(Tokens.Keyword, Lexemes.Else);
                        Node.Expression expElse = Parser.Expression.Parse();

                        n = new Node.IfThenElse
                        {
                            Test = test,
                            If   = expIf,
                            Else = expElse
                        };
                    }
                    else
                    {
                        n = new Node.IfThen
                        {
                            Test = test,
                            If   = expIf
                        }
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.While))
                {
                    Match(Tokens.Keyword, Lexemes.While);
                    Node.Expression test = Parser.Expression.Parse();
                    Match(Tokens.Keyword, Lexemes.Loop);
                    Node.Expression exp = Parser.Expression.Parse();

                    n = new Node.WhileLoop
                    {
                        Test = test,
                        Loop = exp
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.Loop))
                {
                    Match(Tokens.Keyword, Lexemes.Loop);
                    Node.Expression exp = Parser.Expression.Parse();
                    Match(Tokens.Keyword, Lexemes.While);
                    Node.Expression test = Parser.Expression.Parse();

                    n = new Node.LoopWhile
                    {
                        Test = test,
                        Loop = exp
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.Do))
                {
                    Match(Tokens.Keyword, Lexemes.Do);

                    n = new Node.Do
                    {
                        E = Parser.Expression.Parse()
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.Async))
                {
                    Match(Tokens.Keyword, Lexemes.Async);

                    n = new Node.Async
                    {
                        E = Parser.Expression.Parse()
                    };
                }
                else if (IsLookahead(1, Tokens.Identifier) ||
                         IsLookahead(1, Tokens.Number) ||
                         IsLookahead(1, Tokens.Keyword, Lexemes.Function) ||
                         IsLookahead(1, Tokens.Operator, Lexemes.BrackOpen) ||
                         IsLookahead(1, Tokens.Operator, Lexemes.BraceOpen) ||
                         IsLookahead(1, Tokens.Keyword, Lexemes.Undefined))
                {
                    n = new Node.Structured
                    {
                        V = Parser.Value.Parse(),
                        C = Parser.Continuation.Parse()
                    };
                }
                else
                {
                    throw new Exceptions.Parser.UnexpectedLookahead("Parse(Expression)", Lexer.Lookahead(1), 1);
                }

                return(n);
            }
Пример #3
0
            public static Node.Value Parse()
            {
                Node.Value n;

                if (IsLookahead(1, Tokens.Number))
                {
                    Match(Tokens.Number);

                    n = new Node.Number
                    {
                        n = (string)Parser.Lexer.Current().Attribute
                    };
                }
                else if (IsLookahead(1, Tokens.Identifier))
                {
                    Match(Tokens.Identifier);
                    Token identifier = Parser.Lexer.Current();

                    if (IsLookahead(1, Tokens.Operator, Lexemes.Dot) &&
                        IsLookahead(2, Tokens.Identifier, "reference"))
                    {
                        Match(Tokens.Operator, Lexemes.Dot);
                        Match(Tokens.Identifier);
                        n = new Node.Reference {
                            id = (string)identifier.Attribute
                        };
                    }
                    else
                    {
                        n = new Node.Identifier
                        {
                            id = (string)identifier.Attribute
                        }
                    };
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.Function))
                {
                    Match(Tokens.Keyword, Lexemes.Function);
                    Match(Tokens.Operator, Lexemes.ParenOpen);
                    Node.Names pl = Parser.Names.Parse();
                    Match(Tokens.Operator, Lexemes.ParenClose);
                    Node.Expression body = Parser.Expression.Parse();

                    n = new Node.Function
                    {
                        N = pl,
                        E = body
                    };
                }
                else if (IsLookahead(1, Tokens.Operator, Lexemes.BrackOpen))
                {
                    Match(Tokens.Operator, Lexemes.BrackOpen);

                    n = new Node.Wrapped
                    {
                        E = Parser.Expression.Parse()
                    };

                    Match(Tokens.Operator, Lexemes.BrackClose);
                }
                else if (IsLookahead(1, Tokens.Operator, Lexemes.BraceOpen))
                {
                    n = Parser.Block.Parse();
                }
                else if (IsLookahead(1, Tokens.Keyword, Lexemes.Undefined))
                {
                    Match(Tokens.Keyword, Lexemes.Undefined);
                    n = new Node.Undefined {
                    };
                }
                else
                {
                    throw new Exceptions.Parser.UnexpectedLookahead("Parse(Value)", Lexer.Lookahead(1), 1);
                }

                return(n);
            }