Example #1
0
 public Choice(OutputModelFactory factory,
               GrammarAST blkOrEbnfRootAST,
               IList <CodeBlockForAlt> alts)
     : base(factory, blkOrEbnfRootAST)
 {
     this.alts = alts;
 }
Example #2
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);
            }
        }
Example #3
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();
        }
Example #4
0
 public Choice(OutputModelFactory factory,
               GrammarAST blkOrEbnfRootAST,
               IList<CodeBlockForAlt> alts)
     : base(factory, blkOrEbnfRootAST)
 {
     this.alts = alts;
 }
Example #5
0
 public StructDecl(OutputModelFactory factory, Rule r)
     : base(factory, factory.GetTarget().GetRuleFunctionContextStructName(r))
 {
     AddDispatchMethods(r);
     derivedFromName = r.name;
     provideCopyFrom = r.HasAltSpecificContexts();
 }
Example #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);
        }
Example #7
0
 public RuleActionFunction(OutputModelFactory factory, Rule r, string ctxType)
     : base(factory)
 {
     name = r.name;
     ruleIndex = r.index;
     this.ctxType = ctxType;
 }
 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;
 }
Example #9
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;
 }
Example #10
0
 public StructDecl(OutputModelFactory factory, Rule r)
     : base(factory, factory.GetTarget().GetRuleFunctionContextStructName(r))
 {
     AddDispatchMethods(r);
     derivedFromName = r.name;
     provideCopyFrom = r.HasAltSpecificContexts();
 }
Example #11
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());
        }
Example #12
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);
 }
Example #13
0
 public RuleActionFunction(OutputModelFactory factory, Rule r, string ctxType)
     : base(factory)
 {
     name         = r.name;
     ruleIndex    = r.index;
     this.ctxType = ctxType;
 }
Example #14
0
 public RuleElement(OutputModelFactory factory, GrammarAST ast)
     : base(factory, ast)
 {
     if (ast != null && ast.atnState != null)
     {
         stateNumber = ast.atnState.stateNumber;
     }
 }
Example #15
0
 protected SrcOp(OutputModelFactory factory, GrammarAST ast)
     : base(factory, ast)
 {
     if (ast != null)
         uniqueID = ast.Token.TokenIndex;
     enclosingBlock = factory.GetCurrentBlock();
     enclosingRuleRunction = factory.GetCurrentRuleFunction();
 }
Example #16
0
 public Loop(OutputModelFactory factory,
             GrammarAST blkOrEbnfRootAST,
             IList<CodeBlockForAlt> alts)
     : base(factory, blkOrEbnfRootAST, alts)
 {
     bool nongreedy = (blkOrEbnfRootAST is QuantifierAST) && !((QuantifierAST)blkOrEbnfRootAST).GetGreedy();
     exitAlt = nongreedy ? 1 : alts.Count + 1;
 }
Example #17
0
 public ExceptionClause(OutputModelFactory factory,
                        ActionAST catchArg,
                        ActionAST catchAction)
     : base(factory, catchArg)
 {
     this.catchArg    = new Action(factory, catchArg);
     this.catchAction = new Action(factory, catchAction);
 }
Example #18
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);
        }
Example #19
0
 public ExceptionClause(OutputModelFactory factory,
                        ActionAST catchArg,
                        ActionAST catchAction)
     : base(factory, catchArg)
 {
     this.catchArg = new Action(factory, catchArg);
     this.catchAction = new Action(factory, catchAction);
 }
Example #20
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;
        }
Example #21
0
        //	@ModelElement public ThrowNoViableAlt error;

        public AltBlock(OutputModelFactory factory,
                        GrammarAST blkOrEbnfRootAST,
                        IList<CodeBlockForAlt> alts)
            : base(factory, blkOrEbnfRootAST, alts)
        {
            decision = ((BlockStartState)blkOrEbnfRootAST.atnState).decision;
            // interp.predict() throws exception
            //		this.error = new ThrowNoViableAlt(factory, blkOrEbnfRootAST, null);
        }
Example #22
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;
 }
Example #23
0
        public Loop(OutputModelFactory factory,
                    GrammarAST blkOrEbnfRootAST,
                    IList <CodeBlockForAlt> alts)
            : base(factory, blkOrEbnfRootAST, alts)
        {
            bool nongreedy = (blkOrEbnfRootAST is QuantifierAST) && !((QuantifierAST)blkOrEbnfRootAST).GetGreedy();

            exitAlt = nongreedy ? 1 : alts.Count + 1;
        }
