예제 #1
0
        public virtual IntervalSet GetSetFromCharSetLiteral(GrammarAST charSetAST)
        {
            string chars = charSetAST.Text;

            chars = chars.Substring(1, chars.Length - 2);
            string      cset = '"' + chars + '"';
            IntervalSet set  = new IntervalSet();

            if (chars.Length == 0)
            {
                g.tool.errMgr.GrammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED,
                                           g.fileName, charSetAST.Token, "[]");
                return(set);
            }

            // unescape all valid escape char like \n, leaving escaped dashes as '\-'
            // so we can avoid seeing them as '-' range ops.
            chars = CharSupport.GetStringFromGrammarStringLiteral(cset);
            if (chars == null)
            {
                g.tool.errMgr.GrammarError(ErrorType.INVALID_ESCAPE_SEQUENCE,
                                           g.fileName, charSetAST.Token);
                return(set);
            }
            int n = chars.Length;

            // now make x-y become set of char
            for (int i = 0; i < n; i++)
            {
                int c = chars[i];
                if (c == '\\' && (i + 1) < n && chars[i + 1] == '-')
                { // \-
                    CheckSetCollision(charSetAST, set, '-');
                    set.Add('-');
                    i++;
                }
                else if ((i + 2) < n && chars[i + 1] == '-')
                { // range x-y
                    int x = c;
                    int y = chars[i + 2];
                    if (x <= y)
                    {
                        CheckSetCollision(charSetAST, set, x, y);
                        set.Add(x, y);
                    }
                    else
                    {
                        g.tool.errMgr.GrammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED,
                                                   g.fileName, charSetAST.Token, "[" + (char)x + "-" + (char)y + "]");
                    }
                    i += 2;
                }
                else
                {
                    CheckSetCollision(charSetAST, set, c);
                    set.Add(c);
                }
            }
            return(set);
        }
예제 #2
0
        public virtual IList <GrammarAST> GetNodesWithType(IntervalSet types)
        {
            IList <GrammarAST>      nodes = new List <GrammarAST>();
            LinkedList <GrammarAST> work  = new LinkedList <GrammarAST>();

            work.AddLast(this);
            GrammarAST t;

            while (work.Count > 0)
            {
                t = work.First.Value;
                work.RemoveFirst();
                if (types == null || types.Contains(t.Type))
                {
                    nodes.Add(t);
                }
                if (t.Children != null)
                {
                    foreach (var child in t.GetChildrenAsArray())
                    {
                        work.AddLast(child);
                    }
                }
            }
            return(nodes);
        }
예제 #3
0
        public virtual IList <GrammarAST> GetNodesWithTypePreorderDFS(IntervalSet types)
        {
            List <GrammarAST> nodes = new List <GrammarAST>();

            GetNodesWithTypePreorderDFS_(nodes, types);
            return(nodes);
        }
예제 #4
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();
        }
예제 #5
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());
        }
예제 #6
0
        public static Antlr4.Runtime.Misc.IntervalSet Of(int a)
        {
            Antlr4.Runtime.Misc.IntervalSet s = new Antlr4.Runtime.Misc.IntervalSet();

            s.Add(a);
            return(s);
        }
예제 #7
0
 public virtual Antlr4.Runtime.Misc.IntervalSet Or(IIntSet a)
 {
     Antlr4.Runtime.Misc.IntervalSet o = new Antlr4.Runtime.Misc.IntervalSet();
     o.AddAll(this);
     o.AddAll(a);
     return(o);
 }
예제 #8
0
        /** Return whether lookahead sets are disjoint; no lookahead ⇒ not disjoint */
        public static bool Disjoint(IntervalSet[] altLook)
        {
            bool        collision = false;
            IntervalSet combined  = new IntervalSet();

            if (altLook == null)
            {
                return(false);
            }
            foreach (IntervalSet look in altLook)
            {
                if (look == null)
                {
                    return(false); // lookahead must've computation failed
                }
                if (!look.And(combined).IsNil)
                {
                    collision = true;
                    break;
                }

                combined.AddAll(look);
            }
            return(!collision);
        }
예제 #9
0
 public virtual Antlr4.Runtime.Misc.IntervalSet AddAll(IIntSet set)
 {
     if (set == null)
     {
         return(this);
     }
     if (set is Antlr4.Runtime.Misc.IntervalSet)
     {
         Antlr4.Runtime.Misc.IntervalSet other = (Antlr4.Runtime.Misc.IntervalSet)set;
         // walk set and add each interval
         int n = other.intervals.Count;
         for (int i = 0; i < n; i++)
         {
             Interval I = other.intervals[i];
             this.Add(I.a, I.b);
         }
     }
     else
     {
         foreach (int value in set.ToList())
         {
             Add(value);
         }
     }
     return(this);
 }
예제 #10
0
 protected virtual void CheckSetCollision(GrammarAST ast, IntervalSet set, int el)
 {
     if (set.Contains(el))
     {
         g.tool.errMgr.GrammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, ast.Token,
                                    (char)el, ast.Text);
     }
 }
예제 #11
0
 /// <summary>
 /// Are two IntervalSets equal?  Because all intervals are sorted
 /// and disjoint, equals is a simple linear walk over both lists
 /// to make sure they are the same.
 /// </summary>
 /// <remarks>
 /// Are two IntervalSets equal?  Because all intervals are sorted
 /// and disjoint, equals is a simple linear walk over both lists
 /// to make sure they are the same.  Interval.equals() is used
 /// by the List.equals() method to check the ranges.
 /// </remarks>
 public override bool Equals(object obj)
 {
     if (obj == null || !(obj is Antlr4.Runtime.Misc.IntervalSet))
     {
         return(false);
     }
     Antlr4.Runtime.Misc.IntervalSet other = (Antlr4.Runtime.Misc.IntervalSet)obj;
     return(this.intervals.SequenceEqual(other.intervals));
 }
예제 #12
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";
 }
예제 #13
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";
 }
예제 #14
0
 /// <summary>combine all sets in the array returned the or'd value</summary>
 public static Antlr4.Runtime.Misc.IntervalSet Or(Antlr4.Runtime.Misc.IntervalSet[] sets)
 {
     Antlr4.Runtime.Misc.IntervalSet r = new Antlr4.Runtime.Misc.IntervalSet();
     foreach (Antlr4.Runtime.Misc.IntervalSet s in sets)
     {
         r.AddAll(s);
     }
     return(r);
 }
