use of org.antlr.v4.codegen.model.Action in project antlr4 by antlr.
the class LexerATNFactory method lexerCallCommand.
@Override
public Handle lexerCallCommand(GrammarAST ID, GrammarAST arg) {
LexerAction lexerAction = createLexerAction(ID, arg);
if (lexerAction != null) {
return action(ID, lexerAction);
}
// fall back to standard action generation for the command
ST cmdST = codegenTemplates.getInstanceOf("Lexer" + CharSupport.capitalize(ID.getText()) + "Command");
if (cmdST == null) {
g.tool.errMgr.grammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText());
return epsilon(ID);
}
if (cmdST.impl.formalArguments == null || !cmdST.impl.formalArguments.containsKey("arg")) {
g.tool.errMgr.grammarError(ErrorType.UNWANTED_LEXER_COMMAND_ARGUMENT, g.fileName, ID.token, ID.getText());
return epsilon(ID);
}
cmdST.add("arg", arg.getText());
cmdST.add("grammar", arg.g);
return action(cmdST.render());
}
use of org.antlr.v4.codegen.model.Action in project antlr4 by antlr.
the class ActionTranslator method attr.
@Override
public void attr(String expr, Token x) {
gen.g.tool.log("action-translator", "attr " + x);
Attribute a = node.resolver.resolveToAttribute(x.getText(), node);
if (a != null) {
switch(a.dict.type) {
case ARG:
chunks.add(new ArgRef(nodeContext, x.getText()));
break;
case RET:
chunks.add(new RetValueRef(rf.ruleCtx, x.getText()));
break;
case LOCAL:
chunks.add(new LocalRef(nodeContext, x.getText()));
break;
case PREDEFINED_RULE:
chunks.add(getRulePropertyRef(x));
break;
}
}
if (node.resolver.resolvesToToken(x.getText(), node)) {
// $label
chunks.add(new TokenRef(nodeContext, getTokenLabel(x.getText())));
return;
}
if (node.resolver.resolvesToLabel(x.getText(), node)) {
// $x for x=ID etc...
chunks.add(new LabelRef(nodeContext, getTokenLabel(x.getText())));
return;
}
if (node.resolver.resolvesToListLabel(x.getText(), node)) {
// $ids for ids+=ID etc...
chunks.add(new ListLabelRef(nodeContext, x.getText()));
return;
}
Rule r = factory.getGrammar().getRule(x.getText());
if (r != null) {
// $r for r rule ref
chunks.add(new LabelRef(nodeContext, getRuleLabel(x.getText())));
}
}
use of org.antlr.v4.codegen.model.Action in project antlr4 by antlr.
the class ActionTranslator method translateAction.
public static List<ActionChunk> translateAction(OutputModelFactory factory, RuleFunction rf, Token tokenWithinAction, ActionAST node) {
String action = tokenWithinAction.getText();
if (action != null && action.length() > 0 && action.charAt(0) == '{') {
int firstCurly = action.indexOf('{');
int lastCurly = action.lastIndexOf('}');
if (firstCurly >= 0 && lastCurly >= 0) {
// trim {...}
action = action.substring(firstCurly + 1, lastCurly);
}
}
return translateActionChunk(factory, rf, action, node);
}
use of org.antlr.v4.codegen.model.Action in project antlr4 by antlr.
the class BaseCppTest method testActions.
public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException {
int lp = templates.indexOf('(');
String name = templates.substring(0, lp);
STGroup group = new STGroupString(templates);
ST st = group.getInstanceOf(name);
st.add(actionName, action);
String grammar = st.render();
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammar, equeue);
if (g.ast != null && !g.ast.hasErrors) {
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
ATNFactory factory = new ParserATNFactory(g);
if (g.isLexer())
factory = new LexerATNFactory((LexerGrammar) g);
g.atn = factory.createATN();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser();
String output = outputFileST.render();
//System.out.println(output);
String b = "#" + actionName + "#";
int start = output.indexOf(b);
String e = "#end-" + actionName + "#";
int end = output.indexOf(e);
String snippet = output.substring(start + b.length(), end);
assertEquals(expected, snippet);
}
if (equeue.size() > 0) {
System.err.println(equeue.toString());
}
}
use of org.antlr.v4.codegen.model.Action in project antlr4 by antlr.
the class BaseBrowserTest method testActions.
public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException {
int lp = templates.indexOf('(');
String name = templates.substring(0, lp);
STGroup group = new STGroupString(templates);
ST st = group.getInstanceOf(name);
st.add(actionName, action);
String grammar = st.render();
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammar, equeue);
if (g.ast != null && !g.ast.hasErrors) {
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
ATNFactory factory = new ParserATNFactory(g);
if (g.isLexer())
factory = new LexerATNFactory((LexerGrammar) g);
g.atn = factory.createATN();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser();
String output = outputFileST.render();
//System.out.println(output);
String b = "#" + actionName + "#";
int start = output.indexOf(b);
String e = "#end-" + actionName + "#";
int end = output.indexOf(e);
String snippet = output.substring(start + b.length(), end);
assertEquals(expected, snippet);
}
if (equeue.size() > 0) {
System.err.println(equeue.toString());
}
}
Aggregations