コード例 #1
0
ファイル: Parser.cs プロジェクト: zgramana/IronLanguages.PCG
        // _parseExpr parses an expression from the Lexer passed in.
        //
        private SymplExpr ParseExprAux(Lexer lexer)
        {
            Token     token = lexer.GetToken();
            SymplExpr res   = null;

            if (token == SyntaxToken.EOF)
            {
                throw new SymplParseException(
                          "Unexpected EOF encountered while parsing expression.");
            }
            if (token == SyntaxToken.Quote)
            {
                lexer.PutToken(token);
                res = ParseQuoteExpr(lexer);
            }
            else if (token == SyntaxToken.Paren)
            {
                lexer.PutToken(token);
                res = ParseForm(lexer);
            }
            else if (token is IdOrKeywordToken)
            {
                // If we encounter literal kwd constants, they get turned into ID
                // Exprs.  Code that accepts Id Exprs, needs to check if the token is
                // kwd or not when it matters.
                if ((token is KeywordToken) && !(token == KeywordToken.Nil) &&
                    !(token == KeywordToken.True) && !(token == KeywordToken.False))
                {
                    throw new InvalidCastException("Keyword cannot be an expression");
                }
                else
                {
                    res = new SymplIdExpr((IdOrKeywordToken)token);
                }
            }
            else if (token is LiteralToken)
            {
                res = new SymplLiteralExpr(((LiteralToken)token).Value);
            }
            // check for dotted expr
            if (res != null)
            {
                Token next = lexer.GetToken();
                lexer.PutToken(next);
                if (next == SyntaxToken.Dot)
                {
                    return(ParseDottedExpr(lexer, res));
                }
                else
                {
                    return(res);
                }
            }
            throw new SymplParseException(
                      "Unexpected token when expecting " +
                      "beginning of expression -- " + token.ToString() + " ... " +
                      lexer.GetToken().ToString() + lexer.GetToken().ToString() +
                      lexer.GetToken().ToString() + lexer.GetToken().ToString());
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: rudimk/dlr-dotnet
		// _parseExpr parses an expression from the Lexer passed in.
		//
        private SymplExpr ParseExprAux(Lexer lexer) {
            Token token = lexer.GetToken();
            SymplExpr res = null;
            if (token == SyntaxToken.EOF) {
                throw new SymplParseException(
                    "Unexpected EOF encountered while parsing expression.");
            }
            if (token == SyntaxToken.Quote) {
                lexer.PutToken(token);
                res = ParseQuoteExpr(lexer);
            } else if (token == SyntaxToken.Paren) {
                lexer.PutToken(token);
                res = ParseForm(lexer);
            } else if (token is IdOrKeywordToken) {
                // If we encounter literal kwd constants, they get turned into ID
                // Exprs.  Code that accepts Id Exprs, needs to check if the token is
                // kwd or not when it matters.
                if ((token is KeywordToken) && !(token == KeywordToken.Nil) &&
                    !(token == KeywordToken.True) && !(token == KeywordToken.False)) {
                    throw new InvalidCastException("Keyword cannot be an expression");
                } else {
                    res = new SymplIdExpr((IdOrKeywordToken)token);
                }
            } else if (token is LiteralToken) {
                res = new SymplLiteralExpr(((LiteralToken)token).Value);
            }
            // check for dotted expr
            if (res != null) {
                Token next = lexer.GetToken();
                lexer.PutToken(next);
                if (next == SyntaxToken.Dot) {
                    return ParseDottedExpr(lexer, res);
                } else {
                    return res;
                }
            }
            throw new SymplParseException(
                "Unexpected token when expecting " +
                "beginning of expression -- " + token.ToString() + " ... " + 
                lexer.GetToken().ToString() + lexer.GetToken().ToString() + 
                lexer.GetToken().ToString() + lexer.GetToken().ToString());
        }