Пример #1
0
 public LexerFile(OutputModelFactory factory, string fileName)
     : base(factory, fileName)
 {
     namedActions = BuildNamedActions(factory.GetGrammar());
     genPackage = factory.GetGrammar().tool.genPackage;
     exportMacro = factory.GetGrammar().GetOptionString("exportMacro");
     genListener = factory.GetGrammar().tool.gen_listener;
     genVisitor = factory.GetGrammar().tool.gen_visitor;
 }
Пример #2
0
 public LexerFile(OutputModelFactory factory, string fileName)
     : base(factory, fileName)
 {
     namedActions = BuildNamedActions(factory.GetGrammar());
     genPackage   = factory.GetGrammar().tool.genPackage;
     exportMacro  = factory.GetGrammar().GetOptionString("exportMacro");
     genListener  = factory.GetGrammar().tool.gen_listener;
     genVisitor   = factory.GetGrammar().tool.gen_visitor;
 }
Пример #3
0
        public ListenerFile(OutputModelFactory factory, string fileName)
            : base(factory, fileName)
        {
            Grammar g = factory.GetGrammar();

            parserName  = g.GetRecognizerName();
            grammarName = g.name;

            namedActions = BuildNamedActions(factory.GetGrammar());

            foreach (KeyValuePair <string, IList <RuleAST> > entry in g.contextASTs)
            {
                foreach (RuleAST ruleAST in entry.Value)
                {
                    try
                    {
                        IDictionary <string, IList <System.Tuple <int, AltAST> > > labeledAlternatives = g.GetLabeledAlternatives(ruleAST);
                        listenerNames.UnionWith(labeledAlternatives.Keys);
                    }
                    catch (RecognitionException)
                    {
                    }
                }
            }

            foreach (Rule r in g.rules.Values)
            {
                listenerNames.Add(r.GetBaseContext());
            }

            foreach (Rule r in g.rules.Values)
            {
                IDictionary <string, IList <System.Tuple <int, AltAST> > > labels = r.GetAltLabels();
                if (labels != null)
                {
                    foreach (KeyValuePair <string, IList <System.Tuple <int, AltAST> > > pair in labels)
                    {
                        listenerLabelRuleNames[pair.Key] = r.name;
                    }
                }
            }

            ActionAST ast;

            if (g.namedActions.TryGetValue("header", out ast) && ast != null)
            {
                header = new Action(factory, ast);
            }

            genPackage  = factory.GetGrammar().tool.genPackage;
            exportMacro = factory.GetGrammar().GetOptionString("exportMacro");
        }
Пример #4
0
        public ListenerFile(OutputModelFactory factory, string fileName)
            : base(factory, fileName)
        {
            Grammar g = factory.GetGrammar();
            parserName = g.GetRecognizerName();
            grammarName = g.name;

            namedActions = BuildNamedActions(factory.GetGrammar());

            foreach (KeyValuePair<string, IList<RuleAST>> entry in g.contextASTs)
            {
                foreach (RuleAST ruleAST in entry.Value)
                {
                    try
                    {
                        IDictionary<string, IList<System.Tuple<int, AltAST>>> labeledAlternatives = g.GetLabeledAlternatives(ruleAST);
                        listenerNames.UnionWith(labeledAlternatives.Keys);
                    }
                    catch (RecognitionException)
                    {
                    }
                }
            }

            foreach (Rule r in g.rules.Values)
            {
                listenerNames.Add(r.GetBaseContext());
            }

            foreach (Rule r in g.rules.Values)
            {
                IDictionary<string, IList<System.Tuple<int, AltAST>>> labels = r.GetAltLabels();
                if (labels != null)
                {
                    foreach (KeyValuePair<string, IList<System.Tuple<int, AltAST>>> pair in labels)
                    {
                        listenerLabelRuleNames[pair.Key] = r.name;
                    }
                }
            }

            ActionAST ast;
            if (g.namedActions.TryGetValue("header", out ast) && ast != null)
                header = new Action(factory, ast);

            genPackage = factory.GetGrammar().tool.genPackage;
            exportMacro = factory.GetGrammar().GetOptionString("exportMacro");
        }
Пример #5
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);
 }