예제 #15
0
        /** for all alts, find which ref X or r needs List
         * Must see across alts.  If any alt needs X or r as list, then
         * define as list.
         */
        public virtual ISet <Decl.Decl> GetDeclsForAllElements(IList <AltAST> altASTs)
        {
            ISet <string>      needsList = new HashSet <string>();
            ISet <string>      optional  = new HashSet <string>();
            ISet <string>      suppress  = new HashSet <string>();
            IList <GrammarAST> allRefs   = new List <GrammarAST>();

            foreach (AltAST ast in altASTs)
            {
                IntervalSet        reftypes = new IntervalSet(ANTLRParser.RULE_REF, ANTLRParser.TOKEN_REF);
                IList <GrammarAST> refs     = ast.GetNodesWithType(reftypes);
                foreach (var @ref in refs)
                {
                    allRefs.Add(@ref);
                }

                System.Tuple <FrequencySet <string>, FrequencySet <string> > minAndAltFreq = GetElementFrequenciesForAlt(ast);
                FrequencySet <string> minFreq = minAndAltFreq.Item1;
                FrequencySet <string> altFreq = minAndAltFreq.Item2;
                foreach (GrammarAST t in refs)
                {
                    string refLabelName = GetLabelName(rule.g, t);
                    if (altFreq.GetCount(refLabelName) == 0)
                    {
                        suppress.Add(refLabelName);
                    }
                    if (minFreq.GetCount(refLabelName) == 0)
                    {
                        optional.Add(refLabelName);
                    }
                    if (altFreq.GetCount(refLabelName) > 1)
                    {
                        needsList.Add(refLabelName);
                    }
                }
            }

            ISet <Decl.Decl> decls = new LinkedHashSet <Decl.Decl>();

            foreach (GrammarAST t in allRefs)
            {
                string refLabelName = GetLabelName(rule.g, t);
                if (suppress.Contains(refLabelName))
                {
                    continue;
                }

                IList <Decl.Decl> d = GetDeclForAltElement(t,
                                                           refLabelName,
                                                           needsList.Contains(refLabelName),
                                                           optional.Contains(refLabelName));
                decls.UnionWith(d);
            }

            return(decls);
        }
예제 #16
0
파일: Choice.cs 프로젝트: sharwell/antlr4cs
        public virtual IList<string[]> GetAltLookaheadAsStringLists(IntervalSet[] altLookSets)
        {
            IList<string[]> altLook = new List<string[]>();
            foreach (IntervalSet s in altLookSets)
            {
                altLook.Add(factory.GetTarget().GetTokenTypesAsTargetLabels(factory.GetGrammar(), s.ToArray()));
            }

            return altLook;
        }
예제 #17
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);
 }
예제 #18
0
        /** [Aa\t \u1234a-z\]\-] char sets */
        public override Handle CharSetLiteral(GrammarAST charSetAST)
        {
            ATNState    left  = NewState(charSetAST);
            ATNState    right = NewState(charSetAST);
            IntervalSet set   = GetSetFromCharSetLiteral(charSetAST);

            left.AddTransition(new SetTransition(right, set));
            charSetAST.atnState = left;
            return(new Handle(left, right));
        }
예제 #19
0
        public virtual SrcOp AddCodeForLoopLookaheadTempVar(IntervalSet look)
        {
            TestSetInline expr = AddCodeForLookaheadTempVar(look);
            if (expr != null)
            {
                CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
                AddIterationOp(nextType);
            }

            return expr;
        }
예제 #20
0
파일: Sync.cs 프로젝트: yuanyong00/antlr4cs
        //	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);
        }
예제 #21
0
        public virtual SrcOp AddCodeForLoopLookaheadTempVar(IntervalSet look)
        {
            TestSetInline expr = AddCodeForLookaheadTempVar(look);

            if (expr != null)
            {
                CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
                AddIterationOp(nextType);
            }

            return(expr);
        }
예제 #22
0
        public virtual ATN CreateATN()
        {
            _CreateATN(g.rules.Values);

            Debug.Assert(atn.maxTokenType == g.GetMaxTokenType());
            AddRuleFollowLinks();
            AddEOFTransitionToStartRules();
            ATNOptimizer.Optimize(g, atn);

            foreach (System.Tuple <Rule, ATNState, ATNState> pair in preventEpsilonClosureBlocks)
            {
                LL1Analyzer analyzer  = new LL1Analyzer(atn);
                ATNState    blkStart  = pair.Item2;
                ATNState    blkStop   = pair.Item3;
                IntervalSet lookahead = analyzer.Look(blkStart, blkStop, PredictionContext.EmptyLocal);
                if (lookahead.Contains(Antlr4.Runtime.TokenConstants.Epsilon))
                {
                    ErrorType errorType = pair.Item1 is LeftRecursiveRule ? ErrorType.EPSILON_LR_FOLLOW : ErrorType.EPSILON_CLOSURE;
                    g.tool.errMgr.GrammarError(errorType, g.fileName, ((GrammarAST)pair.Item1.ast.GetChild(0)).Token, pair.Item1.name);
                }
            }

            foreach (System.Tuple <Rule, ATNState, ATNState> pair in preventEpsilonOptionalBlocks)
            {
                int bypassCount = 0;
                for (int i = 0; i < pair.Item2.NumberOfTransitions; i++)
                {
                    ATNState startState = pair.Item2.Transition(i).target;
                    if (startState == pair.Item3)
                    {
                        bypassCount++;
                        continue;
                    }

                    LL1Analyzer analyzer = new LL1Analyzer(atn);
                    if (analyzer.Look(startState, pair.Item3, PredictionContext.EmptyLocal).Contains(Antlr4.Runtime.TokenConstants.Epsilon))
                    {
                        g.tool.errMgr.GrammarError(ErrorType.EPSILON_OPTIONAL, g.fileName, ((GrammarAST)pair.Item1.ast.GetChild(0)).Token, pair.Item1.name);
                        goto continueOptionalCheck;
                    }
                }

                if (bypassCount != 1)
                {
                    throw new InvalidOperationException("Expected optional block with exactly 1 bypass alternative.");
                }

continueOptionalCheck:
                ;
            }

            return(atn);
        }
예제 #23
0
 public virtual void GetNodesWithTypePreorderDFS_(IList <GrammarAST> nodes, IntervalSet types)
 {
     if (types.Contains(this.Type))
     {
         nodes.Add(this);
     }
     // walk all children of root.
     for (int i = 0; i < ChildCount; i++)
     {
         GrammarAST child = (GrammarAST)GetChild(i);
         child.GetNodesWithTypePreorderDFS_(nodes, types);
     }
 }
예제 #24
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);
        }
예제 #25
0
파일: Choice.cs 프로젝트: sharwell/antlr4cs
 public virtual TestSetInline AddCodeForLookaheadTempVar(IntervalSet look)
 {
     IList<SrcOp> testOps = factory.GetLL1Test(look, ast);
     TestSetInline expr = Utils.Find<TestSetInline>(testOps);
     if (expr != null)
     {
         Decl.Decl d = new TokenTypeDecl(factory, expr.varName);
         factory.GetCurrentRuleFunction().AddLocalDecl(d);
         CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
         AddPreambleOp(nextType);
     }
     return expr;
 }
