/// <summary>
        /// Produces a syntax tree by parsing the source text.
        /// </summary>
        public static SyntaxTree ParseText(
            SourceText text,
            string path = "",
            CSharpParseOptions options          = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            using (Logger.LogBlock(FunctionId.CSharp_SyntaxTree_FullParse, path, text.Length, cancellationToken))
            {
                options = options ?? CSharpParseOptions.Default;

                using (var lexer = new Syntax.InternalSyntax.Lexer(text, options))
                {
                    using (var parser = new Syntax.InternalSyntax.LanguageParser(lexer, oldTree: null, changes: null, cancellationToken: cancellationToken))
                    {
                        var compilationUnit = (CompilationUnitSyntax)parser.ParseCompilationUnit().CreateRed();
                        var tree            = new ParsedSyntaxTree(text, path, options, compilationUnit, parser.Directives);
                        tree.VerifySource();
                        return(tree);
                    }
                }
            }
        }
        private SyntaxTree WithChanges(SourceText newText, IReadOnlyList <TextChangeRange> changes)
        {
            if (changes == null)
            {
                throw new ArgumentNullException("changes");
            }

            var oldTree = this;

            // if changes is entire text do a full reparse
            if (changes.Count == 1 && changes[0].Span == new TextSpan(0, this.Length) && changes[0].NewLength == newText.Length)
            {
                // parser will do a full parse if we give it no changes
                changes = null;
                oldTree = null;
            }

            using (var lexer = new InternalSyntax.Lexer(newText, this.Options))
            {
                CSharp.CSharpSyntaxNode oldRoot = oldTree != null?oldTree.GetRoot() : null;

                using (var parser = new InternalSyntax.LanguageParser(lexer, oldRoot, changes))
                {
                    var compilationUnit = (CompilationUnitSyntax)parser.ParseCompilationUnit().CreateRed();
                    var tree            = new ParsedSyntaxTree(newText, this.FilePath, this.Options, compilationUnit, parser.Directives);
                    tree.VerifySource(changes);
                    return(tree);
                }
            }
        }
Example #3
0
        private SyntaxTree WithChanges(SourceText newText, IReadOnlyList <TextChangeRange> changes)
        {
            if (changes == null)
            {
                throw new ArgumentNullException(nameof(changes));
            }

            var oldTree = this;

            // if changes is entire text do a full reparse
            if (changes.Count == 1 && changes[0].Span == new TextSpan(0, this.Length) && changes[0].NewLength == newText.Length)
            {
                // parser will do a full parse if we give it no changes
                changes = null;
                oldTree = null;
            }

            using (var lexer = new InternalSyntax.Lexer(newText, this.Options))
                using (var parser = new InternalSyntax.LanguageParser(lexer, oldTree?.GetRoot(), changes))
                {
                    var compilationUnit = (CompilationUnitSyntax)parser.ParseCompilationUnit().CreateRed();
                    var tree            = new ParsedSyntaxTree(
                        newText,
                        newText.Encoding,
                        newText.ChecksumAlgorithm,
                        FilePath,
                        Options,
                        compilationUnit,
                        parser.Directives,
                        DiagnosticOptions);
                    tree.VerifySource(changes);
                    return(tree);
                }
        }
Example #4
0
        /// <summary>
        /// Produces a syntax tree by parsing the source text.
        /// </summary>
        public static SyntaxTree ParseText(
            SourceText text,
            CSharpParseOptions options = null,
            string path = "",
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            options = options ?? CSharpParseOptions.Default;

            using (var lexer = new InternalSyntax.Lexer(text, options))
            {
                using (var parser = new InternalSyntax.LanguageParser(lexer, oldTree: null, changes: null, cancellationToken: cancellationToken))
                {
                    var compilationUnit = (CompilationUnitSyntax)parser.ParseCompilationUnit().CreateRed();
                    var tree            = new ParsedSyntaxTree(text, text.Encoding, text.ChecksumAlgorithm, path, options, compilationUnit, parser.Directives);
                    tree.VerifySource();
                    return(tree);
                }
            }
        }
Example #5
0
        public static SyntaxTree ParseText(SourceText text)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            using (var lexer = new InternalSyntax.Lexer(text))
            {
                using (var parser = new InternalSyntax.LanguageParser(lexer))
                {
                    var compilationUnit = (CompilationUnitSyntax)parser.ParseCompilationUnit().CreateRedNode();
                    var tree            = new ParsedSyntaxTree(text, compilationUnit);
                    return(tree);
                }
            }
        }
Example #6
0
 /// <summary>
 /// Produces a syntax tree by parsing the source text.
 /// </summary>
 public static MetaSyntaxTree ParseText(
     SourceText text,
     MetaParseOptions options = null,
     string path = "",
     CancellationToken cancellationToken = default(CancellationToken))
 {
     if (text == null)
     {
         throw new ArgumentNullException(nameof(text));
     }
     options = options ?? MetaParseOptions.Default;
     using (var parser = new MetaSyntaxParser(text, options, oldTree: null, changes: null, cancellationToken: cancellationToken))
     {
         var compilationUnit = (MainSyntax)parser.ParseMain().CreateRed();
         var tree            = new ParsedSyntaxTree(text, text.Encoding, text.ChecksumAlgorithm, path, options, compilationUnit, parser.Directives);
         tree.VerifySource();
         return(tree);
     }
 }
