|
9 | 9 | using System.Linq; |
10 | 10 | using System.Reflection; |
11 | 11 | using System.Text; |
12 | | -using System.Xml.Linq; |
13 | 12 |
|
14 | 13 |
|
15 | 14 | namespace CSharpToJavaScript; |
@@ -40,9 +39,10 @@ internal class Walker : CSharpSyntaxWalker |
40 | 39 | private bool _IgnoreArrayType = false; |
41 | 40 | private bool _IgnoreAsParenthesis = false; |
42 | 41 | private bool _IgnoreTailingDot = false; |
| 42 | + private bool _GlobalStatement = false; |
43 | 43 |
|
44 | | - private int _EnumMembers = 0; |
45 | 44 |
|
| 45 | + private int _EnumMembers = 0; |
46 | 46 |
|
47 | 47 | //Debug |
48 | 48 | private int _Line = 0; |
@@ -265,36 +265,6 @@ public override void Visit(SyntaxNode? node) |
265 | 265 | { |
266 | 266 | case SyntaxKind.UsingDirective: |
267 | 267 | 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 | | - } |
298 | 268 | default: |
299 | 269 | //CSTOJS.Log($"{syntaxKind}"); |
300 | 270 | break; |
@@ -3098,10 +3068,64 @@ public override void VisitCollectionExpression(CollectionExpressionSyntax node) |
3098 | 3068 | } |
3099 | 3069 | public override void VisitCompilationUnit(CompilationUnitSyntax node) |
3100 | 3070 | { |
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 | + } |
3103 | 3120 |
|
3104 | | - base.VisitCompilationUnit(node); |
| 3121 | + break; |
| 3122 | + } |
| 3123 | + default: |
| 3124 | + Log.ErrorLine($"asToken : {kind}", _Options); |
| 3125 | + break; |
| 3126 | + } |
| 3127 | + } |
| 3128 | + } |
3105 | 3129 | } |
3106 | 3130 | public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node) |
3107 | 3131 | { |
@@ -3388,10 +3412,58 @@ public override void VisitFieldExpression(FieldExpressionSyntax node) |
3388 | 3412 | } |
3389 | 3413 | public override void VisitFileScopedNamespaceDeclaration(FileScopedNamespaceDeclarationSyntax node) |
3390 | 3414 | { |
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(); |
3393 | 3455 |
|
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 | + } |
3395 | 3467 | } |
3396 | 3468 | public override void VisitFinallyClause(FinallyClauseSyntax node) |
3397 | 3469 | { |
@@ -3472,10 +3544,42 @@ public override void VisitFunctionPointerUnmanagedCallingConventionList(Function |
3472 | 3544 | } |
3473 | 3545 | public override void VisitGlobalStatement(GlobalStatementSyntax node) |
3474 | 3546 | { |
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(); |
3477 | 3552 |
|
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 | + } |
3479 | 3583 | } |
3480 | 3584 | public override void VisitGotoStatement(GotoStatementSyntax node) |
3481 | 3585 | { |
@@ -3710,10 +3814,62 @@ public override void VisitNameMemberCref(NameMemberCrefSyntax node) |
3710 | 3814 | } |
3711 | 3815 | public override void VisitNamespaceDeclaration(NamespaceDeclarationSyntax node) |
3712 | 3816 | { |
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(); |
3715 | 3826 |
|
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 | + } |
3717 | 3873 | } |
3718 | 3874 | public override void VisitNullableDirectiveTrivia(NullableDirectiveTriviaSyntax node) |
3719 | 3875 | { |
@@ -4563,7 +4719,7 @@ public bool IdentifierToken(SyntaxNode node) |
4563 | 4719 | }*/ |
4564 | 4720 | } |
4565 | 4721 |
|
4566 | | - if (iSymbol.ContainingNamespace.ToString().Contains(_NameSpaceStr)) |
| 4722 | + if (iSymbol.ContainingNamespace.ToString().Contains(_NameSpaceStr) && !_GlobalStatement) |
4567 | 4723 | { |
4568 | 4724 | if (iSymbol.Kind == SymbolKind.Parameter || |
4569 | 4725 | iSymbol.Kind == SymbolKind.Local) |
|
0 commit comments