Skip to content

Commit 53e34b1

Browse files
committed
Added support for top-level statements.
1 parent c64d3e9 commit 53e34b1

File tree

1 file changed

+201
-45
lines changed

1 file changed

+201
-45
lines changed

CSharpToJavaScript/Walker.cs

Lines changed: 201 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Linq;
1010
using System.Reflection;
1111
using System.Text;
12-
using System.Xml.Linq;
1312

1413

1514
namespace CSharpToJavaScript;
@@ -40,9 +39,10 @@ internal class Walker : CSharpSyntaxWalker
4039
private bool _IgnoreArrayType = false;
4140
private bool _IgnoreAsParenthesis = false;
4241
private bool _IgnoreTailingDot = false;
42+
private bool _GlobalStatement = false;
4343

44-
private int _EnumMembers = 0;
4544

45+
private int _EnumMembers = 0;
4646

4747
//Debug
4848
private int _Line = 0;
@@ -265,36 +265,6 @@ public override void Visit(SyntaxNode? node)
265265
{
266266
case SyntaxKind.UsingDirective:
267267
return;
268-
case SyntaxKind.FileScopedNamespaceDeclaration:
269-
{
270-
_NameSpaceStr = (node as FileScopedNamespaceDeclarationSyntax).Name.ToString();
271-
foreach (MemberDeclarationSyntax member in (node as FileScopedNamespaceDeclarationSyntax).Members)
272-
{
273-
Visit(member);
274-
}
275-
if (_Options.Debug)
276-
{
277-
JSSB.Append("/*");
278-
JSSB.Append(node.ToFullString().Replace("*/", ""));
279-
JSSB.Append("*/");
280-
}
281-
return;
282-
}
283-
case SyntaxKind.NamespaceDeclaration:
284-
{
285-
_NameSpaceStr = (node as NamespaceDeclarationSyntax).Name.ToString();
286-
foreach (MemberDeclarationSyntax member in (node as NamespaceDeclarationSyntax).Members)
287-
{
288-
Visit(member);
289-
}
290-
if (_Options.Debug)
291-
{
292-
JSSB.Append("/*");
293-
JSSB.Append(node.ToFullString().Replace("*/", ""));
294-
JSSB.Append("*/");
295-
}
296-
return;
297-
}
298268
default:
299269
//CSTOJS.Log($"{syntaxKind}");
300270
break;
@@ -3098,10 +3068,64 @@ public override void VisitCollectionExpression(CollectionExpressionSyntax node)
30983068
}
30993069
public override void VisitCompilationUnit(CompilationUnitSyntax node)
31003070
{
3101-
if (_Options.Debug)
3102-
Log.WarningLine($"Not implemented or unlikely to be implemented. Calling base! ({node.FullSpan}|l:{_Line}|{node.FullSpan.Start - _Characters}-{node.FullSpan.End - _Characters})", _Options);
3071+
ChildSyntaxList nodesAndTokens = node.ChildNodesAndTokens();
3072+
3073+
for (int i = 0; i < nodesAndTokens.Count; i++)
3074+
{
3075+
SyntaxNode? asNode = nodesAndTokens[i].AsNode();
3076+
3077+
if (asNode != null)
3078+
{
3079+
SyntaxKind kind = asNode.Kind();
3080+
3081+
switch (kind)
3082+
{
3083+
case SyntaxKind.UsingDirective:
3084+
break;
3085+
case SyntaxKind.GlobalStatement:
3086+
{
3087+
_GlobalStatement = true;
3088+
VisitGlobalStatement((GlobalStatementSyntax)asNode);
3089+
_GlobalStatement = false;
3090+
break;
3091+
}
3092+
case SyntaxKind.NamespaceDeclaration:
3093+
VisitNamespaceDeclaration((NamespaceDeclarationSyntax)asNode);
3094+
break;
3095+
case SyntaxKind.FileScopedNamespaceDeclaration:
3096+
VisitFileScopedNamespaceDeclaration((FileScopedNamespaceDeclarationSyntax)asNode);
3097+
break;
3098+
default:
3099+
Log.ErrorLine($"asNode : {kind}", _Options);
3100+
break;
3101+
}
3102+
}
3103+
else
3104+
{
3105+
SyntaxToken asToken = nodesAndTokens[i].AsToken();
3106+
SyntaxKind kind = asToken.Kind();
3107+
3108+
switch (kind)
3109+
{
3110+
case SyntaxKind.EndOfFileToken:
3111+
{
3112+
VisitToken(asToken);
3113+
3114+
if (_Options.Debug)
3115+
{
3116+
JSSB.Append("/*");
3117+
JSSB.Append(node.ToFullString().Replace("*/", ""));
3118+
JSSB.Append("*/");
3119+
}
31033120

3104-
base.VisitCompilationUnit(node);
3121+
break;
3122+
}
3123+
default:
3124+
Log.ErrorLine($"asToken : {kind}", _Options);
3125+
break;
3126+
}
3127+
}
3128+
}
31053129
}
31063130
public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
31073131
{
@@ -3388,10 +3412,58 @@ public override void VisitFieldExpression(FieldExpressionSyntax node)
33883412
}
33893413
public override void VisitFileScopedNamespaceDeclaration(FileScopedNamespaceDeclarationSyntax node)
33903414
{
3391-
if (_Options.Debug)
3392-
Log.WarningLine($"Not implemented or unlikely to be implemented. Calling base! ({node.FullSpan}|l:{_Line}|{node.FullSpan.Start - _Characters}-{node.FullSpan.End - _Characters})", _Options);
3415+
ChildSyntaxList nodesAndTokens = node.ChildNodesAndTokens();
3416+
3417+
for (int i = 0; i < nodesAndTokens.Count; i++)
3418+
{
3419+
SyntaxNode? asNode = nodesAndTokens[i].AsNode();
3420+
3421+
if (asNode != null)
3422+
{
3423+
SyntaxKind kind = asNode.Kind();
3424+
3425+
switch (kind)
3426+
{
3427+
case SyntaxKind.DelegateDeclaration:
3428+
case SyntaxKind.InterfaceDeclaration:
3429+
case SyntaxKind.StructDeclaration:
3430+
break;
3431+
case SyntaxKind.QualifiedName:
3432+
case SyntaxKind.IdentifierName:
3433+
{
3434+
_NameSpaceStr = node.Name.ToString();
3435+
break;
3436+
}
3437+
case SyntaxKind.ClassDeclaration:
3438+
VisitClassDeclaration((ClassDeclarationSyntax)asNode);
3439+
break;
3440+
case SyntaxKind.EnumDeclaration:
3441+
VisitEnumDeclaration((EnumDeclarationSyntax)asNode);
3442+
break;
3443+
case SyntaxKind.NamespaceDeclaration:
3444+
VisitNamespaceDeclaration((NamespaceDeclarationSyntax)asNode);
3445+
break;
3446+
default:
3447+
Log.ErrorLine($"asNode : {kind}", _Options);
3448+
break;
3449+
}
3450+
}
3451+
else
3452+
{
3453+
SyntaxToken asToken = nodesAndTokens[i].AsToken();
3454+
SyntaxKind kind = asToken.Kind();
33933455

3394-
base.VisitFileScopedNamespaceDeclaration(node);
3456+
switch (kind)
3457+
{
3458+
case SyntaxKind.SemicolonToken:
3459+
case SyntaxKind.NamespaceKeyword:
3460+
break;
3461+
default:
3462+
Log.ErrorLine($"asToken : {kind}", _Options);
3463+
break;
3464+
}
3465+
}
3466+
}
33953467
}
33963468
public override void VisitFinallyClause(FinallyClauseSyntax node)
33973469
{
@@ -3472,10 +3544,42 @@ public override void VisitFunctionPointerUnmanagedCallingConventionList(Function
34723544
}
34733545
public override void VisitGlobalStatement(GlobalStatementSyntax node)
34743546
{
3475-
if (_Options.Debug)
3476-
Log.WarningLine($"Not implemented or unlikely to be implemented. Calling base! ({node.FullSpan}|l:{_Line}|{node.FullSpan.Start - _Characters}-{node.FullSpan.End - _Characters})", _Options);
3547+
ChildSyntaxList nodesAndTokens = node.ChildNodesAndTokens();
3548+
3549+
for (int i = 0; i < nodesAndTokens.Count; i++)
3550+
{
3551+
SyntaxNode? asNode = nodesAndTokens[i].AsNode();
34773552

3478-
base.VisitGlobalStatement(node);
3553+
if (asNode != null)
3554+
{
3555+
SyntaxKind kind = asNode.Kind();
3556+
3557+
switch (kind)
3558+
{
3559+
case SyntaxKind.ExpressionStatement:
3560+
VisitExpressionStatement((ExpressionStatementSyntax)asNode);
3561+
break;
3562+
case SyntaxKind.LocalDeclarationStatement:
3563+
VisitLocalDeclarationStatement((LocalDeclarationStatementSyntax)asNode);
3564+
break;
3565+
default:
3566+
Log.ErrorLine($"asNode : {kind}", _Options);
3567+
break;
3568+
}
3569+
}
3570+
else
3571+
{
3572+
SyntaxToken asToken = nodesAndTokens[i].AsToken();
3573+
SyntaxKind kind = asToken.Kind();
3574+
3575+
switch (kind)
3576+
{
3577+
default:
3578+
Log.ErrorLine($"asToken : {kind}", _Options);
3579+
break;
3580+
}
3581+
}
3582+
}
34793583
}
34803584
public override void VisitGotoStatement(GotoStatementSyntax node)
34813585
{
@@ -3710,10 +3814,62 @@ public override void VisitNameMemberCref(NameMemberCrefSyntax node)
37103814
}
37113815
public override void VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
37123816
{
3713-
if (_Options.Debug)
3714-
Log.WarningLine($"Not implemented or unlikely to be implemented. Calling base! ({node.FullSpan}|l:{_Line}|{node.FullSpan.Start - _Characters}-{node.FullSpan.End - _Characters})", _Options);
3817+
ChildSyntaxList nodesAndTokens = node.ChildNodesAndTokens();
3818+
3819+
for (int i = 0; i < nodesAndTokens.Count; i++)
3820+
{
3821+
SyntaxNode? asNode = nodesAndTokens[i].AsNode();
3822+
3823+
if (asNode != null)
3824+
{
3825+
SyntaxKind kind = asNode.Kind();
37153826

3716-
base.VisitNamespaceDeclaration(node);
3827+
switch (kind)
3828+
{
3829+
case SyntaxKind.DelegateDeclaration:
3830+
case SyntaxKind.InterfaceDeclaration:
3831+
case SyntaxKind.StructDeclaration:
3832+
break;
3833+
case SyntaxKind.QualifiedName:
3834+
case SyntaxKind.IdentifierName:
3835+
{
3836+
_NameSpaceStr = node.Name.ToString();
3837+
break;
3838+
}
3839+
case SyntaxKind.ClassDeclaration:
3840+
VisitClassDeclaration((ClassDeclarationSyntax)asNode);
3841+
break;
3842+
case SyntaxKind.EnumDeclaration:
3843+
VisitEnumDeclaration((EnumDeclarationSyntax)asNode);
3844+
break;
3845+
case SyntaxKind.NamespaceDeclaration:
3846+
VisitNamespaceDeclaration((NamespaceDeclarationSyntax)asNode);
3847+
break;
3848+
default:
3849+
Log.ErrorLine($"asNode : {kind}", _Options);
3850+
break;
3851+
}
3852+
}
3853+
else
3854+
{
3855+
SyntaxToken asToken = nodesAndTokens[i].AsToken();
3856+
SyntaxKind kind = asToken.Kind();
3857+
3858+
switch (kind)
3859+
{
3860+
//Todo? make a scope??? {...}
3861+
//OpenBraceToken and CloseBraceToken
3862+
case SyntaxKind.OpenBraceToken:
3863+
case SyntaxKind.CloseBraceToken:
3864+
case SyntaxKind.NamespaceKeyword:
3865+
break;
3866+
3867+
default:
3868+
Log.ErrorLine($"asToken : {kind}", _Options);
3869+
break;
3870+
}
3871+
}
3872+
}
37173873
}
37183874
public override void VisitNullableDirectiveTrivia(NullableDirectiveTriviaSyntax node)
37193875
{
@@ -4563,7 +4719,7 @@ public bool IdentifierToken(SyntaxNode node)
45634719
}*/
45644720
}
45654721

4566-
if (iSymbol.ContainingNamespace.ToString().Contains(_NameSpaceStr))
4722+
if (iSymbol.ContainingNamespace.ToString().Contains(_NameSpaceStr) && !_GlobalStatement)
45674723
{
45684724
if (iSymbol.Kind == SymbolKind.Parameter ||
45694725
iSymbol.Kind == SymbolKind.Local)

0 commit comments

Comments
 (0)