예제 #26
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);
        }
예제 #27
0
 public virtual Antlr4.Runtime.Misc.IntervalSet Subtract(IIntSet a)
 {
     if (a == null || a.IsNil)
     {
         return(new Antlr4.Runtime.Misc.IntervalSet(this));
     }
     if (a is Antlr4.Runtime.Misc.IntervalSet)
     {
         return(Subtract(this, (Antlr4.Runtime.Misc.IntervalSet)a));
     }
     Antlr4.Runtime.Misc.IntervalSet other = new Antlr4.Runtime.Misc.IntervalSet();
     other.AddAll(a);
     return(Subtract(this, other));
 }
예제 #28
0
        public virtual TestSetInline AddCodeForLookaheadTempVar(IntervalSet look)
        {
            IList <SrcOp> testOps = factory.GetLL1Test(look, ast);
            TestSetInline expr    = Utils.Find <TestSetInline>(testOps);

            if (expr != null)
            {
                Decl.Decl d = new TokenTypeDecl(factory, expr.varName);
                factory.GetCurrentRuleFunction().AddLocalDecl(d);
                CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
                AddPreambleOp(nextType);
            }
            return(expr);
        }
예제 #29
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);
        }
예제 #30
0
        protected virtual void ProcessLexer()
        {
            // make sure all non-fragment lexer rules must match at least one symbol
            foreach (Rule rule in g.rules.Values)
            {
                if (rule.IsFragment())
                {
                    continue;
                }

                LL1Analyzer analyzer = new LL1Analyzer(g.atn);
                IntervalSet look     = analyzer.Look(g.atn.ruleToStartState[rule.index], PredictionContext.EmptyLocal);
                if (look.Contains(TokenConstants.Epsilon))
                {
                    g.tool.errMgr.GrammarError(ErrorType.EPSILON_TOKEN, g.fileName, ((GrammarAST)rule.ast.GetChild(0)).Token, rule.name);
                }
            }
        }
예제 #31
0
 /// <summary>
 /// <inheritDoc/>
 ///
 /// </summary>
 public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary)
 {
     if (vocabulary == null || vocabulary.IsNil)
     {
         return(null);
     }
     // nothing in common with null set
     Antlr4.Runtime.Misc.IntervalSet vocabularyIS;
     if (vocabulary is Antlr4.Runtime.Misc.IntervalSet)
     {
         vocabularyIS = (Antlr4.Runtime.Misc.IntervalSet)vocabulary;
     }
     else
     {
         vocabularyIS = new Antlr4.Runtime.Misc.IntervalSet();
         vocabularyIS.AddAll(vocabulary);
     }
     return(vocabularyIS.Subtract(this));
 }
예제 #32
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);
        }
예제 #33
0
        public virtual AltAST AddPrecedenceArgToRules(AltAST t, int prec)
        {
            if (t == null)
            {
                return(null);
            }
            // get all top-level rule refs from ALT
            IList <GrammarAST> outerAltRuleRefs = t.GetNodesWithTypePreorderDFS(IntervalSet.Of(RULE_REF));

            foreach (GrammarAST x in outerAltRuleRefs)
            {
                RuleRefAST rref      = (RuleRefAST)x;
                bool       recursive = rref.Text.Equals(ruleName);
                bool       rightmost = rref == outerAltRuleRefs[outerAltRuleRefs.Count - 1];
                if (recursive && rightmost)
                {
                    GrammarAST dummyValueNode = new GrammarAST(new CommonToken(ANTLRParser.INT, "" + prec));
                    rref.SetOption(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME, dummyValueNode);
                }
            }
            return(t);
        }
예제 #34
0
        protected virtual void CheckSetCollision(GrammarAST ast, IntervalSet set, int a, int b)
        {
            for (int i = a; i <= b; i++)
            {
                if (set.Contains(i))
                {
                    string setText;
                    if (ast.Children == null)
                    {
                        setText = ast.Text;
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (object child in ast.Children)
                        {
                            if (child is RangeAST)
                            {
                                sb.Append(((RangeAST)child).GetChild(0).Text);
                                sb.Append("..");
                                sb.Append(((RangeAST)child).GetChild(1).Text);
                            }
                            else
                            {
                                sb.Append(((GrammarAST)child).Text);
                            }
                            sb.Append(" | ");
                        }
                        sb.Remove(sb.Length - 3, 3);
                        setText = sb.ToString();
                    }

                    g.tool.errMgr.GrammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, ast.Token,
                                               (char)a + "-" + (char)b, setText);
                    break;
                }
            }
        }
예제 #35
0
        public virtual Handle Set([NotNull] GrammarAST associatedAST, [NotNull] IList <GrammarAST> terminals, bool invert)
        {
            ATNState left = NewState(associatedAST);

            ATNState    right = NewState(associatedAST);
            IntervalSet set   = new IntervalSet();

            foreach (GrammarAST t in terminals)
            {
                int ttype = g.GetTokenType(t.Text);
                set.Add(ttype);
            }
            if (invert)
            {
                left.AddTransition(new NotSetTransition(right, set));
            }
            else
            {
                left.AddTransition(new SetTransition(right, set));
            }
            associatedAST.atnState = left;
            return(new Handle(left, right));
        }
예제 #36
0
        protected virtual void ProcessParser()
        {
            g.decisionLOOK = new List<IntervalSet[]>(g.atn.NumberOfDecisions + 1);
            foreach (DecisionState s in g.atn.decisionToState)
            {
                g.tool.Log("LL1", "\nDECISION " + s.decision + " in rule " + g.GetRule(s.ruleIndex).name);
                IntervalSet[] look;
                if (s.nonGreedy)
                { // nongreedy decisions can't be LL(1)
                    look = new IntervalSet[s.NumberOfTransitions + 1];
                }
                else
                {
                    LL1Analyzer anal = new LL1Analyzer(g.atn);
                    look = anal.GetDecisionLookahead(s);
                    g.tool.Log("LL1", "look=[" + string.Join(", ", look.AsEnumerable()) + "]");
                }

                Debug.Assert(s.decision + 1 >= g.decisionLOOK.Count);
                Utils.SetSize(g.decisionLOOK, s.decision + 1);
                g.decisionLOOK[s.decision] = look;
                g.tool.Log("LL1", "LL(1)? " + Disjoint(look));
            }
        }
예제 #37
0
파일: Choice.cs 프로젝트: sharwell/antlr4cs
 public virtual ThrowNoViableAlt GetThrowNoViableAlt(OutputModelFactory factory,
                                             GrammarAST blkAST,
                                             IntervalSet expecting)
 {
     return new ThrowNoViableAlt(factory, blkAST, expecting);
 }
