-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCParser.cls
More file actions
1701 lines (1339 loc) · 49.1 KB
/
Copy pathCParser.cls
File metadata and controls
1701 lines (1339 loc) · 49.1 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CParser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'Author: David Zimmer <dzzie@yahoo.com>
'AI: Claude.ai
'Site: http://sandsprite.com
'License: MIT
'todo: barfing on parsing regex literals, and named for loops like outer: for(){ (see es5.txt
'------------------------------------------------------------
' CParser.cls - JavaScript Recursive Descent Parser
'
' "To infinity and beyond!" - Still Buzz
'
' This is where the magic happens. Tokens ? AST.
' Recursive descent parsing. In VB6. We're INSANE.
'------------------------------------------------------------
Option Explicit
Private scanner As CScanner
Private lookahead As CToken
Private lastToken As CToken
Private Sub Class_Initialize()
Set scanner = New CScanner
End Sub
Friend Function ParseSingleExpression(Source As String) As CNode
scanner.Init Source
Set lookahead = scanner.Lex()
Set ParseSingleExpression = ParseExpression()
End Function
Public Function ParseScript(Source As String) As CNode
On Error GoTo ErrorHandler
scanner.Init Source
Set lookahead = scanner.Lex()
Dim program As New CNode
Dim stmt As CNode
' Force initialization by accessing a property first
program.LineNumber = 1
program.tType = Program_Node
' Parse all top-level statements
Do While lookahead.tType <> EOF_TOKEN
Set stmt = ParseStatementListItem()
If stmt Is Nothing Then
Err.Raise vbObjectError + 2001, "CParser", "ParseStatementListItem returned Nothing"
End If
program.Body.add stmt
Loop
Set ParseScript = program
Exit Function
ErrorHandler:
Debug.Print "ERROR in ParseScript: " & Err.description
Debug.Print "Error Number: " & Err.Number
Debug.Print "Current token: " & lookahead.value & " (type " & lookahead.tType & ")"
Err.Raise Err.Number, Err.Source, Err.description
End Function
' ============================================
' Token Management
' ============================================
Private Function Expect(tokenValue As String) As CToken
Dim token As CToken
Set token = lookahead
If lookahead.tType <> Punctuator And lookahead.tType <> Keyword Then
ThrowUnexpectedToken lookahead
End If
If lookahead.value <> tokenValue Then
ThrowUnexpectedToken lookahead
End If
Set lastToken = lookahead
Set lookahead = scanner.Lex()
Set Expect = token
End Function
Private Function match(tokenValue As String) As Boolean
match = (lookahead.tType = Punctuator Or lookahead.tType = Keyword) _
And lookahead.value = tokenValue
End Function
Private Function MatchKeyword(kw As String) As Boolean
MatchKeyword = (lookahead.tType = Keyword) And (lookahead.value = kw)
End Function
Private Sub ThrowUnexpectedToken(token As CToken)
Dim column As Long
column = token.Start - token.LineStart + 1 ' +1 for 1-based column
Err.Raise vbObjectError + 2000, "CParser", _
"Unexpected token: " & token.value & " at line " & token.LineNumber & ", column " & column
End Sub
' ============================================
' Statement Parsing
' ============================================
Private Function ParseStatementListItem() As CNode
On Error GoTo ErrorHandler
If lookahead.tType = Keyword Then
Select Case lookahead.value
Case "function"
Set ParseStatementListItem = ParseFunctionDeclaration()
Case Else
Set ParseStatementListItem = ParseStatement()
End Select
Else
Set ParseStatementListItem = ParseStatement()
End If
Exit Function
ErrorHandler:
Debug.Print "ERROR in ParseStatementListItem"
Debug.Print " Token: '" & lookahead.value & "' (type " & lookahead.tType & ")"
Err.Raise Err.Number, "ParseStatementListItem", Err.description
End Function
Private Function ParseStatement() As CNode
On Error GoTo ErrorHandler
Dim stmt As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' CAPTURE LINE NUMBER
Select Case lookahead.tType
Case Punctuator
Select Case lookahead.value
Case ";"
Set stmt = ParseEmptyStatement()
Case "{"
Set stmt = ParseBlockStatement()
Case Else
Set stmt = ParseExpressionStatement()
End Select
Case Keyword
Select Case lookahead.value
Case "var"
Set stmt = ParseVariableStatement()
Case "if"
Set stmt = ParseIfStatement()
Case "while"
Set stmt = ParseWhileStatement()
Case "for"
Set stmt = ParseForStatement()
Case "return"
Set stmt = ParseReturnStatement()
Case "break"
Set stmt = ParseBreakStatement()
Case "continue"
Set stmt = ParseContinueStatement()
Case "throw"
Set stmt = ParseThrowStatement()
Case "try"
Set stmt = ParseTryStatement()
Case "switch"
Set stmt = ParseSwitchStatement()
Case "do"
Set stmt = ParseDoWhileStatement()
Case "debugger"
Set stmt = ParseDebuggerStatement() ' ADD THIS
Case "with"
Set stmt = ParseWithStatement()
Case Else
Set stmt = ParseExpressionStatement()
End Select
Case Else
Set stmt = ParseExpressionStatement()
End Select
' SET LINE NUMBER ON NODE
If Not stmt Is Nothing Then
stmt.LineNumber = startLine
End If
Set ParseStatement = stmt
Exit Function
ErrorHandler:
Debug.Print "ERROR in ParseStatement"
Debug.Print " Token: '" & lookahead.value & "' (type " & lookahead.tType & ")"
Err.Raise Err.Number, "ParseStatement", Err.description
End Function
Private Function ParseWithStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of identifier
Expect "with"
Expect "("
Dim Node As New CNode
Node.tType = EmptyStatement_Node ' Just skip for navigation
Node.LineNumber = startLine
' Parse the object expression (but ignore it)
Dim obj As CNode
Set obj = ParseExpression()
Expect ")"
' Parse the body (but ignore it for navigation)
Dim Body As CNode
Set Body = ParseStatement()
' Just return empty - 'with' is bad practice anyway
Set ParseWithStatement = Node
End Function
Private Function ParseDebuggerStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of identifier
Expect "debugger"
If match(";") Then Expect ";"
Dim Node As New CNode
Node.tType = EmptyStatement_Node ' Just treat as empty for navigation
Node.LineNumber = startLine
Set ParseDebuggerStatement = Node
End Function
Private Function ParseEmptyStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of identifier
Expect ";"
Dim Node As New CNode
Node.tType = EmptyStatement_Node
Node.LineNumber = startLine
Set ParseEmptyStatement = Node
End Function
Private Function ParseBlockStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of identifier
Expect "{"
Dim Node As New CNode
Node.tType = BlockStatement_Node
Node.LineNumber = startLine
Do While Not match("}")
Node.Body.add ParseStatementListItem()
Loop
Expect "}"
Set ParseBlockStatement = Node
End Function
Private Function ParseExpressionStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of identifier
Dim expr As CNode
Set expr = ParseExpression()
' Consume optional semicolon
If match(";") Then
Expect ";"
End If
Dim Node As New CNode
Node.tType = ExpressionStatement_Node
Node.LineNumber = startLine
Set Node.Test = expr ' Reuse Test field for expression
Set ParseExpressionStatement = Node
End Function
Private Function ParseVariableStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of identifier
Expect "var"
Dim Node As New CNode
Node.tType = VariableDeclaration_Node
Node.Kind = "var"
Node.LineNumber = startLine
' Parse declarators
Do
Node.Declarations.add ParseVariableDeclarator()
If Not match(",") Then Exit Do
Expect ","
Loop
' Consume optional semicolon
If match(";") Then
Expect ";"
End If
Set ParseVariableStatement = Node
End Function
Private Function ParseVariableDeclarator() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of identifier
Dim Node As New CNode
Node.tType = VariableDeclarator_Node
Node.LineNumber = startLine
' Parse identifier
If lookahead.tType <> Identifier Then
ThrowUnexpectedToken lookahead
End If
Set Node.VarID = ParsePrimaryExpression() ' Gets identifier
' Parse initializer if present
If match("=") Then
Expect "="
Set Node.Init = ParseAssignmentExpression()
End If
Set ParseVariableDeclarator = Node
End Function
Private Function ParseIfStatement() As CNode
Dim ln As Long
ln = lookahead.LineNumber
Expect "if"
Expect "("
Dim Node As New CNode
Node.tType = IfStatement_Node
Node.LineNumber = ln
Set Node.IfTest = ParseExpression()
Expect ")"
Set Node.IfConsequent = ParseStatement()
' Check for else clause
If MatchKeyword("else") Then
Expect "else"
Set Node.IfAlternate = ParseStatement()
End If
Set ParseIfStatement = Node
End Function
Private Function ParseWhileStatement() As CNode
Dim ln As Long
ln = lookahead.LineNumber
Expect "while"
Expect "("
Dim Node As New CNode
Node.tType = WhileStatement_Node
Node.LineNumber = ln
Set Node.WhileTest = ParseExpression()
Expect ")"
Set Node.WhileBody = ParseStatement()
Set ParseWhileStatement = Node
End Function
Private Function ParseDoWhileStatement() As CNode
Dim ln As Long
ln = lookahead.LineNumber
Expect "do"
Dim Node As New CNode
Node.tType = DoWhileStatement_Node
Node.LineNumber = ln
Set Node.WhileBody = ParseStatement()
Expect "while"
Expect "("
Set Node.WhileTest = ParseExpression()
Expect ")"
If match(";") Then Expect ";"
Set ParseDoWhileStatement = Node
End Function
Private Function ParseForStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of "for"
Expect "for"
Expect "("
Dim Node As New CNode
Dim varDecl As CNode
Dim declNode As CNode
Dim seqNode As CNode
If MatchKeyword("var") Then
Dim varStartLine As Long
varStartLine = lookahead.LineNumber ' Capture line of "var"
' Parse var declaration (without semicolon)
Expect "var"
Set varDecl = New CNode
varDecl.tType = VariableDeclaration_Node
varDecl.Kind = "var"
varDecl.LineNumber = varStartLine
' Parse the declarator
If lookahead.tType <> Identifier Then
ThrowUnexpectedToken lookahead
End If
Dim declStartLine As Long
declStartLine = lookahead.LineNumber ' Capture line of identifier
Set declNode = New CNode
declNode.tType = VariableDeclarator_Node
declNode.LineNumber = declStartLine
Set declNode.VarID = ParsePrimaryExpression()
' Now check what comes next
If MatchKeyword("in") Then
' for-in with var
varDecl.Declarations.add declNode
Node.tType = ForInStatement_Node
Node.LineNumber = startLine
Set Node.Left = varDecl
Expect "in"
Set Node.Right = ParseExpression()
Expect ")"
Set Node.ForBody = ParseStatement()
Set ParseForStatement = Node
Exit Function
ElseIf match("=") Then
' Has initializer, must be regular for
Expect "="
Set declNode.Init = ParseAssignmentExpression()
varDecl.Declarations.add declNode
' Continue with regular for loop logic
Do While match(",")
Expect ","
varDecl.Declarations.add ParseVariableDeclarator()
Loop
Set Node.ForInit = varDecl
Else
' No initializer, no 'in' - regular for with just declaration
varDecl.Declarations.add declNode
Do While match(",")
Expect ","
varDecl.Declarations.add ParseVariableDeclarator()
Loop
Set Node.ForInit = varDecl
End If
ElseIf Not match(";") Then
' Could be:
' - for (i = 0, j = 1; ...) - comma-separated assignments
' - for (i in obj) - for-in loop
' - for (expr; ...) - single expression
' Parse first expression - use UnaryExpression to avoid consuming 'in'
Dim firstExpr As CNode
Set firstExpr = ParseUnaryExpression() ' Stops before 'in' operator
' Check if this is for-in
If MatchKeyword("in") Then
' for-in without var
Node.tType = ForInStatement_Node
Node.LineNumber = startLine
Set Node.Left = firstExpr
Expect "in"
Set Node.Right = ParseExpression()
Expect ")"
Set Node.ForBody = ParseStatement()
Set ParseForStatement = Node
Exit Function
End If
' Not for-in - we may have only parsed part of the expression
' We need to continue parsing to get the full expression
' Check for assignment operators
If lookahead.tType = Punctuator And IsAssignmentOperator(lookahead.value) Then
Dim assignStartLine As Long
assignStartLine = firstExpr.LineNumber ' Use line from left expr
Dim op As String
op = lookahead.value
Expect op
Dim assignNode As New CNode
assignNode.tType = AssignmentExpression_Node
assignNode.operator = op
assignNode.LineNumber = assignStartLine
Set assignNode.Left = firstExpr
Set assignNode.Right = ParseAssignmentExpression()
Set firstExpr = assignNode
End If
' Check for comma-separated expressions
If match(",") Then
Dim seqStartLine As Long
seqStartLine = firstExpr.LineNumber ' Use line from first expr
' Multiple expressions: i = 0, j = 1, ...
Dim exprs As New Collection
exprs.add firstExpr
Do While match(",")
Expect ","
exprs.add ParseAssignmentExpression()
Loop
' Create sequence expression
Set seqNode = New CNode
seqNode.tType = SequenceExpression_Node
seqNode.LineNumber = seqStartLine
Set seqNode.Body = exprs
Set Node.ForInit = seqNode
Else
' Single expression
Set Node.ForInit = firstExpr
End If
End If
Expect ";"
If Not match(";") Then
Set Node.ForTest = ParseExpression()
End If
Expect ";"
If Not match(")") Then
Set Node.ForUpdate = ParseExpression()
End If
Expect ")"
Node.tType = ForStatement_Node
Node.LineNumber = startLine ' Set line for for statement
Set Node.ForBody = ParseStatement()
Set ParseForStatement = Node
End Function
Private Function ParseReturnStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber
Expect "return"
Dim Node As New CNode
Node.tType = ReturnStatement_Node
Node.LineNumber = startLine
' Check for return value
If Not match(";") And lookahead.tType <> EOF_TOKEN Then
Set Node.ReturnArgument = ParseExpression()
End If
If match(";") Then Expect ";"
Set ParseReturnStatement = Node
End Function
Private Function ParseBreakStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber
Expect "break"
If match(";") Then Expect ";"
Dim Node As New CNode
Node.tType = BreakStatement_Node
Node.LineNumber = startLine
Set ParseBreakStatement = Node
End Function
Private Function ParseContinueStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber
Expect "continue"
If match(";") Then Expect ";"
Dim Node As New CNode
Node.tType = ContinueStatement_Node
Node.LineNumber = startLine
Set ParseContinueStatement = Node
End Function
Private Function ParseThrowStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber
Expect "throw"
Dim Node As New CNode
Node.tType = ThrowStatement_Node
Node.LineNumber = startLine
Set Node.ReturnArgument = ParseExpression()
If match(";") Then Expect ";"
Set ParseThrowStatement = Node
End Function
Private Function ParseTryStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber ' Capture line of "try"
Expect "try"
Dim Node As New CNode
Node.tType = TryStatement_Node
Node.LineNumber = startLine ' Set line for try statement
Set Node.TryBlock = ParseBlockStatement()
If MatchKeyword("catch") Then
Dim catchStartLine As Long
catchStartLine = lookahead.LineNumber ' Capture line of "catch"
Expect "catch"
Expect "("
Dim handler As New CNode
handler.tType = CatchClause_Node
handler.LineNumber = catchStartLine ' Set line for catch clause
Set handler.param = ParsePrimaryExpression()
Expect ")"
Set handler.CatchBody = ParseBlockStatement()
Set Node.TryHandler = handler
End If
If MatchKeyword("finally") Then
Expect "finally"
Set Node.TryFinalizer = ParseBlockStatement()
End If
Set ParseTryStatement = Node
End Function
Private Function ParseSwitchStatement() As CNode
Dim startLine As Long
startLine = lookahead.LineNumber
Expect "switch"
Expect "("
Dim Node As New CNode
Dim switchCase As New CNode
Node.tType = SwitchStatement_Node
Node.LineNumber = startLine ' Set line for switch statement
Set Node.discriminant = ParseExpression()
Expect ")"
Expect "{"
Do While Not match("}")
Dim caseStartLine As Long
caseStartLine = lookahead.LineNumber ' Capture line of "case" or "default"
Set switchCase = New CNode
switchCase.tType = SwitchCase_Node
switchCase.LineNumber = caseStartLine ' Set line for each case
If MatchKeyword("default") Then
Expect "default"
Expect ":"
' Test is Nothing for default
Else
Expect "case"
Set switchCase.CaseTest = ParseExpression()
Expect ":"
End If
' Parse consequent statements
Do While Not match("}") And Not MatchKeyword("case") And Not MatchKeyword("default")
switchCase.CaseConsequent.add ParseStatementListItem()
Loop
Node.Cases.add switchCase
Loop
Expect "}"
Set ParseSwitchStatement = Node
End Function
' ============================================
' Expression Parsing - The Fun Part!
' ============================================
Friend Function ParseExpression() As CNode 'used in new Function()
' Sequence expression (comma operator)
Dim expr As CNode, startLine As Long
Set expr = ParseAssignmentExpression()
If match(",") Then
Dim exprs As New Collection
exprs.add expr
startLine = lookahead.LineNumber
Do While match(",")
Expect ","
exprs.add ParseAssignmentExpression()
Loop
Dim Node As New CNode
Node.tType = SequenceExpression_Node
Node.LineNumber = startLine
Set Node.Body = exprs
Set expr = Node
End If
Set ParseExpression = expr
End Function
Private Function ParseAssignmentExpression() As CNode
Dim expr As CNode, startLine As Long
Set expr = ParseConditionalExpression()
' Check for assignment operators
If lookahead.tType = Punctuator Then
If IsAssignmentOperator(lookahead.value) Then
Dim op As String
op = lookahead.value
startLine = lookahead.LineNumber
Expect op
Dim Node As New CNode
Node.tType = AssignmentExpression_Node
Node.operator = op
Node.LineNumber = startLine
Set Node.Left = expr
Set Node.Right = ParseAssignmentExpression()
Set expr = Node
End If
End If
Set ParseAssignmentExpression = expr
End Function
Private Function IsAssignmentOperator(op As String) As Boolean
IsAssignmentOperator = (op = "=" Or op = "+=" Or op = "-=" Or _
op = "*=" Or op = "/=" Or op = "%=" Or _
op = "<<=" Or op = ">>=" Or op = ">>>=" Or _
op = "&=" Or op = "|=" Or op = "^=")
End Function
Private Function ParseConditionalExpression() As CNode
Dim expr As CNode, startLine As Long
Set expr = ParseLogicalORExpression()
' Ternary operator: test ? consequent : alternate
If match("?") Then
startLine = lookahead.LineNumber
Expect "?"
Dim Consequent As CNode
Set Consequent = ParseAssignmentExpression()
Expect ":"
Dim Alternate As CNode
Set Alternate = ParseAssignmentExpression()
Dim Node As New CNode
Node.tType = ConditionalExpression_Node
Set Node.Test = expr
Node.LineNumber = startLine
Set Node.Consequent = Consequent
Set Node.Alternate = Alternate
Set expr = Node
End If
Set ParseConditionalExpression = expr
End Function
' ============================================
' Binary Expression Precedence Climbing
' ============================================
Private Function ParseLogicalORExpression() As CNode
Dim expr As CNode, Node As New CNode, startLine As Long
Set expr = ParseLogicalANDExpression()
Do While match("||")
Dim op As String
op = lookahead.value
startLine = lookahead.LineNumber
Expect op
Set Node = New CNode
Node.tType = LogicalExpression_Node
Node.LineNumber = startLine
Node.operator = op
Set Node.Left = expr
Set Node.Right = ParseLogicalANDExpression()
Set expr = Node
Loop
Set ParseLogicalORExpression = expr
End Function
Private Function ParseLogicalANDExpression() As CNode
Dim expr As CNode, Node As New CNode, startLine As Long
Set expr = ParseBitwiseORExpression()
Do While match("&&")
Dim op As String
op = lookahead.value
startLine = lookahead.LineNumber
Expect op
Set Node = New CNode
Node.tType = LogicalExpression_Node
Node.operator = op
Node.LineNumber = startLine
Set Node.Left = expr
Set Node.Right = ParseBitwiseORExpression()
Set expr = Node
Loop
Set ParseLogicalANDExpression = expr
End Function
Private Function ParseBitwiseORExpression() As CNode
Dim expr As CNode, Node As New CNode, startLine As Long
Set expr = ParseBitwiseXORExpression()
Do While match("|")
Dim op As String
op = lookahead.value
startLine = lookahead.LineNumber
Expect op
Set Node = New CNode
Node.tType = BinaryExpression_Node
Node.operator = op
Node.LineNumber = startLine
Set Node.Left = expr
Set Node.Right = ParseBitwiseXORExpression()
Set expr = Node
Loop
Set ParseBitwiseORExpression = expr
End Function
Private Function ParseBitwiseXORExpression() As CNode
Dim expr As CNode, Node As New CNode, startLine As Long
Set expr = ParseBitwiseANDExpression()
Do While match("^")
Dim op As String
op = lookahead.value
startLine = lookahead.LineNumber
Expect op
Set Node = New CNode
Node.tType = BinaryExpression_Node
Node.operator = op
Node.LineNumber = startLine
Set Node.Left = expr
Set Node.Right = ParseBitwiseANDExpression()
Set expr = Node
Loop
Set ParseBitwiseXORExpression = expr
End Function
Private Function ParseBitwiseANDExpression() As CNode
Dim expr As CNode, Node As New CNode, startLine As Long
Set expr = ParseEqualityExpression()
Do While match("&")
Dim op As String
startLine = lookahead.LineNumber
op = lookahead.value
Expect op
Set Node = New CNode
Node.tType = BinaryExpression_Node
Node.operator = op
Node.LineNumber = startLine
Set Node.Left = expr
Set Node.Right = ParseEqualityExpression()
Set expr = Node
Loop
Set ParseBitwiseANDExpression = expr
End Function
Private Function ParseEqualityExpression() As CNode
Dim expr As CNode, Node As New CNode, startLine As Long
Set expr = ParseRelationalExpression()
Do While match("==") Or match("!=") Or match("===") Or match("!==")
Dim op As String
startLine = lookahead.LineNumber
op = lookahead.value
Expect op
Set Node = New CNode
Node.tType = BinaryExpression_Node
Node.operator = op
Node.LineNumber = startLine
Set Node.Left = expr
Set Node.Right = ParseRelationalExpression()
Set expr = Node
Loop
Set ParseEqualityExpression = expr
End Function
Private Function ParseRelationalExpression() As CNode
Dim expr As CNode, Node As New CNode, startLine As Long
Set expr = ParseShiftExpression()
Do While match("<") Or match(">") Or match("<=") Or match(">=") Or _
MatchKeyword("instanceof") Or MatchKeyword("in")
Dim op As String
startLine = lookahead.LineNumber
op = lookahead.value
Expect op
Set Node = New CNode
Node.tType = BinaryExpression_Node
Node.operator = op
Node.LineNumber = startLine
Set Node.Left = expr
Set Node.Right = ParseShiftExpression()
Set expr = Node
Loop
Set ParseRelationalExpression = expr
End Function
Private Function ParseShiftExpression() As CNode
Dim expr As CNode, Node As New CNode, startLine As Long
Set expr = ParseAdditiveExpression()
Do While match("<<") Or match(">>") Or match(">>>")
Dim op As String
op = lookahead.value
startLine = lookahead.LineNumber
Expect op
Set Node = New CNode
Node.tType = BinaryExpression_Node
Node.operator = op
Node.LineNumber = startLine
Set Node.Left = expr
Set Node.Right = ParseAdditiveExpression()
Set expr = Node