Search in sources :

Example 11 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class UseDefAnalyzer method actionIsContextDependent.

public static boolean actionIsContextDependent(ActionAST actionAST) {
    ANTLRStringStream in = new ANTLRStringStream(actionAST.token.getText());
    in.setLine(actionAST.token.getLine());
    in.setCharPositionInLine(actionAST.token.getCharPositionInLine());
    // can't be simple bool with anon class
    final boolean[] dependent = new boolean[] { false };
    ActionSplitterListener listener = new BlankActionSplitterListener() {

        @Override
        public void nonLocalAttr(String expr, Token x, Token y) {
            dependent[0] = true;
        }

        @Override
        public void qualifiedAttr(String expr, Token x, Token y) {
            dependent[0] = true;
        }

        @Override
        public void setAttr(String expr, Token x, Token rhs) {
            dependent[0] = true;
        }

        @Override
        public void setExprAttribute(String expr) {
            dependent[0] = true;
        }

        @Override
        public void setNonLocalAttr(String expr, Token x, Token y, Token rhs) {
            dependent[0] = true;
        }

        @Override
        public void attr(String expr, Token x) {
            dependent[0] = true;
        }
    };
    ActionSplitter splitter = new ActionSplitter(in, listener);
    // forces eval, triggers listener methods
    splitter.getActionTokens();
    return dependent[0];
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) ActionSplitter(org.antlr.v4.parse.ActionSplitter) ActionSplitterListener(org.antlr.v4.parse.ActionSplitterListener) Token(org.antlr.runtime.Token)

Example 12 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class OutputModelController method buildRuleFunction.

/** Create RuleFunction per rule and update sempreds,actions of parser
	 *  output object with stuff found in r.
	 */
public void buildRuleFunction(Parser parser, Rule r) {
    RuleFunction function = rule(r);
    parser.funcs.add(function);
    pushCurrentRule(function);
    function.fillNamedActions(delegate, r);
    if (r instanceof LeftRecursiveRule) {
        buildLeftRecursiveRuleFunction((LeftRecursiveRule) r, (LeftRecursiveRuleFunction) function);
    } else {
        buildNormalRuleFunction(r, function);
    }
    Grammar g = getGrammar();
    for (ActionAST a : r.actions) {
        if (a instanceof PredAST) {
            PredAST p = (PredAST) a;
            RuleSempredFunction rsf = parser.sempredFuncs.get(r);
            if (rsf == null) {
                rsf = new RuleSempredFunction(delegate, r, function.ctxType);
                parser.sempredFuncs.put(r, rsf);
            }
            rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
        }
    }
    popCurrentRule();
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Action(org.antlr.v4.codegen.model.Action) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) PredAST(org.antlr.v4.tool.ast.PredAST) RuleSempredFunction(org.antlr.v4.codegen.model.RuleSempredFunction) Grammar(org.antlr.v4.tool.Grammar) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 13 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class OutputModelController method buildLexerRuleActions.

public void buildLexerRuleActions(Lexer lexer, final Rule r) {
    if (r.actions.isEmpty()) {
        return;
    }
    CodeGenerator gen = delegate.getGenerator();
    Grammar g = delegate.getGrammar();
    String ctxType = gen.getTarget().getRuleFunctionContextStructName(r);
    RuleActionFunction raf = lexer.actionFuncs.get(r);
    if (raf == null) {
        raf = new RuleActionFunction(delegate, r, ctxType);
    }
    for (ActionAST a : r.actions) {
        if (a instanceof PredAST) {
            PredAST p = (PredAST) a;
            RuleSempredFunction rsf = lexer.sempredFuncs.get(r);
            if (rsf == null) {
                rsf = new RuleSempredFunction(delegate, r, ctxType);
                lexer.sempredFuncs.put(r, rsf);
            }
            rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
        } else if (a.getType() == ANTLRParser.ACTION) {
            raf.actions.put(g.lexerActions.get(a), new Action(delegate, a));
        }
    }
    if (!raf.actions.isEmpty() && !lexer.actionFuncs.containsKey(r)) {
        // only add to lexer if the function actually contains actions
        lexer.actionFuncs.put(r, raf);
    }
}
Also used : RuleActionFunction(org.antlr.v4.codegen.model.RuleActionFunction) Action(org.antlr.v4.codegen.model.Action) PredAST(org.antlr.v4.tool.ast.PredAST) RuleSempredFunction(org.antlr.v4.codegen.model.RuleSempredFunction) Grammar(org.antlr.v4.tool.Grammar) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 14 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class ScopeParser method parse.

