Пример #1
0
        public string GenerateSourceMap(string fileName, string content, Action <SourceMapBuilder> before = null)
        {
            if (this.AssemblyInfo.SourceMap.Enabled)
            {
                var projectPath = Path.GetDirectoryName(this.Location);

                SourceMapGenerator.Generate(fileName, projectPath, ref content,
                                            before,
                                            (sourceRelativePath) =>
                {
                    string path = null;
                    ParsedSourceFile sourceFile = null;

                    try
                    {
                        path       = Path.Combine(projectPath, sourceRelativePath);
                        sourceFile = this.ParsedSourceFiles.First(pf => pf.ParsedFile.FileName == path);

                        return(sourceFile.SyntaxTree.TextSource ?? sourceFile.SyntaxTree.ToString(Translator.GetFormatter()));
                    }
                    catch (Exception ex)
                    {
                        throw (TranslatorException)TranslatorException.Create(
                            "Could not get ParsedSourceFile for SourceMap. Exception: {0}; projectPath: {1}; sourceRelativePath: {2}; path: {3}.",
                            ex.ToString(), projectPath, sourceRelativePath, path);
                    }
                },
                                            new string[0], this.SourceFiles, this.AssemblyInfo.SourceMap.Eol, this.Log
                                            );
            }

            return(content);
        }
        protected void BuildSyntaxTree()
        {
            for (int i = 0; i < this.SourceFiles.Count; i++)
            {
                var fileName = this.SourceFiles[i];

                using (var reader = new StreamReader(fileName))
                {
                    var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser();
                    if (!string.IsNullOrEmpty(Defines))
                    {
                        string[] defs = Defines.Split(';');
                        foreach (string def in defs)
                        {
                            parser.CompilerSettings.ConditionalSymbols.Add(def);
                        }
                    }

                    if (this.DefineConstants != null && this.DefineConstants.Count > 0)
                    {
                        foreach (var defineConstant in this.DefineConstants)
                        {
                            parser.CompilerSettings.ConditionalSymbols.Add(defineConstant);
                        }
                    }

                    var syntaxTree = parser.Parse(reader, fileName);

                    if (parser.HasErrors)
                    {
                        foreach (var error in parser.Errors)
                        {
                            throw new EmitterException(syntaxTree, string.Format("Parsing error in a file {0} {2}: {1}", fileName, error.Message, error.Region.Begin.ToString()));
                        }
                    }

                    var expandResult = new QueryExpressionExpander().ExpandQueryExpressions(syntaxTree);
                    syntaxTree = (expandResult != null ? (SyntaxTree)expandResult.AstNode : syntaxTree);

                    var emptyLambdaDetecter = new EmptyLambdaDetecter();
                    syntaxTree.AcceptVisitor(emptyLambdaDetecter);

                    if (emptyLambdaDetecter.Found)
                    {
                        var fixer   = new EmptyLambdaFixer();
                        var astNode = syntaxTree.AcceptVisitor(fixer);
                        syntaxTree = (astNode != null ? (SyntaxTree)astNode : syntaxTree);
                    }

                    var f = new ParsedSourceFile(syntaxTree, new CSharpUnresolvedFile
                    {
                        FileName = fileName
                    });
                    this.ParsedSourceFiles.Add(f);

                    var tcv = new TypeSystemConvertVisitor(f.ParsedFile);
                    f.SyntaxTree.AcceptVisitor(tcv);
                }
            }
        }
        protected void BuildSyntaxTree()
        {
            this.Log.Info("Building syntax tree...");

            var rewriter = new SharpSixRewriter(this);

            for (int i = 0; i < this.SourceFiles.Count; i++)
            {
                var fileName = this.SourceFiles[i];

                this.Log.Trace("Source file " + (fileName ?? string.Empty) + " ...");

                var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser();

                if (this.DefineConstants != null && this.DefineConstants.Count > 0)
                {
                    foreach (var defineConstant in this.DefineConstants)
                    {
                        parser.CompilerSettings.ConditionalSymbols.Add(defineConstant);
                    }
                }

                var syntaxTree = parser.Parse(rewriter.Rewrite(i), fileName);
                syntaxTree.FileName = fileName;
                //var syntaxTree = parser.Parse(reader, fileName);
                this.Log.Trace("\tParsing syntax tree done");

                if (parser.HasErrors)
                {
                    foreach (var error in parser.Errors)
                    {
                        throw new EmitterException(syntaxTree, string.Format("Parsing error in a file {0} {2}: {1}", fileName, error.Message, error.Region.Begin.ToString()));
                    }
                }

                var expandResult = new QueryExpressionExpander().ExpandQueryExpressions(syntaxTree);
                this.Log.Trace("\tExpanding query expressions done");

                syntaxTree = (expandResult != null ? (SyntaxTree)expandResult.AstNode : syntaxTree);

                var emptyLambdaDetecter = new EmptyLambdaDetecter();
                syntaxTree.AcceptVisitor(emptyLambdaDetecter);
                this.Log.Trace("\tAccepting lambda detector visitor done");

                if (emptyLambdaDetecter.Found)
                {
                    var fixer   = new EmptyLambdaFixer();
                    var astNode = syntaxTree.AcceptVisitor(fixer);
                    this.Log.Trace("\tAccepting lambda fixer visitor done");
                    syntaxTree          = (astNode != null ? (SyntaxTree)astNode : syntaxTree);
                    syntaxTree.FileName = fileName;
                }

                var f = new ParsedSourceFile(syntaxTree, new CSharpUnresolvedFile
                {
                    FileName = fileName
                });
                this.ParsedSourceFiles.Add(f);

                var tcv = new TypeSystemConvertVisitor(f.ParsedFile);
                f.SyntaxTree.AcceptVisitor(tcv);
                this.Log.Trace("\tAccepting type system convert visitor done");

                this.Log.Trace("Source file " + (fileName ?? string.Empty) + " done");
            }

            this.Log.Info("Building syntax tree done");
        }