Пример #6
0
        public static IList <ActionChunk> TranslateActionChunk(OutputModelFactory factory,
                                                               RuleFunction rf,
                                                               string action,
                                                               ActionAST node)
        {
            IToken           tokenWithinAction = node.Token;
            ActionTranslator translator        = new ActionTranslator(factory, node);

            translator.rf = rf;
            factory.GetGrammar().tool.Log("action-translator", "translate " + action);
            string altLabel = node.GetAltLabel();

            if (rf != null)
            {
                translator.nodeContext = rf.ruleCtx;
                if (altLabel != null)
                {
                    AltLabelStructDecl decl;
                    rf.altLabelCtxs.TryGetValue(altLabel, out decl);
                    translator.nodeContext = decl;
                }
            }
            ANTLRStringStream @in = new ANTLRStringStream(action);

            @in.Line = tokenWithinAction.Line;
            @in.CharPositionInLine = tokenWithinAction.CharPositionInLine;
            ActionSplitter trigger = new ActionSplitter(@in, translator);

            // forces eval, triggers listener methods
            trigger.GetActionTokens();
            return(translator.chunks);
        }
Пример #7
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();
        }
Пример #8
0
        public ParserFile(OutputModelFactory factory, string fileName)
            : base(factory, fileName)
        {
            Grammar g = factory.GetGrammar();
            namedActions = BuildNamedActions(factory.GetGrammar());
            genPackage = g.tool.genPackage;
            exportMacro = factory.GetGrammar().GetOptionString("exportMacro");
            // need the below members in the ST for Python, C++
            genListener = g.tool.gen_listener;
            genVisitor = g.tool.gen_visitor;
            grammarName = g.name;

            if (g.GetOptionString("contextSuperClass") != null)
            {
                contextSuperClass = new ActionText(null, g.GetOptionString("contextSuperClass"));
            }
        }
Пример #9
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);
        }
Пример #10
0
        /** Build a file with a parser containing rule functions. Use the
         *  controller as factory in SourceGenTriggers so it triggers code generation
         *  extensions too, not just the factory functions in this factory.
         */
        public virtual OutputModelObject BuildParserOutputModel(bool header)
        {
            CodeGenerator gen  = @delegate.GetGenerator();
            ParserFile    file = ParserFile(gen.GetRecognizerFileName(header));

            SetRoot(file);
            file.parser = Parser(file);

            Grammar g = @delegate.GetGrammar();

            foreach (Rule r in g.rules.Values)
            {
                BuildRuleFunction(file.parser, r);
            }

            return(file);
        }
Пример #11
0
        public ParserFile(OutputModelFactory factory, string fileName)
            : base(factory, fileName)
        {
            Grammar g = factory.GetGrammar();

            namedActions = BuildNamedActions(factory.GetGrammar());
            genPackage   = g.tool.genPackage;
            exportMacro  = factory.GetGrammar().GetOptionString("exportMacro");
            // need the below members in the ST for Python, C++
            genListener = g.tool.gen_listener;
            genVisitor  = g.tool.gen_visitor;
            grammarName = g.name;

            if (g.GetOptionString("contextSuperClass") != null)
            {
                contextSuperClass = new ActionText(null, g.GetOptionString("contextSuperClass"));
            }
        }
Пример #12
0
        public Lexer(OutputModelFactory factory, LexerFile file)
            : base(factory)
        {
            this.file = file; // who contains us?

            Grammar g = factory.GetGrammar();
            channels = new LinkedHashMap<string, int>(g.channelNameToValueMap);
            modes = ((LexerGrammar)g).modes.Keys;
        }
Пример #13
0
 public ThrowRecognitionException(OutputModelFactory factory, GrammarAST ast, IntervalSet expecting)
     : base(factory, ast)
 {
     //this.decision = ((BlockStartState)ast.ATNState).decision;
     grammarLine = ast.Line;
     grammarLine = ast.CharPositionInLine;
     grammarFile = factory.GetGrammar().fileName;
     //this.expecting = factory.createExpectingBitSet(ast, decision, expecting, "error");
     //		factory.defineBitSet(this.expecting);
 }
