private void applyLexical(RoslynDocument document) { var handler = _lexical as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; Debug.Assert(handler != null); handler.apply(document); }
protected override IDocument <SyntaxToken, SyntaxNode, SemanticModel> createDocument() { var result = new RoslynDocument(_scope); _scope.set <IDocument <SyntaxToken, SyntaxNode, SemanticModel> >(result); applyLexical(result); return(result); }
public ExpressionSyntax CompileExpression(string expr) { var document = new RoslynDocument(_scope, expr); var handler = _lexical as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; _scope.set <IDocument <SyntaxToken, SyntaxNode, SemanticModel> >(document); handler.apply(document); document.applyChanges(CompilerStage.Lexical); return(CSharp.ParseExpression(document.LexicalText)); }
public SyntaxNode ApplyLexicalPass(string text, out string newText) { var document = new RoslynDocument(_scope, text); var handler = _lexical as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; _scope.set <IDocument <SyntaxToken, SyntaxNode, SemanticModel> >(document); handler.apply(document); document.applyChanges(CompilerStage.Lexical); newText = document.LexicalText; return(document.SyntaxRoot); }
public SyntaxTree ApplySyntacticalPass(string text, out string result) { var document = new RoslynDocument(_scope, text); //we actually dont touch our own state during these calls var lHandler = _lexical as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; var sHandler = _syntax as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; lHandler.apply(document); sHandler.apply(document); document.applyChanges(CompilerStage.Syntactical); result = document.SyntaxRoot.NormalizeWhitespace().ToFullString(); return(document.SyntaxRoot.SyntaxTree); }
public SyntaxTree ApplySemanticalPass(RoslynDocument document, out string result) { var lHandler = _lexical as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; var sHandler = _syntax as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; var ssHandler = _semantics as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; lHandler.apply(document); sHandler.apply(document); ssHandler.apply(document); document.applyChanges(CompilerStage.Syntactical); var tree = document.SyntaxRoot.SyntaxTree; var compilation = CSharpCompilation.Create("semantical-pass", syntaxTrees: new[] { tree }, references: new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), MetadataReference.CreateFromFile(typeof(Dictionary <int, int>).Assembly.Location), }, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); while (true) { document.Model = compilation.GetSemanticModel(tree); if (document.applyChanges(CompilerStage.Semantical)) { break; } var oldTree = tree; tree = document.SyntaxRoot.SyntaxTree; compilation = compilation.ReplaceSyntaxTree(oldTree, tree); } result = document.SyntaxRoot.NormalizeWhitespace().ToFullString(); return(document.SyntaxRoot.SyntaxTree); }
private ExcessDocument loadDocument(Project project, string fileName, out RoslynCompiler compiler) { if (_extensions == null || !_extensions.Any()) { throw new InvalidOperationException("no extensions registered, plain c#?"); } var source = File.ReadAllText(fileName); var document = new RoslynDocument(new Scope(_scope), source, fileName); var enviroment = new RoslynEnvironment(_scope, new SolutionStorage(project)); var compilerResult = new RoslynCompiler(enviroment, _scope); var tree = CSharpSyntaxTree.ParseText(source); var usings = (tree.GetRoot() as CompilationUnitSyntax) ?.Usings .Where(@using => { var usingId = @using.Name.ToString(); if (!usingId.StartsWith("xs.")) { return(false); } usingId = usingId.Substring("xs.".Length); var action = null as ExtensionFunc; if (_extensions.TryGetValue(usingId, out action)) { action(compilerResult, null); //td: props? return(true); } return(false); }) .ToArray(); compiler = compilerResult; return(document); }
public SyntaxTree ApplySemanticalPass(string text, out string result) { var document = new RoslynDocument(_scope, text); return(ApplySemanticalPass(document, out result)); }