Пример #4
0
        private void BuildSyntaxTreeForFile(int index, ref string[] rewriten)
        {
            var fileName = this.SourceFiles[index];

            this.Log.Trace("Source file " + (fileName ?? string.Empty) + " ...");

            var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser();

            if (this.DefineConstants != null && this.DefineConstants.Count > 0)
            {
                foreach (var defineConstant in this.DefineConstants)
                {
                    parser.CompilerSettings.ConditionalSymbols.Add(defineConstant);
                }
            }

            var syntaxTree = parser.Parse(rewriten[index], fileName);

            syntaxTree.FileName = fileName;
            this.Log.Trace("\tParsing syntax tree done");

            if (parser.HasErrors)
            {
                var errors = new List <string>();
                foreach (var error in parser.Errors)
                {
                    errors.Add(fileName + ":" + error.Region.BeginLine + "," + error.Region.BeginColumn + ": " + error.Message);
                }

                throw new EmitterException(syntaxTree, "Error parsing code." + Environment.NewLine + String.Join(Environment.NewLine, errors));
            }

            var expandResult = new QueryExpressionExpander().ExpandQueryExpressions(syntaxTree);

            this.Log.Trace("\tExpanding query expressions done");

            syntaxTree = (expandResult != null ? (SyntaxTree)expandResult.AstNode : syntaxTree);

            var emptyLambdaDetecter = new EmptyLambdaDetecter();

            syntaxTree.AcceptVisitor(emptyLambdaDetecter);
            this.Log.Trace("\tAccepting lambda detector visitor done");

            if (emptyLambdaDetecter.Found)
            {
                var fixer   = new EmptyLambdaFixer();
                var astNode = syntaxTree.AcceptVisitor(fixer);
                this.Log.Trace("\tAccepting lambda fixer visitor done");
                syntaxTree          = (astNode != null ? (SyntaxTree)astNode : syntaxTree);
                syntaxTree.FileName = fileName;
            }

            var f = new ParsedSourceFile(syntaxTree, new CSharpUnresolvedFile
            {
                FileName = fileName
            });

            this.ParsedSourceFiles[index] = f;

            var tcv = new TypeSystemConvertVisitor(f.ParsedFile);

            f.SyntaxTree.AcceptVisitor(tcv);
            this.Log.Trace("\tAccepting type system convert visitor done");

            this.Log.Trace("Source file " + (fileName ?? string.Empty) + " done");
        }
        protected void BuildSyntaxTree()
        {
            for (int i = 0; i < this.SourceFiles.Count; i++)
            {
                var fileName = this.SourceFiles[i];

                using (var reader = new StreamReader(fileName))
                {
                    var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser();

                    if (this.DefineConstants != null && this.DefineConstants.Count > 0)
                    {
                        foreach (var defineConstant in this.DefineConstants)
                        {
                            parser.CompilerSettings.ConditionalSymbols.Add(defineConstant);
                        }
                    }

                    var syntaxTree = parser.Parse(reader, fileName);

                    if (parser.HasErrors)
                    {
                        foreach (var error in parser.Errors)
                        {
                            throw new EmitterException(syntaxTree, string.Format("Parsing error in a file {0} {2}: {1}", fileName, error.Message, error.Region.Begin.ToString()));
                        }
                    }

                    var expandResult = new QueryExpressionExpander().ExpandQueryExpressions(syntaxTree);
                    syntaxTree = (expandResult != null ? (SyntaxTree)expandResult.AstNode : syntaxTree);

                    var emptyLambdaDetecter = new EmptyLambdaDetecter();
                    syntaxTree.AcceptVisitor(emptyLambdaDetecter);

                    if (emptyLambdaDetecter.Found)
                    {
                        var fixer = new EmptyLambdaFixer();
                        var astNode = syntaxTree.AcceptVisitor(fixer);
                        syntaxTree = (astNode != null ? (SyntaxTree)astNode : syntaxTree);
                    }

                    var f = new ParsedSourceFile(syntaxTree, new CSharpUnresolvedFile { FileName = fileName });
                    this.ParsedSourceFiles.Add(f);

                    var tcv = new TypeSystemConvertVisitor(f.ParsedFile);
                    f.SyntaxTree.AcceptVisitor(tcv);
                }
            }
        }