Search in sources :

Example 16 with Action

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());
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ActionAST(org.antlr.v4.tool.ast.ActionAST) TerminalAST(org.antlr.v4.tool.ast.TerminalAST) RangeAST(org.antlr.v4.tool.ast.RangeAST) ST(org.stringtemplate.v4.ST) LexerAction(org.antlr.v4.runtime.atn.LexerAction)

Example 17 with Action

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())));
    }
}
Also used : ListLabelRef(org.antlr.v4.codegen.model.chunk.ListLabelRef) Attribute(org.antlr.v4.tool.Attribute) TokenRef(org.antlr.v4.codegen.model.chunk.TokenRef) QRetValueRef(org.antlr.v4.codegen.model.chunk.QRetValueRef) RetValueRef(org.antlr.v4.codegen.model.chunk.RetValueRef) Rule(org.antlr.v4.tool.Rule) ArgRef(org.antlr.v4.codegen.model.chunk.ArgRef) LabelRef(org.antlr.v4.codegen.model.chunk.LabelRef) ListLabelRef(org.antlr.v4.codegen.model.chunk.ListLabelRef) LocalRef(org.antlr.v4.codegen.model.chunk.LocalRef)

Example 18 with Action

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);
}
Also used : TokenPropertyRef_int(org.antlr.v4.codegen.model.chunk.TokenPropertyRef_int)

Example 19 with Action

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());
    }
}
Also used : ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) STGroup(org.stringtemplate.v4.STGroup) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) STGroupString(org.stringtemplate.v4.STGroupString)

Example 20 with Action

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());
    }
}
Also used : ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) STGroup(org.stringtemplate.v4.STGroup) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) STGroupString(org.stringtemplate.v4.STGroupString)

Aggregations

ST (org.stringtemplate.v4.ST)11 ActionAST (org.antlr.v4.tool.ast.ActionAST)9 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)9 Grammar (org.antlr.v4.tool.Grammar)8 LexerGrammar (org.antlr.v4.tool.LexerGrammar)8 STGroup (org.stringtemplate.v4.STGroup)8 ArrayList (java.util.ArrayList)7 ATNFactory (org.antlr.v4.automata.ATNFactory)6 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)6 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)6 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)6 ATNState (org.antlr.v4.runtime.atn.ATNState)6 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)6 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 Rule (org.antlr.v4.tool.Rule)5 HashMap (java.util.HashMap)4 Action (org.antlr.v4.codegen.model.Action)4