public virtual void Parse()
        {
            Workspaces.Document document = Item;
            string code        = document.Code;
            string ffn         = document.FullPath;
            bool   has_changed = document.Changed;

            document.Changed = false;
            if (!has_changed)
            {
                return;
            }

            var gd = ParserDescriptionFactory.Create(document);

            if (gd == null)
            {
                throw new Exception();
            }

            bool bail = QuietAfter == 0;

            gd.Parse(this, bail);

            AllNodes      = DFSVisitor.DFS(ParseTree as ParserRuleContext);
            Comments      = gd.ExtractComments(code);
            Defs          = new Dictionary <TerminalNodeImpl, int>();
            Refs          = new Dictionary <TerminalNodeImpl, int>();
            PopupList     = new Dictionary <TerminalNodeImpl, int>();
            Errors        = new HashSet <IParseTree>();
            Imports       = new HashSet <string>();
            Attributes    = new Dictionary <IParseTree, IList <CombinedScopeSymbol> >();
            ColorizedList = new Dictionary <Antlr4.Runtime.IToken, int>();
            Cleanup();
        }
        public virtual List <string> Candidates(int char_index)
        {
            Workspaces.Document document = Item;
            string ffn = document.FullPath;
            var    gd  = ParserDescriptionFactory.Create(document);

            if (gd == null)
            {
                throw new Exception();
            }

            string code = Code.Substring(0, char_index);

            gd.Parse(code, out CommonTokenStream tok_stream, out Parser parser, out Lexer lexer, out IParseTree pt);
            LASets        la_sets = new LASets();
            IntervalSet   int_set = la_sets.Compute(parser, tok_stream);
            List <string> result  = new List <string>();

            foreach (int r in int_set.ToList())
            {
                string rule_name = Lexer.RuleNames[r];
                result.Add(rule_name);
            }
            return(result);
        }
        public virtual void GatherErrors()
        {
            Workspaces.Document document = Item;
            string ffn = document.FullPath;
            var    gd  = ParserDescriptionFactory.Create(document);

            if (gd == null)
            {
                throw new Exception();
            }

            {
                IEnumerable <IParseTree> it = AllNodes.Where(t => t as Antlr4.Runtime.Tree.ErrorNodeImpl != null);
                foreach (IParseTree t in it)
                {
                    Errors.Add(t);
                }
            }
        }
Exemple #4
0
        public int GetTag(int index, Document doc)
        {
            ParsingResults pd        = ParsingResultsFactory.Create(doc);
            var            workspace = doc.Workspace;

            if (pd.ParseTree == null)
            {
                Compile(workspace);
            }

            Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc);
            var gd = ParserDescriptionFactory.Create(doc);

            if (pt == null)
            {
                return(-1);
            }

            Antlr4.Runtime.Tree.IParseTree p = pt;
            TerminalNodeImpl q     = p as Antlr4.Runtime.Tree.TerminalNodeImpl;
            bool             found = pd.PopupList.TryGetValue(q, out int tag_type);

            if (found)
            {
                return(tag_type);
            }

            if (q.Symbol == null)
            {
                return(-1);
            }

            bool found2 = pd.Comments.TryGetValue(q.Symbol, out int tag2);

            if (found2)
            {
                return(tag2);
            }

            return(-1);
        }
Exemple #5
0
        public static ParsingResults Create(Workspaces.Document document)
        {
            if (document == null)
            {
                return(null);
            }

            string ffn = document.FullPath;

            foreach (KeyValuePair <string, ParsingResults> pd in _per_file_parser_details)
            {
                if (pd.Key == ffn)
                {
                    return(pd.Value);
                }
            }
            ParsingResults result = ParserDescriptionFactory.Create(document);

            _per_file_parser_details[ffn] = result;
            return(result);
        }
Exemple #6
0
        public DocumentSymbol GetDocumentSymbol(int index, Document doc)
        {
            ParsingResults pd        = ParsingResultsFactory.Create(doc);
            var            workspace = doc.Workspace;

            if (pd.ParseTree == null)
            {
                Compile(workspace);
            }

            Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc);
            var gd = ParserDescriptionFactory.Create(doc);

            if (pt == null)
            {
                return(default(DocumentSymbol));
            }

            Antlr4.Runtime.Tree.IParseTree p = pt;
            TerminalNodeImpl q     = p as Antlr4.Runtime.Tree.TerminalNodeImpl;
            bool             found = pd.PopupList.TryGetValue(q, out int tag_type);

            if (!found)
            {
                return(null);
            }

            if (q.Symbol == null)
            {
                return(null);
            }

            return(new DocumentSymbol()
            {
                name = q.Symbol.Text,
                range = new Workspaces.Range(q.Symbol.StartIndex, q.Symbol.StopIndex),
                kind = tag_type
            });
        }
