-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWalker2.cs
More file actions
69 lines (53 loc) · 2 KB
/
Walker2.cs
File metadata and controls
69 lines (53 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using CSharpToJavaScript.Utils;
using System.Collections.Immutable;
namespace CSharpToJavaScript;
internal class Walker2
{
public Walker2(FileData file)
{
SyntaxTree tree = CSharpSyntaxTree.ParseText(file.SourceStr);
tree = CSTOJS.AddGlobalUsings(tree);
MetadataReference[]? references = CSTOJS.GetReferences(file.OptionsForFile);
CSharpCompilation compilation = CSharpCompilation
.Create("Walker2")
.AddReferences(references)
.AddSyntaxTrees(tree);
SemanticModel model = compilation.GetSemanticModel(tree);
ImmutableArray<Diagnostic> diagnostics = model.GetDiagnostics();
for (int i = 0; i < diagnostics.Length; i++)
{
if (file.OptionsForFile.Debug)
Log.WarningLine(diagnostics[i].ToString());
//Print an error if compilation fails.
if (diagnostics[i].Severity == DiagnosticSeverity.Error)
{
if (file.OptionsForFile.DisableCompilationErrors == false)
Log.ErrorLine(diagnostics[i].ToString());
}
}
SyntaxNode root = tree.GetRoot();
WithSemanticRewriter withSemanticRewriter = new(model, file.OptionsForFile);
WithoutSemanticRewriter withoutSemanticRewriter = new(file.OptionsForFile);
StringBuilderWalker stringBuilderWalker = new();
SyntaxNode newRoot1 = withSemanticRewriter.Visit(root);
if (root != newRoot1)
root = newRoot1;
root = root.ReplaceTokens(withSemanticRewriter.ReplaceTokens.Keys, (o, r) =>
{
return withSemanticRewriter.ReplaceTokens[o];
});
if (file.OptionsForFile.Debug)
file.Debug_WithSemanticRewriter = root.ToFullString();
SyntaxNode newRoot2 = withoutSemanticRewriter.Visit(root);
if (root != newRoot2)
root = newRoot2;
if (file.OptionsForFile.Debug)
file.Debug_WithoutSemanticRewriter = root.ToFullString();
stringBuilderWalker.JSSB.Append(file.OptionsForFile.AddSBAtTheTop);
stringBuilderWalker.Visit(root);
stringBuilderWalker.JSSB.Append(file.OptionsForFile.AddSBAtTheBottom);
file.TranslatedStr = stringBuilderWalker.JSSB.ToString();
}
}