Ejemplo n.º 1
0
        /// <summary>
        ///  
        /// </summary>
        /// <param name="text"></param>
        public Tree Parse(string text)
        {
            _scanner = new Scanner(text);
            _tree = new Tree();

            Token token = this.NextToken();
            if (token == Token.EndOfFile)
                return _tree;
            this.CheckStartableToken(token);

            while (token != Token.EndOfFile)
            {
                if (Parser.IsConstTocken(token))
                    ParseConst();
                else if (_tree._isStart &&
                    ((token == Token.Subtract) || (token == Token.Add)))
                {
                    Constant constant = new Constant((int)0);
                    _tree.AddOperand(constant);
                    BinaryOp bo = BinaryOp.CreateOp(token);
                    _tree.AddOperator(bo);
                }
                else if (Parser.IsBinaryOp(token))
                    ParseBinaryOp();
                else if (Parser.IsUnaryOp(token))
                    ParseUnaryOp();
                else
                {
                    switch (token)
                    {
                        case Token.Identifier:
                            ParseIdentifier();
                            break;
                        case Token.LeftIndexer:
                            this.ParseIndexer();
                            break;
                        case Token.RightIndexer:
                            _tree.Pop('[');
                            break;
                        case Token.ConditionalIf:
                            ParseConditional();
                            break;
                        case Token.ConditionalSemicolon:
                            ParseConditionalSemicolon();
                            break;
                        case Token.Member:
                            ParseMember();
                            break;
                        case Token.LeftParen:
                            if (_tree._top.Expression is MemberOp)
                            {
                                MemberOp memberOp = (MemberOp)_tree._top.Expression;
                                if (memberOp.IsFunction)
                                    throw new ParserException("()()not allowed");
                                memberOp.IsFunction = true;
                            }
                            this._tree.Push('(');
                            break;
                        case Token.RightParen:
                            _tree.Pop('(');
                            break;
                        case Token.Comma:
                            this._tree.Pop(',');
                            this._tree.Push(',');
                            break;
                        default:
                            throw BuildException(Error.InternalError);
                    }
                }
                token = this.NextToken();
            }

            _tree.Complete();
            return _tree;
        }
Ejemplo n.º 2
0
 void ParseConst()
 {
     Token token = this.PeekToken();
     if (!Parser.IsCanFollowConst(token))
     {
         throw this.BuildException(Error.BinaryCommaRightParenRightIndexerExpected);
     }
     Constant expr = new Constant(_current.value);
     _tree.AddOperand( expr);
 }