Exemplo n.º 1
0
        public static void Refresh(Irony.Parsing.ParseTree tree)
        {
            if (isScanning)
            {
                return;
            }

            var file = FileManager.CurrentFile;

            if (file == null || BabePackage.DTEHelper.DTE.ActiveDocument == null || file.Path != BabePackage.DTEHelper.DTE.ActiveDocument.FullName)
            {
                return;
            }

            if (System.IO.File.Exists(file.Path))
            {
                var tp = new TreeParser();
                tp.Refresh(tree);

                BabePackage.WindowManager.RefreshEditorOutline();
                BabePackage.WindowManager.RefreshOutlineWnd();
            }
            else
            {
                //文件已经被移除
                IntellisenseHelper.RemoveFile(file.Path);
                FileManager.CurrentFile = null;
            }
        }
Exemplo n.º 2
0
        private static ParseTree Parse(string input, bool allowPartial)
        {
            ParseTree parseTree = null;

            IronyParser.Context.Mode = allowPartial ? ParseMode.CommandLine : ParseMode.File;
            try
            {
                parseTree = IronyParser.Parse(input);
            }
            catch (Exception)
            {
                var msg = "The parser internally crashed and gets reinitialized." + Environment.NewLine +
                          "Although this shouldn't happen, it's likely that it happened because of invalid syntax.";
                IronyParser = new IronyParser(Grammar);
                throw new ParseException(msg);
            }

            if (parseTree.HasErrors()) // ParseTreeStatus is Error
            {
                var logMessage = parseTree.ParserMessages.First();
                throw new ParseException(logMessage.Message, logMessage.Location.Line, logMessage.Location.Column,
                                         parseTree.SourceText);
            }
            return(parseTree);
        }
