Esempio n. 1
0
        public void Execute(Repl repl, ReplParser.RupContext tree, bool piped)
        {
            var expr = tree.StringLiteral()?.GetText();

            expr = expr?.Substring(1, expr.Length - 2);
            var doc                 = repl.stack.Peek();
            var pr                  = ParsingResultsFactory.Create(doc);
            var aparser             = pr.Parser;
            var atree               = pr.ParseTree;
            List <IParseTree> nodes = null;

            if (expr != null)
            {
                using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = new AntlrTreeEditing.AntlrDOM.ConvertToDOM().Try(atree, aparser))
                {
                    org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine();
                    nodes = engine.parseExpression(expr,
                                                   new StaticContextBuilder()).evaluate(dynamicContext, new object[] { dynamicContext.Document })
                            .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree).ToList();
                }
            }
            var results = LanguageServer.Transform.RemoveUselessParentheses(doc, nodes);

            repl.EnactEdits(results);
        }
Esempio n. 2
0
        public void Execute(Repl repl, ReplParser.AglContext tree, bool piped)
        {
            string lines            = repl.input_output_stack.Pop();
            var    doc              = repl.stack.Peek();
            var    pr               = ParsingResultsFactory.Create(doc);
            var    lexer            = pr.Lexer;
            var    parser           = pr.Parser;
            var    serializeOptions = new JsonSerializerOptions();

            serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter());
            serializeOptions.WriteIndented = false;
            var parse_info = JsonSerializer.Deserialize <AntlrJson.ParsingResultSet>(lines, serializeOptions);
            var nodes      = parse_info.Nodes;

            System.Windows.Forms.Form form = new System.Windows.Forms.Form();
            Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
            Microsoft.Msagl.Drawing.Graph          graph  = CreateGraph(nodes, parser.RuleNames.ToList());
            graph.LayoutAlgorithmSettings = new Microsoft.Msagl.Layout.Layered.SugiyamaLayoutSettings();
            viewer.Graph = graph;
            form.SuspendLayout();
            viewer.Dock = System.Windows.Forms.DockStyle.Fill;
            form.Controls.Add(viewer);
            form.ResumeLayout();
            form.ShowDialog();
        }
Esempio n. 3
0
        public static Document CheckDoc(string path)
        {
            string   file_name = path;
            Document document  = Workspaces.Workspace.Instance.FindDocument(file_name);

            if (document == null)
            {
                document = new Workspaces.Document(file_name);
                try
                {   // Open the text file using a stream reader.
                    using (StreamReader sr = new StreamReader(file_name))
                    {
                        // Read the stream to a string, and write the string to the console.
                        string str = sr.ReadToEnd();
                        document.Code = str;
                    }
                }
                catch (IOException)
                {
                }
                Project project = Workspaces.Workspace.Instance.FindProject("Misc");
                if (project == null)
                {
                    project = new Project("Misc", "Misc", "Misc");
                    Workspaces.Workspace.Instance.AddChild(project);
                }
                project.AddDocument(document);
            }
            document.Changed = true;
            _ = ParsingResultsFactory.Create(document);
            var workspace = document.Workspace;

            _ = new LanguageServer.Module().Compile(workspace);
            return(document);
        }
Esempio n. 4
0
        public void Execute(Repl repl, ReplParser.UnulliteralContext tree, bool piped)
        {
            var type = tree.uclc()?.GetText();
            var expr = tree.StringLiteral()?.GetText();

            expr = expr?.Substring(1, expr.Length - 2);
            var doc     = repl.stack.Peek();
            var pr      = ParsingResultsFactory.Create(doc);
            var aparser = pr.Parser;
            var atree   = pr.ParseTree;

            if (expr != null)
            {
                using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = new AntlrTreeEditing.AntlrDOM.ConvertToDOM().Try(atree, aparser))
                {
                    org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine();
                    var nodes = engine.parseExpression(expr,
                                                       new StaticContextBuilder()).evaluate(dynamicContext, new object[] { dynamicContext.Document })
                                .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree as TerminalNodeImpl).ToList();
                    var results = LanguageServer.Transform.UnUpperLowerCaseLiteral(nodes, type == "uc", doc);
                    repl.EnactEdits(results);
                }
            }
            else
            {
                var results = LanguageServer.Transform.UnUpperLowerCaseLiteral(null, type == "uc", doc);
                repl.EnactEdits(results);
            }
        }
