Exemple #1
0
        /** $x.y, x can be surrounding rule, token/rule/label ref. y is visible
         *  attr in that dictionary.  Can't see args on rule refs.
         */
        public virtual Attribute ResolveToAttribute(string x, string y, ActionAST node)
        {
            if (tokenRefs.ContainsKey(x) && tokenRefs[x] != null)
            {
                // token ref in this alt?
                return(rule.GetPredefinedScope(LabelType.TOKEN_LABEL).Get(y));
            }

            if (ruleRefs.ContainsKey(x) && ruleRefs[x] != null)
            {
                // rule ref in this alt?
                // look up rule, ask it to resolve y (must be retval or predefined)
                return(rule.g.GetRule(x).ResolveRetvalOrProperty(y));
            }

            LabelElementPair anyLabelDef = GetAnyLabelDef(x);

            if (anyLabelDef != null && anyLabelDef.type == LabelType.RULE_LABEL)
            {
                return(rule.g.GetRule(anyLabelDef.element.Text).ResolveRetvalOrProperty(y));
            }
            else if (anyLabelDef != null)
            {
                AttributeDict scope = rule.GetPredefinedScope(anyLabelDef.type);
                if (scope == null)
                {
                    return(null);
                }

                return(scope.Get(y));
            }
            return(null);
        }
Exemple #2
0
        public virtual bool ResolvesToListLabel(string x, ActionAST node)
        {
            LabelElementPair anyLabelDef = GetAnyLabelDef(x);

            return(anyLabelDef != null &&
                   (anyLabelDef.type == LabelType.RULE_LIST_LABEL ||
                    anyLabelDef.type == LabelType.TOKEN_LIST_LABEL));
        }
Exemple #3
0
        public virtual bool ResolvesToToken(string x, ActionAST node)
        {
            LabelElementPair anyLabelDef = GetAnyLabelDef(x);

            if (anyLabelDef != null && anyLabelDef.type == LabelType.TOKEN_LABEL)
            {
                return(true);
            }
            return(false);
        }
Exemple #4
0
        public virtual Rule resolveToRule(string x)
        {
            if (x.Equals(this.name))
            {
                return(this);
            }
            LabelElementPair anyLabelDef = GetAnyLabelDef(x);

            if (anyLabelDef != null && anyLabelDef.type == LabelType.RULE_LABEL)
            {
                return(g.GetRule(anyLabelDef.element.Text));
            }
            return(g.GetRule(x));
        }
Exemple #5
0
        public virtual bool ResolvesToToken(string x, ActionAST node)
        {
            if (tokenRefs.ContainsKey(x) && tokenRefs[x] != null)
            {
                return(true);
            }

            LabelElementPair anyLabelDef = GetAnyLabelDef(x);

            if (anyLabelDef != null && anyLabelDef.type == LabelType.TOKEN_LABEL)
            {
                return(true);
            }

            return(false);
        }
Exemple #6
0
        /** x can be ruleref or rule label. */
        public virtual Rule ResolveToRule(string x)
        {
            if (ruleRefs.ContainsKey(x) && ruleRefs[x] != null)
            {
                return(rule.g.GetRule(x));
            }

            LabelElementPair anyLabelDef = GetAnyLabelDef(x);

            if (anyLabelDef != null && anyLabelDef.type == LabelType.RULE_LABEL)
            {
                return(rule.g.GetRule(anyLabelDef.element.Text));
            }

            return(null);
        }
Exemple #7
0
        public virtual bool ResolvesToAttributeDict(string x, ActionAST node)
        {
            if (ResolvesToToken(x, node))
            {
                return(true);
            }
            if (ruleRefs.ContainsKey(x) && ruleRefs[x] != null)
            {
                return(true); // rule ref in this alt?
            }
            LabelElementPair anyLabelDef = GetAnyLabelDef(x);

            if (anyLabelDef != null && anyLabelDef.type == LabelType.RULE_LABEL)
            {
                return(true);
            }
            return(false);
        }
Exemple #8
0
        /** $x.y	Attribute: x is surrounding rule, label ref (in any alts) */
        public virtual Attribute ResolveToAttribute(string x, string y, ActionAST node)
        {
            LabelElementPair anyLabelDef = GetAnyLabelDef(x);

            if (anyLabelDef != null)
            {
                if (anyLabelDef.type == LabelType.RULE_LABEL)
                {
                    return(g.GetRule(anyLabelDef.element.Text).ResolveRetvalOrProperty(y));
                }
                else
                {
                    AttributeDict scope = GetPredefinedScope(anyLabelDef.type);
                    if (scope == null)
                    {
                        return(null);
                    }

                    return(scope.Get(y));
                }
            }
            return(null);
        }
Exemple #9
0
 public override void Label(GrammarAST op, GrammarAST ID, GrammarAST element)
 {
     LabelElementPair lp = new LabelElementPair(g, ID, element, op.Type);
     currentRule.alt[currentOuterAltNumber].labelDefs.Map(ID.Text, lp);
 }
