コード例 #1
0
        public static bool StartsBeforeStartOf(IParseTree a, IParseTree b)
        {
            Interval sourceIntervalA = a.SourceInterval;
            Interval sourceIntervalB = b.SourceInterval;

            return(sourceIntervalA.a < sourceIntervalB.a);
        }
コード例 #2
0
        public override bool Equals(object o)
        {
            if (!(o is Antlr4.Runtime.Misc.Interval))
            {
                return(false);
            }

            Antlr4.Runtime.Misc.Interval other = (Antlr4.Runtime.Misc.Interval)o;
            return(this.a == other.a && this.b == other.b);
        }
コード例 #3
0
        /** Return a list of parse trees, one for each alternative in a decision
         *  given the same input.
         *
         *  Very similar to {@link #getAllPossibleParseTrees} except
         *  that it re-parses the input for every alternative in a decision,
         *  not just the ambiguous ones (there is no alts parameter here).
         *  This method also tries to reduce the size of the parse trees
         *  by stripping away children of the tree that are completely out of range
         *  of startIndex..stopIndex. Also, because errors are expected, we
         *  use a specialized error handler that more or less bails out
         *  but that also consumes the first erroneous token at least. This
         *  ensures that an error node will be in the parse tree for display.
         *
         *  NOTES:
         * // we must parse the entire input now with decision overrides
         * // we cannot parse a subset because it could be that a decision
         * // above our decision of interest needs to read way past
         * // lookaheadInfo.stopIndex. It seems like there is no escaping
         * // the use of a full and complete token stream if we are
         * // resetting to token index 0 and re-parsing from the start symbol.
         * // It's not easy to restart parsing somewhere in the middle like a
         * // continuation because our call stack does not match the
         * // tree stack because of left recursive rule rewriting. grrrr!
         *
         * @since 4.5.1
         */
        public static IList <ParserRuleContext> GetLookaheadParseTrees(Grammar g,
                                                                       ParserInterpreter originalParser,
                                                                       ITokenStream tokens,
                                                                       int startRuleIndex,
                                                                       int decision,
                                                                       int startIndex,
                                                                       int stopIndex)
        {
            IList <ParserRuleContext> trees = new List <ParserRuleContext>();
            // Create a new parser interpreter to parse the ambiguous subphrase
            ParserInterpreter parser = DeriveTempParserInterpreter(g, originalParser, tokens);

            DecisionState decisionState = originalParser.Atn.decisionToState[decision];

            for (int alt = 1; alt <= decisionState.Transitions.Length; alt++)
            {
                // re-parse entire input for all ambiguous alternatives
                // (don't have to do first as it's been parsed, but do again for simplicity
                //  using this temp parser.)
                BailButConsumeErrorStrategy errorHandler = new BailButConsumeErrorStrategy();
                parser.ErrorHandler = errorHandler;
                parser.Reset();
                parser.AddDecisionOverride(decision, startIndex, alt);
                ParserRuleContext tt = parser.Parse(startRuleIndex);
                int stopTreeAt       = stopIndex;
                if (errorHandler.firstErrorTokenIndex >= 0)
                {
                    stopTreeAt = errorHandler.firstErrorTokenIndex; // cut off rest at first error
                }

                Interval overallRange = tt.SourceInterval;
                if (stopTreeAt > overallRange.b)
                {
                    // If we try to look beyond range of tree, stopTreeAt must be EOF
                    // for which there is no EOF ref in grammar. That means tree
                    // will not have node for stopTreeAt; limit to overallRange.b
                    stopTreeAt = overallRange.b;
                }

                ParserRuleContext subtree =
                    Trees.GetRootOfSubtreeEnclosingRegion(tt,
                                                          startIndex,
                                                          stopTreeAt);
                // Use higher of overridden decision tree or tree enclosing all tokens
                if (Trees.IsAncestorOf(parser.OverrideDecisionRoot, subtree))
                {
                    subtree = parser.OverrideDecisionRoot;
                }
                Trees.StripChildrenOutOfRange(subtree, parser.OverrideDecisionRoot, startIndex, stopTreeAt);
                trees.Add(subtree);
            }

            return(trees);
        }
