예제 #1
0
 internal static SyntaxTree ProcessTrees(SyntaxTree[] trees, CSharpParseOptions options)
 {
     if (trees.Length > 0)
     {
         var tree    = trees[0];
         var lp      = new XSharpLanguageParser(tree.FilePath, null, options, null, null);
         var newtree = lp.processTrees(trees, options);
         return(newtree);
     }
     return(null);
 }
예제 #2
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(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
        }