Ejemplo n.º 1
0
        static void PrintLispTree(DialogicParser parser, ParserRuleContext prc)
        {
            string tree        = prc.ToStringTree(parser);
            int    indentation = 1;

            Console.WriteLine("\nPARSE-TREE");
            foreach (char c in tree)
            {
                if (c == '(')
                {
                    if (indentation > 1)
                    {
                        Console.WriteLine();
                    }
                    for (int i = 0; i < indentation; i++)
                    {
                        Console.Write("  ");
                    }
                    indentation++;
                }
                else if (c == ')')
                {
                    indentation--;
                }
                Console.Write(c);
            }
            Console.WriteLine("\n");
        }
Ejemplo n.º 2
0
        public void Test()
        {
            string            source  = "split.gs";
            ITokenSource      lexer   = new DialogicLexer(new AntlrInputStream(source));
            CommonTokenStream tokens  = new CommonTokenStream(lexer);
            DialogicParser    parser  = new DialogicParser(tokens);
            ParserRuleContext context = parser.script();

            Visit(context);
            String tree = context.ToStringTree(parser);

            printPrettyLispTree(tree);
        }
Ejemplo n.º 3
0
        private void Parse(Assembly assembly, string filePath, string startRule)
        {
            string fileString = File.ReadAllText(filePath);

            var inputStream = new AntlrInputStream(fileString);
            var lexer       = this.GetLexer(assembly, inputStream);
            var tokenStream = new CommonTokenStream(lexer);

            tokenStream.Fill();

            if (this.ShowTokens)
            {
                foreach (var token in tokenStream.GetTokens())
                {
                    if (token is CommonToken)
                    {
                        Console.WriteLine(this.TokenToString(token, lexer));
                    }
                    else
                    {
                        Console.WriteLine(token.ToString());
                    }
                }
            }

            var parser = this.GetParser(assembly, tokenStream);

            parser.BuildParseTree = true;

            ParserRuleContext tree = this.RunRule(parser, startRule);

            MainWindow.Tree               = tree;
            MainWindow.Parser             = parser;
            MainWindow.Lexer              = lexer;
            MainWindow.LexerSymbolicNames = this.tokenSymbolicNames;

            if (this.ShowTree)
            {
                Console.WriteLine(tree.ToStringTree(parser));
            }
        }
Ejemplo n.º 4
0
        private static void PrintDebugInfo(Lexer lexer)
        {
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            tokens.Fill();
            Console.WriteLine("\n[TOKENS]");
            foreach (var t in tokens.GetTokens())
            {
                String symbolicName = lexer.Vocabulary.GetSymbolicName(t.Type);
                String literalName  = lexer.Vocabulary.GetLiteralName(t.Type);

                Console.WriteLine(" {0:-20} {1}", symbolicName == "" ? literalName : symbolicName,
                                  t.Text.Replace("\r", "\\r").Replace("\n", "\\n").Replace("\t", "\\t"));
            }

            Console.WriteLine("\n[PARSE-TREE]");
            var parser = new SRZParser(tokens);
            ParserRuleContext context = parser.expression();
            String            tree    = context.ToStringTree(parser);

            printPrettyLispTree(tree);
        }