コード例 #4
0
 /// <summary>
 /// Return the interval with elements from
 /// <c>this</c>
 /// not in
 /// <paramref name="other"/>
 /// ;
 /// <paramref name="other"/>
 /// must not be totally enclosed (properly contained)
 /// within
 /// <c>this</c>
 /// , which would result in two disjoint intervals
 /// instead of the single one returned by this method.
 /// </summary>
 public Antlr4.Runtime.Misc.Interval?DifferenceNotProperlyContained(Antlr4.Runtime.Misc.Interval other)
 {
     Antlr4.Runtime.Misc.Interval?diff = null;
     // other.a to left of this.a (or same)
     if (other.StartsBeforeNonDisjoint(this))
     {
         diff = Antlr4.Runtime.Misc.Interval.Of(Math.Max(this.a, other.b + 1), this.b);
     }
     else
     {
         // other.a to right of this.a
         if (other.StartsAfterNonDisjoint(this))
         {
             diff = Antlr4.Runtime.Misc.Interval.Of(this.a, other.a - 1);
         }
     }
     return(diff);
 }
コード例 #5
0
        public static System.Collections.Generic.List <string> GetVariableNames()
        {
            System.Collections.Generic.List <string> ls = new System.Collections.Generic.List <string>();

            string text = @"
SELECT BE_Name FROM T_Benutzer WHERE Name =@username 
OR Name LIKE '%' + @foo + '%'
";

            System.IO.StringReader reader = new System.IO.StringReader(text);

            // Antlr4.Runtime.AntlrInputStream input = new Antlr4.Runtime.AntlrInputStream(reader);

            Antlr4.Runtime.ICharStream            input1 = new Antlr4.Runtime.AntlrInputStream(reader);
            Antlr4.Runtime.CaseChangingCharStream input  = new Antlr4.Runtime.CaseChangingCharStream(input1, true);


            TSqlLexer lexer = new TSqlLexer(input);

            Antlr4.Runtime.CommonTokenStream tokenStream = new Antlr4.Runtime.CommonTokenStream(lexer);
            tokenStream.Fill();

            int lastIndex = 0;

            foreach (Antlr4.Runtime.IToken token in tokenStream.GetTokens())
            {
                // System.Console.WriteLine(token.Text);
                string tokenTypeName = lexer.Vocabulary.GetSymbolicName(token.Type);
                Antlr4.Runtime.Misc.Interval ival = new Antlr4.Runtime.Misc.Interval(lastIndex, token.StopIndex);
                string extracted = token.InputStream.GetText(ival);

                // table_name, cte_name: ID, SQUARE_BRACKET_ID
                // Local variables: LOCAL_ID
                if (token.Type == TSqlLexer.LOCAL_ID)
                {
                    extracted = extracted.Trim(new char[] { ' ', '\t', '\v', '\r', '\n' });
                    ls.Add(extracted);
                } // End if (token.Type == TSqlLexer.LOCAL_ID)

                lastIndex = token.StopIndex + 1;
            } // Next token

            return(ls);
        }
コード例 #6
0
        private static string CreateErrorMessageForContext(Antlr4.Runtime.ParserRuleContext context, string message, string fileName = "(unknown)")
        {
            if (context == null)
            {
                return(message);
            }

            int lineNumber = context.Start.Line;

            // getting the text that has the issue inside
            var interval = new Antlr4.Runtime.Misc.Interval(0, context.Start.InputStream.Size);

            string[] lines = context.Start.InputStream.GetText(interval).Split('\n');
            string   line  = lines[context.Start.Line - 1];

            string theMessage = string.Format(CultureInfo.CurrentCulture, "Error in {3} line {0}\n{1}\n{2}", lineNumber, line, message, fileName);

            return(theMessage);
        }
コード例 #7
0
 public bool ProperlyContains(Antlr4.Runtime.Misc.Interval other)
 {
     return(other.a >= this.a && other.b <= this.b);
 }
コード例 #8
0
 /// <summary>Return the interval in common between this and o</summary>
 public Antlr4.Runtime.Misc.Interval Intersection(Antlr4.Runtime.Misc.Interval other)
 {
     return(Antlr4.Runtime.Misc.Interval.Of(Math.Max(a, other.a), Math.Min(b, other.b)));
 }
コード例 #9
0
 // this.b>=other.b implied
 /// <summary>Are both ranges disjoint? I.e., no overlap?</summary>
 public bool Disjoint(Antlr4.Runtime.Misc.Interval other)
 {
     return(StartsBeforeDisjoint(other) || StartsAfterDisjoint(other));
 }
