コード例 #1
0
        public virtual void Parse()
        {
            Workspaces.Document item = Item;
            string code        = item.Code;
            string ffn         = item.FullPath;
            bool   has_changed = item.Changed;

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

            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

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

            gd.Parse(this);

            AllNodes   = DFSVisitor.DFS(ParseTree as ParserRuleContext);
            Comments   = gd.ExtractComments(code);
            Defs       = new Dictionary <TerminalNodeImpl, int>();
            Refs       = new Dictionary <TerminalNodeImpl, int>();
            Tags       = new Dictionary <TerminalNodeImpl, int>();
            Errors     = new HashSet <IParseTree>();
            Imports    = new HashSet <string>();
            Attributes = new Dictionary <IParseTree, IList <CombinedScopeSymbol> >();
            Cleanup();
        }
コード例 #2
0
        public static ParserDetails Create(Workspaces.Document item)
        {
            if (item == null)
            {
                return(null);
            }

            string ffn = item.FullPath;

            foreach (KeyValuePair <string, ParserDetails> pd in _per_file_parser_details)
            {
                if (pd.Key == ffn)
                {
                    return(pd.Value);
                }
            }
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

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

            ParserDetails result = gd.CreateParserDetails(item);

            result.Gd = gd;
            _per_file_parser_details[ffn] = result;
            return(result);
        }
コード例 #3
0
        public virtual List <string> Candidates(int char_index)
        {
            Workspaces.Document item = Item;
            string ffn             = item.FullPath;
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

            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);
        }
コード例 #4
0
ファイル: Module.cs プロジェクト: wilsonk/AntlrVSIX
        public static DocumentSymbol GetDocumentSymbol(int index, Document doc)
        {
            var pd = ParserDetailsFactory.Create(doc);

            if (pd.ParseTree == null)
            {
                return(null);
            }
            Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc);
            var gd = GrammarDescriptionFactory.Create(doc.FullPath);

            if (pt == null)
            {
                return(default(DocumentSymbol));
            }
            Antlr4.Runtime.Tree.IParseTree p = pt;
            var q     = p as Antlr4.Runtime.Tree.TerminalNodeImpl;
            var found = pd.Tags.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
            });
        }
コード例 #5
0
ファイル: Module.cs プロジェクト: wilsonk/AntlrVSIX
        public static int GetTag(int index, Document doc)
        {
            var pd = ParserDetailsFactory.Create(doc);

            if (pd.ParseTree == null)
            {
                return(-1);
            }
            Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc);
            var gd = GrammarDescriptionFactory.Create(doc.FullPath);

            if (pt == null)
            {
                return(-1);
            }
            Antlr4.Runtime.Tree.IParseTree p = pt;
            var q     = p as Antlr4.Runtime.Tree.TerminalNodeImpl;
            var found = pd.Tags.TryGetValue(q, out int tag_type);

            if (found)
            {
                return(tag_type);
            }
            if (q.Symbol == null)
            {
                return(-1);
            }
            var found2 = pd.Comments.TryGetValue(q.Symbol, out int tag2);

            if (found2)
            {
                return(tag2);
            }
            return(-1);
        }
コード例 #6
0
        public virtual void GatherDefs()
        {
            Workspaces.Document item = Item;
            string ffn             = item.FullPath;
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

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

            for (int classification = 0; classification < gd.IdentifyDefinition.Count; ++classification)
            {
                Func <IGrammarDescription, Dictionary <IParseTree, IList <CombinedScopeSymbol> >, IParseTree, bool> fun = gd.IdentifyDefinition[classification];
                if (fun == null)
                {
                    continue;
                }

                IEnumerable <IParseTree> it = AllNodes.Where(t => fun(gd, Attributes, t));
                foreach (IParseTree t in it)
                {
                    TerminalNodeImpl x = (t as TerminalNodeImpl);
                    if (x == null)
                    {
                        continue;
                    }

                    if (x.Symbol == null)
                    {
                        continue;
                    }

                    try
                    {
                        Defs.Add(x, classification);
                        Tags.Add(x, classification);
                    }
                    catch (ArgumentException)
                    {
                        // Duplicate
                    }
                }
            }
        }
コード例 #7
0
ファイル: ParserDetails.cs プロジェクト: wilsonk/AntlrVSIX
        public virtual List <string> Candidates(int index)
        {
            var item = Item;
            var ffn  = item.FullPath;
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

            if (gd == null)
            {
                throw new Exception();
            }
            string code = this.Code.Substring(0, index);

            gd.Parse(code, out CommonTokenStream tok_stream, out Parser parser, out Lexer lexer, out IParseTree pt);
            LASets la_sets = new LASets();
            var    result  = la_sets.Compute(parser, tok_stream);

            return(null);
        }
コード例 #8
0
ファイル: ParserDetails.cs プロジェクト: wilsonk/AntlrVSIX
        public virtual void GatherErrors()
        {
            var item = Item;
            var ffn  = item.FullPath;
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

            if (gd == null)
            {
                throw new Exception();
            }
            {
                var it = this.AllNodes.Where(t => t as Antlr4.Runtime.Tree.ErrorNodeImpl != null);
                foreach (var t in it)
                {
                    this.Errors.Add(t);
                }
            }
        }
