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;
}
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);
}
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;
}
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");
}
}
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));
}
}
Aggregations