コード例 #10
0
 /// <summary>Are two intervals adjacent such as 0..41 and 42..42?</summary>
 public bool Adjacent(Antlr4.Runtime.Misc.Interval other)
 {
     return(this.a == other.b + 1 || this.b == other.a - 1);
 }
コード例 #11
0
 public string GetText(Antlr4.Runtime.Misc.Interval interval)
 {
     throw new NotImplementedException();
 }
コード例 #12
0
 /// <summary>Does this start after other? NonDisjoint</summary>
 public bool StartsAfterNonDisjoint(Antlr4.Runtime.Misc.Interval other)
 {
     return(this.a > other.a && this.a <= other.b);
 }
コード例 #13
0
        static void SplitMultiTableInsertScript()
        {
            string fileName = @"D:\SQL\TESS\Anlage_Refdaten.txt";

            fileName = @"D:\SQL\TESS\Adressdaten.txt";
            fileName = @"D:\SQL\TESS\Anlagedaten.txt";
            fileName = @"D:\SQL\TESS\Anlagerechte.txt";
            fileName = @"D:\SQL\TESS\Kontaktdaten.txt";
            fileName = @"D:\SQL\TESS\Navigation.txt";
            fileName = @"D:\username\Desktop\Raumdaten\Raumdaten.sql";
            fileName = @"D:\username\Desktop\Raumdaten\Vertragsdaten.sql";



            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            // https://github.com/antlr/grammars-v4/tree/master/tsql
            // https://github.com/antlr/grammars-v4/tree/master/plsql/CSharp


            System.Text.Encoding enc = GetSystemEncoding();


            string text = System.IO.File.ReadAllText(fileName, enc);

            System.IO.StringReader reader = new System.IO.StringReader(text);

            // Antlr4.Runtime.AntlrInputStream input = new Antlr4.Runtime.AntlrInputStream(reader);

            Antlr4.Runtime.ICharStream            input1 = new Antlr4.Runtime.AntlrInputStream(reader);
            Antlr4.Runtime.CaseChangingCharStream input  = new Antlr4.Runtime.CaseChangingCharStream(input1, true);


            TSqlLexer lexer = new TSqlLexer(input);

            Antlr4.Runtime.CommonTokenStream tokenStream = new Antlr4.Runtime.CommonTokenStream(lexer);

            tokenStream.Fill();


            int lastIndex = 0;


            System.Collections.Generic.Dictionary <string, System.Collections.Generic.List <string> > dict =
                new System.Collections.Generic.Dictionary <string, System.Collections.Generic.List <string> >(System.StringComparer.InvariantCultureIgnoreCase);


            System.Collections.Generic.List <string> lsTableName = new System.Collections.Generic.List <string>();


            bool ignoreThis      = true;
            bool partOfTableName = false;

            int lastTokenType       = -1;
            int secondLastTokenType = -1;


            foreach (Antlr4.Runtime.IToken token in tokenStream.GetTokens())
            {
                // System.Console.WriteLine(token.Text);
                string tokenTypeName = lexer.Vocabulary.GetSymbolicName(token.Type);
                Antlr4.Runtime.Misc.Interval ival = new Antlr4.Runtime.Misc.Interval(lastIndex, token.StopIndex);
                string extracted = token.InputStream.GetText(ival);
                extracted = extracted.Trim(new char[] { '\t', '\v', '\r', '\n' });

                if (token.Type == TSqlLexer.INSERT)
                {
                    if (sb.Length > 0)
                    {
                        string tn = string.Join(".", lsTableName.ToArray()).Replace("[", "").Replace("]", "").Trim();
                        lsTableName.Clear();
                        System.Console.WriteLine(tn);

                        if (!dict.ContainsKey(tn))
                        {
                            dict[tn] = new System.Collections.Generic.List <string>();
                        }


                        sb.Append(";");
                        dict[tn].Add(sb.ToString());
                    }


                    sb.Clear();
                    ignoreThis      = false;
                    partOfTableName = true;
                }
                else if (token.Type == TSqlLexer.GO)
                {
                    ignoreThis      = true;
                    partOfTableName = false;
                }
                else if (token.Type == TSqlLexer.USE)
                {
                    ignoreThis      = true;
                    partOfTableName = false;
                }
                else if (token.Type == TSqlLexer.SEMI)
                {
                    ignoreThis      = true;
                    partOfTableName = false;
                }
                else if (token.Type == TSqlLexer.Eof)
                {
                }
                else if (token.Type == TSqlLexer.LR_BRACKET)
                {
                    partOfTableName = false;
                }
                else if (token.Type == TSqlLexer.RR_BRACKET)
                {
                }
                else if (token.Type == TSqlLexer.COMMA)
                {
                }
                else if (token.Type == TSqlLexer.INTO)
                {
                }
                else if (token.Type == TSqlLexer.VALUES || token.Type == TSqlLexer.SELECT)
                {
                }
                else if (token.Type == TSqlLexer.ID || token.Type == TSqlLexer.SQUARE_BRACKET_ID)
                {
                    if (partOfTableName)
                    {
                        lsTableName.Add(extracted);
                    }
                }
                else if (token.Type == TSqlLexer.DOT)
                {
                }
                else if (token.Type == TSqlLexer.STRING)
                {
                }
                else if (token.Type == TSqlLexer.DECIMAL)
                {
                }
                else if (token.Type == TSqlLexer.FLOAT)
                {
                }
                else if (token.Type == TSqlLexer.NULL)
                {
                }
                else if (token.Type == TSqlLexer.CAST)
                {
                }
                else if (token.Type == TSqlLexer.AS)
                {
                    // CAST(xxx AS datetime)
                }
                else if (token.Type == TSqlLexer.MINUS)
                {
                    // Negative Number
                }
                else
                {
                    System.Console.WriteLine(tokenTypeName);
                }



                // System.Console.WriteLine((extracted));
                if (!ignoreThis && token.Type != TSqlLexer.SEMI)
                {
                    sb.Append(extracted);
                }



                // System.Console.WriteLine(token.Text);
                // System.Console.WriteLine(token.Type);
                // System.Console.WriteLine(tokenTypeName);

                lastIndex = token.StopIndex + 1;


                secondLastTokenType = lastTokenType;
                lastTokenType       = token.Type;
            } // Next token

            if (sb.Length > 0)
            {
                string tn = string.Join(".", lsTableName.ToArray()).Replace("[", "").Replace("]", "").Trim();
                lsTableName.Clear();
                System.Console.WriteLine(tn);

                if (!dict.ContainsKey(tn))
                {
                    dict[tn] = new System.Collections.Generic.List <string>();
                }

                sb.Append(";");
                dict[tn].Add(sb.ToString());
            } // End if (sb.Length > 0)


            sb.Clear();
            sb = null;


            string baseDir = System.IO.Path.GetFileNameWithoutExtension(fileName);

            string outputDirectory = System.IO.Path.GetDirectoryName(fileName);

            baseDir = System.IO.Path.Combine(outputDirectory, baseDir);
            if (!System.IO.Directory.Exists(baseDir))
            {
                System.IO.Directory.CreateDirectory(baseDir);
            }

            foreach (System.Collections.Generic.KeyValuePair <string, System.Collections.Generic.List <string> > kvp in dict)
            {
                string dir     = kvp.Key;
                string content = string.Join("\r\n\r\n", kvp.Value.ToArray());
                System.Console.WriteLine(content);

                string fn = System.IO.Path.Combine(baseDir, kvp.Key + ".sql");
                System.IO.File.WriteAllText(fn, content, System.Text.Encoding.UTF8);
            } // Next kvp


            System.Console.WriteLine(dict);
        } // End Sub SplitMultiTableInsertScript