Esempio n. 5
0
        public void Execute(Repl repl, ReplParser.XgrepContext tree, bool piped)
        {
            var expr = repl.GetArg(tree.arg());

            IParseTree[] atrees;
            Parser       parser;
            Lexer        lexer;
            string       text;
            string       fn;
            ITokenStream tokstream;

            if (piped)
            {
                var lines            = repl.input_output_stack.Pop();
                var serializeOptions = new JsonSerializerOptions();
                serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter());
                serializeOptions.WriteIndented = false;
                var parse_info = JsonSerializer.Deserialize <AntlrJson.ParsingResultSet>(lines, serializeOptions);
                text      = parse_info.Text;
                fn        = parse_info.FileName;
                atrees    = parse_info.Nodes;
                parser    = parse_info.Parser;
                lexer     = parse_info.Lexer;
                tokstream = parse_info.Stream;
            }
            else
            {
                var doc = repl.stack.Peek();
                var pr  = ParsingResultsFactory.Create(doc);
                parser    = pr.Parser;
                lexer     = pr.Lexer;
                text      = pr.Code;
                fn        = pr.FullFileName;
                tokstream = pr.TokStream;
                IParseTree atree = pr.ParseTree;
                atrees = new IParseTree[] { atree };
            }
            org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine();
            IParseTree root = atrees.First().Root();
            var        ate  = new AntlrTreeEditing.AntlrDOM.ConvertToDOM();

            using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = ate.Try(root, parser))
            {
                var l     = atrees.Select(t => ate.FindDomNode(t));
                var nodes = engine.parseExpression(expr,
                                                   new StaticContextBuilder()).evaluate(dynamicContext, l.ToArray())
                            .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree).ToArray();

                var serializeOptions = new JsonSerializerOptions();
                serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter());
                serializeOptions.WriteIndented = false;
                var parse_info_out = new AntlrJson.ParsingResultSet()
                {
                    Text = text, FileName = fn, Lexer = lexer, Parser = parser, Stream = tokstream, Nodes = nodes
                };
                string js1 = JsonSerializer.Serialize(parse_info_out, serializeOptions);
                repl.input_output_stack.Push(js1);
            }
        }
Esempio n. 6
0
        public void ParseDoc(Document document, int quiet_after, string grammar = null)
        {
            document.Changed = true;
            document.ParseAs = grammar;
            var pd = ParsingResultsFactory.Create(document);

            pd.QuietAfter = quiet_after;
            var workspace = document.Workspace;

            _ = new LanguageServer.Module().Compile(workspace);
        }
Esempio n. 7
0
        public void AnalyzeDoc(Document document)
        {
            _ = ParsingResultsFactory.Create(document);
            var results = LanguageServer.Analysis.PerformAnalysis(document);

            foreach (var r in results)
            {
                System.Console.Write(r.Document + " " + r.Severify + " " + r.Start + " " + r.Message);
                System.Console.WriteLine();
            }
        }
Esempio n. 8
0
        public void Execute(Repl repl, ReplParser.ReorderContext tree, bool piped)
        {
            Dictionary <string, string> results = new Dictionary <string, string>();
            var    doc  = repl.stack.Peek();
            string expr = null;

            if (tree.modes() != null)
            {
                results = LanguageServer.Transform.SortModes(doc);
            }
            else
            {
                LspAntlr.ReorderType order = default;
                if (tree.alpha() != null)
                {
                    order = LspAntlr.ReorderType.Alphabetically;
                }
                else if (tree.bfs() != null)
                {
                    order = LspAntlr.ReorderType.BFS;
                    expr  = tree.bfs().StringLiteral().GetText();
                }
                else if (tree.dfs() != null)
                {
                    order = LspAntlr.ReorderType.DFS;
                    expr  = tree.dfs().StringLiteral().GetText();
                }
                else
                {
                    throw new Exception("unknown sorting type");
                }
                List <IParseTree> nodes = null;
                if (expr != null)
                {
                    expr = expr.Substring(1, expr.Length - 2);
                    var pr      = ParsingResultsFactory.Create(doc);
                    var aparser = pr.Parser;
                    var atree   = pr.ParseTree;
                    using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = new AntlrTreeEditing.AntlrDOM.ConvertToDOM().Try(atree, aparser))
                    {
                        org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine();
                        nodes = engine.parseExpression(expr,
                                                       new StaticContextBuilder()).evaluate(dynamicContext, new object[] { dynamicContext.Document })
                                .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree).ToList();
                    }
                }
                results = LanguageServer.Transform.ReorderParserRules(doc, order, nodes);
            }
            repl.EnactEdits(results);
        }
Esempio n. 9
0
 public void Execute(Repl repl, ReplParser.UnfoldContext tree, bool piped)
 {
     var expr = repl.GetArg(tree.arg());
     var doc = repl.stack.Peek();
     var pr = ParsingResultsFactory.Create(doc);
     var aparser = pr.Parser;
     var atree = pr.ParseTree;
     using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = new AntlrTreeEditing.AntlrDOM.ConvertToDOM().Try(atree, aparser))
     {
         org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine();
         var nodes = engine.parseExpression(expr,
                 new StaticContextBuilder()).evaluate(dynamicContext, new object[] { dynamicContext.Document })
             .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree as TerminalNodeImpl).ToList();
         var results = LanguageServer.Transform.Unfold(nodes, doc);
         repl.EnactEdits(results);
     }
 }
