Search in sources :

Example 1 with AltLabelStructDecl

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

use of org.antlr.v4.codegen.model.decl.AltLabelStructDecl 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)

Example 3 with AltLabelStructDecl

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

the class RuleFunction method addContextGetters.

public void addContextGetters(OutputModelFactory factory, Rule r) {
    // Add ctx labels for elements in alts with no -> label
    List<AltAST> altsNoLabels = r.getUnlabeledAltASTs();
    if (altsNoLabels != null) {
        Set<Decl> decls = getDeclsForAllElements(altsNoLabels);
        // we know to put in rule ctx, so do it directly
        for (Decl d : decls) ruleCtx.addDecl(d);
    }
    // make structs for -> labeled alts, define ctx labels for elements
    altLabelCtxs = new HashMap<String, AltLabelStructDecl>();
    Map<String, List<Pair<Integer, AltAST>>> labels = r.getAltLabels();
    if (labels != null) {
        for (Map.Entry<String, List<Pair<Integer, AltAST>>> entry : labels.entrySet()) {
            String label = entry.getKey();
            List<AltAST> alts = new ArrayList<AltAST>();
            for (Pair<Integer, AltAST> pair : entry.getValue()) {
                alts.add(pair.b);
            }
            Set<Decl> decls = getDeclsForAllElements(alts);
            for (Pair<Integer, AltAST> pair : entry.getValue()) {
                Integer altNum = pair.a;
                altToContext[altNum] = new AltLabelStructDecl(factory, r, altNum, label);
                if (!altLabelCtxs.containsKey(label)) {
                    altLabelCtxs.put(label, altToContext[altNum]);
                }
                // we know which ctx to put in, so do it directly
                for (Decl d : decls) {
                    altToContext[altNum].addDecl(d);
                }
            }
        }
    }
}
Also used : 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) ArrayList(java.util.ArrayList) List(java.util.List) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with AltLabelStructDecl

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

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

AltLabelStructDecl (org.antlr.v4.codegen.model.decl.AltLabelStructDecl)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 AttributeDecl (org.antlr.v4.codegen.model.decl.AttributeDecl)2 ContextRuleGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl)2 ContextRuleListGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl)2 ContextRuleListIndexedGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl)2 ContextTokenGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl)2 ContextTokenListGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl)2 ContextTokenListIndexedGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl)2 Decl (org.antlr.v4.codegen.model.decl.Decl)2 StructDecl (org.antlr.v4.codegen.model.decl.StructDecl)2 AltAST (org.antlr.v4.tool.ast.AltAST)2 LinkedHashMap (java.util.LinkedHashMap)1 RecognitionException (org.antlr.runtime.RecognitionException)1 RuleAST (org.antlr.v4.tool.ast.RuleAST)1