示例#1
0
        public SemPred(OutputModelFactory factory, [NotNull] ActionAST ast)
            : base(factory, ast)
        {

            Debug.Assert(ast.atnState != null
                && ast.atnState.NumberOfTransitions == 1
                && ast.atnState.Transition(0) is AbstractPredicateTransition);

            GrammarAST failNode = ast.GetOptionAST("fail");
            predicate = ast.Text;
            if (predicate.StartsWith("{") && predicate.EndsWith("}?"))
            {
                predicate = predicate.Substring(1, predicate.Length - 3);
            }
            predicate = factory.GetTarget().GetTargetStringLiteralFromString(predicate);

            if (failNode == null)
                return;

            if (failNode is ActionAST)
            {
                ActionAST failActionNode = (ActionAST)failNode;
                RuleFunction rf = factory.GetCurrentRuleFunction();
                failChunks = ActionTranslator.TranslateAction(factory, rf,
                                                              failActionNode.Token,
                                                              failActionNode);
            }
            else
            {
                msg = factory.GetTarget().GetTargetStringLiteralFromANTLRStringLiteral(factory.GetGenerator(),
                                                                              failNode.Text,
                                                                              true);
            }
        }
示例#2
0
 public virtual string GetTokenLabel(string x)
 {
     if (node.resolver.ResolvesToLabel(x, node))
     {
         return(x);
     }
     return(factory.GetTarget().GetImplicitTokenLabel(x));
 }
示例#3
0
 public AltLabelStructDecl(OutputModelFactory factory, Rule r, string label)
     : base(factory, r)
 {
     this.name = // override name set in super to the label ctx
                 factory.GetTarget().GetAltLabelContextStructName(label);
     derivedFromName = label;
 }
示例#4
0
 public MatchToken(OutputModelFactory factory, TerminalAST ast)
     : base(factory, ast)
 {
     Grammar g = factory.GetGrammar();
     ttype = g.GetTokenType(ast.Text);
     name = factory.GetTarget().GetTokenTypeAsTargetLabel(g, ttype);
 }
示例#5
0
 public StructDecl(OutputModelFactory factory, Rule r)
     : base(factory, factory.GetTarget().GetRuleFunctionContextStructName(r))
 {
     AddDispatchMethods(r);
     derivedFromName = r.name;
     provideCopyFrom = r.HasAltSpecificContexts();
 }
示例#6
0
        private static Bitset[] CreateBitsets(OutputModelFactory factory,
                                              IntervalSet set,
                                              int wordSize,
                                              bool useZeroOffset)
        {
            IList <Bitset> bitsetList = new List <Bitset>();

            foreach (int ttype in set.ToArray())
            {
                Bitset current = bitsetList.Count > 0 ? bitsetList[bitsetList.Count - 1] : null;
                if (current == null || ttype > (current.shift + wordSize - 1))
                {
                    current = new Bitset();
                    if (useZeroOffset && ttype >= 0 && ttype < wordSize - 1)
                    {
                        current.shift = 0;
                    }
                    else
                    {
                        current.shift = ttype;
                    }

                    bitsetList.Add(current);
                }

                current.ttypes.Add(factory.GetTarget().GetTokenTypeAsTargetLabel(factory.GetGrammar(), ttype));
            }

            return(bitsetList.ToArray());
        }
示例#7
0
 public AltLabelStructDecl(OutputModelFactory factory, Rule r, string label)
     : base(factory, r)
 {
     this.name = // override name set in super to the label ctx
         factory.GetTarget().GetAltLabelContextStructName(label);
     derivedFromName = label;
 }
示例#8
0
 public StructDecl(OutputModelFactory factory, Rule r)
     : base(factory, factory.GetTarget().GetRuleFunctionContextStructName(r))
 {
     AddDispatchMethods(r);
     derivedFromName = r.name;
     provideCopyFrom = r.HasAltSpecificContexts();
 }
示例#9
0
        private static Bitset[] CreateBitsets(OutputModelFactory factory,
                                              IntervalSet set,
                                              int wordSize,
                                              bool useZeroOffset)
        {
            IList<Bitset> bitsetList = new List<Bitset>();
            foreach (int ttype in set.ToArray())
            {
                Bitset current = bitsetList.Count > 0 ? bitsetList[bitsetList.Count - 1] : null;
                if (current == null || ttype > (current.shift + wordSize - 1))
                {
                    current = new Bitset();
                    if (useZeroOffset && ttype >= 0 && ttype < wordSize - 1)
                    {
                        current.shift = 0;
                    }
                    else
                    {
                        current.shift = ttype;
                    }

                    bitsetList.Add(current);
                }

                current.ttypes.Add(factory.GetTarget().GetTokenTypeAsTargetLabel(factory.GetGrammar(), ttype));
            }

            return bitsetList.ToArray();
        }