Exemple #10
0
        internal virtual void CheckForTypeMismatch(LabelElementPair prevLabelPair, LabelElementPair labelPair)
        {
            // label already defined; if same type, no problem
            if (prevLabelPair.type != labelPair.type)
            {
                string typeMismatchExpr = labelPair.type + "!=" + prevLabelPair.type;
                errMgr.GrammarError(
                    ErrorType.LABEL_TYPE_CONFLICT,
                    g.fileName,
                    labelPair.label.Token,
                    labelPair.label.Text,
                    typeMismatchExpr);
            }

            if (!prevLabelPair.element.Text.Equals(labelPair.element.Text) &&
                (prevLabelPair.type.Equals(LabelType.RULE_LABEL) || prevLabelPair.type.Equals(LabelType.RULE_LIST_LABEL)) &&
                (labelPair.type.Equals(LabelType.RULE_LABEL) || labelPair.type.Equals(LabelType.RULE_LIST_LABEL)))
            {

                string prevLabelOp = prevLabelPair.type.Equals(LabelType.RULE_LIST_LABEL) ? "+=" : "=";
                string labelOp = labelPair.type.Equals(LabelType.RULE_LIST_LABEL) ? "+=" : "=";
                errMgr.GrammarError(
                        ErrorType.LABEL_TYPE_CONFLICT,
                        g.fileName,
                        labelPair.label.Token,
                        labelPair.label.Text + labelOp + labelPair.element.Text,
                        prevLabelPair.label.Text + prevLabelOp + prevLabelPair.element.Text);
            }
        }
        /** Return true if successful */
        public virtual bool TranslateLeftRecursiveRule(GrammarRootAST ast,
                                                  LeftRecursiveRule r,
                                                  string language)
        {
            //tool.log("grammar", ruleAST.toStringTree());
            GrammarAST prevRuleAST = r.ast;
            string ruleName = prevRuleAST.GetChild(0).Text;
            LeftRecursiveRuleAnalyzer leftRecursiveRuleWalker =
                new LeftRecursiveRuleAnalyzer(prevRuleAST, tool, ruleName, language);
            bool isLeftRec;
            try
            {
                //System.Console.WriteLine("TESTING ---------------\n" +
                //                   leftRecursiveRuleWalker.Text(ruleAST));
                isLeftRec = leftRecursiveRuleWalker.rec_rule();
            }
            catch (RecognitionException)
            {
                isLeftRec = false; // didn't match; oh well
            }
            if (!isLeftRec)
                return false;

            // replace old rule's AST; first create text of altered rule
            GrammarAST RULES = (GrammarAST)ast.GetFirstChildWithType(ANTLRParser.RULES);
            string newRuleText = leftRecursiveRuleWalker.GetArtificialOpPrecRule();
            //System.Console.WriteLine("created: " + newRuleText);
            // now parse within the context of the grammar that originally created
            // the AST we are transforming. This could be an imported grammar so
            // we cannot just reference this.g because the role might come from
            // the imported grammar and not the root grammar (this.g)
            RuleAST t = ParseArtificialRule(prevRuleAST.g, newRuleText);

            // reuse the name token from the original AST since it refers to the proper source location in the original grammar
            ((GrammarAST)t.GetChild(0)).Token = ((GrammarAST)prevRuleAST.GetChild(0)).Token;

            // update grammar AST and set rule's AST.
            RULES.SetChild(prevRuleAST.ChildIndex, t);
            r.ast = t;

            // Reduce sets in newly created rule tree
            GrammarTransformPipeline transform = new GrammarTransformPipeline(g, g.tool);
            transform.ReduceBlocksToSets(r.ast);
            transform.ExpandParameterizedLoops(r.ast);

            // Rerun semantic checks on the new rule
            RuleCollector ruleCollector = new RuleCollector(g);
            ruleCollector.Visit(t, "rule");
            BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
            // disable the assoc element option checks because they are already
            // handled for the pre-transformed rule.
            basics.checkAssocElementOption = false;
            basics.Visit(t, "rule");

            // track recursive alt info for codegen
            r.recPrimaryAlts = new List<LeftRecursiveRuleAltInfo>();
            foreach (var altInfo in leftRecursiveRuleWalker.prefixAndOtherAlts)
                r.recPrimaryAlts.Add(altInfo);
            if (r.recPrimaryAlts.Count == 0)
            {
                tool.errMgr.GrammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST)r.ast.GetChild(0)).Token, r.name);
            }

            r.recOpAlts = new OrderedHashMap<int, LeftRecursiveRuleAltInfo>();
            foreach (var pair in leftRecursiveRuleWalker.binaryAlts)
                r.recOpAlts[pair.Key] = pair.Value;
            foreach (var pair in leftRecursiveRuleWalker.ternaryAlts)
                r.recOpAlts[pair.Key] = pair.Value;
            foreach (var pair in leftRecursiveRuleWalker.suffixAlts)
                r.recOpAlts[pair.Key] = pair.Value;

            // walk alt info records and set their altAST to point to appropriate ALT subtree
            // from freshly created AST
            SetAltASTPointers(r, t);

            // update Rule to just one alt and add prec alt
            ActionAST arg = (ActionAST)r.ast.GetFirstChildWithType(ANTLRParser.ARG_ACTION);
            if (arg != null)
            {
                r.args = ScopeParser.ParseTypedArgList(arg, arg.Text, g);
                r.args.type = AttributeDict.DictType.ARG;
                r.args.ast = arg;
                arg.resolver = r.alt[1]; // todo: isn't this Rule or something?
            }

            // define labels on recursive rule refs we delete; they don't point to nodes of course
            // these are so $label in action translation works
            foreach (System.Tuple<GrammarAST, string> pair in leftRecursiveRuleWalker.leftRecursiveRuleRefLabels)
            {
                GrammarAST labelNode = pair.Item1;
                GrammarAST labelOpNode = (GrammarAST)labelNode.Parent;
                GrammarAST elementNode = (GrammarAST)labelOpNode.GetChild(1);
                LabelElementPair lp = new LabelElementPair(g, labelNode, elementNode, labelOpNode.Type);
                r.alt[1].labelDefs.Map(labelNode.Text, lp);
            }
            // copy to rule from walker
            r.leftRecursiveRuleRefLabels = leftRecursiveRuleWalker.leftRecursiveRuleRefLabels;

            tool.Log("grammar", "added: " + t.ToStringTree());
            return true;
        }