Ejemplo n.º 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();
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        private void OnTextChanged(object sender, TextContentChangedEventArgs args)
        {
            AntlrLanguageClient alc = AntlrLanguageClient.Instance;

            if (alc == null)
            {
                return;
            }

            string ffn = _buffer.GetFFN();

            if (ffn == null)
            {
                return;
            }

            Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
            if (document == null)
            {
                return;
            }

            LanguageServer.IParserDescription _grammar_description = LanguageServer.ParserDescriptionFactory.Create(document);
            if (_grammar_description == null)
            {
                return;
            }

            if (_buffer.CurrentSnapshot == null)
            {
                return;
            }

            document.Code = _buffer.CurrentSnapshot.GetText();
        }
Ejemplo n.º 5
0
        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();
        }
Ejemplo n.º 6
0
        public async void VsTextViewCreated(IVsTextView textViewAdapter)
        {
            try
            {
                IWpfTextView view = AdaptersFactory.GetWpfTextView(textViewAdapter);
                if (view == null)
                {
                    return;
                }
                view.Closed += OnViewClosed;
                var buffer = view.TextBuffer;
                if (buffer == null)
                {
                    return;
                }
                string ffn = await buffer.GetFFN();

                if (ffn == null)
                {
                    return;
                }
                var grammar_description = LanguageServer.GrammarDescriptionFactory.Create(ffn);
                if (grammar_description == null)
                {
                    return;
                }
                var document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    var name    = "Miscellaneous Files";
                    var project = Workspaces.Workspace.Instance.FindProject(name);
                    if (project == null)
                    {
                        project = new Workspaces.Project(name, name, name);
                        Workspaces.Workspace.Instance.AddChild(project);
                    }
                    document = new Workspaces.Document(ffn, ffn);
                    project.AddDocument(document);
                }
                Workspaces.Loader.LoadAsync().Wait();
                var content_type = buffer.ContentType;
                System.Collections.Generic.List <IContentType> content_types = ContentTypeRegistryService.ContentTypes.ToList();
                var new_content_type     = content_types.Find(ct => ct.TypeName == "Antlr");
                var type_of_content_type = new_content_type.GetType();
                var assembly             = type_of_content_type.Assembly;
                buffer.ChangeContentType(new_content_type, null);
                if (!PreviousContentType.ContainsKey(ffn))
                {
                    PreviousContentType[ffn] = content_type;
                }
                var to_do = LanguageServer.Module.Compile();
                var att   = buffer.Properties.GetOrCreateSingletonProperty(() => new AntlrVSIX.AggregateTagger.AntlrClassifier(buffer));
                att.Raise();
            }
            catch (Exception e)
            {
                Logger.Log.Notify(e.ToString());
            }
        }