示例#10
0
        public MatchToken(OutputModelFactory factory, TerminalAST ast)
            : base(factory, ast)
        {
            Grammar g = factory.GetGrammar();

            ttype = g.GetTokenType(ast.Text);
            name  = factory.GetTarget().GetTokenTypeAsTargetLabel(g, ttype);
        }
示例#11
0
        protected static string TranslateTokenStringToTarget(string tokenName, OutputModelFactory factory)
        {
            if (tokenName == null)
            {
                return(null);
            }

            if (tokenName[0] == '\'')
            {
                bool   addQuotes    = false;
                string targetString =
                    factory.GetTarget().GetTargetStringLiteralFromANTLRStringLiteral(factory.GetGenerator(), tokenName, addQuotes);
                return("\"'" + targetString + "'\"");
            }
            else
            {
                return(factory.GetTarget().GetTargetStringLiteralFromString(tokenName, true));
            }
        }
示例#12
0
 public StarBlock(OutputModelFactory factory,
                  GrammarAST blkOrEbnfRootAST,
                  IList<CodeBlockForAlt> alts)
     : base(factory, blkOrEbnfRootAST, alts)
 {
     loopLabel = factory.GetTarget().GetLoopLabel(blkOrEbnfRootAST);
     StarLoopEntryState star = (StarLoopEntryState)blkOrEbnfRootAST.atnState;
     loopBackStateNumber = star.loopBackState.stateNumber;
     decision = star.decision;
 }
示例#13
0
        public StarBlock(OutputModelFactory factory,
                         GrammarAST blkOrEbnfRootAST,
                         IList <CodeBlockForAlt> alts)
            : base(factory, blkOrEbnfRootAST, alts)
        {
            loopLabel = factory.GetTarget().GetLoopLabel(blkOrEbnfRootAST);
            StarLoopEntryState star = (StarLoopEntryState)blkOrEbnfRootAST.atnState;

            loopBackStateNumber = star.loopBackState.stateNumber;
            decision            = star.decision;
        }
示例#14
0
 public SerializedATN(OutputModelFactory factory, ATN atn, IList<string> ruleNames)
     : base(factory)
 {
     List<int> data = ATNSerializer.GetSerialized(atn, ruleNames);
     serialized = new List<string>(data.Count);
     foreach (int c in data)
     {
         string encoded = factory.GetTarget().EncodeIntAsCharEscape(c == -1 ? char.MaxValue : c);
         serialized.Add(encoded);
     }
     //System.Console.WriteLine(ATNSerializer.GetDecoded(factory.GetGrammar(), atn));
 }
示例#15
0
        public SerializedATN(OutputModelFactory factory, ATN atn, IList <string> ruleNames)
            : base(factory)
        {
            List <int> data = ATNSerializer.GetSerialized(atn, ruleNames);

            serialized = new List <string>(data.Count);
            foreach (int c in data)
            {
                string encoded = factory.GetTarget().EncodeIntAsCharEscape(c == -1 ? char.MaxValue : c);
                serialized.Add(encoded);
            }
            //System.Console.WriteLine(ATNSerializer.GetDecoded(factory.GetGrammar(), atn));
        }
