private static IEnumerable <StatementSyntax> ExplodeStatements(SyntaxList <StatementSyntax> statements, SyntaxNode toReplace, BlockSyntax block) { var nodeID = RoslynCompiler.NodeMark(toReplace); foreach (var statement in statements) { var innerID = RoslynCompiler.NodeMark(statement); if (innerID == nodeID) { foreach (var inner in block.Statements) { yield return(inner); } } else { yield return(statement); } } }
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 int GetExcessId(SyntaxToken token) { string xsStr = RoslynCompiler.TokenMark(token); return(xsStr == null ? -1 : int.Parse(xsStr)); }
public SyntaxNode MarkNode(SyntaxNode node) { return(RoslynCompiler.MarkNode(node, RoslynCompiler.uniqueId())); //td: scope ids }
public static Func <SyntaxNode, Scope, SyntaxNode> ReplaceNode(SyntaxNode newNode) { return((node, scope) => RoslynCompiler.ReplaceExcessId(newNode, node)); }
public SyntaxToken InitToken(SyntaxToken token, int xsId) { return(RoslynCompiler.MarkToken(token, xsId.ToString())); }
public SyntaxToken MarkToken(SyntaxToken token) { string strId = RoslynCompiler.uniqueId(); return(RoslynCompiler.MarkToken(token, strId)); }
public string TokenToString(SyntaxToken token, out string xsId) { xsId = RoslynCompiler.TokenMark(token); return(token.ToFullString()); }
public bool isIdentifier(SyntaxToken token) { return(RoslynCompiler.isLexicalIdentifier(token.Kind())); }
public string GetExcessStringId(SyntaxNode node) { return(RoslynCompiler.NodeMark(node)); }
public int GetExcessId(SyntaxNode node) { string xsStr = RoslynCompiler.NodeMark(node); return(xsStr == null ? -1 : int.Parse(xsStr)); }
//rewriters private Func <SyntaxNode, Scope, LookAheadResult> CheckCodeExtension(SyntaxNode original, SyntacticalExtension <SyntaxNode> extension) { return((node, scope) => { var code = node as BlockSyntax; if (code == null) { return new LookAheadResult { Matched = false } } ; _lookahead = null; extension.Body = base.Visit(code); SyntaxNode resulSyntaxNode = null; if (original is LocalDeclarationStatementSyntax) { extension.Kind = ExtensionKind.Expression; resulSyntaxNode = extension.Handler(node, scope, extension); if (!(resulSyntaxNode is ExpressionSyntax)) { //td: error, expecting expression return new LookAheadResult { Matched = false }; } var localDecl = original as LocalDeclarationStatementSyntax; resulSyntaxNode = localDecl .WithDeclaration(localDecl.Declaration .WithVariables(CSharp.SeparatedList(new[] { localDecl.Declaration.Variables[0] .WithInitializer(localDecl.Declaration.Variables[0].Initializer .WithValue((ExpressionSyntax)resulSyntaxNode)) }))) .WithSemicolonToken(CSharp.ParseToken(";")); } else if (original is ExpressionStatementSyntax) { var exprStatement = original as ExpressionStatementSyntax; var assignment = exprStatement.Expression as AssignmentExpressionSyntax; if (assignment != null) { extension.Kind = ExtensionKind.Expression; resulSyntaxNode = extension.Handler(node, scope, extension); if (!(resulSyntaxNode is ExpressionSyntax)) { //td: error, expecting expression return new LookAheadResult { Matched = false }; } resulSyntaxNode = exprStatement .WithExpression(assignment .WithRight((ExpressionSyntax)resulSyntaxNode)) .WithSemicolonToken(CSharp.ParseToken(";")); } else { resulSyntaxNode = extension.Handler(node, scope, extension); if (resulSyntaxNode != null) { resulSyntaxNode = RoslynCompiler.UpdateExcessId(resulSyntaxNode, node); } } } else { throw new NotImplementedException(); } return new LookAheadResult { Matched = true, Result = resulSyntaxNode, }; }); }
public void addDocument(string id, IDocument <SyntaxToken, SyntaxNode, SemanticModel> document, RoslynCompiler compiler) { if (_documents .Where(doc => doc.Id == id) .Any()) { throw new InvalidOperationException($"duplicate file: {id}"); } var newDoc = new CompilationDocument { Id = id, Stage = CompilerStage.Started, Document = document, Compiler = compiler, }; //build the document var documentInjector = newDoc.Compiler as IDocumentInjector <SyntaxToken, SyntaxNode, SemanticModel>; documentInjector.apply(newDoc.Document); _documents.Add(newDoc); }