Exemple #7
0
        public QuickInfo GetQuickInfo(int index, Document doc)
        {
            ParsingResults pd        = ParsingResultsFactory.Create(doc);
            var            workspace = doc.Workspace;

            if (pd.ParseTree == null)
            {
                Compile(workspace);
            }

            Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc);
            var gd = ParserDescriptionFactory.Create(doc);

            if (pt == null)
            {
                return(null);
            }

            Antlr4.Runtime.Tree.IParseTree p = pt;
            pd.Attributes.TryGetValue(p, out IList <CombinedScopeSymbol> list_value);
            if (list_value == null)
            {
                return(null);
            }

            TerminalNodeImpl q     = p as Antlr4.Runtime.Tree.TerminalNodeImpl;
            Range            range = new Workspaces.Range(new Workspaces.Index(q.Symbol.StartIndex), new Workspaces.Index(q.Symbol.StopIndex + 1));
            bool             found = pd.PopupList.TryGetValue(q, out int tag_type);

            if (!found)
            {
                return(null);
            }

            if (list_value == null || list_value.Count == 0)
            {
                return(new QuickInfo()
                {
                    Display = gd.Map[tag_type], Range = range
                });
            }
            if (list_value.Count == 1)
            {
                CombinedScopeSymbol value = list_value.First();
                Symtab.ISymbol      name  = value as Symtab.ISymbol;
                string show = name?.Name;
                if (value is Symtab.Literal)
                {
                    show = ((Symtab.Literal)value).Cleaned;
                }
                if (gd.PopUpDefinition[tag_type] != null)
                {
                    Func <ParsingResults, IParseTree, string> fun = gd.PopUpDefinition[tag_type];
                    string mess = fun(pd, p);
                    if (mess != null)
                    {
                        return(new QuickInfo()
                        {
                            Display = mess, Range = range
                        });
                    }
                }
                string display = gd.Map[tag_type]
                                 + "\n"
                                 + show;
                return(new QuickInfo()
                {
                    Display = display, Range = range
                });
            }
            {
                string display = "Ambiguous -- ";
                foreach (CombinedScopeSymbol value in list_value)
                {
                    Symtab.ISymbol name = value as Symtab.ISymbol;
                    string         show = name?.Name;
                    if (value is Symtab.Literal)
                    {
                        show = ((Symtab.Literal)value).Cleaned;
                    }
                    if (gd.PopUpDefinition[tag_type] != null)
                    {
                        Func <ParsingResults, IParseTree, string> fun = gd.PopUpDefinition[tag_type];
                        string mess = fun(pd, p);
                        if (mess != null)
                        {
                            display = display + mess;
                        }
                    }
                    else
                    {
                        display = display + gd.Map[tag_type]
                                  + "\n"
                                  + show;
                    }
                }
                return(new QuickInfo()
                {
                    Display = display, Range = range
                });
            }
        }
        public virtual void GatherRefsDefsAndOthers()
        {
            try
            {
                Workspaces.Document document = Item;
                string ffn = document.FullPath;
                var    gd  = ParserDescriptionFactory.Create(document);
                if (gd == null)
                {
                    throw new Exception();
                }

                if (AllNodes != null)
                {
                    ColorizedList = new Dictionary <IToken, int>();
                    Refs          = new Dictionary <TerminalNodeImpl, int>();
                    Defs          = new Dictionary <TerminalNodeImpl, int>();
                    PopupList     = new Dictionary <TerminalNodeImpl, int>();
                    Func <IParserDescription, Dictionary <IParseTree, IList <CombinedScopeSymbol> >, IParseTree, int> fun = gd.Classify;
                    IEnumerable <IParseTree> it = AllNodes.Where(n => n is TerminalNodeImpl);
                    foreach (var n in it)
                    {
                        var t = n as TerminalNodeImpl;
                        int i = -1;
                        try
                        {
                            i = gd.Classify(gd, Attributes, t);
                            if (i >= 0)
                            {
                                ColorizedList.Add(t.Symbol, i);
                            }
                        }
                        catch (Exception eeks) { }
                        try
                        {
                            if (i == (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationNonterminalRef ||
                                i == (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationTerminalRef ||
                                i == (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationModeRef ||
                                i == (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationChannelRef
                                )
                            {
                                Refs.Add(t, i);
                                PopupList.Add(t, i);
                            }
                        }
                        catch (Exception eeks) { }
                        try
                        {
                            if (i == (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationNonterminalDef ||
                                i == (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationTerminalDef ||
                                i == (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationModeDef ||
                                i == (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationChannelDef
                                )
                            {
                                Defs.Add(t, i);
                                PopupList.Add(t, i);
                            }
                        }
                        catch (Exception eeks) { }
                    }
                }

                if (Comments != null)
                {
                    foreach (KeyValuePair <Antlr4.Runtime.IToken, int> p in Comments)
                    {
                        IToken t = p.Key;
                        ColorizedList.Add(t, (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationComment);
                    }
                }
            }
#pragma warning disable 0168
            catch (Exception eeks) { }
#pragma warning restore 0168
        }