public override IList <Token <TokenType> > Parse() { this.Current = new Token(this.Index); char ch = GetNextChar(); while (ch != (char)0) { this.Status = this.Matrix.GetStatusTransform(this.Status, ch); if (this.Status == ParserStatus.Error) { ExceptionHelper.ThrowParseError(this.Index - 1); } if (this.Status == ParserStatus.DPeekNext) { char next = PeekNextChar(); if (this.Matrix.GetCharType(next) == CharType.DQuot) { GetNextChar();//Note:skip the next double quot. this.Current.Add(ch); this.Status = ParserStatus.DQuot; } else { this.Current.Add(ch); this.Status = ParserStatus.Start;//Note:accept the close quot and set to Start,so when scan next char,this token will be accepted. } } else if (this.Status == ParserStatus.SPeekNext) { char next = PeekNextChar(); if (this.Matrix.GetCharType(next) == CharType.SQuot) { GetNextChar();//Note:skip the next single quot. this.Current.Add(ch); this.Status = ParserStatus.SQuot; } else { this.Current.Add(ch); this.Status = ParserStatus.Start;//Note:accept the close quot and set to Start,so when scan next char,this token will be accepted. } } else { if (!this.Matrix.TokenTerminated) { this.Current.Add(ch); } } ch = GetNextChar(); } AcceptLastToken(null, new LexicalParseEventArgs(ch, this.Status));//Note:Last token. PostProcess(); return(this.Tokens); }
//BNF: //Value ::= Expr ? Expr : Expr | Expr //Expr ::= Logic LogicOp(||,&&) Logic | Logic //Logic ::= Compare CompareOp(>,>=,<,<=,==,!=) Compare | Compare //Compare ::= Term (+-) Term | Term //Term ::= Exponent (*/%) Exponent | Exponent //Exponent ::= Unary (^) Unary //Unary ::= Factor | -Factor | !Factor //Factor ::= Obj.ID | Obj.ID(ValueList) | Obj[ValueList] | ID(ValueList) | Obj //Obj ::= (Value) | ID | obj.ID,obj.ID(),obj[] | array[ValueList] | tuple[ID-Value pair] | regex[pattern] | new[ClassQualifier ValueList] | ClassQualifier //ValueList ::= | Value | Value,ValueList public override ExpressionLanguageAST Parse() { ExpressionLanguageAST ast = new ExpressionLanguageAST(Value()); if (this.Index < this.Tokens.Count) { ExceptionHelper.ThrowParseError(this.Tokens[this.Index].Position); } Trace(ast); return(ast); }
private Node Obj() { Token <TokenType> t = PeekNextToken(); switch (t.TokenType) { case TokenType.Array: return(Array()); case TokenType.Hash: return(Hash()); case TokenType.Regex: return(Regex()); case TokenType.New: return(New()); case TokenType.Left_Parenthesis: GetNextToken(); Node node = Value(); Token <TokenType> rightToken = GetNextToken(); if (rightToken.TokenType != TokenType.Right_Parenthesis) { ExceptionHelper.ThrowExpectToken(TokenType.Right_Parenthesis, t.Position); } return(node); case TokenType.ID: Token <TokenType> idToken = GetNextToken(); Token <TokenType> next = PeekNextToken(); if (next.TokenType == TokenType.ClassQualifier) { List <string> strList = new List <string>(); strList.Add(idToken.TokenValue); while (next.TokenType == TokenType.ClassQualifier) { GetNextToken(); //Note:Get the :: operator idToken = GetNextToken(); if (idToken.TokenType != TokenType.ID) { ExceptionHelper.ThrowExpectToken(TokenType.ID, idToken.Position); } strList.Add(idToken.TokenValue); next = PeekNextToken(); } return(new ClassQualifierNode(strList)); } else { return(new SimpleNode(idToken)); } case TokenType.String: case TokenType.Long: case TokenType.Decimal: case TokenType.Boolean: Token <TokenType> valToken = GetNextToken(); return(new SimpleNode(valToken)); default: ExceptionHelper.ThrowParseError(t.Position); return(null); } }