Пример #14
0
 protected OutputFile(OutputModelFactory factory, string fileName)
     : base(factory)
 {
     this.fileName = fileName;
     Grammar g = factory.GetGrammar();
     grammarFileName = g.fileName;
     ANTLRVersion = AntlrTool.VERSION;
     TokenLabelType = g.GetOptionString("TokenLabelType");
     InputSymbolType = TokenLabelType;
 }
Пример #15
0
        public Lexer(OutputModelFactory factory, LexerFile file)
            : base(factory)
        {
            this.file = file; // who contains us?

            Grammar g = factory.GetGrammar();

            channels = new LinkedHashMap <string, int>(g.channelNameToValueMap);
            modes    = ((LexerGrammar)g).modes.Keys;
        }
Пример #16
0
        protected OutputFile(OutputModelFactory factory, string fileName)
            : base(factory)
        {
            this.fileName = fileName;
            Grammar g = factory.GetGrammar();

            grammarFileName = g.fileName;
            ANTLRVersion    = AntlrTool.VERSION;
            TokenLabelType  = g.GetOptionString("TokenLabelType");
            InputSymbolType = TokenLabelType;
        }
Пример #17
0
        public LL1AltBlock(OutputModelFactory factory, GrammarAST blkAST, IList<CodeBlockForAlt> alts)
            : base(factory, blkAST, alts)
        {
            this.decision = ((DecisionState)blkAST.atnState).decision;

            /* Lookahead for each alt 1..n */
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];
            altLook = GetAltLookaheadAsStringLists(altLookSets);

            IntervalSet expecting = IntervalSet.Or(altLookSets); // combine alt sets
            this.error = GetThrowNoViableAlt(factory, blkAST, expecting);
        }
Пример #18
0
        public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST starRoot, IList<CodeBlockForAlt> alts)
            : base(factory, starRoot, alts)
        {

            StarLoopEntryState star = (StarLoopEntryState)starRoot.atnState;
            loopBackStateNumber = star.loopBackState.stateNumber;
            this.decision = star.decision;
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];
            Debug.Assert(altLookSets.Length == 2);
            IntervalSet enterLook = altLookSets[0];
            IntervalSet exitLook = altLookSets[1];
            loopExpr = AddCodeForLoopLookaheadTempVar(enterLook);
        }
Пример #19
0
        public LL1AltBlock(OutputModelFactory factory, GrammarAST blkAST, IList <CodeBlockForAlt> alts)
            : base(factory, blkAST, alts)
        {
            this.decision = ((DecisionState)blkAST.atnState).decision;

            /* Lookahead for each alt 1..n */
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];
            altLook = GetAltLookaheadAsStringLists(altLookSets);

            IntervalSet expecting = IntervalSet.Or(altLookSets); // combine alt sets

            this.error = GetThrowNoViableAlt(factory, blkAST, expecting);
        }
Пример #20
0
        public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST starRoot, IList <CodeBlockForAlt> alts)
            : base(factory, starRoot, alts)
        {
            StarLoopEntryState star = (StarLoopEntryState)starRoot.atnState;

            loopBackStateNumber = star.loopBackState.stateNumber;
            this.decision       = star.decision;
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];
            Debug.Assert(altLookSets.Length == 2);
            IntervalSet enterLook = altLookSets[0];
            IntervalSet exitLook  = altLookSets[1];

            loopExpr = AddCodeForLoopLookaheadTempVar(enterLook);
        }
Пример #21
0
        public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST plusRoot, IList<CodeBlockForAlt> alts)
            : base(factory, plusRoot, alts)
        {
            BlockAST blkAST = (BlockAST)plusRoot.GetChild(0);
            PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;

            stateNumber = blkStart.loopBackState.stateNumber;
            blockStartStateNumber = blkStart.stateNumber;
            PlusBlockStartState plus = (PlusBlockStartState)blkAST.atnState;
            this.decision = plus.loopBackState.decision;
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];

            IntervalSet loopBackLook = altLookSets[0];
            loopExpr = AddCodeForLoopLookaheadTempVar(loopBackLook);
        }