Example #7
0
        private SyntaxTree WithChanges(SourceText newText, IReadOnlyList <TextChangeRange> changes)
        {
            if (changes == null)
            {
                throw new ArgumentNullException(nameof(changes));
            }
            var oldTree = this;

            // if changes is entire text do a full reparse
            if (changes.Count == 1 && new TextSpan(0, this.Length).Equals(changes[0].Span) && changes[0].NewLength == newText.Length)
            {
                // parser will do a full parse if we give it no changes
                changes = null;
                oldTree = null;
            }
            using (var parser = SoalLanguage.Instance.SyntaxFactory.MakeParser(newText, this.Options, oldTree?.GetRoot(), changes))
            {
                var compilationUnit = (MainSyntax)parser.Parse().CreateRed();
                var tree            = new ParsedSyntaxTree(newText, newText.Encoding, newText.ChecksumAlgorithm, this.FilePath, this.Options, compilationUnit, parser.Directives);
                tree.VerifySource(changes);
                return(tree);
            }
        }
Example #8
0
        /// <summary>
        /// Produces a syntax tree by parsing the source text.
        /// </summary>
        public static SyntaxTree ParseText(
            SourceText text,
            CSharpParseOptions options = null,
            string path = "",
            ImmutableDictionary <string, ReportDiagnostic> diagnosticOptions = null,
            bool?isGeneratedCode = null,
            CancellationToken cancellationToken = default)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            options = options ?? CSharpParseOptions.Default;

            using (var lexer = new InternalSyntax.Lexer(text, options))
            {
                using (var parser = new InternalSyntax.LanguageParser(lexer, oldTree: null, changes: null, cancellationToken: cancellationToken))
                {
                    var compilationUnit = (CompilationUnitSyntax)parser.ParseCompilationUnit().CreateRed();
                    var tree            = new ParsedSyntaxTree(
                        text,
                        text.Encoding,
                        text.ChecksumAlgorithm,
                        path,
                        options,
                        compilationUnit,
                        parser.Directives,
                        diagnosticOptions: diagnosticOptions,
                        isGeneratedCode: isGeneratedCode,
                        cloneRoot: true);
                    tree.VerifySource();
                    return(tree);
                }
            }
        }
        /// <summary>
        /// Produces a syntax tree by parsing the source text.
        /// </summary>
        public static SyntaxTree ParseText(
            SourceText text,
            CSharpParseOptions options = null,
            string path = "",
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

#if XSHARPPRE
            {
                string s = text.ToString();
                s    = s.Replace("Microsoft.CodeAnalysis.CSharp", "LanguageService.CodeAnalysis.XSharp");
                s    = s.Replace("Microsoft.CodeAnalysis", "LanguageService.CodeAnalysis");
                s    = s.Replace("Microsoft.Cci", "LanguageService.Cci");
                s    = (new System.Text.RegularExpressions.Regex(@"(?<!\.)CSharpResources")).Replace(s, "LanguageService.CodeAnalysis.XSharpResources");
                s    = (new System.Text.RegularExpressions.Regex(@"(?<!Microsoft[\._][A-Za-z0-9_\.]*)CSharp")).Replace(s, "XSharp");
                s    = s.Replace("XSharp.XSharpResources", "XSharpResources");
                s    = s.Replace("Antlr4.Runtime", "LanguageService.SyntaxTree");
                s    = s.Replace("XSHARP_RUNTIME", "true");
                text = SourceText.From(s, text.Encoding);
            }
#endif

            options = options ?? CSharpParseOptions.Default;

#if XSHARP
            using (var parser = new InternalSyntax.XSharpLanguageParser(path, text, options, oldTree: null, changes: null, cancellationToken: cancellationToken))
            {
                var compilationUnit = (CompilationUnitSyntax)parser.ParseCompilationUnit().CreateRed();
                var tree            = new ParsedSyntaxTree(text, text.Encoding, text.ChecksumAlgorithm, path, options, compilationUnit, default(InternalSyntax.DirectiveStack));
                //tree.VerifySource();
                if (options.SaveAsCSharp)
                {
                    path = System.IO.Path.ChangeExtension(path, ".cs");
                    string source = compilationUnit.ToString();
                    source = source.Replace(";", ";\r\n");
                    source = source.Replace("{", "\r\n{\r\n");
                    source = source.Replace("}", "\r\n}\r\n");
                    source = source.Replace(" . ", ".");
                    source = source.Replace(" :: ", "::");
                    source = source.Replace("}", "}\r\n");
                    source = source.Replace("$", "_");
                    System.IO.File.WriteAllText(path, source);
                }
                return(tree);
            }
#else
            using (var lexer = new InternalSyntax.Lexer(text, options))
            {
                using (var parser = new InternalSyntax.LanguageParser(lexer, oldTree: null, changes: null, cancellationToken: cancellationToken))
                {
                    var compilationUnit = (CompilationUnitSyntax)parser.ParseCompilationUnit().CreateRed();
                    var tree            = new ParsedSyntaxTree(text, text.Encoding, text.ChecksumAlgorithm, path, options, compilationUnit, parser.Directives);
                    tree.VerifySource();
                    return(tree);
                }
            }
#endif
        }