Ejemplo n.º 5
0
 /// <exception cref="System.IO.IOException"/>
 /// <exception cref="System.MemberAccessException"/>
 /// <exception cref="System.Reflection.TargetInvocationException"/>
 /// <exception cref="Javax.Print.PrintException"/>
 protected internal virtual void Process <_T0>(Lexer lexer, Type <_T0> parserClass, Parser parser, Stream @is, TextReader r)
     where _T0 : Parser
 {
     try
     {
         AntlrInputStream input = new AntlrInputStream(r);
         lexer.SetInputStream(input);
         CommonTokenStream tokens = new CommonTokenStream(lexer);
         tokens.Fill();
         if (showTokens)
         {
             foreach (object tok in tokens.GetTokens())
             {
                 System.Console.Out.WriteLine(tok);
             }
         }
         if (startRuleName.Equals(LexerStartRuleName))
         {
             return;
         }
         if (diagnostics)
         {
             parser.AddErrorListener(new DiagnosticErrorListener());
             parser.Interpreter.PredictionMode = PredictionMode.LlExactAmbigDetection;
         }
         if (printTree || gui || psFile != null)
         {
             parser.BuildParseTree = true;
         }
         if (Sll)
         {
             // overrides diagnostics
             parser.Interpreter.PredictionMode = PredictionMode.Sll;
         }
         parser.SetInputStream(tokens);
         parser.Trace = trace;
         try
         {
             MethodInfo        startRule = parserClass.GetMethod(startRuleName, (Type[])null);
             ParserRuleContext tree      = (ParserRuleContext)startRule.Invoke(parser, (object[])null);
             if (printTree)
             {
                 System.Console.Out.WriteLine(tree.ToStringTree(parser));
             }
             if (gui)
             {
                 tree.Inspect(parser);
             }
             if (psFile != null)
             {
                 tree.Save(parser, psFile);
             }
         }
         catch (NoSuchMethodException)
         {
             // Generate postscript
             System.Console.Error.WriteLine("No method for rule " + startRuleName + " or it has arguments");
         }
     }
     finally
     {
         if (r != null)
         {
             r.Close();
         }
         if (@is != null)
         {
             @is.Close();
         }
     }
 }
Ejemplo n.º 6
0
        public override void ExitEveryRule([NotNull] ParserRuleContext context)
        {
            if (this.writer != default)
            {
                writer($"{System.Reflection.MethodBase.GetCurrentMethod().Name} | {context.GetText()} | {context.ToStringTree(parser)}");
            }

            base.ExitEveryRule(context);
        }
Ejemplo n.º 7
0
 public void EnterEveryRule(ParserRuleContext ctx)
 {
     this.output(this.Ident + "R: -> " + ctx.ToStringTree(this.parser));
     this.idents.Push(" ");
 }
Ejemplo n.º 8
0
 public override string ToString()
 {
     return(_result.ToStringTree(_parser));
 }
Ejemplo n.º 9
0
        private int parseQuery(string query)
        {
            var t = string.Empty;
            IMySQLRecognizerCommon mySQLRecognizerCommon = new MySQLRecognizerCommon(serverVersion, sqlMode);
            var lastErrors    = new StringBuilder();
            var errorListener = new TestErrorListener(lastErrors);
            var errorStrategy = new BailErrorStrategy();
            var input         = new AntlrInputStream(query);
            var lexer         = new MySQLLexer(input, mySQLRecognizerCommon);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(errorListener);

            lexer.serverVersion = serverVersion;
            lexer.sqlMode       = sqlMode;
            lexer.charsets      = charsets;
            var tokens = new CommonTokenStream(lexer);

            var parser = new MySQLParser(tokens, mySQLRecognizerCommon);

            parser.serverVersion  = serverVersion;
            parser.sqlMode        = sqlMode;
            parser.BuildParseTree = buildParserTree;

            parser.ErrorHandler = errorStrategy;
            parser.Interpreter.PredictionMode = PredictionMode.SLL;
            parser.RemoveParseListeners();

            tokens.Fill();

            ParserRuleContext tree = default;

            try
            {
                tree = parser.query();
            }
            catch (Antlr4.Runtime.Misc.ParseCanceledException)
            {
                // If parsing was cancelled we either really have a syntax error or we need to do a second step,
                // now with the default strategy and LL parsing.
                tokens.Reset();
                parser.Reset();
                parser.ErrorHandler = new DefaultErrorStrategy();
                parser.Interpreter.PredictionMode = PredictionMode.LL;
                parser.AddErrorListener(errorListener);

                tree = parser.query();
            }

            var toks = tokens.GetTokens();

            t = input.GetText(new Antlr4.Runtime.Misc.Interval(toks[0].StartIndex, int.MaxValue));

            if (dump && buildParserTree)
            {
                if (tree == null)
                {
                    Trace.TraceInformation(@"No parse tree available");
                }
                else
                {
                    t             = tree.GetText();
                    parseTreeView = tree.ToStringTree(parser);
                    var text = $"Token count: {tokens.Size}{Environment.NewLine}{MySQLRecognizerCommon.dumpTree(tree, parser.Vocabulary)}";
                    Trace.TraceInformation(text.Trim());
                }
            }

            return(parser.NumberOfSyntaxErrors);
        }