示例#16
0
        public SemPred(OutputModelFactory factory, [NotNull] ActionAST ast)
            : base(factory, ast)
        {
            Debug.Assert(ast.atnState != null &&
                         ast.atnState.NumberOfTransitions == 1 &&
                         ast.atnState.Transition(0) is AbstractPredicateTransition);

            GrammarAST failNode = ast.GetOptionAST("fail");

            predicate = ast.Text;
            if (predicate.StartsWith("{") && predicate.EndsWith("}?"))
            {
                predicate = predicate.Substring(1, predicate.Length - 3);
            }
            predicate = factory.GetTarget().GetTargetStringLiteralFromString(predicate);

            if (failNode == null)
            {
                return;
            }

            if (failNode is ActionAST)
            {
                ActionAST    failActionNode = (ActionAST)failNode;
                RuleFunction rf             = factory.GetCurrentRuleFunction();
                failChunks = ActionTranslator.TranslateAction(factory, rf,
                                                              failActionNode.Token,
                                                              failActionNode);
            }
            else
            {
                msg = factory.GetTarget().GetTargetStringLiteralFromANTLRStringLiteral(factory.GetGenerator(),
                                                                                       failNode.Text,
                                                                                       true);
            }
        }
        public LeftRecursiveRuleFunction(OutputModelFactory factory, LeftRecursiveRule r)
            : base(factory, r)
        {
            // Since we delete x=lr, we have to manually add decls for all labels
            // on left-recur refs to proper structs
            foreach (System.Tuple <GrammarAST, string> pair in r.leftRecursiveRuleRefLabels)
            {
                GrammarAST idAST    = pair.Item1;
                string     altLabel = pair.Item2;
                string     label    = idAST.Text;
                GrammarAST rrefAST  = (GrammarAST)idAST.Parent.GetChild(1);
                if (rrefAST.Type == ANTLRParser.RULE_REF)
                {
                    Rule            targetRule = factory.GetGrammar().GetRule(rrefAST.Text);
                    string          ctxName    = factory.GetTarget().GetRuleFunctionContextStructName(targetRule);
                    RuleContextDecl d;
                    if (idAST.Parent.Type == ANTLRParser.ASSIGN)
                    {
                        d = new RuleContextDecl(factory, label, ctxName);
                    }
                    else
                    {
                        d = new RuleContextListDecl(factory, label, ctxName);
                    }

                    StructDecl @struct = ruleCtx;
                    if (altLabelCtxs != null)
                    {
                        AltLabelStructDecl s;
                        if (altLabel != null && altLabelCtxs.TryGetValue(altLabel, out s) && s != null)
                        {
                            @struct = s; // if alt label, use subctx
                        }
                    }

                    @struct.AddDecl(d); // stick in overall rule's ctx
                }
            }
        }
        public LeftRecursiveRuleFunction(OutputModelFactory factory, LeftRecursiveRule r)
            : base(factory, r)
        {
            // Since we delete x=lr, we have to manually add decls for all labels
            // on left-recur refs to proper structs
            foreach (System.Tuple<GrammarAST, string> pair in r.leftRecursiveRuleRefLabels)
            {
                GrammarAST idAST = pair.Item1;
                string altLabel = pair.Item2;
                string label = idAST.Text;
                GrammarAST rrefAST = (GrammarAST)idAST.Parent.GetChild(1);
                if (rrefAST.Type == ANTLRParser.RULE_REF)
                {
                    Rule targetRule = factory.GetGrammar().GetRule(rrefAST.Text);
                    string ctxName = factory.GetTarget().GetRuleFunctionContextStructName(targetRule);
                    RuleContextDecl d;
                    if (idAST.Parent.Type == ANTLRParser.ASSIGN)
                    {
                        d = new RuleContextDecl(factory, label, ctxName);
                    }
                    else
                    {
                        d = new RuleContextListDecl(factory, label, ctxName);
                    }

                    StructDecl @struct = ruleCtx;
                    if (altLabelCtxs != null)
                    {
                        AltLabelStructDecl s;
                        if (altLabel != null && altLabelCtxs.TryGetValue(altLabel, out s) && s != null)
                            @struct = s; // if alt label, use subctx
                    }

                    @struct.AddDecl(d); // stick in overall rule's ctx
                }
            }
        }