Пример #22
0
        public LL1PlusBlockSingleAlt(OutputModelFactory factory, GrammarAST plusRoot, IList <CodeBlockForAlt> alts)
            : base(factory, plusRoot, alts)
        {
            BlockAST            blkAST   = (BlockAST)plusRoot.GetChild(0);
            PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;

            stateNumber           = blkStart.loopBackState.stateNumber;
            blockStartStateNumber = blkStart.stateNumber;
            PlusBlockStartState plus = (PlusBlockStartState)blkAST.atnState;

            this.decision = plus.loopBackState.decision;
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];

            IntervalSet loopBackLook = altLookSets[0];

            loopExpr = AddCodeForLoopLookaheadTempVar(loopBackLook);
        }
Пример #23
0
        public IList<SrcOp> followExpr; // might not work in template if size>1

        public LL1OptionalBlockSingleAlt(OutputModelFactory factory,
                                         GrammarAST blkAST,
                                         IList<CodeBlockForAlt> alts)
            : base(factory, blkAST, alts)
        {
            this.decision = ((DecisionState)blkAST.atnState).decision;

            /* Lookahead for each alt 1..n */
            //		IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa);
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];
            altLook = GetAltLookaheadAsStringLists(altLookSets);
            IntervalSet look = altLookSets[0];
            IntervalSet followLook = altLookSets[1];

            IntervalSet expecting = look.Or(followLook);
            this.error = GetThrowNoViableAlt(factory, blkAST, expecting);

            expr = AddCodeForLookaheadTempVar(look);
            followExpr = factory.GetLL1Test(followLook, blkAST);
        }
Пример #24
0
        public virtual void Attr(string expr, IToken x)
        {
            gen.g.tool.Log("action-translator", "attr " + x);
            Attribute a = node.resolver.ResolveToAttribute(x.Text, node);

            if (a != null)
            {
                switch (a.dict.type)
                {
                case AttributeDict.DictType.ARG:
                    chunks.Add(new ArgRef(nodeContext, x.Text));
                    break;

                case AttributeDict.DictType.RET:
                    chunks.Add(new RetValueRef(rf.ruleCtx, x.Text));
                    break;

                case AttributeDict.DictType.LOCAL:
                    chunks.Add(new LocalRef(nodeContext, x.Text));
                    break;

                case AttributeDict.DictType.PREDEFINED_RULE:
                    chunks.Add(GetRulePropertyRef(x));
                    break;
                }
            }
            if (node.resolver.ResolvesToToken(x.Text, node))
            {
                chunks.Add(new TokenRef(nodeContext, GetTokenLabel(x.Text))); // $label
                return;
            }
            if (node.resolver.ResolvesToLabel(x.Text, node))
            {
                chunks.Add(new LabelRef(nodeContext, GetTokenLabel(x.Text))); // $x for x=ID etc...
                return;
            }
            if (node.resolver.ResolvesToListLabel(x.Text, node))
            {
                chunks.Add(new ListLabelRef(nodeContext, x.Text)); // $ids for ids+=ID etc...
                return;
            }
            Rule r = factory.GetGrammar().GetRule(x.Text);

            if (r != null)
            {
                chunks.Add(new LabelRef(nodeContext, GetRuleLabel(x.Text))); // $r for r rule ref
            }
        }
Пример #25
0
        public IList <SrcOp> followExpr; // might not work in template if size>1

        public LL1OptionalBlockSingleAlt(OutputModelFactory factory,
                                         GrammarAST blkAST,
                                         IList <CodeBlockForAlt> alts)
            : base(factory, blkAST, alts)
        {
            this.decision = ((DecisionState)blkAST.atnState).decision;

            /* Lookahead for each alt 1..n */
            //		IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa);
            IntervalSet[] altLookSets = factory.GetGrammar().decisionLOOK[decision];
            altLook = GetAltLookaheadAsStringLists(altLookSets);
            IntervalSet look       = altLookSets[0];
            IntervalSet followLook = altLookSets[1];

            IntervalSet expecting = look.Or(followLook);

            this.error = GetThrowNoViableAlt(factory, blkAST, expecting);

            expr       = AddCodeForLookaheadTempVar(look);
            followExpr = factory.GetLL1Test(followLook, blkAST);
        }
Пример #26
0
        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
                }
            }
        }
