Search in sources :

Example 76 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class RuleFunction method getLabelName.

public static String getLabelName(Grammar g, GrammarAST t) {
    String labelName = t.getText();
    Rule referencedRule = g.rules.get(labelName);
    if (referencedRule != null) {
        labelName = referencedRule.getBaseContext();
    }
    return labelName;
}
Also used : Rule(org.antlr.v4.tool.Rule)

Example 77 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class RuleFunction method addContextDecl.

/**
 * Add decl to struct ctx for rule or alt if labeled
 */
public void addContextDecl(String altLabel, Decl d) {
    CodeBlockForOuterMostAlt alt = d.getOuterMostAltCodeBlock();
    // if we found code blk and might be alt label, try to add to that label ctx
    if (alt != null && altLabelCtxs != null) {
        // System.out.println(d.name+" lives in alt "+alt.alt.altNum);
        AltLabelStructDecl altCtx = altLabelCtxs.get(altLabel);
        if (altCtx != null) {
            // we have an alt ctx
            // System.out.println("ctx is "+ altCtx.name);
            altCtx.addDecl(d);
            return;
        }
    }
    // stick in overall rule's ctx
    ruleCtx.addDecl(d);
}
Also used : AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl)

Example 78 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class GrammarASTAdaptor method create.

@Override
public /**
 * Make sure even imaginary nodes know the input stream
 */
GrammarAST create(int tokenType, String text) {
    GrammarAST t;
    if (tokenType == ANTLRParser.RULE) {
        // needed by TreeWizard to make RULE tree
        t = new RuleAST(new CommonToken(tokenType, text));
    } else if (tokenType == ANTLRParser.STRING_LITERAL) {
        // implicit lexer construction done with wizard; needs this node type
        // whereas grammar ANTLRParser.g can use token option to spec node type
        t = new TerminalAST(new CommonToken(tokenType, text));
    } else {
        t = (GrammarAST) super.create(tokenType, text);
    }
    t.token.setInputStream(input);
    return t;
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CommonToken(org.antlr.runtime.CommonToken) TerminalAST(org.antlr.v4.tool.ast.TerminalAST)

Example 79 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class TestRig method process.

protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, CharStream input) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
    lexer.setInputStream(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    if (showTokens) {
        for (Token tok : tokens.getTokens()) {
            if (tok instanceof CommonToken) {
                System.out.println(((CommonToken) tok).toString(lexer));
            } else {
                System.out.println(tok.toString());
            }
        }
    }
    if (startRuleName.equals(LEXER_START_RULE_NAME))
        return;
    if (diagnostics) {
        parser.addErrorListener(new DiagnosticErrorListener());
        parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    }
    if (printTree || gui || psFile != null) {
        parser.setBuildParseTree(true);
    }
    if (SLL) {
        // overrides diagnostics
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    }
    parser.setInputStream(tokens);
    parser.setTrace(trace);
    try {
        Method startRule = parserClass.getMethod(startRuleName);
        ParserRuleContext tree = (ParserRuleContext) startRule.invoke(parser, (Object[]) null);
        if (printTree) {
            System.out.println(tree.toStringTree(parser));
        }
        if (gui) {
            Trees.inspect(tree, parser);
        }
        if (psFile != null) {
            // Generate postscript
            Trees.save(tree, parser, psFile);
        }
    } catch (NoSuchMethodException nsme) {
        System.err.println("No method for rule " + startRuleName + " or it has arguments");
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DiagnosticErrorListener(org.antlr.v4.runtime.DiagnosticErrorListener) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token) Method(java.lang.reflect.Method) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 80 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.

the class AltLabelStructDecl method addDispatchMethods.

@Override
public void addDispatchMethods(Rule r) {
    dispatchMethods = new ArrayList<DispatchMethod>();
    if (factory.getGrammar().tool.gen_listener) {
        dispatchMethods.add(new ListenerDispatchMethod(factory, true));
        dispatchMethods.add(new ListenerDispatchMethod(factory, false));
    }
    if (factory.getGrammar().tool.gen_visitor) {
        dispatchMethods.add(new VisitorDispatchMethod(factory));
    }
}
Also used : VisitorDispatchMethod(org.antlr.v4.codegen.model.VisitorDispatchMethod) ListenerDispatchMethod(org.antlr.v4.codegen.model.ListenerDispatchMethod) ListenerDispatchMethod(org.antlr.v4.codegen.model.ListenerDispatchMethod) VisitorDispatchMethod(org.antlr.v4.codegen.model.VisitorDispatchMethod) DispatchMethod(org.antlr.v4.codegen.model.DispatchMethod)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)100 Rule (org.antlr.v4.tool.Rule)95 Test (org.junit.Test)94 ATN (org.antlr.v4.runtime.atn.ATN)87 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)69 ArrayList (java.util.ArrayList)59 Grammar (org.antlr.v4.tool.Grammar)58 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)51 ATNState (org.antlr.v4.runtime.atn.ATNState)42 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)37 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)36 Token (org.antlr.v4.runtime.Token)21 ActionAST (org.antlr.v4.tool.ast.ActionAST)21 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)20 ParseTree (org.antlr.v4.runtime.tree.ParseTree)19 RuleAST (org.antlr.v4.tool.ast.RuleAST)19 HashMap (java.util.HashMap)18 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)16 AltAST (org.antlr.v4.tool.ast.AltAST)16 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)14