예제 #38
0
        public virtual void Process()
        {
            CodeGenerator gen = new CodeGenerator(g);
            AbstractTarget target = gen.GetTarget();
            if (target == null)
            {
                return;
            }

            IntervalSet idTypes = new IntervalSet();
            idTypes.Add(ANTLRParser.ID);
            idTypes.Add(ANTLRParser.RULE_REF);
            idTypes.Add(ANTLRParser.TOKEN_REF);
            IList<GrammarAST> idNodes = g.ast.GetNodesWithType(idTypes);
            foreach (GrammarAST idNode in idNodes)
            {
                if (target.GrammarSymbolCausesIssueInGeneratedCode(idNode))
                {
                    g.tool.errMgr.GrammarError(ErrorType.USE_OF_BAD_WORD,
                                               g.fileName, idNode.Token,
                                               idNode.Text);
                }
            }

            // all templates are generated in memory to report the most complete
            // error information possible, but actually writing output files stops
            // after the first error is reported
            int errorCount = g.tool.errMgr.GetNumErrors();

            if (g.IsLexer())
            {
                if (target.NeedsHeader())
                {
                    Template lexerHeader = gen.GenerateLexer(true); // Header file if needed.
                    if (g.tool.errMgr.GetNumErrors() == errorCount)
                    {
                        WriteRecognizer(lexerHeader, gen, true);
                    }
                }
                Template lexer = gen.GenerateLexer(false);
                if (g.tool.errMgr.GetNumErrors() == errorCount)
                {
                    WriteRecognizer(lexer, gen, false);
                }
            }
            else
            {
                if (target.NeedsHeader())
                {
                    Template parserHeader = gen.GenerateParser(true);
                    if (g.tool.errMgr.GetNumErrors() == errorCount)
                    {
                        WriteRecognizer(parserHeader, gen, true);
                    }
                }
                Template parser = gen.GenerateParser(false);
                if (g.tool.errMgr.GetNumErrors() == errorCount)
                {
                    WriteRecognizer(parser, gen, false);
                }

                if (g.tool.gen_listener)
                {
                    if (target.NeedsHeader())
                    {
                        Template listenerHeader = gen.GenerateListener(true);
                        if (g.tool.errMgr.GetNumErrors() == errorCount)
                        {
                            gen.WriteListener(listenerHeader, true);
                        }
                    }
                    Template listener = gen.GenerateListener(false);
                    if (g.tool.errMgr.GetNumErrors() == errorCount)
                    {
                        gen.WriteListener(listener, false);
                    }

                    if (target.NeedsHeader())
                    {
                        Template baseListener = gen.GenerateBaseListener(true);
                        if (g.tool.errMgr.GetNumErrors() == errorCount)
                        {
                            gen.WriteBaseListener(baseListener, true);
                        }
                    }
                    if (target.WantsBaseListener())
                    {
                        Template baseListener = gen.GenerateBaseListener(false);
                        if (g.tool.errMgr.GetNumErrors() == errorCount)
                        {
                            gen.WriteBaseListener(baseListener, false);
                        }
                    }
                }
                if (g.tool.gen_visitor)
                {
                    if (target.NeedsHeader())
                    {
                        Template visitorHeader = gen.GenerateVisitor(true);
                        if (g.tool.errMgr.GetNumErrors() == errorCount)
                        {
                            gen.WriteVisitor(visitorHeader, true);
                        }
                    }
                    Template visitor = gen.GenerateVisitor(false);
                    if (g.tool.errMgr.GetNumErrors() == errorCount)
                    {
                        gen.WriteVisitor(visitor, false);
                    }

                    if (target.NeedsHeader())
                    {
                        Template baseVisitor = gen.GenerateBaseVisitor(true);
                        if (g.tool.errMgr.GetNumErrors() == errorCount)
                        {
                            gen.WriteBaseVisitor(baseVisitor, true);
                        }
                    }
                    if (target.WantsBaseVisitor())
                    {
                        Template baseVisitor = gen.GenerateBaseVisitor(false);
                        if (g.tool.errMgr.GetNumErrors() == errorCount)
                        {
                            gen.WriteBaseVisitor(baseVisitor, false);
                        }
                    }
                }
            }

            gen.WriteVocabFile();
        }