コード例 #9
0
        public virtual void GatherErrors()
        {
            Workspaces.Document item = Item;
            string ffn             = item.FullPath;
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

            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);
                }
            }
        }
コード例 #10
0
ファイル: ParserDetails.cs プロジェクト: wilsonk/AntlrVSIX
        public virtual void GatherDefs()
        {
            var item = Item;
            var ffn  = item.FullPath;
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

            if (gd == null)
            {
                throw new Exception();
            }
            for (int classification = 0; classification < gd.IdentifyDefinition.Count; ++classification)
            {
                var fun = gd.IdentifyDefinition[classification];
                if (fun == null)
                {
                    continue;
                }
                var it = this.AllNodes.Where(t => fun(gd, this.Attributes, t));
                foreach (var t in it)
                {
                    var x = (t as TerminalNodeImpl);
                    if (x == null)
                    {
                        continue;
                    }
                    if (x.Symbol == null)
                    {
                        continue;
                    }
                    try
                    {
                        this.Defs.Add(x, classification);
                        this.Tags.Add(x, classification);
                    }
                    catch (ArgumentException)
                    {
                        // Duplicate
                    }
                }
            }
        }
コード例 #11
0
ファイル: Module.cs プロジェクト: tnsr1/AntlrVSIX
        public static QuickInfo GetQuickInfo(int index, Document doc)
        {
            ParserDetails pd = ParserDetailsFactory.Create(doc);

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

            Antlr4.Runtime.Tree.IParseTree pt = LanguageServer.Util.Find(index, doc);
            IGrammarDescription            gd = GrammarDescriptionFactory.Create(doc.FullPath);

            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.Tags.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();
                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 <ParserDetails, 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)
                {
                    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 <ParserDetails, 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
                });
            }
        }
コード例 #12
0
        public virtual void GatherRefs()
        {
            Workspaces.Document item = Item;
            string ffn             = item.FullPath;
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

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

            for (int classification = 0; classification < gd.Identify.Count; ++classification)
            {
                Func <IGrammarDescription, Dictionary <IParseTree, IList <CombinedScopeSymbol> >, IParseTree, bool> fun = gd.Identify[classification];
                if (fun == null)
                {
                    continue;
                }

                IEnumerable <IParseTree> it = AllNodes.Where(t => fun(gd, Attributes, t));
                foreach (IParseTree t in it)
                {
                    TerminalNodeImpl x = (t as TerminalNodeImpl);
                    if (x == null)
                    {
                        continue;
                    }

                    if (x.Symbol == null)
                    {
                        continue;
                    }

                    try
                    {
                        Attributes.TryGetValue(x, out IList <CombinedScopeSymbol> attr_list);
                        if (attr_list == null)
                        {
                            continue;
                        }

                        foreach (CombinedScopeSymbol attr in attr_list)
                        {
                            Tags.Add(x, classification);
                            if (attr == null)
                            {
                                continue;
                            }

                            ISymbol sym = attr as Symtab.ISymbol;
                            if (sym == null)
                            {
                                continue;
                            }

                            ISymbol def = sym.resolve();
                            if (def != null && def.file != null && def.file != "" &&
                                def.file != ffn)
                            {
                                Workspaces.Document def_item = Workspaces.Workspace.Instance.FindDocument(def.file);
                                ParserDetails       def_pd   = ParserDetailsFactory.Create(def_item);
                                def_pd.PropagateChangesTo.Add(ffn);
                            }
                            Refs.Add(x, classification);
                        }
                    }
                    catch (ArgumentException)
                    {
                        // Duplicate
                    }
                }
            }
        }
コード例 #13
0
ファイル: ParserDetails.cs プロジェクト: wilsonk/AntlrVSIX
        public virtual void GatherRefs()
        {
            var item = Item;
            var ffn  = item.FullPath;
            IGrammarDescription gd = GrammarDescriptionFactory.Create(ffn);

            if (gd == null)
            {
                throw new Exception();
            }
            for (int classification = 0; classification < gd.Identify.Count; ++classification)
            {
                var fun = gd.Identify[classification];
                if (fun == null)
                {
                    continue;
                }
                var it = this.AllNodes.Where(t => fun(gd, this.Attributes, t));
                foreach (var t in it)
                {
                    var x = (t as TerminalNodeImpl);
                    if (x == null)
                    {
                        continue;
                    }
                    if (x.Symbol == null)
                    {
                        continue;
                    }
                    try
                    {
                        this.Attributes.TryGetValue(x, out IList <CombinedScopeSymbol> attr_list);
                        if (attr_list == null)
                        {
                            continue;
                        }
                        foreach (var attr in attr_list)
                        {
                            this.Tags.Add(x, classification);
                            if (attr == null)
                            {
                                continue;
                            }
                            var sym = attr as Symtab.ISymbol;
                            if (sym == null)
                            {
                                continue;
                            }
                            var def = sym.resolve();
                            if (def != null && def.file != null && def.file != "" &&
                                def.file != ffn)
                            {
                                var def_item = Workspaces.Workspace.Instance.FindDocument(def.file);
                                var def_pd   = ParserDetailsFactory.Create(def_item);
                                def_pd.PropagateChangesTo.Add(ffn);
                            }
                            this.Refs.Add(x, classification);
                        }
                    }
                    catch (ArgumentException)
                    {
                        // Duplicate
                    }
                }
            }
        }