Search in sources :

Example 6 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BaseNodeTest method semanticProcess.

protected void semanticProcess(Grammar g) {
    if (g.ast != null && !g.ast.hasErrors) {
        System.out.println(g.ast.toStringTree());
        Tool antlr = new Tool();
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        if (g.getImportedGrammars() != null) {
            // (if any)
            for (Grammar imp : g.getImportedGrammars()) {
                antlr.processNonCombinedGrammar(imp, false);
            }
        }
    }
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool)

Example 7 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BaseBrowserTest method semanticProcess.

protected void semanticProcess(Grammar g) {
    if (g.ast != null && !g.ast.hasErrors) {
        System.out.println(g.ast.toStringTree());
        Tool antlr = new Tool();
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        if (g.getImportedGrammars() != null) {
            // process imported grammars (if any)
            for (Grammar imp : g.getImportedGrammars()) {
                antlr.processNonCombinedGrammar(imp, false);
            }
        }
    }
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool)

Example 8 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BasePythonTest method semanticProcess.

protected void semanticProcess(Grammar g) {
    if (g.ast != null && !g.ast.hasErrors) {
        System.out.println(g.ast.toStringTree());
        Tool antlr = new Tool();
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        if (g.getImportedGrammars() != null) {
            // process imported grammars (if any)
            for (Grammar imp : g.getImportedGrammars()) {
                antlr.processNonCombinedGrammar(imp, false);
            }
        }
    }
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool)

Example 9 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class LeftRecursiveRuleAnalyzer method loadPrecRuleTemplates.

public void loadPrecRuleTemplates() {
    String templateGroupFile = "org/antlr/v4/tool/templates/LeftRecursiveRules.stg";
    recRuleTemplates = new STGroupFile(templateGroupFile);
    if (!recRuleTemplates.isDefined("recRule")) {
        tool.errMgr.toolError(ErrorType.MISSING_CODE_GEN_TEMPLATES, "LeftRecursiveRules");
    }
    // use codegen to get correct language templates; that's it though
    CodeGenerator gen = new CodeGenerator(tool, null, language);
    codegenTemplates = gen.getTemplates();
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) STGroupFile(org.stringtemplate.v4.STGroupFile)

Example 10 with Tool

use of org.antlr.v4.Tool 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

Tool (org.antlr.v4.Tool)9 Grammar (org.antlr.v4.tool.Grammar)7 LexerGrammar (org.antlr.v4.tool.LexerGrammar)7 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 File (java.io.File)5 RecognitionException (org.antlr.runtime.RecognitionException)3 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)2 GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2 ST (org.stringtemplate.v4.ST)2 STGroupFile (org.stringtemplate.v4.STGroupFile)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Field (java.lang.reflect.Field)1 LinkedHashMap (java.util.LinkedHashMap)1