示例#19
0
        protected static string TranslateTokenStringToTarget(string tokenName, OutputModelFactory factory)
        {
            if (tokenName == null)
            {
                return null;
            }

            if (tokenName[0] == '\'')
            {
                bool addQuotes = false;
                string targetString =
                    factory.GetTarget().GetTargetStringLiteralFromANTLRStringLiteral(factory.GetGenerator(), tokenName, addQuotes);
                return "\"'" + targetString + "'\"";
            }
            else
            {
                return factory.GetTarget().GetTargetStringLiteralFromString(tokenName, true);
            }
        }
        public virtual void BuildLeftRecursiveRuleFunction(LeftRecursiveRule r, LeftRecursiveRuleFunction function)
        {
            BuildNormalRuleFunction(r, function);

            // now inject code to start alts
            AbstractTarget target           = @delegate.GetTarget();
            TemplateGroup  codegenTemplates = target.GetTemplates();

            // pick out alt(s) for primaries
            CodeBlockForOuterMostAlt outerAlt        = (CodeBlockForOuterMostAlt)function.code[0];
            IList <CodeBlockForAlt>  primaryAltsCode = new List <CodeBlockForAlt>();
            SrcOp primaryStuff = outerAlt.ops[0];

            if (primaryStuff is Choice)
            {
                Choice primaryAltBlock = (Choice)primaryStuff;
                foreach (var alt in primaryAltBlock.alts)
                {
                    primaryAltsCode.Add(alt);
                }
            }
            else
            { // just a single alt I guess; no block
                primaryAltsCode.Add((CodeBlockForAlt)primaryStuff);
            }

            // pick out alt(s) for op alts
            StarBlock               opAltStarBlock   = (StarBlock)outerAlt.ops[1];
            CodeBlockForAlt         altForOpAltBlock = opAltStarBlock.alts[0];
            IList <CodeBlockForAlt> opAltsCode       = new List <CodeBlockForAlt>();
            SrcOp opStuff = altForOpAltBlock.ops[0];

            if (opStuff is AltBlock)
            {
                AltBlock opAltBlock = (AltBlock)opStuff;
                foreach (var alt in opAltBlock.alts)
                {
                    opAltsCode.Add(alt);
                }
            }
            else
            { // just a single alt I guess; no block
                opAltsCode.Add((CodeBlockForAlt)opStuff);
            }

            // Insert code in front of each primary alt to create specialized context if there was a label
            for (int i = 0; i < primaryAltsCode.Count; i++)
            {
                LeftRecursiveRuleAltInfo altInfo = r.recPrimaryAlts[i];
                if (altInfo.altLabel == null)
                {
                    continue;
                }
                Template altActionST = codegenTemplates.GetInstanceOf("recRuleReplaceContext");
                altActionST.Add("ctxName", Utils.Capitalize(altInfo.altLabel));
                AltLabelStructDecl ctx = null;
                if (altInfo.altLabel != null)
                {
                    function.altLabelCtxs.TryGetValue(altInfo.altLabel, out ctx);
                }
                Action          altAction = new Action(@delegate, ctx, altActionST);
                CodeBlockForAlt alt       = primaryAltsCode[i];
                alt.InsertOp(0, altAction);
            }

            // Insert code to set ctx.stop after primary block and before op * loop
            Template setStopTokenAST    = codegenTemplates.GetInstanceOf("recRuleSetStopToken");
            Action   setStopTokenAction = new Action(@delegate, function.ruleCtx, setStopTokenAST);

            outerAlt.InsertOp(1, setStopTokenAction);

            // Insert code to set _prevctx at start of * loop
            Template setPrevCtx       = codegenTemplates.GetInstanceOf("recRuleSetPrevCtx");
            Action   setPrevCtxAction = new Action(@delegate, function.ruleCtx, setPrevCtx);

            opAltStarBlock.AddIterationOp(setPrevCtxAction);

            // Insert code in front of each op alt to create specialized context if there was an alt label
            for (int i = 0; i < opAltsCode.Count; i++)
            {
                Template altActionST;
                LeftRecursiveRuleAltInfo altInfo = r.recOpAlts.GetElement(i);
                string templateName;
                if (altInfo.altLabel != null)
                {
                    templateName = "recRuleLabeledAltStartAction";
                    altActionST  = codegenTemplates.GetInstanceOf(templateName);
                    altActionST.Add("currentAltLabel", altInfo.altLabel);
                }
                else
                {
                    templateName = "recRuleAltStartAction";
                    altActionST  = codegenTemplates.GetInstanceOf(templateName);
                    altActionST.Add("ctxName", Utils.Capitalize(r.name));
                }
                altActionST.Add("ruleName", r.name);
                // add label of any LR ref we deleted
                altActionST.Add("label", altInfo.leftRecursiveRuleRefLabel);
                if (altActionST.impl.FormalArguments.Any(x => x.Name == "isListLabel"))
                {
                    altActionST.Add("isListLabel", altInfo.isListLabel);
                }
                else if (altInfo.isListLabel)
                {
                    @delegate.GetGenerator().tool.errMgr.ToolError(ErrorType.CODE_TEMPLATE_ARG_ISSUE, templateName, "isListLabel");
                }
                AltLabelStructDecl ctx = null;
                if (altInfo.altLabel != null)
                {
                    function.altLabelCtxs.TryGetValue(altInfo.altLabel, out ctx);
                }
                Action          altAction = new Action(@delegate, ctx, altActionST);
                CodeBlockForAlt alt       = opAltsCode[i];
                alt.InsertOp(0, altAction);
            }
        }