Ejemplo n.º 1
0
        public static Dictionary <string, string> ReorderParserRules(int pos, Document document, LspAntlr.ReorderType type)
        {
            var result = new Dictionary <string, string>();

            // Check if lexer grammar.
            AntlrGrammarDetails pd_parser = ParserDetailsFactory.Create(document) as AntlrGrammarDetails;
            ExtractGrammarType  lp        = new ExtractGrammarType();

            ParseTreeWalker.Default.Walk(lp, pd_parser.ParseTree);
            var is_lexer = lp.Type == ExtractGrammarType.GrammarType.Lexer;

            if (is_lexer)
            {
                return(result);
            }

            Table table = new Table(pd_parser, document);

            table.ReadRules();
            table.FindPartitions();
            table.FindStartRules();

            // Find new order or rules.
            string old_code = document.Code;
            List <Pair <int, int> > reorder = new List <Pair <int, int> >();

            if (type == LspAntlr.ReorderType.DFS)
            {
                Digraph <string> graph = new Digraph <string>();
                foreach (var r in table.rules)
                {
                    if (!r.is_parser_rule)
                    {
                        continue;
                    }
                    graph.AddVertex(r.LHS);
                }
                foreach (var r in table.rules)
                {
                    if (!r.is_parser_rule)
                    {
                        continue;
                    }
                    var j = r.RHS;
                    //j.Reverse();
                    foreach (var rhs in j)
                    {
                        var sym = table.rules.Where(t => t.LHS == rhs).FirstOrDefault();
                        if (!sym.is_parser_rule)
                        {
                            continue;
                        }
                        var e = new DirectedEdge <string>(r.LHS, rhs);
                        graph.AddEdge(e);
                    }
                }
                List <string> starts = new List <string>();
                foreach (var r in table.rules)
                {
                    if (r.is_parser_rule && r.is_start)
                    {
                        starts.Add(r.LHS);
                    }
                }
                Graphs.DepthFirstOrder <string, DirectedEdge <string> > sort = new DepthFirstOrder <string, DirectedEdge <string> >(graph, starts);
                var ordered = sort.ToList();
                foreach (var s in ordered)
                {
                    var row = table.rules[table.nt_to_index[s]];
                    reorder.Add(new Pair <int, int>(row.start_index, row.end_index));
                }
            }
            else if (type == LspAntlr.ReorderType.BFS)
            {
                Digraph <string> graph = new Digraph <string>();
                foreach (var r in table.rules)
                {
                    if (!r.is_parser_rule)
                    {
                        continue;
                    }
                    graph.AddVertex(r.LHS);
                }
                foreach (var r in table.rules)
                {
                    if (!r.is_parser_rule)
                    {
                        continue;
                    }
                    var j = r.RHS;
                    //j.Reverse();
                    foreach (var rhs in j)
                    {
                        var sym = table.rules.Where(t => t.LHS == rhs).FirstOrDefault();
                        if (!sym.is_parser_rule)
                        {
                            continue;
                        }
                        var e = new DirectedEdge <string>(r.LHS, rhs);
                        graph.AddEdge(e);
                    }
                }
                List <string> starts = new List <string>();
                foreach (var r in table.rules)
                {
                    if (r.is_parser_rule && r.is_start)
                    {
                        starts.Add(r.LHS);
                    }
                }
                Graphs.BreadthFirstOrder <string, DirectedEdge <string> > sort = new BreadthFirstOrder <string, DirectedEdge <string> >(graph, starts);
                var ordered = sort.ToList();
                foreach (var s in ordered)
                {
                    var row = table.rules[table.nt_to_index[s]];
                    reorder.Add(new Pair <int, int>(row.start_index, row.end_index));
                }
            }
            else if (type == LspAntlr.ReorderType.Alphabetically)
            {
                var ordered = table.rules
                              .Where(r => r.is_parser_rule)
                              .Select(r => r.LHS)
                              .OrderBy(r => r).ToList();
                foreach (var s in ordered)
                {
                    var row = table.rules[table.nt_to_index[s]];
                    reorder.Add(new Pair <int, int>(row.start_index, row.end_index));
                }
            }
            else
            {
                return(result);
            }

            StringBuilder sb       = new StringBuilder();
            int           previous = 0;

            {
                int    index_start = table.rules[0].start_index;
                int    len         = 0;
                string pre         = old_code.Substring(previous, index_start - previous);
                sb.Append(pre);
                previous = index_start + len;
            }
            foreach (var l in reorder)
            {
                int    index_start = l.a;
                int    len         = l.b - l.a;
                string add         = old_code.Substring(index_start, len);
                sb.Append(add);
            }
            // Now add all non-parser rules.
            foreach (var r in table.rules)
            {
                if (r.is_parser_rule)
                {
                    continue;
                }
                int    index_start = r.start_index;
                int    len         = r.end_index - r.start_index;
                string add         = old_code.Substring(index_start, len);
                sb.Append(add);
            }
            //string rest = old_code.Substring(previous);
            //sb.Append(rest);
            string new_code = sb.ToString();

            result.Add(document.FullPath, new_code);

            return(result);
        }