Ejemplo n.º 7
0
        internal void Initialize(IClassificationTypeRegistryService service,
                                 IClassificationFormatMapService ClassificationFormatMapService)
        {
            try
            {
                if (initialized)
                {
                    return;
                }

                string ffn = _buffer.GetFFN().Result;
                if (ffn == null)
                {
                    return;
                }

                LanguageServer.IGrammarDescription _grammar_description = LanguageServer.GrammarDescriptionFactory.Create(ffn);
                if (_grammar_description == null)
                {
                    return;
                }

                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                _lsptype_to_classifiertype = new Dictionary <int, IClassificationType>();
                for (int i = 0; i < _grammar_description.Map.Length; ++i)
                {
                    int    key = i;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType == null?service.CreateClassificationType(val, new IClassificationType[] { })
                                                                          : identiferClassificationType;

                    IClassificationFormatMap classificationFormatMap = ClassificationFormatMapService.GetClassificationFormatMap(category: "text");
                    Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                              .GetExplicitTextProperties(classificationType);
                    System.Drawing.Color color = !Themes.IsInvertedTheme() ? _grammar_description.MapColor[key]
                        : _grammar_description.MapInvertedColor[key];
                    System.Windows.Media.Color newColor = System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
                    Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties newProperties = identifierProperties.SetForeground(newColor);
                    classificationFormatMap.AddExplicitTextProperties(classificationType, newProperties);
                }
                {
                    int    key = (int)SymbolKind.Variable;
                    string val = _grammar_description.Map[0];
                    _lsptype_to_classifiertype[key] = service.GetClassificationType(val);
                }
                {
                    int    key = (int)SymbolKind.Enum;
                    string val = _grammar_description.Map[1];
                    _lsptype_to_classifiertype[key] = service.GetClassificationType(val);
                }
                initialized = true;
            }
            catch (Exception)
            {
                //Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 8
0
        private void MenuItemCallback(object sender, EventArgs e, bool split)
        {
            try
            {
                ////////////////////////
                /// Reorder parser productions.
                ////////////////////////

                IVsTextManager manager = ((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) as IVsTextManager;
                if (manager == null)
                {
                    return;
                }

                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }

                view.GetCaretPos(out int l, out int c);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }

                IWpfTextView xxx    = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view);
                ITextBuffer  buffer = xxx.TextBuffer;
                string       ffn    = buffer.GetFFN().Result;
                if (ffn == null)
                {
                    return;
                }
                current_grammar_ffn = ffn;

                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }

                int pos = LanguageServer.Module.GetIndex(l, c, document);
                AntlrLanguageClient alc = AntlrLanguageClient.Instance;
                if (alc == null)
                {
                    return;
                }
                Dictionary <string, string> changes = alc.CMSplitCombineGrammarsServer(ffn, pos, split);
                EnterChanges(changes);
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 9
0
        private void MenuItemCallback(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            try
            {
                ////////////////////////
                /// Eliminate left recursion.
                ////////////////////////

                if (!(((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) is IVsTextManager manager))
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetSelection(out int ls, out int cs, out int le, out int ce);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
                string      ffn    = buffer?.GetFFN();
                if (ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }
                int start = new LanguageServer.Module().GetIndex(ls, cs, document);
                int end   = new LanguageServer.Module().GetIndex(le, ce, document);
                AntlrLanguageClient.CMEliminateIndirectLeftRecursion(ffn, start, end);
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 10
0
        private void MenuItemCallback(object sender, EventArgs e)
        {
            try
            {
                ////////////////////////
                /// Replace multiple rules that priorize operators with one rule and multiple ordered alts.
                ////////////////////////

                IVsTextManager manager = ((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) as IVsTextManager;
                if (manager == null)
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetSelection(out int ls, out int cs, out int le, out int ce);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
                string      ffn    = buffer.GetFFN();
                if (ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }
                int start = new LanguageServer.Module().GetIndex(ls, cs, document);
                int end   = new LanguageServer.Module().GetIndex(le, ce, document);
                AntlrLanguageClient.CMReplacePriorization(ffn, start, end);
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 11
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
                    }
                }
            }
        }
Ejemplo n.º 12
0
 public PythonParserDetails(Workspaces.Document item)
     : base(item)
 {
     Passes.Add(() =>
     {
         return(false);
     });
     Passes.Add(() =>
     {
         ParseTreeWalker.Default.Walk(new Pass1Listener(this), ParseTree);
         return(false);
     });
     Passes.Add(() =>
     {
         ParseTreeWalker.Default.Walk(new Pass2Listener(this), ParseTree);
         return(false);
     });
 }
Ejemplo n.º 13
0
        private void MenuItemCallback(object sender, EventArgs e)
        {
            try
            {
                ////////////////////////
                /// Unfold (substitute RHS of a rule into uses of the LHS symbol).
                ////////////////////////

                if (!(((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) is IVsTextManager manager))
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetSelection(out int ls, out int cs, out int le, out int ce);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view).TextBuffer;
                string      ffn    = buffer.GetFFN();
                if (ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }
                int start = new LanguageServer.Module().GetIndex(ls, cs, document);
                int end   = new LanguageServer.Module().GetIndex(le, ce, document);
                AntlrLanguageClient.CMUnfold(ffn, start, end);
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 14
0
        private void MenuItemCallback(object sender, EventArgs e)
        {
            try
            {
                ////////////////////////
                /// Remove useless parser productions.
                ////////////////////////

                IVsTextManager manager = ((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) as IVsTextManager;
                if (manager == null)
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
                string      ffn    = buffer.GetFFN();
                if (ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }
                int pos = new LanguageServer.Module().GetIndex(l, c, document);
                AntlrLanguageClient.CMMoveStartRuleToTop(ffn);
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 15
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);
                }
            }
        }
Ejemplo n.º 16
0
        private void MenuItemCallback(object sender, EventArgs e)
        {
            try
            {
                ////////////////////////
                /// Eliminate Antlr keywords mistakenly used as non-terminal names!
                ////////////////////////

                if (!(((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) is IVsTextManager manager))
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
                string      ffn    = buffer?.GetFFN();
                if (ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }
                int pos = new LanguageServer.Module().GetIndex(l, c, document);
                AntlrLanguageClient.CMEliminateAntlrKeywordsInRules(ffn);
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 17
0
        private void MenuItemCallback(ReorderType type)
        {
            try
            {
                ////////////////////////
                /// Reorder parser productions.
                ////////////////////////

                if (!(((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) is IVsTextManager manager))
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
                string      ffn    = buffer.GetFFN();
                if (ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }
                AntlrLanguageClient.CMReorderParserRules(ffn, type);
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
0
        private static Dictionary <string, SyntaxTree> ReadCsharpSource(Document document)
        {
            Dictionary <string, SyntaxTree> trees = new Dictionary <string, SyntaxTree>();
            string g4_file_path = document.FullPath;
            string current_dir  = Path.GetDirectoryName(g4_file_path);

            if (current_dir == null)
            {
                return(trees);
            }
            foreach (string f in Directory.EnumerateFiles(current_dir))
            {
                if (Path.GetExtension(f).ToLower() != ".cs")
                {
                    continue;
                }

                string file_name = f;
                string suffix    = Path.GetExtension(file_name);
                if (suffix != ".cs")
                {
                    continue;
                }

                try
                {
                    string       ffn  = file_name;
                    StreamReader sr   = new StreamReader(ffn);
                    string       code = sr.ReadToEnd();
                    SyntaxTree   tree = CSharpSyntaxTree.ParseText(code);
                    trees[ffn] = tree;
                }
                catch (Exception)
                {
                }
            }
            return(trees);
        }
Ejemplo n.º 20
0
 private void MenuItemCallback(object sender, EventArgs e)
 {
     try
     {
         IVsTextManager manager = ((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) as IVsTextManager;
         if (manager == null)
         {
             return;
         }
         manager.GetActiveView(1, null, out IVsTextView view);
         if (view == null)
         {
             return;
         }
         view.GetCaretPos(out int l, out int c);
         view.GetBuffer(out IVsTextLines buf);
         if (buf == null)
         {
             return;
         }
         ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
         string      ffn    = buffer.GetFFN();
         if (ffn == null)
         {
             return;
         }
         Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
         if (document == null)
         {
             return;
         }
         AntlrLanguageClient.CMPerformAnalysis(ffn);
     }
     catch (Exception exception)
     {
         Logger.Log.Notify(exception.StackTrace);
     }
 }
Ejemplo n.º 21
0
        private async void OnTextChanged(object sender, TextContentChangedEventArgs args)
        {
            AntlrLanguageClient alc = AntlrLanguageClient.Instance;

            if (alc == null)
            {
                return;
            }

            string ffn = _buffer.GetFFN().Result;

            Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
            if (document == null)
            {
                return;
            }

            if (_buffer.CurrentSnapshot == null)
            {
                return;
            }

            document.Code = _buffer.CurrentSnapshot.GetText();
        }
Ejemplo n.º 22
0
        public JavaParserDetails(Workspaces.Document item)
            : base(item)
        {
            // Passes executed in order for all files.
            Passes.Add(() =>
            {
                // Gather Imports from grammars.
                // Gather _dependent_grammars map.
                ParseTreeWalker.Default.Walk(new Pass3Listener(this), ParseTree);
                return(false);
            });
            Passes.Add(() =>
            {
                // For all imported grammars across the entire universe,
                // make sure all are loaded in the workspace,
                // then restart.
                foreach (KeyValuePair <string, List <string> > dep in _dependent_grammars)
                {
                    var name = dep.Key;
                    var x    = Workspaces.Workspace.Instance.FindDocument(name);
                    if (x == null)
                    {
                        // Add document.
                        var proj    = this.Item.Parent;
                        var new_doc = new Workspaces.Document(name, name);
                        proj.AddChild(new_doc);
                        return(true);
                    }
                    foreach (var y in dep.Value)
                    {
                        var z = Workspaces.Workspace.Instance.FindDocument(y);
                        if (z == null)
                        {
                            // Add document.
                            var proj    = this.Item.Parent;
                            var new_doc = new Workspaces.Document(y, y);
                            proj.AddChild(new_doc);
                            return(true);
                        }
                    }
                }

                // The workspace is completely loaded. Create scopes for all files in workspace
                // if they don't already exist.
                foreach (KeyValuePair <string, List <string> > dep in _dependent_grammars)
                {
                    var name = dep.Key;
                    _scopes.TryGetValue(name, out IScope file_scope);
                    if (file_scope != null)
                    {
                        continue;
                    }
                    _scopes[name] = new FileScope(name, null);
                }

                // Set up search path scopes for Imports relationship.
                var root = _scopes[this.FullFileName];
                foreach (var dep in this.Imports)
                {
                    var import = new SearchPathScope(root);
                    _scopes.TryGetValue(dep, out IScope dep_scope);
                    if (dep_scope == null)
                    {
                        _scopes[dep] = new FileScope(dep, null);
                    }
                    dep_scope = _scopes[dep];
                    import.nest(dep_scope);
                    root.nest(import);
                }
                root.empty();
                this.RootScope = root;
                return(false);
            });
            Passes.Add(() =>
            {
                ParseTreeWalker.Default.Walk(new Pass1Listener(this), ParseTree);
                return(false);
            });
            Passes.Add(() =>
            {
                ParseTreeWalker.Default.Walk(new Pass2Listener(this), ParseTree);
                return(false);
            });
        }
Ejemplo n.º 23
0
        public IEnumerable <ITagSpan <ClassificationTag> > GetTags(NormalizedSnapshotSpanCollection spans)
        {
            AntlrLanguageClient alc = AntlrLanguageClient.Instance;

            if (alc == null)
            {
                yield break;
            }
            string ffn = _buffer.GetFFN().Result;

            if (ffn == null)
            {
                yield break;
            }
            Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
            IVsTextView         vstv     = IVsTextViewExtensions.FindTextViewFor(ffn);

            foreach (SnapshotSpan curSpan in spans)
            {
                SnapshotPoint       start                  = curSpan.Start;
                SnapshotPoint       end                    = curSpan.End;
                string              text                   = curSpan.GetText();
                int                 curLocStart            = start.Position;
                int                 curLocEnd              = end.Position;
                SymbolInformation[] sorted_combined_tokens = alc.CMGetClassifiersSendServer(curLocStart, curLocEnd - 1, ffn);
                if (sorted_combined_tokens == null)
                {
                    continue;
                }

                foreach (SymbolInformation r in sorted_combined_tokens)
                {
                    int l  = r.Location.Range.Start.Line;
                    int c  = r.Location.Range.Start.Character;
                    int i  = LanguageServer.Module.GetIndex(l, c, document);
                    int l2 = r.Location.Range.End.Line;
                    int c2 = r.Location.Range.End.Character;
                    int i2 = LanguageServer.Module.GetIndex(l2, c2, document);
                    int start_token_start = i;
                    int end_token_end     = i2;
                    int type   = (int)r.Kind;
                    int length = end_token_end - start_token_start + 1;
                    if (length < 0)
                    {
                        continue;
                    }
                    TagSpan <ClassificationTag> result = null;
                    try
                    {
                        if (type >= 0)
                        {
                            // Make sure the length doesn't go past the end of the current span.
                            if (start_token_start + length > curLocEnd)
                            {
                                var new_length = curLocEnd - start_token_start;
                                if (new_length >= 0)
                                {
                                    length = new_length;
                                }
                            }

                            ITextSnapshot a = curSpan.Snapshot.TextBuffer.CurrentSnapshot;
                            ITextSnapshot b = curSpan.Snapshot;

                            SnapshotSpan tokenSpan = new SnapshotSpan(
                                curSpan.Snapshot.TextBuffer.CurrentSnapshot,
                                //curSpan.Snapshot,
                                new Span(start_token_start, length));

                            result = new TagSpan <ClassificationTag>(tokenSpan,
                                                                     new ClassificationTag(_lsptype_to_classifiertype[type]));
                        }
                    }
                    catch (Exception)
                    {
                        //Logger.Log.Notify(exception.StackTrace);
                    }
                    if (result != null)
                    {
                        yield return(result);
                    }
                }
            }
        }
Ejemplo n.º 24
0
        internal void Initialize(IClassificationTypeRegistryService service,
                                 IClassificationFormatMapService ClassificationFormatMapService)
        {
            try
            {
                if (initialized)
                {
                    return;
                }

                string ffn = _buffer.GetFFN();
                if (ffn == null)
                {
                    return;
                }

                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                LanguageServer.IParserDescription _grammar_description = LanguageServer.ParserDescriptionFactory.Create(document);
                if (_grammar_description == null)
                {
                    return;
                }

                _to_classifiertype = new Dictionary <int, IClassificationType>();

                IClassificationFormatMap classificationFormatMap = ClassificationFormatMapService.GetClassificationFormatMap(category: "text");
                // hardwire colors to VS's colors of selected types.
                System.Collections.ObjectModel.ReadOnlyCollection <IClassificationType> list_of_formats = classificationFormatMap.CurrentPriorityOrder;

                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationNonterminalDef;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrNonterminalDef")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable CS0168 // Variable is declared but never used
                        catch (Exception eeks) { }
#pragma warning restore CS0168 // Variable is declared but never used
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationNonterminalRef;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrNonterminalRef")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationTerminalDef;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrTerminalDef")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationTerminalRef;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrTerminalRef")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationComment;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrComment")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationKeyword;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrKeyword")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationLiteral;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrLiteral")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationModeDef;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrModeDef")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationModeRef;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrModeRef")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationChannelDef;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrChannelDef")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationChannelRef;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrChannelRef")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationPunctuation;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrPunctuation")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                {
                    int    i   = (int)LanguageServer.Antlr4ParsingResults.AntlrClassifications.ClassificationOperator;
                    string val = _grammar_description.Map[i];
                    IClassificationType identiferClassificationType = service.GetClassificationType(val);
                    IClassificationType classificationType          = identiferClassificationType ?? service.CreateClassificationType(val, Array.Empty <IClassificationType>());
                    IClassificationType t = list_of_formats.Where(f => f != null && f.Classification == Options.Option.GetString("AntlrOperator")).FirstOrDefault();
                    if (t != null)
                    {
                        try
                        {
                            Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties identifierProperties = classificationFormatMap
                                                                                                                      .GetExplicitTextProperties(t);
                            classificationFormatMap.AddExplicitTextProperties(classificationType, identifierProperties);
                        }
#pragma warning disable 0168
                        catch (Exception eeks) { }
#pragma warning restore 0168
                    }
                    _to_classifiertype[i] = classificationType;
                }
                initialized = true;
            }
#pragma warning disable 0168
            catch (Exception eeks) { }
#pragma warning restore 0168
        }
Ejemplo n.º 25
0
        private void MenuItemCallback(object sender, EventArgs e, bool forward)
        {
            try
            {
                ////////////////////////
                /// Next rule.
                ////////////////////////

                IVsTextManager manager = ((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) as IVsTextManager;
                if (manager == null)
                {
                    return;
                }

                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }

                view.GetCaretPos(out int l, out int c);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }

                IWpfTextView xxx    = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view);
                ITextBuffer  buffer = xxx.TextBuffer;
                string       ffn    = buffer.GetFFN().Result;
                if (ffn == null)
                {
                    return;
                }

                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }

                int pos = LanguageServer.Module.GetIndex(l, c, document);
                AntlrLanguageClient alc = AntlrLanguageClient.Instance;
                if (alc == null)
                {
                    return;
                }

                int new_pos = alc.CMNextSymbolSendServer(ffn, pos, forward);
                if (new_pos < 0)
                {
                    return;
                }

                List <IToken> where = new List <IToken>();
                List <ParserDetails> where_details = new List <ParserDetails>();
                IVsTextView          vstv          = IVsTextViewExtensions.FindTextViewFor(ffn);
                if (vstv == null)
                {
                    return;
                }

                IWpfTextView wpftv = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(vstv);
                if (wpftv == null)
                {
                    return;
                }

                vstv.GetLineAndColumn(new_pos, out int line_number, out int colum_number);
                ITextSnapshot cc = wpftv.TextBuffer.CurrentSnapshot;
                SnapshotSpan  ss = new SnapshotSpan(cc, new_pos, 1);
                SnapshotPoint sp = ss.Start;
                // Put cursor on symbol.
                wpftv.Caret.MoveTo(sp);
                if (line_number > 0)
                {
                    vstv.CenterLines(line_number - 1, 2);
                }
                else
                {
                    vstv.CenterLines(line_number, 1);
                }
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
        public static ParsingResults Create(Workspaces.Document document)
        {
            if (_parsing_results.ContainsKey(document) && _parsing_results[document] != null)
            {
                return(_parsing_results[document]);
            }

            ParsingResults result = null;

            if (document.ParseAs != null)
            {
                var parse_as = document.ParseAs;
                if (parse_as == "antlr2")
                {
                    result = new Antlr2ParsingResults(document);
                }
                else if (parse_as == "antlr3")
                {
                    result = new Antlr3ParsingResults(document);
                }
                else if (parse_as == "antlr4")
                {
                    result = new Antlr4ParsingResults(document);
                }
                else if (parse_as == "bison")
                {
                    result = new BisonParsingResults(document);
                }
                else if (parse_as == "ebnf")
                {
                    result = new W3CebnfParsingResults(document);
                }
                else if (parse_as == "iso14977")
                {
                    result = new Iso14977ParsingResults(document);
                }
                else if (parse_as == "lbnf")
                {
                    result = new lbnfParsingResults(document);
                }
                else
                {
                    result = null;
                }
            }
            else if (document.FullPath.EndsWith(".ebnf"))
            {
                document.ParseAs = "ebnf";
                result           = new W3CebnfParsingResults(document);
            }
            else if (document.FullPath.EndsWith(".g2"))
            {
                document.ParseAs = "antlr2";
                result           = new Antlr2ParsingResults(document);
            }
            else if (document.FullPath.EndsWith(".g3"))
            {
                document.ParseAs = "antlr3";
                result           = new Antlr3ParsingResults(document);
            }
            else if (document.FullPath.EndsWith(".g4"))
            {
                document.ParseAs = "antlr4";
                result           = new Antlr4ParsingResults(document);
            }
            else if (document.FullPath.EndsWith(".y"))
            {
                document.ParseAs = "bison";
                result           = new BisonParsingResults(document);
            }
            else if (document.FullPath.EndsWith(".iso14977"))
            {
                document.ParseAs = "iso14977";
                result           = new Iso14977ParsingResults(document);
            }
            else if (document.FullPath.EndsWith(".cf"))
            {
                document.ParseAs = "lbnf";
                result           = new lbnfParsingResults(document);
            }
            else
            {
                result = null;
            }
            _parsing_results[document] = result;
            return(result);
        }
Ejemplo n.º 27
0
        private void MenuItemCallback(bool split)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            try
            {
                ////////////////////////
                /// Reorder parser productions.
                ////////////////////////

                if (!(((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) is IVsTextManager manager))
                {
                    return;
                }
                manager.GetActiveView(1, null, out IVsTextView view);
                if (view == null)
                {
                    return;
                }
                view.GetCaretPos(out int l, out int c);
                view.GetBuffer(out IVsTextLines buf);
                if (buf == null)
                {
                    return;
                }
                ITextBuffer buffer = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view)?.TextBuffer;
                string      ffn    = buffer.GetFFN();
                if (ffn == null)
                {
                    return;
                }
                Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn);
                if (document == null)
                {
                    return;
                }
                int pos = new LanguageServer.Module().GetIndex(l, c, document);
                Dictionary <string, string> changes = AntlrLanguageClient.CMSplitCombineGrammars(ffn, split);
                EnvDTE.Project project       = null;
                string         the_namespace = "";
                for (; ;)
                {
                    string current_grammar_ffn = ffn;
                    (EnvDTE.Project, EnvDTE.ProjectItem)p_f_original_grammar = LspAntlr.MakeChanges.FindProjectAndItem(current_grammar_ffn);
                    project = p_f_original_grammar.Item1;
                    try
                    {
                        object prop = p_f_original_grammar.Item2.Properties.Item("CustomToolNamespace").Value;
                        the_namespace = prop.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    break;
                }
                if (project == null)
                {
                    EnvDTE.DTE      dte      = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
                    EnvDTE.Projects projects = dte.Solution.Projects;
                    project = projects.Item(EnvDTE.Constants.vsMiscFilesProjectUniqueName);
                }
                if (changes == null)
                {
                    return;
                }
                MakeChanges.EnterChanges(changes, project, the_namespace);
            }
            catch (Exception exception)
            {
                Logger.Log.Notify(exception.StackTrace);
            }
        }
Ejemplo n.º 28
0
 public ParserDetails(Workspaces.Document item)
 {
     Item         = item;
     Item.Changed = true;
 }
Ejemplo n.º 29
0
        public AntlrGrammarDetails(Workspaces.Document item)
            : base(item)
        {
            // Passes executed in order for all files.
            Passes.Add(() =>
            {
                // Gather Imports from grammars.
                // Gather _dependent_grammars map.
                int before_count = 0;
                foreach (KeyValuePair <string, List <string> > x in AntlrGrammarDetails._dependent_grammars)
                {
                    before_count++;
                    before_count = before_count + x.Value.Count;
                }
                ParseTreeWalker.Default.Walk(new Pass3Listener(this), ParseTree);
                int after_count = 0;
                foreach (KeyValuePair <string, List <string> > dep in AntlrGrammarDetails._dependent_grammars)
                {
                    string name           = dep.Key;
                    Workspaces.Document x = Workspaces.Workspace.Instance.FindDocument(name);
                    if (x == null)
                    {
                        // Add document.
                        Workspaces.Container proj   = Item.Parent;
                        Workspaces.Document new_doc = new Workspaces.Document(name);
                        proj.AddChild(new_doc);
                        after_count++;
                    }
                    after_count++;
                    after_count = after_count + dep.Value.Count;
                }
                return(before_count != after_count);
            });
            Passes.Add(() =>
            {
                // For all imported grammars across the entire universe,
                // make sure all are loaded in the workspace,
                // then restart.
                foreach (KeyValuePair <string, List <string> > dep in AntlrGrammarDetails._dependent_grammars)
                {
                    string name           = dep.Key;
                    Workspaces.Document x = Workspaces.Workspace.Instance.FindDocument(name);
                    if (x == null)
                    {
                        // Add document.
                        Workspaces.Container proj   = Item.Parent;
                        Workspaces.Document new_doc = new Workspaces.Document(name);
                        proj.AddChild(new_doc);
                        return(true);
                    }
                    foreach (string y in dep.Value)
                    {
                        Workspaces.Document z = Workspaces.Workspace.Instance.FindDocument(y);
                        if (z == null)
                        {
                            // Add document.
                            Workspaces.Container proj   = Item.Parent;
                            Workspaces.Document new_doc = new Workspaces.Document(y);
                            proj.AddChild(new_doc);
                            return(true);
                        }
                    }
                }

                // The workspace is completely loaded. Create scopes for all files in workspace
                // if they don't already exist.
                foreach (KeyValuePair <string, List <string> > dep in _dependent_grammars)
                {
                    string name = dep.Key;
                    _scopes.TryGetValue(name, out IScope file_scope);
                    if (file_scope != null)
                    {
                        continue;
                    }

                    _scopes[name] = new FileScope(name, null);
                }

                // Set up search path scopes for Imports relationship.
                IScope root = _scopes[FullFileName];
                foreach (string dep in Imports)
                {
                    // Don't add if already have this search path.
                    IScope dep_scope = _scopes[dep];
                    bool found       = false;
                    foreach (IScope scope in root.NestedScopes)
                    {
                        if (scope is SearchPathScope)
                        {
                            SearchPathScope spc = scope as SearchPathScope;
                            if (spc.NestedScopes.First() == dep_scope)
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found)
                    {
                        SearchPathScope import = new SearchPathScope(root);
                        import.nest(dep_scope);
                        root.nest(import);
                    }
                }
                root.empty();
                RootScope = root;
                return(false);
            });
            Passes.Add(() =>
            {
                ParseTreeWalker.Default.Walk(new Pass1Listener(this), ParseTree);
                return(false);
            });
            Passes.Add(() =>
            {
                ParseTreeWalker.Default.Walk(new Pass2Listener(this), ParseTree);
                return(false);
            });
        }
Ejemplo n.º 30
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
                    }
                }
            }
        }