Beispiel #1
0
        public bool ParseValid(string text)
        {
            //var inputStream = new StreamReader(Console.OpenStandardInput());
            var input  = new AntlrInputStream(text);
            var lexer  = new DOTLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new DOTParser(tokens);

            parser.RemoveErrorListeners();

            var errorListener = new ErrorListener(_errorHandler);

            parser.AddErrorListener(errorListener);

            IParseTree tree = parser.graph();
            var        tmp  = tree.ToStringTree(parser);

//            Console.WriteLine(tree.ToStringTree(parser));
//            CalculatorVisitor visitor = new CalculatorVisitor();
//            Console.WriteLine(visitor.Visit(tree));

            return(!errorListener.HasParserErrors);
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            var routeBuilder = new RouteBuilder(app);

            routeBuilder.MapPost("parse", context =>
            {
                string text = "";
                using (StreamReader reader = new StreamReader(context.Request.Body))
                {
                    text = reader.ReadToEnd();
                }

                AntlrInputStream inputStream = new AntlrInputStream(text);
                DOTLexer lexer = new DOTLexer(inputStream);
                CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
                DOTParser parser = new DOTParser(commonTokenStream);
                // the listener gathers the names for the hover information
                DOTLanguageListener listener = new DOTLanguageListener();

                DOTErrorListener errorListener           = new DOTErrorListener();
                DOTLexerErrorListener lexerErrorListener = new DOTLexerErrorListener();

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

                GraphContext graph = parser.graph();

                ParseTreeWalker.Default.Walk(listener, graph);

                StringBuilder json = new StringBuilder();
                json.Append("{");
                json.Append("\"errors\": [");

                json.Append(convertMessagesToJson(lexerErrorListener.Messages));
                json.Append(convertMessagesToJson(errorListener.Messages));

                if (lexerErrorListener.Messages.Count + errorListener.Messages.Count > 0)
                {
                    json.Remove(json.Length - 2, 1);
                }

                json.Append("], ");
                json.Append("\"names\": [");
                json.Append(convertNamesToJson(listener.Names));
                json.Append("]");
                json.Append("}");

                return(context.Response.WriteAsync(json.ToString()));
            });

            var routes = routeBuilder.Build();

            app.UseRouter(routes);
        }