예제 #39
0
파일: IntervalSet.cs 프로젝트: antlr/antlr4
 /// <summary>
 /// <inheritDoc/>
 ///
 /// </summary>
 public virtual Antlr4.Runtime.Misc.IntervalSet And(IIntSet other)
 {
     if (other == null)
     {
         //|| !(other instanceof IntervalSet) ) {
         return null;
     }
     // nothing in common with null set
     IList<Interval> myIntervals = this.intervals;
     IList<Interval> theirIntervals = ((Antlr4.Runtime.Misc.IntervalSet)other).intervals;
     Antlr4.Runtime.Misc.IntervalSet intersection = null;
     int mySize = myIntervals.Count;
     int theirSize = theirIntervals.Count;
     int i = 0;
     int j = 0;
     // iterate down both interval lists looking for nondisjoint intervals
     while (i < mySize && j < theirSize)
     {
         Interval mine = myIntervals[i];
         Interval theirs = theirIntervals[j];
         //System.out.println("mine="+mine+" and theirs="+theirs);
         if (mine.StartsBeforeDisjoint(theirs))
         {
             // move this iterator looking for interval that might overlap
             i++;
         }
         else
         {
             if (theirs.StartsBeforeDisjoint(mine))
             {
                 // move other iterator looking for interval that might overlap
                 j++;
             }
             else
             {
                 if (mine.ProperlyContains(theirs))
                 {
                     // overlap, add intersection, get next theirs
                     if (intersection == null)
                     {
                         intersection = new Antlr4.Runtime.Misc.IntervalSet();
                     }
                     intersection.Add(mine.Intersection(theirs));
                     j++;
                 }
                 else
                 {
                     if (theirs.ProperlyContains(mine))
                     {
                         // overlap, add intersection, get next mine
                         if (intersection == null)
                         {
                             intersection = new Antlr4.Runtime.Misc.IntervalSet();
                         }
                         intersection.Add(mine.Intersection(theirs));
                         i++;
                     }
                     else
                     {
                         if (!mine.Disjoint(theirs))
                         {
                             // overlap, add intersection
                             if (intersection == null)
                             {
                                 intersection = new Antlr4.Runtime.Misc.IntervalSet();
                             }
                             intersection.Add(mine.Intersection(theirs));
                             // Move the iterator of lower range [a..b], but not
                             // the upper range as it may contain elements that will collide
                             // with the next iterator. So, if mine=[0..115] and
                             // theirs=[115..200], then intersection is 115 and move mine
                             // but not theirs as theirs may collide with the next range
                             // in thisIter.
                             // move both iterators to next ranges
                             if (mine.StartsAfterNonDisjoint(theirs))
                             {
                                 j++;
                             }
                             else
                             {
                                 if (theirs.StartsAfterNonDisjoint(mine))
                                 {
                                     i++;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (intersection == null)
     {
         return new Antlr4.Runtime.Misc.IntervalSet();
     }
     return intersection;
 }
예제 #40
0
        public virtual string Text(GrammarAST t)
        {
            if (t == null)
                return "";

            int tokenStartIndex = t.TokenStartIndex;
            int tokenStopIndex = t.TokenStopIndex;

            // ignore tokens from existing option subtrees like:
            //    (ELEMENT_OPTIONS (= assoc right))
            //
            // element options are added back according to the values in the map
            // returned by getOptions().
            IntervalSet ignore = new IntervalSet();
            IList<GrammarAST> optionsSubTrees = t.GetNodesWithType(ELEMENT_OPTIONS);
            foreach (GrammarAST sub in optionsSubTrees)
            {
                ignore.Add(sub.TokenStartIndex, sub.TokenStopIndex);
            }

            // Individual labels appear as RULE_REF or TOKEN_REF tokens in the tree,
            // but do not support the ELEMENT_OPTIONS syntax. Make sure to not try
            // and add the tokenIndex option when writing these tokens.
            IntervalSet noOptions = new IntervalSet();
            IList<GrammarAST> labeledSubTrees = t.GetNodesWithType(new IntervalSet(ASSIGN, PLUS_ASSIGN));
            foreach (GrammarAST sub in labeledSubTrees)
            {
                noOptions.Add(sub.GetChild(0).TokenStartIndex);
            }

            StringBuilder buf = new StringBuilder();
            int i = tokenStartIndex;
            while (i <= tokenStopIndex)
            {
                if (ignore.Contains(i))
                {
                    i++;
                    continue;
                }

                IToken tok = tokenStream.Get(i);

                // Compute/hold any element options
                StringBuilder elementOptions = new StringBuilder();
                if (!noOptions.Contains(i))
                {
                    GrammarAST node = t.GetNodeWithTokenIndex(tok.TokenIndex);
                    if (node != null &&
                         (tok.Type == TOKEN_REF ||
                          tok.Type == STRING_LITERAL ||
                          tok.Type == RULE_REF))
                    {
                        elementOptions.Append("tokenIndex=").Append(tok.TokenIndex);
                    }

                    if (node is GrammarASTWithOptions)
                    {
                        GrammarASTWithOptions o = (GrammarASTWithOptions)node;
                        foreach (KeyValuePair<string, GrammarAST> entry in o.GetOptions())
                        {
                            if (elementOptions.Length > 0)
                            {
                                elementOptions.Append(',');
                            }

                            elementOptions.Append(entry.Key);
                            elementOptions.Append('=');
                            elementOptions.Append(entry.Value.Text);
                        }
                    }
                }

                buf.Append(tok.Text); // add actual text of the current token to the rewritten alternative
                i++;                       // move to the next token

                // Are there args on a rule?
                if (tok.Type == RULE_REF && i <= tokenStopIndex && tokenStream.Get(i).Type == ARG_ACTION)
                {
                    buf.Append('[' + tokenStream.Get(i).Text + ']');
                    i++;
                }

                // now that we have the actual element, we can add the options.
                if (elementOptions.Length > 0)
                {
                    buf.Append('<').Append(elementOptions).Append('>');
                }
            }
            return buf.ToString();
        }
예제 #41
0
파일: IntervalSet.cs 프로젝트: antlr/antlr4
 public static Antlr4.Runtime.Misc.IntervalSet Subtract(Antlr4.Runtime.Misc.IntervalSet left, Antlr4.Runtime.Misc.IntervalSet right)
 {
     if (left == null || left.IsNil)
     {
         return new Antlr4.Runtime.Misc.IntervalSet();
     }
     Antlr4.Runtime.Misc.IntervalSet result = new Antlr4.Runtime.Misc.IntervalSet(left);
     if (right == null || right.IsNil)
     {
         // right set has no elements; just return the copy of the current set
         return result;
     }
     int resultI = 0;
     int rightI = 0;
     while (resultI < result.intervals.Count && rightI < right.intervals.Count)
     {
         Interval resultInterval = result.intervals[resultI];
         Interval rightInterval = right.intervals[rightI];
         // operation: (resultInterval - rightInterval) and update indexes
         if (rightInterval.b < resultInterval.a)
         {
             rightI++;
             continue;
         }
         if (rightInterval.a > resultInterval.b)
         {
             resultI++;
             continue;
         }
         Interval? beforeCurrent = null;
         Interval? afterCurrent = null;
         if (rightInterval.a > resultInterval.a)
         {
             beforeCurrent = new Interval(resultInterval.a, rightInterval.a - 1);
         }
         if (rightInterval.b < resultInterval.b)
         {
             afterCurrent = new Interval(rightInterval.b + 1, resultInterval.b);
         }
         if (beforeCurrent != null)
         {
             if (afterCurrent != null)
             {
                 // split the current interval into two
                 result.intervals[resultI] = beforeCurrent.Value;
                 result.intervals.Insert(resultI + 1, afterCurrent.Value);
                 resultI++;
                 rightI++;
                 continue;
             }
             else
             {
                 // replace the current interval
                 result.intervals[resultI] = beforeCurrent.Value;
                 resultI++;
                 continue;
             }
         }
         else
         {
             if (afterCurrent != null)
             {
                 // replace the current interval
                 result.intervals[resultI] = afterCurrent.Value;
                 rightI++;
                 continue;
             }
             else
             {
                 // remove the current interval (thus no need to increment resultI)
                 result.intervals.RemoveAt(resultI);
                 continue;
             }
         }
     }
     // If rightI reached right.intervals.size(), no more intervals to subtract from result.
     // If resultI reached result.intervals.size(), we would be subtracting from an empty set.
     // Either way, we are done.
     return result;
 }
예제 #42
0
 public virtual IList<GrammarAST> GetNodesWithType(IntervalSet types)
 {
     IList<GrammarAST> nodes = new List<GrammarAST>();
     LinkedList<GrammarAST> work = new LinkedList<GrammarAST>();
     work.AddLast(this);
     GrammarAST t;
     while (work.Count > 0)
     {
         t = work.First.Value;
         work.RemoveFirst();
         if (types == null || types.Contains(t.Type))
             nodes.Add(t);
         if (t.Children != null)
         {
             foreach (var child in t.GetChildrenAsArray())
                 work.AddLast(child);
         }
     }
     return nodes;
 }
예제 #43
0
        /** for all alts, find which ref X or r needs List
           Must see across alts.  If any alt needs X or r as list, then
           define as list.
         */
        public virtual ISet<Decl.Decl> GetDeclsForAllElements(IList<AltAST> altASTs)
        {
            ISet<string> needsList = new HashSet<string>();
            ISet<string> optional = new HashSet<string>();
            ISet<string> suppress = new HashSet<string>();
            IList<GrammarAST> allRefs = new List<GrammarAST>();
            foreach (AltAST ast in altASTs)
            {
                IntervalSet reftypes = new IntervalSet(ANTLRParser.RULE_REF, ANTLRParser.TOKEN_REF);
                IList<GrammarAST> refs = ast.GetNodesWithType(reftypes);
                foreach (var @ref in refs)
                    allRefs.Add(@ref);

                System.Tuple<FrequencySet<string>, FrequencySet<string>> minAndAltFreq = GetElementFrequenciesForAlt(ast);
                FrequencySet<string> minFreq = minAndAltFreq.Item1;
                FrequencySet<string> altFreq = minAndAltFreq.Item2;
                foreach (GrammarAST t in refs)
                {
                    string refLabelName = GetLabelName(rule.g, t);
                    if (altFreq.GetCount(refLabelName) == 0)
                    {
                        suppress.Add(refLabelName);
                    }
                    if (minFreq.GetCount(refLabelName) == 0)
                    {
                        optional.Add(refLabelName);
                    }
                    if (altFreq.GetCount(refLabelName) > 1)
                    {
                        needsList.Add(refLabelName);
                    }
                }
            }

            ISet<Decl.Decl> decls = new LinkedHashSet<Decl.Decl>();
            foreach (GrammarAST t in allRefs)
            {
                string refLabelName = GetLabelName(rule.g, t);
                if (suppress.Contains(refLabelName))
                {
                    continue;
                }

                IList<Decl.Decl> d = GetDeclForAltElement(t,
                                                    refLabelName,
                                                    needsList.Contains(refLabelName),
                                                    optional.Contains(refLabelName));
                decls.UnionWith(d);
            }

            return decls;
        }
예제 #44
0
파일: IntervalSet.cs 프로젝트: antlr/antlr4
 /// <summary>
 /// <inheritDoc/>
 ///
 /// </summary>
 public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary)
 {
     if (vocabulary == null || vocabulary.IsNil)
     {
         return null;
     }
     // nothing in common with null set
     Antlr4.Runtime.Misc.IntervalSet vocabularyIS;
     if (vocabulary is Antlr4.Runtime.Misc.IntervalSet)
     {
         vocabularyIS = (Antlr4.Runtime.Misc.IntervalSet)vocabulary;
     }
     else
     {
         vocabularyIS = new Antlr4.Runtime.Misc.IntervalSet();
         vocabularyIS.AddAll(vocabulary);
     }
     return vocabularyIS.Subtract(this);
 }
예제 #45
0
        public static IDictionary<int, Interval> GetStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes)
        {
            IDictionary<int, Interval> stateToGrammarRegionMap = new Dictionary<int, Interval>();
            if (ast == null)
                return stateToGrammarRegionMap;

            IList<GrammarAST> nodes = ast.GetNodesWithType(grammarTokenTypes);
            foreach (GrammarAST n in nodes)
            {
                if (n.atnState != null)
                {
                    Interval tokenRegion = Interval.Of(n.TokenStartIndex, n.TokenStopIndex);
                    Antlr.Runtime.Tree.ITree ruleNode = null;
                    // RULEs, BLOCKs of transformed recursive rules point to original token interval
                    switch (n.Type)
                    {
                    case ANTLRParser.RULE:
                        ruleNode = n;
                        break;
                    case ANTLRParser.BLOCK:
                    case ANTLRParser.CLOSURE:
                        ruleNode = n.GetAncestor(ANTLRParser.RULE);
                        break;
                    }
                    if (ruleNode is RuleAST)
                    {
                        string ruleName = ((RuleAST)ruleNode).GetRuleName();
                        Rule r = ast.g.GetRule(ruleName);
                        if (r is LeftRecursiveRule)
                        {
                            RuleAST originalAST = ((LeftRecursiveRule)r).GetOriginalAST();
                            tokenRegion = Interval.Of(originalAST.TokenStartIndex, originalAST.TokenStopIndex);
                        }
                    }
                    stateToGrammarRegionMap[n.atnState.stateNumber] = tokenRegion;
                }
            }
            return stateToGrammarRegionMap;
        }
예제 #46
0
        public virtual IntervalSet GetSetFromCharSetLiteral(GrammarAST charSetAST)
        {
            string chars = charSetAST.Text;
            chars = chars.Substring(1, chars.Length - 2);
            string cset = '"' + chars + '"';
            IntervalSet set = new IntervalSet();

            if (chars.Length == 0)
            {
                g.tool.errMgr.GrammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED,
                        g.fileName, charSetAST.Token, "[]");
                return set;
            }

            // unescape all valid escape char like \n, leaving escaped dashes as '\-'
            // so we can avoid seeing them as '-' range ops.
            chars = CharSupport.GetStringFromGrammarStringLiteral(cset);
            if (chars == null)
            {
                g.tool.errMgr.GrammarError(ErrorType.INVALID_ESCAPE_SEQUENCE,
                                           g.fileName, charSetAST.Token);
                return set;
            }
            int n = chars.Length;
            // now make x-y become set of char
            for (int i = 0; i < n; i++)
            {
                int c = chars[i];
                if (c == '\\' && (i + 1) < n && chars[i + 1] == '-')
                { // \-
                    CheckSetCollision(charSetAST, set, '-');
                    set.Add('-');
                    i++;
                }
                else if ((i + 2) < n && chars[i + 1] == '-')
                { // range x-y
                    int x = c;
                    int y = chars[i + 2];
                    if (x <= y)
                    {
                        CheckSetCollision(charSetAST, set, x, y);
                        set.Add(x, y);
                    }
                    else
                    {
                        g.tool.errMgr.GrammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED,
                                                   g.fileName, charSetAST.Token, "[" + (char)x + "-" + (char)y + "]");
                    }
                    i += 2;
                }
                else
                {
                    CheckSetCollision(charSetAST, set, c);
                    set.Add(c);
                }
            }
            return set;
        }
예제 #47
0
파일: IntervalSet.cs 프로젝트: antlr/antlr4
 public virtual Antlr4.Runtime.Misc.IntervalSet Subtract(IIntSet a)
 {
     if (a == null || a.IsNil)
     {
         return new Antlr4.Runtime.Misc.IntervalSet(this);
     }
     if (a is Antlr4.Runtime.Misc.IntervalSet)
     {
         return Subtract(this, (Antlr4.Runtime.Misc.IntervalSet)a);
     }
     Antlr4.Runtime.Misc.IntervalSet other = new Antlr4.Runtime.Misc.IntervalSet();
     other.AddAll(a);
     return Subtract(this, other);
 }
예제 #48
0
파일: IntervalSet.cs 프로젝트: antlr/antlr4
 public virtual Antlr4.Runtime.Misc.IntervalSet Or(IIntSet a)
 {
     Antlr4.Runtime.Misc.IntervalSet o = new Antlr4.Runtime.Misc.IntervalSet();
     o.AddAll(this);
     o.AddAll(a);
     return o;
 }
예제 #49
0
 public virtual void GetNodesWithTypePreorderDFS_(IList<GrammarAST> nodes, IntervalSet types)
 {
     if (types.Contains(this.Type))
         nodes.Add(this);
     // walk all children of root.
     for (int i = 0; i < ChildCount; i++)
     {
         GrammarAST child = (GrammarAST)GetChild(i);
         child.GetNodesWithTypePreorderDFS_(nodes, types);
     }
 }
        protected virtual bool TranslateLeftFactoredDecision(GrammarAST block, string factoredRule, bool variant, DecisionFactorMode mode, bool includeFactoredElement)
        {
            if (mode == DecisionFactorMode.PARTIAL_UNFACTORED && includeFactoredElement)
            {
                throw new ArgumentException("Cannot include the factored element in unfactored alternatives.");
            }
            else if (mode == DecisionFactorMode.COMBINED_FACTOR && !includeFactoredElement)
            {
                throw new ArgumentException("Cannot return a combined answer without the factored element.");
            }

            if (!ExpandOptionalQuantifiersForBlock(block, variant))
            {
                return false;
            }

            IList<GrammarAST> alternatives = block.GetAllChildrenWithType(ANTLRParser.ALT);
            GrammarAST[] factoredAlternatives = new GrammarAST[alternatives.Count];
            GrammarAST[] unfactoredAlternatives = new GrammarAST[alternatives.Count];
            IntervalSet factoredIntervals = new IntervalSet();
            IntervalSet unfactoredIntervals = new IntervalSet();
            for (int i = 0; i < alternatives.Count; i++)
            {
                GrammarAST alternative = alternatives[i];
                if (mode.IncludeUnfactoredAlts())
                {
                    GrammarAST unfactoredAlt = TranslateLeftFactoredAlternative(alternative.DupTree(), factoredRule, variant, DecisionFactorMode.PARTIAL_UNFACTORED, false);
                    unfactoredAlternatives[i] = unfactoredAlt;
                    if (unfactoredAlt != null)
                    {
                        unfactoredIntervals.Add(i);
                    }
                }

                if (mode.IncludeFactoredAlts())
                {
                    GrammarAST factoredAlt = TranslateLeftFactoredAlternative(alternative, factoredRule, variant, mode == DecisionFactorMode.COMBINED_FACTOR ? DecisionFactorMode.PARTIAL_FACTORED : DecisionFactorMode.FULL_FACTOR, includeFactoredElement);
                    factoredAlternatives[i] = factoredAlt;
                    if (factoredAlt != null)
                    {
                        factoredIntervals.Add(alternative.ChildIndex);
                    }
                }
            }

            if (factoredIntervals.IsNil && !mode.IncludeUnfactoredAlts())
            {
                return false;
            }
            else if (unfactoredIntervals.IsNil && !mode.IncludeFactoredAlts())
            {
                return false;
            }

            if (unfactoredIntervals.IsNil && factoredIntervals.Count == alternatives.Count && mode.IncludeFactoredAlts() && !includeFactoredElement)
            {
                for (int i = 0; i < factoredAlternatives.Length; i++)
                {
                    GrammarAST translatedAlt = factoredAlternatives[i];
                    if (translatedAlt.ChildCount == 0)
                    {
                        adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                    }

                    adaptor.SetChild(block, i, translatedAlt);
                }

                return true;
            }
            else if (factoredIntervals.IsNil && unfactoredIntervals.Count == alternatives.Count && mode.IncludeUnfactoredAlts())
            {
                for (int i = 0; i < unfactoredAlternatives.Length; i++)
                {
                    GrammarAST translatedAlt = unfactoredAlternatives[i];
                    if (translatedAlt.ChildCount == 0)
                    {
                        adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                    }

                    adaptor.SetChild(block, i, translatedAlt);
                }

                return true;
            }

            if (mode == DecisionFactorMode.FULL_FACTOR)
            {
                return false;
            }

            /* for a, b, c being arbitrary `element` trees, this block performs
             * this transformation:
             *
             * factoredElement a
             * | factoredElement b
             * | factoredElement c
             * | ...
             *
             * ==>
             *
             * factoredElement (a | b | c | ...)
             */
            GrammarAST newChildren = (GrammarAST)adaptor.Nil();
            for (int i = 0; i < alternatives.Count; i++)
            {
                if (mode.IncludeFactoredAlts() && factoredIntervals.Contains(i))
                {
                    bool combineWithPrevious = i > 0 && factoredIntervals.Contains(i - 1) && (!mode.IncludeUnfactoredAlts() || !unfactoredIntervals.Contains(i - 1));
                    if (combineWithPrevious)
                    {
                        GrammarAST translatedAlt = factoredAlternatives[i];
                        if (translatedAlt.ChildCount == 0)
                        {
                            adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                        }

                        GrammarAST previous = (GrammarAST)newChildren.GetChild(newChildren.ChildCount - 1);

#if false
                        if (LOGGER.isLoggable(Level.FINE))
                        {
                            LOGGER.log(Level.FINE, previous.ToStringTree());
                            LOGGER.log(Level.FINE, translatedAlt.ToStringTree());
                        }
#endif

                        if (previous.ChildCount == 1 || previous.GetChild(1).Type != ANTLRParser.BLOCK)
                        {
                            GrammarAST newBlock = new BlockAST(adaptor.CreateToken(ANTLRParser.BLOCK, "BLOCK"));
                            GrammarAST newAlt = new AltAST(adaptor.CreateToken(ANTLRParser.ALT, "ALT"));
                            adaptor.AddChild(newBlock, newAlt);
                            while (previous.ChildCount > 1)
                            {
                                adaptor.AddChild(newAlt, previous.DeleteChild(1));
                            }

                            if (newAlt.ChildCount == 0)
                            {
                                adaptor.AddChild(newAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                            }

                            adaptor.AddChild(previous, newBlock);
                        }

                        if (translatedAlt.ChildCount == 1 || translatedAlt.GetChild(1).Type != ANTLRParser.BLOCK)
                        {
                            GrammarAST newBlock = new BlockAST(adaptor.CreateToken(ANTLRParser.BLOCK, "BLOCK"));
                            GrammarAST newAlt = new AltAST(adaptor.CreateToken(ANTLRParser.ALT, "ALT"));
                            adaptor.AddChild(newBlock, newAlt);
                            while (translatedAlt.ChildCount > 1)
                            {
                                adaptor.AddChild(newAlt, translatedAlt.DeleteChild(1));
                            }

                            if (newAlt.ChildCount == 0)
                            {
                                adaptor.AddChild(newAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                            }

                            adaptor.AddChild(translatedAlt, newBlock);
                        }

                        GrammarAST combinedBlock = (GrammarAST)previous.GetChild(1);
                        adaptor.AddChild(combinedBlock, translatedAlt.GetChild(1).GetChild(0));

#if false
                        if (LOGGER.isLoggable(Level.FINE))
                        {
                            LOGGER.log(Level.FINE, previous.ToStringTree());
                        }
#endif
                    }
                    else
                    {
                        GrammarAST translatedAlt = factoredAlternatives[i];
                        if (translatedAlt.ChildCount == 0)
                        {
                            adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                        }

                        adaptor.AddChild(newChildren, translatedAlt);
                    }
                }

                if (mode.IncludeUnfactoredAlts() && unfactoredIntervals.Contains(i))
                {
                    GrammarAST translatedAlt = unfactoredAlternatives[i];
                    if (translatedAlt.ChildCount == 0)
                    {
                        adaptor.AddChild(translatedAlt, adaptor.Create(ANTLRParser.EPSILON, "EPSILON"));
                    }

                    adaptor.AddChild(newChildren, translatedAlt);
                }
            }

            adaptor.ReplaceChildren(block, 0, block.ChildCount - 1, newChildren);

            if (!variant && block.Parent is RuleAST)
            {
                RuleAST ruleAST = (RuleAST)block.Parent;
                string ruleName = ruleAST.GetChild(0).Text;
                Rule r = _rules[ruleName];
                IList<GrammarAST> blockAlts = block.GetAllChildrenWithType(ANTLRParser.ALT);
                r.numberOfAlts = blockAlts.Count;
                r.alt = new Alternative[blockAlts.Count + 1];
                for (int i = 0; i < blockAlts.Count; i++)
                {
                    r.alt[i + 1] = new Alternative(r, i + 1);
                    r.alt[i + 1].ast = (AltAST)blockAlts[i];
                }
            }

            return true;
        }
예제 #51
0
 public virtual IList<GrammarAST> GetNodesWithTypePreorderDFS(IntervalSet types)
 {
     List<GrammarAST> nodes = new List<GrammarAST>();
     GetNodesWithTypePreorderDFS_(nodes, types);
     return nodes;
 }
예제 #52
0
 public ThrowNoViableAlt(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST,
                         IntervalSet expecting)
     : base(factory, blkOrEbnfRootAST, expecting)
 {
 }
예제 #53
0
        public override Handle Set(GrammarAST associatedAST, IList<GrammarAST> alts, bool invert)
        {
            ATNState left = NewState(associatedAST);
            ATNState right = NewState(associatedAST);
            IntervalSet set = new IntervalSet();
            foreach (GrammarAST t in alts)
            {
                if (t.Type == ANTLRParser.RANGE)
                {
                    int a = CharSupport.GetCharValueFromGrammarCharLiteral(t.GetChild(0).Text);
                    int b = CharSupport.GetCharValueFromGrammarCharLiteral(t.GetChild(1).Text);
                    if (CheckRange((GrammarAST)t.GetChild(0), (GrammarAST)t.GetChild(1), a, b))
                    {
                        CheckSetCollision(associatedAST, set, a, b);
                        set.Add(a, b);
                    }
                }
                else if (t.Type == ANTLRParser.LEXER_CHAR_SET)
                {
                    set.AddAll(GetSetFromCharSetLiteral(t));
                }
                else if (t.Type == ANTLRParser.STRING_LITERAL)
                {
                    int c = CharSupport.GetCharValueFromGrammarCharLiteral(t.Text);
                    if (c != -1)
                    {
                        CheckSetCollision(associatedAST, set, c);
                        set.Add(c);
                    }
                    else
                    {
                        g.tool.errMgr.GrammarError(ErrorType.INVALID_LITERAL_IN_LEXER_SET,
                                                   g.fileName, t.Token, t.Text);
                    }
                }
                else if (t.Type == ANTLRParser.TOKEN_REF)
                {
                    g.tool.errMgr.GrammarError(ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET,
                                               g.fileName, t.Token, t.Text);
                }
            }
            if (invert)
            {
                left.AddTransition(new NotSetTransition(right, set));
            }
            else
            {
                Transition transition;
                if (set.GetIntervals().Count == 1)
                {
                    Interval interval = set.GetIntervals()[0];
                    transition = new RangeTransition(right, interval.a, interval.b);
                }
                else
                {
                    transition = new SetTransition(right, set);
                }

                left.AddTransition(transition);
            }
            associatedAST.atnState = left;
            return new Handle(left, right);
        }
예제 #54
0
        protected virtual void CheckSetCollision(GrammarAST ast, IntervalSet set, int a, int b)
        {
            for (int i = a; i <= b; i++)
            {
                if (set.Contains(i))
                {
                    string setText;
                    if (ast.Children == null)
                    {
                        setText = ast.Text;
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (object child in ast.Children)
                        {
                            if (child is RangeAST)
                            {
                                sb.Append(((RangeAST)child).GetChild(0).Text);
                                sb.Append("..");
                                sb.Append(((RangeAST)child).GetChild(1).Text);
                            }
                            else
                            {
                                sb.Append(((GrammarAST)child).Text);
                            }
                            sb.Append(" | ");
                        }
                        sb.Remove(sb.Length - 3, 3);
                        setText = sb.ToString();
                    }

                    g.tool.errMgr.GrammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, ast.Token,
                                        (char)a + "-" + (char)b, setText);
                    break;
                }
            }
        }
예제 #55
0
 protected virtual void CheckSetCollision(GrammarAST ast, IntervalSet set, int el)
 {
     if (set.Contains(el))
     {
         g.tool.errMgr.GrammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, ast.Token,
                 (char)el, ast.Text);
     }
 }
예제 #56
0
파일: IntervalSet.cs 프로젝트: antlr/antlr4
 /// <summary>Create a set with all ints within range [a..b] (inclusive)</summary>
 public static Antlr4.Runtime.Misc.IntervalSet Of(int a, int b)
 {
     Antlr4.Runtime.Misc.IntervalSet s = new Antlr4.Runtime.Misc.IntervalSet();
     s.Add(a, b);
     return s;
 }
예제 #57
0
 public virtual IList<SrcOp> GetLL1Test(IntervalSet look, GrammarAST blkAST)
 {
     return null;
 }
예제 #58
0
파일: IntervalSet.cs 프로젝트: antlr/antlr4
 /// <summary>combine all sets in the array returned the or'd value</summary>
 public static Antlr4.Runtime.Misc.IntervalSet Or(Antlr4.Runtime.Misc.IntervalSet[] sets)
 {
     Antlr4.Runtime.Misc.IntervalSet r = new Antlr4.Runtime.Misc.IntervalSet();
     foreach (Antlr4.Runtime.Misc.IntervalSet s in sets)
     {
         r.AddAll(s);
     }
     return r;
 }