Esempio n. 10
0
        public void Execute(Repl repl, ReplParser.XmlContext tree, bool piped)
        {
            string lines            = repl.input_output_stack.Pop();
            var    doc              = repl.stack.Peek();
            var    pr               = ParsingResultsFactory.Create(doc);
            var    lexer            = pr.Lexer;
            var    parser           = pr.Parser;
            var    serializeOptions = new JsonSerializerOptions();

            serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter());
            serializeOptions.WriteIndented = false;
            var parse_info = JsonSerializer.Deserialize <AntlrJson.ParsingResultSet>(lines, serializeOptions);

            foreach (var node in parse_info.Nodes)
            {
                ParseTreeWalker.Default.Walk(new XmlWalk(parser), node);
            }
        }
Esempio n. 11
0
        public void Execute(Repl repl, ReplParser.HasContext tree, bool piped)
        {
            var graph   = tree.GRAPH() != null;
            var expr    = repl.GetArg(tree.arg());
            var doc     = repl.stack.Peek();
            var pr      = ParsingResultsFactory.Create(doc);
            var aparser = pr.Parser;
            var atree   = pr.ParseTree;

            using (AntlrTreeEditing.AntlrDOM.AntlrDynamicContext dynamicContext = new AntlrTreeEditing.AntlrDOM.ConvertToDOM().Try(atree, aparser))
            {
                List <IParseTree> nodes = null;
                if (expr != null)
                {
                    org.eclipse.wst.xml.xpath2.processor.Engine engine = new org.eclipse.wst.xml.xpath2.processor.Engine();
                    nodes = engine.parseExpression(expr,
                                                   new StaticContextBuilder()).evaluate(dynamicContext, new object[] { dynamicContext.Document })
                            .Select(x => (x.NativeValue as AntlrTreeEditing.AntlrDOM.AntlrElement).AntlrIParseTree).ToList();
                }
                if (tree.DR() != null)
                {
                    var result = LanguageServer.Transform.HasDirectRec(doc, nodes);
                    foreach (var r in result)
                    {
                        System.Console.WriteLine(r);
                    }
                }
                else if (tree.IR() != null)
                {
                    var result = LanguageServer.Transform.HasIndirectRec(nodes, graph, doc);
                    foreach (var r in result)
                    {
                        System.Console.WriteLine(r);
                    }
                }
                else
                {
                    throw new Exception("unknown check");
                }
            }
        }
Esempio n. 12
0
 public void Execute(Repl repl, ReplParser.PeriodContext tree, bool piped)
 {
     if (repl.stack.Any())
     {
         var        doc = repl.stack.Peek();
         var        pr  = ParsingResultsFactory.Create(doc);
         IParseTree pt  = pr.ParseTree;
         var        serializeOptions = new JsonSerializerOptions();
         serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter());
         serializeOptions.WriteIndented = false;
         var tuple = new ParsingResultSet()
         {
             Text     = doc.Code,
             FileName = doc.FullPath,
             Stream   = pr.TokStream,
             Nodes    = new IParseTree[] { pt },
             Lexer    = pr.Lexer,
             Parser   = pr.Parser
         };
         string js1 = JsonSerializer.Serialize(tuple, serializeOptions);
         repl.input_output_stack.Push(js1);
     }
 }
Esempio n. 13
0
        public void Execute(Repl repl, ReplParser.CtokensContext tree, bool piped)
        {
            var lines            = repl.input_output_stack.Pop();
            var doc              = repl.stack.Peek();
            var pr               = ParsingResultsFactory.Create(doc);
            var lexer            = pr.Lexer;
            var parser           = pr.Parser;
            var serializeOptions = new JsonSerializerOptions();

            serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter());
            serializeOptions.WriteIndented = false;
            var obj1 = JsonSerializer.Deserialize <IParseTree>(lines, serializeOptions);

            if (obj1 == null)
            {
                return;
            }
            var nodes = new IParseTree[] { obj1 };

            foreach (var node in nodes)
            {
            }
        }
Esempio n. 14
0
        public static Document CreateStringDocument(string input)
        {
            string   file_name = "Dummy" + random_number + ".g4";
            Document document  = Workspaces.Workspace.Instance.FindDocument(file_name);

            if (document == null)
            {
                document      = new Workspaces.Document(file_name);
                document.Code = input;
                Project project = Workspaces.Workspace.Instance.FindProject("Misc");
                if (project == null)
                {
                    project = new Project("Misc", "Misc", "Misc");
                    Workspaces.Workspace.Instance.AddChild(project);
                }
                project.AddDocument(document);
            }
            document.Changed = true;
            _ = ParsingResultsFactory.Create(document);
            var workspace = document.Workspace;

            _ = new LanguageServer.Module().Compile(workspace);
            return(document);
        }