Пример #27
0
        protected Recognizer(OutputModelFactory factory)
            : base(factory)
        {
            Grammar g = factory.GetGrammar();

            grammarFileName = Path.GetFileName(g.fileName);
            grammarName     = g.name;
            name            = g.GetRecognizerName();
            tokens          = new LinkedHashMap <string, int>();
            foreach (KeyValuePair <string, int> entry in g.tokenNameToTypeMap)
            {
                int ttype = entry.Value;
                if (ttype > 0)
                {
                    tokens[entry.Key] = ttype;
                }
            }

            ruleNames = g.rules.Keys;
            rules     = g.rules.Values;
            atn       = new SerializedATN(factory, g.atn, g.GetRuleNames());
            if (g.GetOptionString("superClass") != null)
            {
                superClass = new ActionText(null, g.GetOptionString("superClass"));
            }
            else
            {
                superClass = null;
            }

#pragma warning disable CS0612 // Type or member is obsolete
            tokenNames = TranslateTokenStringsToTarget(g.GetTokenDisplayNames(), factory);
#pragma warning restore CS0612 // Type or member is obsolete
            literalNames       = TranslateTokenStringsToTarget(g.GetTokenLiteralNames(), factory);
            symbolicNames      = TranslateTokenStringsToTarget(g.GetTokenSymbolicNames(), factory);
            abstractRecognizer = g.IsAbstract();
        }
Пример #28
0
        protected Recognizer(OutputModelFactory factory)
            : base(factory)
        {
            Grammar g = factory.GetGrammar();
            grammarFileName = Path.GetFileName(g.fileName);
            grammarName = g.name;
            name = g.GetRecognizerName();
            tokens = new LinkedHashMap<string, int>();
            foreach (KeyValuePair<string, int> entry in g.tokenNameToTypeMap)
            {
                int ttype = entry.Value;
                if (ttype > 0)
                {
                    tokens[entry.Key] = ttype;
                }
            }

            ruleNames = g.rules.Keys;
            rules = g.rules.Values;
            atn = new SerializedATN(factory, g.atn, g.GetRuleNames());
            if (g.GetOptionString("superClass") != null)
            {
                superClass = new ActionText(null, g.GetOptionString("superClass"));
            }
            else
            {
                superClass = null;
            }

#pragma warning disable CS0612 // Type or member is obsolete
            tokenNames = TranslateTokenStringsToTarget(g.GetTokenDisplayNames(), factory);
#pragma warning restore CS0612 // Type or member is obsolete
            literalNames = TranslateTokenStringsToTarget(g.GetTokenLiteralNames(), factory);
            symbolicNames = TranslateTokenStringsToTarget(g.GetTokenSymbolicNames(), factory);
            abstractRecognizer = g.IsAbstract();
        }
Пример #29
0
 public static IList<ActionChunk> TranslateActionChunk(OutputModelFactory factory,
                                                      RuleFunction rf,
                                                      string action,
                                                      ActionAST node)
 {
     IToken tokenWithinAction = node.Token;
     ActionTranslator translator = new ActionTranslator(factory, node);
     translator.rf = rf;
     factory.GetGrammar().tool.Log("action-translator", "translate " + action);
     string altLabel = node.GetAltLabel();
     if (rf != null)
     {
         translator.nodeContext = rf.ruleCtx;
         if (altLabel != null)
         {
             AltLabelStructDecl decl;
             rf.altLabelCtxs.TryGetValue(altLabel, out decl);
             translator.nodeContext = decl;
         }
     }
     ANTLRStringStream @in = new ANTLRStringStream(action);
     @in.Line = tokenWithinAction.Line;
     @in.CharPositionInLine = tokenWithinAction.CharPositionInLine;
     ActionSplitter trigger = new ActionSplitter(@in, translator);
     // forces eval, triggers listener methods
     trigger.GetActionTokens();
     return translator.chunks;
 }