コード例 #14
0
 public string GetText(Interval interval)
 {
     return(_source.GetText(interval));
 }
コード例 #15
0
 /// <summary>Does this start at or before other? Nondisjoint</summary>
 public bool StartsBeforeNonDisjoint(Antlr4.Runtime.Misc.Interval other)
 {
     return(this.a <= other.a && this.b >= other.a);
 }
コード例 #16
0
        } // End Sub SplitMultiTableInsertScript

        static string SubstituteVariablesTest()
        {
            // https://github.com/antlr/grammars-v4/tree/master/tsql
            // https://github.com/antlr/grammars-v4/tree/master/plsql/CSharp

            string text = @"
DECLARE @legalEntity int 
-- SET @legalEntity = 1


;WITH CTE AS 
(
	      SELECT 1 AS id, 123 AS abc 
	UNION SELECT 2 AS id, 456 AS abc 
	UNION SELECT 3 AS id, 789 AS abc 
	UNION SELECT 4 AS id, 012 AS abc 
	UNION SELECT 5 AS id, 345 AS abc 
	UNION SELECT 6 AS id, 678 AS abc 
)

SELECT 
	 *
	,'@legalEntity' AS abcdef -- strings do not get substituted 
	,987 AS [@legalEntity] -- identifiers do not get substituted 
FROM CTE 
WHERE (1=1) 
AND 
(
	'0' IN (@legalEntity,
@legalEntity )
	OR 
	CTE.id IN (@legalEntity) 
	-- CTE.id IN (@legalEntity /* @legalEntity */) 
	
) 
/*
==>
AND 
(
	'0' IN (1,2,6)
	OR 
	CTE.id IN (1,2,6) 
	-- OR CTE.id IN (1,2,3,4,5,6 /* 1,2,3,4,5,6 */) 
)
*/