Exemplo n.º 3
0
 void OnFileContentChanged(ITextSnapshot snapshot, Irony.Parsing.ParseTree tree)
 {
     if (FileContentChanged != null)
     {
         FileContentChanged(this, new FileContentChangedEventArgs(snapshot, tree));
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// 匹配自定义折叠标签,类似  --region [title]  和--endregion之间的内容;
        /// 匹配多行注释,类似--[[ content --]]
        /// </summary>
        /// <param name="root"></param>
        /// <param name="regions"></param>
        void FindUserRegions(ITextSnapshot snapShot, Irony.Parsing.ParseTree tree, ref List <Region> regions)
        {
            Stack <Irony.Parsing.Token> StartRegions = new Stack <Irony.Parsing.Token>();

            foreach (var token in tree.Tokens)
            {
                Region region = null;

                if (token.Category == Irony.Parsing.TokenCategory.Comment)
                {
                    if (token.Text.Contains('\r') || token.Text.Contains('\n'))//多行注释,折叠
                    {
                        region             = new Region();
                        region.StartLine   = token.Location.Line;
                        region.StartOffset = 0;
                        region.EndLine     = snapShot.GetLineFromPosition(token.Location.Position + token.Length).LineNumber;
                        //region.EndLine = token.Location.Line + token.Text.Count(c=>{return c == '\r';});
                        region.EndOffset = snapShot.GetLineFromLineNumber(region.EndLine).Length;

                        region.Preview = snapShot.GetLineFromLineNumber(region.StartLine).GetText().Replace("--[[", "");

                        region.IsCollapsed = true;
                    }
                    else
                    {
                        if (token.Text.StartsWith("--region "))
                        {
                            StartRegions.Push(token);
                        }

                        else if (token.Text.StartsWith("--endregion") && StartRegions.Count > 0)
                        {
                            region = new Region();
                            var startRegion     = StartRegions.Pop();
                            var startLine       = snapShot.GetLineFromLineNumber(startRegion.Location.Line);
                            var startLineOffset = startRegion.Location.Position - startLine.Start.Position;

                            var endLine       = snapShot.GetLineFromLineNumber(token.Location.Line);
                            var endLineOffset = (token.Location.Position + token.Length) - endLine.Start.Position;

                            region.StartLine   = startRegion.Location.Line;
                            region.StartOffset = startLineOffset;

                            region.EndLine   = token.Location.Line;
                            region.EndOffset = endLineOffset;

                            region.Preview = startRegion.Text.Replace("--region ", "");

                            startRegion = null;
                        }
                    }

                    if (region != null)
                    {
                        regions.Add(region);
                    }
                }
            }
        }
Exemplo n.º 5
0
        void ReParse(ITextSnapshot newSnapshot, Irony.Parsing.ParseTree tree)
        {
            List <Region> newRegions = new List <Region>();

            if (tree.Root != null)
            {
                FindHiddenRegions(newSnapshot, tree.Root, ref newRegions);

                FindUserRegions(newSnapshot, tree, ref newRegions);
            }

            //determine the changed span, and send a changed event with the new spans
            List <Span> oldSpans =
                new List <Span>(this.regions.Select(r => AsSnapshotSpan(r, this.snapshot)
                                                    .TranslateTo(newSnapshot, SpanTrackingMode.EdgeExclusive)
                                                    .Span));
            List <Span> newSpans =
                new List <Span>(newRegions.Select(r => AsSnapshotSpan(r, newSnapshot).Span));

            NormalizedSpanCollection oldSpanCollection = new NormalizedSpanCollection(oldSpans);
            NormalizedSpanCollection newSpanCollection = new NormalizedSpanCollection(newSpans);

            //the changed regions are regions that appear in one set or the other, but not both.
            NormalizedSpanCollection removed =
                NormalizedSpanCollection.Difference(oldSpanCollection, newSpanCollection);

            int changeStart = int.MaxValue;
            int changeEnd   = -1;

            if (removed.Count > 0)
            {
                changeStart = removed[0].Start;
                changeEnd   = removed[removed.Count - 1].End;
            }

            if (newSpans.Count > 0)
            {
                changeStart = Math.Min(changeStart, newSpans[0].Start);
                changeEnd   = Math.Max(changeEnd, newSpans[newSpans.Count - 1].End);
            }

            this.snapshot = newSnapshot;
            this.regions  = newRegions;

            if (changeStart <= changeEnd)
            {
                ITextSnapshot snap = this.snapshot;
                if (this.TagsChanged != null)
                {
                    this.TagsChanged(this, new SnapshotSpanEventArgs(
                                         new SnapshotSpan(this.snapshot, Span.FromBounds(changeStart, changeEnd))));
                }
            }
        }
Exemplo n.º 6
0
        public ParseTreeForm(Irony.Parsing.ParseTree i_ParseTree)
        {
            InitializeComponent();

            m_ParseTree = i_ParseTree;

            if (m_ParseTree != null)
            {
                ShowParseTree();
            }

            //ParseTreeView.Nodes.Clear();
            //if (i_CuLe == null) return;
            //if (i_CuLe.m_ParseTree == null) return;

            //AddParseNodeRec(null, i_CuLe.m_ParseTree.Root);
        }
Exemplo n.º 7
0
        private void ReParse(ITextSnapshot newSnapshot, Irony.Parsing.ParseTree tree)
        {
            int previousCount = errorTokens.Count;

            errorTokens.Clear();

            var newErrors = new List <Irony.Parsing.Token>();

            foreach (var token in tree.Tokens)
            {
                if (token.IsError())
                {
                    errorTokens.Add(token);
                }
            }

            if (tree.HasErrors())
            {
                var tok = tree.Tokens.Last();
                errorTokens.Add(tok);
                //if (tok.Length != 0)
                //    errorTokens.Add(tok);
                //else //it is EOF error so before the end(use -2)
                //    errorTokens.Add(tree.Tokens[tree.Tokens.Count - 2]);
                msgParse = tree.ParserMessages[0].ToString();
            }
            else
            {
                msgParse = "";
            }

            if (previousCount != 0 || errorTokens.Count != 0)
            {
                if (this.TagsChanged != null)
                {
                    this.TagsChanged(this, new SnapshotSpanEventArgs(
                                         new SnapshotSpan(newSnapshot, 0, newSnapshot.Length)));
                }
            }
        }
Exemplo n.º 8
0
 internal SqlGeometryParseException(Irony.Parsing.ParseTree tree)
     : this(tree.ParserMessages[0])
 {
     ParseTree = tree;
 }
Exemplo n.º 9
0
        /// <summary>
        /// 匹配自定义折叠标签,类似  --region [title]  和--endregion之间的内容;
        /// 匹配多行注释,类似--[[ content --]]
        /// </summary>
        /// <param name="root"></param>
        /// <param name="regions"></param>
        void FindUserRegions(ITextSnapshot snapShot, Irony.Parsing.ParseTree tree, ref List <Region> regions)
        {
            Irony.Parsing.Token startRegion = null;

            foreach (var token in tree.Tokens)
            {
                Region region = null;

                if (token.Category == Irony.Parsing.TokenCategory.Comment)
                {
                    if (token.Text.Contains('\n'))//多行注释,折叠
                    {
                        region             = new Region();
                        region.StartLine   = token.Location.Line;
                        region.StartOffset = 0;

                        region.EndLine   = token.Location.Line + token.Text.Count(c => { return(c == '\n'); });
                        region.EndOffset = snapShot.GetLineFromLineNumber(region.EndLine).Length;

                        region.Preview = snapShot.GetLineFromLineNumber(region.StartLine).GetText().Replace("--[[", "");

                        region.IsCollapsed = true;
                    }
                    else
                    {
                        if (token.Text.StartsWith("--region ") && startRegion == null)
                        {
                            startRegion = token;
                        }

                        else if (token.Text.StartsWith("--endregion") && startRegion != null)
                        {
                            try
                            {
                                region = new Region();
                                var startLine       = snapShot.GetLineFromLineNumber(startRegion.Location.Line);
                                var startLineOffset = startRegion.Location.Position - startLine.Start.Position;

                                var endLine       = snapShot.GetLineFromLineNumber(token.Location.Line);
                                var endLineOffset = (token.Location.Position + token.Length) - endLine.Start.Position;

                                region.StartLine   = startRegion.Location.Line;
                                region.StartOffset = startLineOffset;

                                region.EndLine   = token.Location.Line;
                                region.EndOffset = endLineOffset;

                                region.Preview = startRegion.Text.Replace("--region ", "");

                                startRegion = null;
                            }
                            catch
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine("LuaOutline.FindUserRegions cause an exception.");
                                sb.AppendLine("Args:");
                                sb.AppendLine("StartLine:" + startRegion.Location.Line + "," + startRegion.Location.Column);
                                sb.AppendLine("EndLine:" + token.Location.Line + "," + token.Location.Column);
                                sb.AppendLine("TextSnapshot Line:" + snapShot.LineCount);

                                Package.BabePackage.Setting.LogError(sb.ToString());
                                return;
                            }
                        }
                    }

                    if (region != null)
                    {
                        regions.Add(region);
                    }
                }
            }
        }
Exemplo n.º 10
0
 public FileContentChangedEventArgs(ITextSnapshot Snapshot, Irony.Parsing.ParseTree Tree)
 {
     this.Tree     = Tree;
     this.Snapshot = Snapshot;
 }