Пример #30
0
        public RuleFunction(OutputModelFactory factory, Rule r)
            : base(factory)
        {
            this.name = r.name;
            this.rule = r;
            if (r.modifiers != null && r.modifiers.Count > 0)
            {
                this.modifiers = new List<string>();
                foreach (GrammarAST t in r.modifiers)
                    modifiers.Add(t.Text);
            }
            modifiers = Utils.NodesToStrings(r.modifiers);

            index = r.index;
            int lfIndex = name.IndexOf(ATNSimulator.RuleVariantDelimiter);
            if (lfIndex >= 0)
            {
                variantOf = name.Substring(0, lfIndex);
            }

            if (r.name.Equals(r.GetBaseContext()))
            {
                ruleCtx = new StructDecl(factory, r);
                AddContextGetters(factory, r.g.contextASTs[r.name]);

                if (r.args != null)
                {
                    ICollection<Attribute> decls = r.args.attributes.Values;
                    if (decls.Count > 0)
                    {
                        args = new List<AttributeDecl>();
                        ruleCtx.AddDecls(decls);
                        foreach (Attribute a in decls)
                        {
                            args.Add(new AttributeDecl(factory, a));
                        }
                        ruleCtx.ctorAttrs = args;
                    }
                }
                if (r.retvals != null)
                {
                    ruleCtx.AddDecls(r.retvals.attributes.Values);
                }
                if (r.locals != null)
                {
                    ruleCtx.AddDecls(r.locals.attributes.Values);
                }
            }
            else
            {
                if (r.args != null || r.retvals != null || r.locals != null)
                {
                    throw new System.NotSupportedException("customized fields are not yet supported for customized context objects");
                }
            }

            ruleLabels = r.GetElementLabelNames();
            tokenLabels = r.GetTokenRefs();
            if (r.exceptions != null)
            {
                exceptions = new List<ExceptionClause>();
                foreach (GrammarAST e in r.exceptions)
                {
                    ActionAST catchArg = (ActionAST)e.GetChild(0);
                    ActionAST catchAction = (ActionAST)e.GetChild(1);
                    exceptions.Add(new ExceptionClause(factory, catchArg, catchAction));
                }
            }

            startState = factory.GetGrammar().atn.ruleToStartState[r.index];
        }
Пример #31
0
        public RuleFunction(OutputModelFactory factory, Rule r)
            : base(factory)
        {
            this.name = r.name;
            this.rule = r;
            if (r.modifiers != null && r.modifiers.Count > 0)
            {
                this.modifiers = new List <string>();
                foreach (GrammarAST t in r.modifiers)
                {
                    modifiers.Add(t.Text);
                }
            }
            modifiers = Utils.NodesToStrings(r.modifiers);

            index = r.index;
            int lfIndex = name.IndexOf(ATNSimulator.RuleVariantDelimiter);

            if (lfIndex >= 0)
            {
                variantOf = name.Substring(0, lfIndex);
            }

            if (r.name.Equals(r.GetBaseContext()))
            {
                ruleCtx = new StructDecl(factory, r);
                AddContextGetters(factory, r.g.contextASTs[r.name]);

                if (r.args != null)
                {
                    ICollection <Attribute> decls = r.args.attributes.Values;
                    if (decls.Count > 0)
                    {
                        args = new List <AttributeDecl>();
                        ruleCtx.AddDecls(decls);
                        foreach (Attribute a in decls)
                        {
                            args.Add(new AttributeDecl(factory, a));
                        }
                        ruleCtx.ctorAttrs = args;
                    }
                }
                if (r.retvals != null)
                {
                    ruleCtx.AddDecls(r.retvals.attributes.Values);
                }
                if (r.locals != null)
                {
                    ruleCtx.AddDecls(r.locals.attributes.Values);
                }
            }
            else
            {
                if (r.args != null || r.retvals != null || r.locals != null)
                {
                    throw new System.NotSupportedException("customized fields are not yet supported for customized context objects");
                }
            }

            ruleLabels  = r.GetElementLabelNames();
            tokenLabels = r.GetTokenRefs();
            if (r.exceptions != null)
            {
                exceptions = new List <ExceptionClause>();
                foreach (GrammarAST e in r.exceptions)
                {
                    ActionAST catchArg    = (ActionAST)e.GetChild(0);
                    ActionAST catchAction = (ActionAST)e.GetChild(1);
                    exceptions.Add(new ExceptionClause(factory, catchArg, catchAction));
                }
            }

            startState = factory.GetGrammar().atn.ruleToStartState[r.index];
        }