";

            System.IO.StringReader reader = new System.IO.StringReader(text);

            // Antlr4.Runtime.AntlrInputStream input = new Antlr4.Runtime.AntlrInputStream(reader);

            Antlr4.Runtime.ICharStream            input1 = new Antlr4.Runtime.AntlrInputStream(reader);
            Antlr4.Runtime.CaseChangingCharStream input  = new Antlr4.Runtime.CaseChangingCharStream(input1, true);


            TSqlLexer lexer = new TSqlLexer(input);

            Antlr4.Runtime.CommonTokenStream tokenStream = new Antlr4.Runtime.CommonTokenStream(lexer);

            tokenStream.Fill();

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            int lastIndex = 0;

            foreach (Antlr4.Runtime.IToken token in tokenStream.GetTokens())
            {
                // System.Console.WriteLine(token.Text);
                string tokenTypeName = lexer.Vocabulary.GetSymbolicName(token.Type);
#if NO_COMMENTS
                if (token.Type == TSqlLexer.LINE_COMMENT || token.Type == TSqlLexer.COMMENT ||
                    token.Type == TSqlLexer.Eof)
                {
                    Antlr4.Runtime.Misc.Interval blankInterval = new Antlr4.Runtime.Misc.Interval(lastIndex, token.StartIndex - 1);
                    string extractedBlank = token.InputStream.GetText(blankInterval);
                    if (string.IsNullOrEmpty(extractedBlank))
                    {
                        sb.Append(" ");
                    }
                    else
                    {
                        sb.Append(extractedBlank);
                    }

                    lastIndex = token.StopIndex + 1;
                    continue;
                } // End if comment
#endif


                // sql += token.Text + " ";
                Antlr4.Runtime.Misc.Interval ival = new Antlr4.Runtime.Misc.Interval(lastIndex, token.StopIndex);
                string extracted = token.InputStream.GetText(ival);

                // table_name, cte_name: ID, SQUARE_BRACKET_ID
                // Local variables: LOCAL_ID
                if (token.Type == TSqlLexer.LOCAL_ID)
                {
                    extracted = extracted.Trim(new char[] { ' ', '\t', '\v', '\r', '\n' });

                    System.Console.WriteLine(extracted);
                } // End if (token.Type == TSqlLexer.LOCAL_ID)

                // System.Console.WriteLine((extracted));
                sb.Append(extracted);


                // System.Console.WriteLine(token.Text);
                // System.Console.WriteLine(token.Type);
                // System.Console.WriteLine(tokenTypeName);

                lastIndex = token.StopIndex + 1;
            } // Next token

            string sql = sb.ToString();
            sb.Clear();
            sb = null;
            System.Console.WriteLine(sql);

            return(sql);
        } // End Sub SubstituteVariablesTest
コード例 #17
0
 public Interval(Antlr4.Runtime.Misc.Interval interval)
 {
     _interval = interval;
 }