Example #24
0
 public TestSetInline(OutputModelFactory factory, GrammarAST ast, IntervalSet set, int wordSize)
     : base(factory, ast)
 {
     bitsetWordSize = wordSize;
     Bitset[] withZeroOffset = CreateBitsets(factory, set, wordSize, true);
     Bitset[] withoutZeroOffset = CreateBitsets(factory, set, wordSize, false);
     this.bitsets = withZeroOffset.Length <= withoutZeroOffset.Length ? withZeroOffset : withoutZeroOffset;
     this.varName = "_la";
 }
Example #25
0
        //	@ModelElement public ThrowNoViableAlt error;

        public AltBlock(OutputModelFactory factory,
                        GrammarAST blkOrEbnfRootAST,
                        IList <CodeBlockForAlt> alts)
            : base(factory, blkOrEbnfRootAST, alts)
        {
            decision = ((BlockStartState)blkOrEbnfRootAST.atnState).decision;
            // interp.predict() throws exception
            //		this.error = new ThrowNoViableAlt(factory, blkOrEbnfRootAST, null);
        }
Example #26
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;
 }
Example #27
0
 public TestSetInline(OutputModelFactory factory, GrammarAST ast, IntervalSet set, int wordSize)
     : base(factory, ast)
 {
     bitsetWordSize = wordSize;
     Bitset[] withZeroOffset    = CreateBitsets(factory, set, wordSize, true);
     Bitset[] withoutZeroOffset = CreateBitsets(factory, set, wordSize, false);
     this.bitsets = withZeroOffset.Length <= withoutZeroOffset.Length ? withZeroOffset : withoutZeroOffset;
     this.varName = "_la";
 }
Example #28
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);
 }
Example #29
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;
 }
Example #30
0
 protected SrcOp(OutputModelFactory factory, GrammarAST ast)
     : base(factory, ast)
 {
     if (ast != null)
     {
         uniqueID = ast.Token.TokenIndex;
     }
     enclosingBlock        = factory.GetCurrentBlock();
     enclosingRuleRunction = factory.GetCurrentRuleFunction();
 }
Example #31
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;
 }
Example #32
0
 public MatchSet(OutputModelFactory factory, GrammarAST ast)
     : base(factory, ast)
 {
     SetTransition st = (SetTransition)ast.atnState.Transition(0);
     int wordSize = factory.GetGenerator().GetTarget().GetInlineTestSetWordSize();
     expr = new TestSetInline(factory, null, st.set, wordSize);
     Decl.Decl d = new TokenTypeDecl(factory, expr.varName);
     factory.GetCurrentRuleFunction().AddLocalDecl(d);
     capture = new CaptureNextTokenType(factory, expr.varName);
 }
Example #33
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;
        }
Example #34
0
        public MatchSet(OutputModelFactory factory, GrammarAST ast)
            : base(factory, ast)
        {
            SetTransition st       = (SetTransition)ast.atnState.Transition(0);
            int           wordSize = factory.GetGenerator().GetTarget().GetInlineTestSetWordSize();

            expr = new TestSetInline(factory, null, st.set, wordSize);
            Decl.Decl d = new TokenTypeDecl(factory, expr.varName);
            factory.GetCurrentRuleFunction().AddLocalDecl(d);
            capture = new CaptureNextTokenType(factory, expr.varName);
        }
Example #35
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;
        }
Example #36
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;
        }
Example #37
0
        //	public BitSetDecl expecting;

        public Sync(OutputModelFactory factory,
                    GrammarAST blkOrEbnfRootAST,
                    IntervalSet expecting,
                    int decision,
                    string position)
            : base(factory, blkOrEbnfRootAST)
        {
            this.decision = decision;
            //		this.expecting = factory.createExpectingBitSet(ast, decision, expecting, position);
            //		factory.defineBitSet(this.expecting);
        }
Example #38
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);
        }
Example #39
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));
 }
Example #40
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");
        }
        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);
        }
Example #42
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));
        }
Example #43
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);
        }
Example #44
0
 public PlusBlock(OutputModelFactory factory,
                  GrammarAST plusRoot,
                  IList<CodeBlockForAlt> alts)
     : base(factory, plusRoot, alts)
 {
     BlockAST blkAST = (BlockAST)plusRoot.GetChild(0);
     PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;
     PlusLoopbackState loop = blkStart.loopBackState;
     stateNumber = blkStart.loopBackState.stateNumber;
     blockStartStateNumber = blkStart.stateNumber;
     loopBackStateNumber = loop.stateNumber;
     this.error = GetThrowNoViableAlt(factory, plusRoot, null);
     decision = loop.decision;
 }
