Search in sources :

Example 1 with ANTLRLexer

use of org.antlr.v4.parse.ANTLRLexer in project antlr4 by tunnelvisionlabs.

the class LeftRecursiveRuleTransformer method parseArtificialRule.

public RuleAST parseArtificialRule(final Grammar g, String ruleText) {
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.getCharStream());
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    lexer.tokens = tokens;
    ToolANTLRParser p = new ToolANTLRParser(tokens, tool);
    p.setTreeAdaptor(adaptor);
    Token ruleStart = null;
    try {
        ParserRuleReturnScope r = p.rule();
        RuleAST tree = (RuleAST) r.getTree();
        ruleStart = (Token) r.getStart();
        GrammarTransformPipeline.setGrammarPtr(g, tree);
        GrammarTransformPipeline.augmentTokensWithOriginalPosition(g, tree);
        return tree;
    } catch (Exception e) {
        tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e, ruleStart, "error parsing rule created during left-recursion detection: " + ruleText);
    }
    return null;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) RuleAST(org.antlr.v4.tool.ast.RuleAST) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ANTLRLexer(org.antlr.v4.parse.ANTLRLexer) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Token(org.antlr.runtime.Token) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 2 with ANTLRLexer

use of org.antlr.v4.parse.ANTLRLexer in project antlr4 by antlr.

the class LeftRecursiveRuleTransformer method parseArtificialRule.

public RuleAST parseArtificialRule(final Grammar g, String ruleText) {
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.getCharStream());
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    lexer.tokens = tokens;
    ToolANTLRParser p = new ToolANTLRParser(tokens, tool);
    p.setTreeAdaptor(adaptor);
    Token ruleStart = null;
    try {
        ParserRuleReturnScope r = p.rule();
        RuleAST tree = (RuleAST) r.getTree();
        ruleStart = (Token) r.getStart();
        GrammarTransformPipeline.setGrammarPtr(g, tree);
        GrammarTransformPipeline.augmentTokensWithOriginalPosition(g, tree);
        return tree;
    } catch (Exception e) {
        tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e, ruleStart, "error parsing rule created during left-recursion detection: " + ruleText);
    }
    return null;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) RuleAST(org.antlr.v4.tool.ast.RuleAST) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ANTLRLexer(org.antlr.v4.parse.ANTLRLexer) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Token(org.antlr.runtime.Token) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 CommonTokenStream (org.antlr.runtime.CommonTokenStream)2 ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)2 RecognitionException (org.antlr.runtime.RecognitionException)2 Token (org.antlr.runtime.Token)2 ANTLRLexer (org.antlr.v4.parse.ANTLRLexer)2 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)2 ToolANTLRParser (org.antlr.v4.parse.ToolANTLRParser)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2