コード例 #18
0
        } // End Sub CommentRemoverLexerTest

        // https://github.com/dotjpg3141/Strings
        static void LexerTest(string text)
        {
            try
            {
                System.IO.StringReader reader = new System.IO.StringReader(text);

                // Antlr4.Runtime.AntlrInputStream input = new Antlr4.Runtime.AntlrInputStream(reader);

                Antlr4.Runtime.ICharStream            input1 = new Antlr4.Runtime.AntlrInputStream(reader);
                Antlr4.Runtime.CaseChangingCharStream input  = new Antlr4.Runtime.CaseChangingCharStream(input1, true);


                TSqlLexer lexer = new TSqlLexer(input);

                Antlr4.Runtime.CommonTokenStream tokenStream = new Antlr4.Runtime.CommonTokenStream(lexer);

                tokenStream.Fill();

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                int lastIndex = 0;

                foreach (Antlr4.Runtime.IToken token in tokenStream.GetTokens())
                {
                    // System.Console.WriteLine(token.Text);
                    string tokenTypeName = lexer.Vocabulary.GetSymbolicName(token.Type);

                    if (token.Type == TSqlLexer.LINE_COMMENT || token.Type == TSqlLexer.COMMENT ||
                        token.Type == TSqlLexer.Eof)
                    {
                        Antlr4.Runtime.Misc.Interval blankInterval = new Antlr4.Runtime.Misc.Interval(lastIndex, token.StartIndex - 1);
                        string extractedBlank = token.InputStream.GetText(blankInterval);
                        if (string.IsNullOrEmpty(extractedBlank))
                        {
                            sb.Append(" ");
                        }
                        else
                        {
                            sb.Append(extractedBlank);
                        }

                        lastIndex = token.StopIndex + 1;
                        continue;
                    } // End if comment

                    // sql += token.Text + " ";
                    Antlr4.Runtime.Misc.Interval ival = new Antlr4.Runtime.Misc.Interval(lastIndex, token.StopIndex);
                    string extracted = token.InputStream.GetText(ival);
                    // System.Console.WriteLine((extracted));
                    sb.Append(extracted);


                    // System.Console.WriteLine(token.Text);
                    // System.Console.WriteLine(token.Type);
                    // System.Console.WriteLine(tokenTypeName);

                    lastIndex = token.StopIndex + 1;
                } // Next token

                string sql = sb.ToString();
                sb.Clear();
                sb = null;
                System.Console.WriteLine(sql);
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
        } // End Sub LexerTest
コード例 #19
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));
        }
コード例 #20
0
ファイル: ATNOptimizer.cs プロジェクト: yuanyong00/antlr4cs
        private static void OptimizeSets(Grammar g, ATN atn)
        {
            if (g.IsParser())
            {
                // parser codegen doesn't currently support SetTransition
                return;
            }

            int removedStates = 0;
            IList <DecisionState> decisions = atn.decisionToState;

            foreach (DecisionState decision in decisions)
            {
                if (decision.ruleIndex >= 0)
                {
                    Rule rule = g.GetRule(decision.ruleIndex);
                    if (char.IsLower(rule.name[0]))
                    {
                        // parser codegen doesn't currently support SetTransition
                        continue;
                    }
                }

                IntervalSet setTransitions = new IntervalSet();
                for (int i = 0; i < decision.NumberOfTransitions; i++)
                {
                    Transition epsTransition = decision.Transition(i);
                    if (!(epsTransition is EpsilonTransition))
                    {
                        continue;
                    }

                    if (epsTransition.target.NumberOfTransitions != 1)
                    {
                        continue;
                    }

                    Transition transition = epsTransition.target.Transition(0);
                    if (!(transition.target is BlockEndState))
                    {
                        continue;
                    }

                    if (transition is NotSetTransition)
                    {
                        // TODO: not yet implemented
                        continue;
                    }

                    if (transition is AtomTransition ||
                        transition is RangeTransition ||
                        transition is SetTransition)
                    {
                        setTransitions.Add(i);
                    }
                }

                // due to min alt resolution policies, can only collapse sequential alts
                for (int i = setTransitions.GetIntervals().Count - 1; i >= 0; i--)
                {
                    Interval interval = setTransitions.GetIntervals()[i];
                    if (interval.Length <= 1)
                    {
                        continue;
                    }

                    ATNState    blockEndState = decision.Transition(interval.a).target.Transition(0).target;
                    IntervalSet matchSet      = new IntervalSet();
                    for (int j = interval.a; j <= interval.b; j++)
                    {
                        Transition matchTransition = decision.Transition(j).target.Transition(0);
                        if (matchTransition is NotSetTransition)
                        {
                            throw new NotImplementedException();
                        }

                        IntervalSet set     = matchTransition.Label;
                        int         minElem = set.MinElement;
                        int         maxElem = set.MaxElement;
                        for (int k = minElem; k <= maxElem; k++)
                        {
                            if (matchSet.Contains(k))
                            {
                                char setMin = (char)set.MinElement;
                                char setMax = (char)set.MaxElement;
                                // TODO: Token is missing (i.e. position in source will not be displayed).
                                g.tool.errMgr.GrammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName,
                                                           null, (char)minElem + "-" + (char)maxElem, "[" + setMin + "-" + setMax + "]");
                                break;
                            }
                        }

                        matchSet.AddAll(set);
                    }

                    Transition newTransition;
                    if (matchSet.GetIntervals().Count == 1)
                    {
                        if (matchSet.Count == 1)
                        {
                            newTransition = new AtomTransition(blockEndState, matchSet.MinElement);
                        }
                        else
                        {
                            Interval matchInterval = matchSet.GetIntervals()[0];
                            newTransition = new RangeTransition(blockEndState, matchInterval.a, matchInterval.b);
                        }
                    }
                    else
                    {
                        newTransition = new SetTransition(blockEndState, matchSet);
                    }

                    decision.Transition(interval.a).target.SetTransition(0, newTransition);
                    for (int j = interval.a + 1; j <= interval.b; j++)
                    {
                        Transition removed = decision.Transition(interval.a + 1);
                        decision.RemoveTransition(interval.a + 1);
                        atn.RemoveState(removed.target);
                        removedStates++;
                    }
                }
            }

            //System.Console.WriteLine("ATN optimizer removed " + removedStates + " states by collapsing sets.");
        }