Example #45
0
 public Action(OutputModelFactory factory, ActionAST ast)
     : base(factory, ast)
 {
     RuleFunction rf = factory.GetCurrentRuleFunction();
     if (ast != null)
     {
         chunks = ActionTranslator.TranslateAction(factory, rf, ast.Token, ast);
     }
     else
     {
         chunks = new List<ActionChunk>();
     }
     //System.out.println("actions="+chunks);
 }
        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);
        }
Example #47
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;
            AccessModifier  = System.StringComparer.OrdinalIgnoreCase.Equals(g.GetOptionString("useInternalAccessModifier"), "true") ? "internal" : "public";
            IncludeDebuggerNonUserCodeAttribute = System.StringComparer.OrdinalIgnoreCase.Equals(g.GetOptionString("includeDebuggerNonUserCodeAttribute"), "true");
            IncludeClsCompliantAttribute        = !System.StringComparer.OrdinalIgnoreCase.Equals(g.GetOptionString("excludeClsCompliantAttribute"), "true"); // default is to include this to maintain existing behaviour
        }
Example #48
0
        public PlusBlock(OutputModelFactory factory,
                         GrammarAST plusRoot,
                         IList <CodeBlockForAlt> alts)
            : base(factory, plusRoot, alts)
        {
            BlockAST            blkAST   = (BlockAST)plusRoot.GetChild(0);
            PlusBlockStartState blkStart = (PlusBlockStartState)blkAST.atnState;
            PlusLoopbackState   loop     = blkStart.loopBackState;

            stateNumber           = blkStart.loopBackState.stateNumber;
            blockStartStateNumber = blkStart.stateNumber;
            loopBackStateNumber   = loop.stateNumber;
            this.error            = GetThrowNoViableAlt(factory, plusRoot, null);
            decision = loop.decision;
        }
Example #49
0
        public virtual void FillNamedActions(OutputModelFactory factory, Rule r)
        {
            if (r.finallyAction != null)
            {
                finallyAction = new Action(factory, r.finallyAction);
            }

            namedActions = new Dictionary <string, Action>();
            foreach (string name in r.namedActions.Keys)
            {
                ActionAST ast;
                r.namedActions.TryGetValue(name, out ast);
                namedActions[name] = new Action(factory, ast);
            }
        }
        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);
        }
Example #51
0
        public Action(OutputModelFactory factory, ActionAST ast)
            : base(factory, ast)
        {
            RuleFunction rf = factory.GetCurrentRuleFunction();

            if (ast != null)
            {
                chunks = ActionTranslator.TranslateAction(factory, rf, ast.Token, ast);
            }
            else
            {
                chunks = new List <ActionChunk>();
            }
            //System.out.println("actions="+chunks);
        }
Example #52
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");
        }
Example #53
0
 public Action(OutputModelFactory factory, StructDecl ctx, string action)
     : base(factory, null)
 {
     ActionAST ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action));
     RuleFunction rf = factory.GetCurrentRuleFunction();
     if (rf != null)
     { // we can translate
         ast.resolver = rf.rule;
         chunks = ActionTranslator.TranslateActionChunk(factory, rf, action, ast);
     }
     else
     {
         chunks = new List<ActionChunk>();
         chunks.Add(new ActionText(ctx, action));
     }
 }
Example #54
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"));
            }
        }
        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);
        }
Example #56
0
        public Action(OutputModelFactory factory, StructDecl ctx, string action)
            : base(factory, null)
        {
            ActionAST    ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action));
            RuleFunction rf  = factory.GetCurrentRuleFunction();

            if (rf != null)
            { // we can translate
                ast.resolver = rf.rule;
                chunks       = ActionTranslator.TranslateActionChunk(factory, rf, action, ast);
            }
            else
            {
                chunks = new List <ActionChunk>();
                chunks.Add(new ActionText(ctx, action));
            }
        }
Example #57
0
        public static IList<ActionChunk> TranslateAction(OutputModelFactory factory,
                                                        RuleFunction rf,
                                                        IToken tokenWithinAction,
                                                        ActionAST node)
        {
            string action = tokenWithinAction.Text;
            if (action != null && action.Length > 0 && action[0] == '{')
            {
                int firstCurly = action.IndexOf('{');
                int lastCurly = action.LastIndexOf('}');
                if (firstCurly >= 0 && lastCurly >= 0)
                {
                    action = action.Substring(firstCurly + 1, lastCurly - firstCurly - 1); // trim {...}
                }
            }

            return TranslateActionChunk(factory, rf, action, node);
        }
        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);
        }