public static AttributeDict parse(ActionAST action, String s, char separator, Grammar g) {
    AttributeDict dict = new AttributeDict();
    List<Pair<String, Integer>> decls = splitDecls(s, separator);
    for (Pair<String, Integer> decl : decls) {
        if (decl.a.trim().length() > 0) {
            Attribute a = parseAttributeDef(action, decl, g);
            dict.add(a);
        }
    }
    return dict;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) AttributeDict(org.antlr.v4.tool.AttributeDict) Pair(org.antlr.v4.runtime.misc.Pair)

Example 15 with ActionAST

use of org.antlr.v4.tool.ast.ActionAST in project antlr4 by antlr.

the class ScopeParser method parseAttributeDef.

/**
	 * For decls like "String foo" or "char *foo32[]" compute the ID
	 * and type declarations.  Also handle "int x=3" and 'T t = new T("foo")'
	 * but if the separator is ',' you cannot use ',' in the initvalue
	 * unless you escape use "\," escape.
	 */
public static Attribute parseAttributeDef(ActionAST action, Pair<String, Integer> decl, Grammar g) {
    if (decl.a == null)
        return null;
    Attribute attr = new Attribute();
    int rightEdgeOfDeclarator = decl.a.length() - 1;
    int equalsIndex = decl.a.indexOf('=');
    if (equalsIndex > 0) {
        // everything after the '=' is the init value
        attr.initValue = decl.a.substring(equalsIndex + 1, decl.a.length()).trim();
        rightEdgeOfDeclarator = equalsIndex - 1;
    }
    String declarator = decl.a.substring(0, rightEdgeOfDeclarator + 1);
    Pair<Integer, Integer> p;
    String text = decl.a;
    text = text.replaceAll("::", "");
    if (text.contains(":")) {
        // declarator has type appearing after the name like "x:T"
        p = _parsePostfixDecl(attr, declarator, action, g);
    } else {
        // declarator has type appearing before the name like "T x"
        p = _parsePrefixDecl(attr, declarator, action, g);
    }
    int idStart = p.a;
    int idStop = p.b;
    attr.decl = decl.a;
    if (action != null) {
        String actionText = action.getText();
        int[] lines = new int[actionText.length()];
        int[] charPositionInLines = new int[actionText.length()];
        for (int i = 0, line = 0, col = 0; i < actionText.length(); i++, col++) {
            lines[i] = line;
            charPositionInLines[i] = col;
            if (actionText.charAt(i) == '\n') {
                line++;
                col = -1;
            }
        }
        int[] charIndexes = new int[actionText.length()];
        for (int i = 0, j = 0; i < actionText.length(); i++, j++) {
            charIndexes[j] = i;
            // skip comments
            if (i < actionText.length() - 1 && actionText.charAt(i) == '/' && actionText.charAt(i + 1) == '/') {
                while (i < actionText.length() && actionText.charAt(i) != '\n') {
                    i++;
                }
            }
        }
        int declOffset = charIndexes[decl.b];
        int declLine = lines[declOffset + idStart];
        int line = action.getToken().getLine() + declLine;
        int charPositionInLine = charPositionInLines[declOffset + idStart];
        if (declLine == 0) {
            /* offset for the start position of the ARG_ACTION token, plus 1
				 * since the ARG_ACTION text had the leading '[' stripped before
				 * reaching the scope parser.
				 */
            charPositionInLine += action.getToken().getCharPositionInLine() + 1;
        }
        int offset = ((CommonToken) action.getToken()).getStartIndex();
        attr.token = new CommonToken(action.getToken().getInputStream(), ANTLRParser.ID, BaseRecognizer.DEFAULT_TOKEN_CHANNEL, offset + declOffset + idStart + 1, offset + declOffset + idStop);
        attr.token.setLine(line);
        attr.token.setCharPositionInLine(charPositionInLine);
        assert attr.name.equals(attr.token.getText()) : "Attribute text should match the pseudo-token text at this point.";
    }
    return attr;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) CommonToken(org.antlr.runtime.CommonToken)

Aggregations

ActionAST (org.antlr.v4.tool.ast.ActionAST)11 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)5 Rule (org.antlr.v4.tool.Rule)3 PredAST (org.antlr.v4.tool.ast.PredAST)3 HashMap (java.util.HashMap)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 CommonToken (org.antlr.runtime.CommonToken)2 Token (org.antlr.runtime.Token)2 Action (org.antlr.v4.codegen.model.Action)2 RuleSempredFunction (org.antlr.v4.codegen.model.RuleSempredFunction)2 ActionSplitter (org.antlr.v4.parse.ActionSplitter)2 ATNState (org.antlr.v4.runtime.atn.ATNState)2 Alternative (org.antlr.v4.tool.Alternative)2 Attribute (org.antlr.v4.tool.Attribute)2 Grammar (org.antlr.v4.tool.Grammar)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1