Search in sources :

Example 1 with ActionChunk

use of org.antlr.v4.codegen.model.chunk.ActionChunk 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 2 with ActionChunk

use of org.antlr.v4.codegen.model.chunk.ActionChunk in project antlr4 by antlr.

the class ActionTranslator method translateActionChunk.

public static List<ActionChunk> translateActionChunk(OutputModelFactory factory, RuleFunction rf, String action, ActionAST node) {
    Token tokenWithinAction = node.token;
    ActionTranslator translator = new ActionTranslator(factory, node);
    translator.rf = rf;
    factory.getGrammar().tool.log("action-translator", "translate " + action);
    String altLabel = node.getAltLabel();
    if (rf != null) {
        translator.nodeContext = rf.ruleCtx;
        if (altLabel != null)
            translator.nodeContext = rf.altLabelCtxs.get(altLabel);
    }
    ANTLRStringStream in = new ANTLRStringStream(action);
    in.setLine(tokenWithinAction.getLine());
    in.setCharPositionInLine(tokenWithinAction.getCharPositionInLine());
    ActionSplitter trigger = new ActionSplitter(in, translator);
    // forces eval, triggers listener methods
    trigger.getActionTokens();
    return translator.chunks;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) ActionSplitter(org.antlr.v4.parse.ActionSplitter) Token(org.antlr.runtime.Token)

Example 3 with ActionChunk

use of org.antlr.v4.codegen.model.chunk.ActionChunk in project antlr4 by antlr.

the class ActionTranslator method setNonLocalAttr.

@Override
public void setNonLocalAttr(String expr, Token x, Token y, Token rhs) {
    gen.g.tool.log("action-translator", "setNonLocalAttr " + x + "::" + y + "=" + rhs);
    Rule r = factory.getGrammar().getRule(x.getText());
    List<ActionChunk> rhsChunks = translateActionChunk(factory, rf, rhs.getText(), node);
    SetNonLocalAttr s = new SetNonLocalAttr(nodeContext, x.getText(), y.getText(), r.index, rhsChunks);
    chunks.add(s);
}
Also used : ActionChunk(org.antlr.v4.codegen.model.chunk.ActionChunk) Rule(org.antlr.v4.tool.Rule) SetNonLocalAttr(org.antlr.v4.codegen.model.chunk.SetNonLocalAttr)

Example 4 with ActionChunk

use of org.antlr.v4.codegen.model.chunk.ActionChunk in project antlr4 by antlr.

the class ActionTranslator method setAttr.

@Override
public void setAttr(String expr, Token x, Token rhs) {
    gen.g.tool.log("action-translator", "setAttr " + x + " " + rhs);
    List<ActionChunk> rhsChunks = translateActionChunk(factory, rf, rhs.getText(), node);
    SetAttr s = new SetAttr(nodeContext, x.getText(), rhsChunks);
    chunks.add(s);
}
Also used : SetAttr(org.antlr.v4.codegen.model.chunk.SetAttr) ActionChunk(org.antlr.v4.codegen.model.chunk.ActionChunk)

Aggregations

ActionChunk (org.antlr.v4.codegen.model.chunk.ActionChunk)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 Token (org.antlr.runtime.Token)1 SetAttr (org.antlr.v4.codegen.model.chunk.SetAttr)1 SetNonLocalAttr (org.antlr.v4.codegen.model.chunk.SetNonLocalAttr)1 TokenPropertyRef_int (org.antlr.v4.codegen.model.chunk.TokenPropertyRef_int)1 ActionSplitter (org.antlr.v4.parse.ActionSplitter)1 Rule (org.antlr.v4.tool.Rule)1