Search in sources :

Example 1 with Decl

use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.

the class ParserFactory method set.

@Override
public List<SrcOp> set(GrammarAST setAST, GrammarAST labelAST, boolean invert) {
    MatchSet matchOp;
    if (invert)
        matchOp = new MatchNotSet(this, setAST);
    else
        matchOp = new MatchSet(this, setAST);
    if (labelAST != null) {
        String label = labelAST.getText();
        RuleFunction rf = getCurrentRuleFunction();
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            defineImplicitLabel(setAST, matchOp);
            TokenListDecl l = getTokenListLabelDecl(label);
            rf.addContextDecl(setAST.getAltLabel(), l);
        } else {
            Decl d = getTokenLabelDecl(label);
            matchOp.labels.add(d);
            rf.addContextDecl(setAST.getAltLabel(), d);
        }
    }
    if (controller.needsImplicitLabel(setAST, matchOp))
        defineImplicitLabel(setAST, matchOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
    return list(matchOp, listLabelOp);
}
Also used : MatchSet(org.antlr.v4.codegen.model.MatchSet) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) LeftFactoredRuleFunction(org.antlr.v4.codegen.model.LeftFactoredRuleFunction) LeftUnfactoredRuleFunction(org.antlr.v4.codegen.model.LeftUnfactoredRuleFunction) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) MatchNotSet(org.antlr.v4.codegen.model.MatchNotSet) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 2 with Decl

use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.

the class ParserFactory method wildcard.

@Override
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST) {
    Wildcard wild = new Wildcard(this, ast);
    // TODO: dup with tokenRef
    if (labelAST != null) {
        String label = labelAST.getText();
        Decl d = getTokenLabelDecl(label);
        wild.labels.add(d);
        getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d);
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            TokenListDecl l = getTokenListLabelDecl(label);
            getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), l);
        }
    }
    if (controller.needsImplicitLabel(ast, wild))
        defineImplicitLabel(ast, wild);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(wild, labelAST);
    return list(wild, listLabelOp);
}
Also used : Wildcard(org.antlr.v4.codegen.model.Wildcard) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 3 with Decl

use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.

the class ParserFactory method tokenRef.

@Override
public List<SrcOp> tokenRef(GrammarAST ID, GrammarAST labelAST, GrammarAST args) {
    MatchToken matchOp = new MatchToken(this, (TerminalAST) ID);
    if (labelAST != null) {
        String label = labelAST.getText();
        RuleFunction rf = getCurrentRuleFunction();
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            // add Token _X and List<Token> X decls
            // adds _X
            defineImplicitLabel(ID, matchOp);
            TokenListDecl l = getTokenListLabelDecl(label);
            rf.addContextDecl(ID.getAltLabel(), l);
        } else {
            Decl d = getTokenLabelDecl(label);
            matchOp.labels.add(d);
            rf.addContextDecl(ID.getAltLabel(), d);
        }
    // Decl d = getTokenLabelDecl(label);
    // ((MatchToken)matchOp).labels.add(d);
    // getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), d);
    // if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
    // TokenListDecl l = getTokenListLabelDecl(label);
    // getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), l);
    // }
    }
    if (controller.needsImplicitLabel(ID, matchOp))
        defineImplicitLabel(ID, matchOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
    return list(matchOp, listLabelOp);
}
Also used : TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) MatchToken(org.antlr.v4.codegen.model.MatchToken) LeftFactoredRuleFunction(org.antlr.v4.codegen.model.LeftFactoredRuleFunction) LeftUnfactoredRuleFunction(org.antlr.v4.codegen.model.LeftUnfactoredRuleFunction) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 4 with Decl

use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.

the class RuleFunction method addContextGetters.

public void addContextGetters(OutputModelFactory factory, Collection<RuleAST> contextASTs) {
    List<AltAST> unlabeledAlternatives = new ArrayList<AltAST>();
    Map<String, List<AltAST>> labeledAlternatives = new LinkedHashMap<String, List<AltAST>>();
    for (RuleAST ast : contextASTs) {
        try {
            unlabeledAlternatives.addAll(rule.g.getUnlabeledAlternatives(ast));
            for (Map.Entry<String, List<Tuple2<Integer, AltAST>>> entry : rule.g.getLabeledAlternatives(ast).entrySet()) {
                List<AltAST> list = labeledAlternatives.get(entry.getKey());
                if (list == null) {
                    list = new ArrayList<AltAST>();
                    labeledAlternatives.put(entry.getKey(), list);
                }
                for (Tuple2<Integer, AltAST> tuple : entry.getValue()) {
                    list.add(tuple.getItem2());
                }
            }
        } catch (RecognitionException ex) {
        }
    }
    // Add ctx labels for elements in alts with no '#' label
    if (!unlabeledAlternatives.isEmpty()) {
        Set<Decl> decls = getDeclsForAllElements(unlabeledAlternatives);
        // put directly in base context
        for (Decl decl : decls) {
            ruleCtx.addDecl(decl);
        }
    }
    // make structs for '#' labeled alts, define ctx labels for elements
    altLabelCtxs = new LinkedHashMap<String, AltLabelStructDecl>();
    if (!labeledAlternatives.isEmpty()) {
        for (Map.Entry<String, List<AltAST>> entry : labeledAlternatives.entrySet()) {
            AltLabelStructDecl labelDecl = new AltLabelStructDecl(factory, rule, entry.getKey());
            altLabelCtxs.put(entry.getKey(), labelDecl);
            Set<Decl> decls = getDeclsForAllElements(entry.getValue());
            for (Decl decl : decls) {
                labelDecl.addDecl(decl);
            }
        }
    }
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) ArrayList(java.util.ArrayList) ContextRuleListGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) StructDecl(org.antlr.v4.codegen.model.decl.StructDecl) AttributeDecl(org.antlr.v4.codegen.model.decl.AttributeDecl) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) ContextTokenListGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl) ContextRuleListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl) ContextRuleGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl) ContextTokenListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl) ContextTokenGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl) AltAST(org.antlr.v4.tool.ast.AltAST) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) RecognitionException(org.antlr.runtime.RecognitionException)

Example 5 with Decl

use of org.antlr.v4.codegen.model.decl.Decl 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);
}
Also used : AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl)

Aggregations

Decl (org.antlr.v4.codegen.model.decl.Decl)18 RuleContextDecl (org.antlr.v4.codegen.model.decl.RuleContextDecl)10 TokenDecl (org.antlr.v4.codegen.model.decl.TokenDecl)10 TokenListDecl (org.antlr.v4.codegen.model.decl.TokenListDecl)10 AltLabelStructDecl (org.antlr.v4.codegen.model.decl.AltLabelStructDecl)8 ArrayList (java.util.ArrayList)6 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)6 AttributeDecl (org.antlr.v4.codegen.model.decl.AttributeDecl)6 ContextRuleGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl)6 ContextRuleListGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl)6 ContextRuleListIndexedGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl)6 ContextTokenGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl)6 ContextTokenListGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl)6 ContextTokenListIndexedGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl)6 StructDecl (org.antlr.v4.codegen.model.decl.StructDecl)6 LeftRecursiveRuleFunction (org.antlr.v4.codegen.model.LeftRecursiveRuleFunction)4 RuleFunction (org.antlr.v4.codegen.model.RuleFunction)4 Attribute (org.antlr.v4.tool.Attribute)4 Rule (org.antlr.v4.tool.Rule)4 AltAST (org.antlr.v4.tool.ast.AltAST)4