forked from paulirwin/JavaToCSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatementVisitor.cs
More file actions
67 lines (58 loc) · 2.85 KB
/
Copy pathStatementVisitor.cs
File metadata and controls
67 lines (58 loc) · 2.85 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
using System;
using System.Collections.Generic;
using System.Linq;
using com.github.javaparser.ast.stmt;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace JavaToCSharp.Statements;
public abstract class StatementVisitor<T> : StatementVisitor
where T : Statement
{
public abstract StatementSyntax? Visit(ConversionContext context, T statement);
protected sealed override StatementSyntax? Visit(ConversionContext context, Statement statement)
{
return Visit(context, (T)statement);
}
}
public abstract class StatementVisitor
{
private static readonly IDictionary<Type, StatementVisitor> _visitors;
static StatementVisitor()
{
_visitors = new Dictionary<Type, StatementVisitor>
{
{ typeof(AssertStmt), new AssertStatementVisitor() },
{ typeof(BlockStmt), new BlockStatementVisitor() },
{ typeof(BreakStmt), new BreakStatementVisitor() },
{ typeof(ContinueStmt), new ContinueStatementVisitor() },
{ typeof(DoStmt), new DoStatementVisitor() },
{ typeof(ExpressionStmt), new ExpressionStatementVisitor() },
{ typeof(ForEachStmt), new ForEachStatementVisitor() },
{ typeof(ForStmt), new ForStatementVisitor() },
{ typeof(IfStmt), new IfStatementVisitor() },
{ typeof(LabeledStmt), new LabeledStatementVisitor() },
{ typeof(ReturnStmt), new ReturnStatementVisitor() },
{ typeof(SwitchStmt), new SwitchStatementVisitor() },
{ typeof(SynchronizedStmt), new SynchronizedStatementVisitor() },
{ typeof(ThrowStmt), new ThrowStatementVisitor() },
{ typeof(TryStmt), new TryStatementVisitor() },
{ typeof(WhileStmt), new WhileStatementVisitor() },
{ typeof(EmptyStmt), new EmptyStatementVisitor() },
{ typeof(LocalClassDeclarationStmt), new TypeDeclarationStatementVisitor() }
};
}
protected abstract StatementSyntax? Visit(ConversionContext context, Statement statement);
public static List<StatementSyntax> VisitStatements(ConversionContext context, IEnumerable<Statement>? statements) =>
statements == null
? new List<StatementSyntax>()
: statements.Select(statement => VisitStatement(context, statement))
.Where(syntax => syntax != null)!.ToList<StatementSyntax>();
public static StatementSyntax? VisitStatement(ConversionContext context, Statement statement)
{
if (!_visitors.TryGetValue(statement.GetType(), out var visitor))
{
var message = $"Statement visitor not implemented for statement `{statement}`, `{statement.getBegin()}` type `{statement.GetType()}`.";
throw new InvalidOperationException(message);
}
return visitor.Visit(context, statement).WithJavaComments(context, statement);
}
}