コード例 #21
0
 public string GetText(Interval interval)
 {
     return _source.GetText(interval);
 }
コード例 #22
0
 /// <summary>Does this.a start after other.b? May or may not be disjoint</summary>
 public bool StartsAfter(Antlr4.Runtime.Misc.Interval other)
 {
     return(this.a > other.a);
 }
コード例 #23
0
        public static System.Collections.Generic.List <string> LexWithAntlr(string text)
        {
            System.Collections.Generic.List <string> ls = new System.Collections.Generic.List <string>();

            System.IO.StringReader reader = new System.IO.StringReader(text);

            // Antlr4.Runtime.AntlrInputStream input = new Antlr4.Runtime.AntlrInputStream(reader);

            Antlr4.Runtime.ICharStream            input1 = new Antlr4.Runtime.AntlrInputStream(reader);
            Antlr4.Runtime.CaseChangingCharStream input  = new Antlr4.Runtime.CaseChangingCharStream(input1, true);



            Rfc5424Lexer lexer = new Rfc5424Lexer(input);

            Antlr4.Runtime.CommonTokenStream tokenStream = new Antlr4.Runtime.CommonTokenStream(lexer);
            tokenStream.Fill();



            Rfc5424Parser parser = new Rfc5424Parser(tokenStream);

            Rfc5424Parser.Syslog_msgContext msgContext = parser.syslog_msg();


            RfcVisitor vis = new RfcVisitor();
            string     s   = vis.Visit(msgContext);


            Antlr4.Runtime.Tree.ParseTreeWalker walker = new Antlr4.Runtime.Tree.ParseTreeWalker();
            // AntlrTsqListener listener = new AntlrTsqListener();
            EverythingListener listener = new EverythingListener();

            // walker.Walk(listener, msgContext);


            // new EverythingListener().EnterBom(parser.bom());
            // new EverythingListener().EnterTimestamp(parser.timestamp());
            // new EverythingListener().EnterEveryRule(parser.version());
            // new EverythingListener().EnterEveryRule(parser.timestamp());



            // var x = parser.msg();
            var x = parser.timestamp();



            Antlr4.Runtime.Misc.Interval msgInt = x.SourceInterval; // new Antlr4.Runtime.Misc.Interval(lastIndex, token.StopIndex);
            string extractedMsg = tokenStream.GetText(msgInt);

            System.Console.WriteLine(extractedMsg);



            int lastIndex = 0;

            foreach (Antlr4.Runtime.IToken token in tokenStream.GetTokens())
            {
                // System.Console.WriteLine(token.Text);
                string tokenTypeName = lexer.Vocabulary.GetSymbolicName(token.Type);
                Antlr4.Runtime.Misc.Interval ival = new Antlr4.Runtime.Misc.Interval(lastIndex, token.StopIndex);
                string extracted = token.InputStream.GetText(ival);

                // table_name, cte_name: ID, SQUARE_BRACKET_ID
                // Local variables: LOCAL_ID

                lastIndex = token.StopIndex + 1;
            } // Next token

            return(ls);
        }