Beispiel #1
0
        internal void Parse(ITextSnapshot snapshot, out LanguageService.SyntaxTree.ITokenStream TokenStream, string path)
        {
            string source = snapshot.GetText();

            // Currently we "eat" all Exception that might be raised
            // by XSharpSyntaxTree.ParseText
            TokenStream = null;
            try
            {
                LanguageService.CodeAnalysis.SyntaxTree tree = XSharpSyntaxTree.ParseText(source, null, path);
                var syntaxRoot = tree.GetRoot();
                // Get the antlr4 parse tree root
                var xtree = ((LanguageService.CodeAnalysis.XSharp.Syntax.CompilationUnitSyntax)syntaxRoot).XSource;
                TokenStream = ((LanguageService.CodeAnalysis.XSharp.Syntax.CompilationUnitSyntax)syntaxRoot).XTokenStream;
                //
                var walker   = new LanguageService.SyntaxTree.Tree.ParseTreeWalker();
                var discover = new XSharpTreeDiscover();
                discover.Snapshot              = snapshot;
                discover.xsharpBraceCloseType  = xsharpBraceCloseType;
                discover.xsharpBraceOpenType   = xsharpBraceOpenType;
                discover.xsharpIdentifierType  = xsharpIdentifierType;
                discover.xsharpRegionStartType = xsharpRegionStartType;
                discover.xsharpRegionStopType  = xsharpRegionStopType;
                // Walk the tree. The TreeDiscover class will collect the tags.
                walker.Walk(discover, xtree);
                this.tags = discover.tags;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public CodeCompileUnit Parse(string source)
        {
            CodeCompileUnit ccu = new CodeCompileUnit();

            //
            try
            {
                // Tab replace, in order to have the good position of Members (Line/col)
                String TabSpace = new String(' ', TabSize);
                source = source.Replace("\t", TabSpace);
                //
                LanguageService.CodeAnalysis.SyntaxTree tree = XSharpSyntaxTree.ParseText(source, _projectNode.ParseOptions);
                var syntaxRoot = tree.GetRoot();
                // Get the antlr4 parse tree root
                var xtree = ((LanguageService.CodeAnalysis.XSharp.Syntax.CompilationUnitSyntax)syntaxRoot).XSource;

                // We need to d 2 steps here:
                // 1 - Scan for the fields , so we know the difference between fields and properties when we perform step 2
                // 2 - Scan for the rest. We pass the list of fields to the tree discover code so it "knows" about all fields

                var discoverFields = new XSharpFieldsDiscover(_projectNode);
                discoverFields.SourceCode  = source;
                discoverFields.CurrentFile = this.FileName;

                var walker = new LanguageService.SyntaxTree.Tree.ParseTreeWalker();
                walker.Walk(discoverFields, xtree);
                // now the discoverFields class should contain a Dictionary with <context, FieldList>
                var discover = new XSharpClassDiscover(_projectNode);
                discover.FieldList   = discoverFields.FieldList;
                discover.SourceCode  = source;
                discover.CurrentFile = this.FileName;
                //
                walker.Walk(discover, xtree);
                //
                ccu = discover.CodeCompileUnit;
                ccu.UserData[XSharpCodeConstants.USERDATA_FILENAME] = this.FileName;
            }
            catch (Exception ex)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }
            //
            return(ccu);
        }