forked from bmx-ng/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iparser.bmx
1976 lines (1588 loc) · 44.2 KB
/
iparser.bmx
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
' Copyright (c) 2013-2021 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
' This software is provided 'as-is', without any express or implied
' warranty. In no event will the authors be held liable for any damages
' arising from the use of this software.
'
' Permission is granted to anyone to use this software for any purpose,
' including commercial applications, and to alter it and redistribute it
' freely, subject to the following restrictions:
'
' 1. The origin of this software must not be misrepresented; you must not
' claim that you wrote the original software. If you use this software
' in a product, an acknowledgment in the product documentation would be
' appreciated but is not required.
'
' 2. Altered source versions must be plainly marked as such, and must not be
' misrepresented as being the original software.
'
' 3. This notice may not be removed or altered from any source
' distribution.
'
'SuperStrict
'Import BRL.MaxUtil
'Import "parser_factory.bmx"
Const DECL_GLOBAL:Int = $10
Const DECL_FIELD:Int = $20
Const DECL_CONST:Int = $40
Const DECL_LOCAL:Int = $80
Type TIParser
Field _toker:TToker
Field _toke$
Field _tokeType:Int
Field _tokeSpace:Int
Field _tokerStack:TList=New TList'<TToker>
Method ParseModuleImport:Int(pmod:TModuleDecl, modpath:String, path:String, imp:String = Null, iData:String = Null, attrs:Int = 0, relPath:String = "", isFileImport:Int = 0)
Const STATE_CLASS:Int = 1
If Not modpath Then
modpath = imp
End If
' already imported??
If _appInstance.IsImported(modpath)
' add import to the scope (so we can find decls in it later)
' but don't add it if pmod is the apps' main module
If _appInstance.mainModule <> pmod Then
pmod.AddImport(modpath, _appInstance.globalImports.ValueForKey(modpath))
End If
Return False
Else If imp Then
' if "imp" is set, this is a file import. We need to check for it too, or we may end up importing it twice.
If _appInstance.IsImported(imp)
' add import to the scope (so we can find decls in it later)
' but don't add it if pmod is the apps' main module
If _appInstance.mainModule <> pmod Then
pmod.AddImport(imp, _appInstance.globalImports.ValueForKey(imp))
End If
Return False
End If
End If
Local prefix:String
If isFileImport And opt_buildtype = BUILDTYPE_APP Then
prefix = "m_"
End If
Local _mod:TModuleDecl = New TModuleDecl.Create(prefix + modpath, "bb" + modpath, path, attrs)
Select modpath
Case "brl.classes", "brl.blitzkeywords"
_mod.UpdateFilePath(_mod.filepath + "." + modpath)
End Select
_mod.declImported = True
_mod.relpath = relPath
_mod.pmod = pmod
If modpath = "brl.blitz" Then
If pmod.imported.Contains(modpath) Then
_mod = TModuleDecl(pmod.imported.ValueForKey(modpath))
Else
pmod.AddImport(modpath, _mod)
End If
' import Object and String definitions
Local par:TIParser = New TIParser
If FileType(modulepath("brl.blitz") + "\blitz_classes." + opt_platform + ".i") Then
par.ParseModuleImport(_mod, "brl.classes", modulepath("brl.blitz"), modulepath("brl.blitz") + "\blitz_classes." + opt_platform + ".i")
Else
par.ParseModuleImport(_mod, "brl.classes", modulepath("brl.blitz"), modulepath("brl.blitz") + "\blitz_classes.i")
End If
' set up built-in keywords
par = New TIParser
par.ParseModuleImport(_mod, "brl.blitzkeywords", "", "", MakeKeywords())
Else
pmod.AddImport(modpath, _mod)
End If
_appInstance.globalImports.Insert(modpath, _mod)
Local ipath:String
'Local ipath:String = path + "\" + ModuleIdent(modpath) + ".release.macos.x86.i"
If imp Then
ipath = imp
' add to imports
pmod.AddImport(ipath, _mod)
_appInstance.globalImports.Insert(ipath, _mod)
Else
ipath = path + "/" + ModuleIdent(modpath) + FileMung() + ".i"
End If
If Not iData Then
If Not FileType(ipath) Then
Err "Can't find interface for module '" + modpath + "'"
Return False
End If
'Local ifile:String[] = LoadString(ipath).Split("~n")
_toker = New TToker.Create( ipath,LoadString( ipath ) )
Else
_toker = New TToker.Create( ipath, iData)
End If
Local toker:TToker = _toker
Repeat
Local pos:Int
pos = toker._tokePos
toker.NextToke
Local line:Int
Local state:Int
Local class:TClassDecl
Local stm:String
Select toker.Toke().ToLower()
Case "superstrict"
_mod.attrs :| MODULE_SUPERSTRICT
Continue
Case "import"
toker.NextToke()
If toker.TokeType()=TOKE_SPACE toker.NextToke()
' skip non-module imports
If toker.TokeType()=TOKE_STRINGLIT
Local iRelPath:String = ParseStringLit()
SetErr
If iRelPath.EndsWith(".bmx") Then
Local origPath:String = RealPath(ExtractDir(path) + "/" + iRelPath)
Local iPath:String = OutputFilePath(origPath, FileMung(), "i")
If FileType( iPath )<>FILETYPE_FILE
Err "File '"+ iPath +"' not found."
EndIf
If _mod.imported.Contains( iPath ) Continue
Local modpath:String
If opt_buildtype = BUILDTYPE_MODULE Then
Local dir:String = ExtractDir(origPath).ToLower()
dir = dir[dir.findLast("/") + 1..]
If dir.EndsWith(".mod") Then
dir = ""
Else
dir :+ "_"
End If
Local file:String = StripDir(origPath).ToLower()
modpath = opt_modulename + "_" + dir + StripExt(file)
'sanitize the path, remove non-allowed chars
modpath = TStringHelper.Sanitize(modpath.ToLower())
Else
' todo file imports for apps
'internalErr
End If
' Local mdecl:TDecl=TDecl(pmod.GetDecl( modpath ))
' If Not mdecl
New TIParser.ParseModuleImport( _mod, modpath, origPath, iPath, , , iRelPath)
' Else
' _mod.imported.Insert(modpath, mdecl)
' EndIf
Else
If iRelPath.StartsWith("-") Then
If Not _mod.fileImports.Contains(iRelPath) Then
_mod.fileImports.AddLast(iRelPath)
End If
End If
End If
Else
Local m:String = toker._toke
toker.NextToke()
Parse(".")
m :+ "." + toker._toke
SetErr
Local mdecl:TDecl=TDecl(pmod.GetDecl( m ))
If Not mdecl
Local path:String = modulepath(m)
' parse the imported module
New TIParser.ParseModuleImport( _mod, m, path )
Else
_mod.AddImport(m, mdecl)
EndIf
End If
Continue
Case "moduleinfo"
toker.nextToke()
If toker.TokeType()=TOKE_SPACE toker.NextToke()
Continue
Case "~r", "~n"
Continue
Default
stm = toker.Toke()
Local v:String = toker.NextToke()
Select v
Case "^"
toker.rollback(pos)
toker.NextToke()
' class decl
class = ParseClassDecl( stm,0 )
class.declImported = True
Local parsed:Int
For Local i:Int = 0 Until _toke.Length
Select _toke[i]
Case Asc("F")
class.attrs :| DECL_FINAL
parsed = True
Case Asc("A")
class.attrs :| DECL_ABSTRACT
'ApplyFunctionAttributes(class, DECL_ABSTRACT)
parsed = True
Case Asc("S")
class.attrs :| CLASS_STRUCT
parsed = True
Case Asc("E")
class.attrs :| DECL_EXTERN
ApplyFunctionAttributes(class, DECL_EXTERN)
parsed = True
Case Asc("W")
If opt_platform = "win32" Then
class.attrs :| DECL_API_STDCALL
ApplyFunctionAttributes(class, DECL_API_STDCALL)
End If
parsed = True
Case Asc("I")
class.attrs :| CLASS_INTERFACE
ApplyFunctionAttributes(class, DECL_ABSTRACT)
parsed = True
Case Asc("G")
class.attrs :| CLASS_GENERIC
parsed = True
Case Asc("P")
class.attrs :| DECL_PRIVATE
parsed = True
End Select
Next
If parsed Then
NextToke
End If
If CParse( "=" )
If Not class.IsExtern() Then
class.munged=ParseStringLit()
If class.ident <> "String" Then
For Local fdecl:TFieldDecl = EachIn class._decls
fdecl.munged = "_" + class.munged + "_" + fdecl.ident
fdecl.munged = fdecl.munged.ToLower()
Next
End If
' process generic details
If class.attrs & CLASS_GENERIC Then
If _toke <> "," Then
Err "Syntax error - unexpected token '" + _toke + "'"
End If
NextToke
Parse "<"
Local args:TType[]
Local nargs:Int
Repeat
Local arg:TType = ParseType()
If args.Length=nargs args=args+ New TType[10]
args[nargs]=arg
nargs:+1
Until Not CParse(",")
args=args[..nargs]
Parse ">"
Parse "{"
' line no
If _tokeType <> TOKE_INTLIT Then
Err "Syntax error - unexpected token '" + _toke + "'"
End If
Local line:Int = _toke.ToInt()
NextToke
Parse ","
' source size
If _tokeType <> TOKE_INTLIT Then
Err "Syntax error - unexpected token '" + _toke + "'"
End If
Local size:Int = _toke.ToInt()
NextToke
Parse ","
' path
If _tokeType <> TOKE_STRINGLIT Then
Err "Syntax error - unexpected token '" + _toke + "'"
End If
Local path:String = BmxUnquote(_toke)
NextToke
Parse ","
' source
If _tokeType <> TOKE_STRINGLIT Then
Err "Syntax error - unexpected token '" + _toke + "'"
End If
Local source:String = BmxUnquote(_toke)
NextToke
Parse "}"
class.templateSource = TTemplateRecord.Load(line, path, size, source)
Local toker:TToker = New TToker.Create(path, class.templateSource.source, False, line)
Local parser:TParser = New TParser.Create( toker, _appInstance )
Local m:TModuleDecl = New TModuleDecl
parser._module = m
Local cdecl:TClassDecl = Null
Select parser._toke
Case "type"
cdecl = parser.ParseClassDecl(parser._toke,0)
Case "interface"
cdecl = parser.ParseClassDecl(parser._toke, CLASS_INTERFACE|DECL_ABSTRACT )
End Select
Local ty:TType = args[0]
Local genDecl:TClassDecl = TClassDecl(_mod.GetDecl(cdecl.IdentLower()))
If Not genDecl Then
genDecl = TClassDecl(pmod.GetDecl(cdecl.identLower()))
End If
If genDecl Then
If Not TIdentType(ty) Or (TIdentType(ty) And TIdentType(ty).ident <> "?") Then
cdecl = genDecl.GenClassInstance(args, True)
cdecl.scope.munged = class.munged
cdecl.scope.scope = _appInstance
End If
' don't add to module
class = Null
Else
class = cdecl
class.declImported = True
If Not TIdentType(ty) Or (TIdentType(ty) And TIdentType(ty).ident <> "?") Then
cdecl = class.GenClassInstance(args)
cdecl.declImported = True
_mod.munged = class.munged
End If
End If
End If
Else
Parse "0"
If Not class.munged Then
class.munged = class.ident
End If
End If
EndIf
If class Then
_mod.InsertDecl(class)
End If
'state = STATE_CLASS
'Exit
Case "\"
toker.rollback(pos)
toker.NextToke()
Local enumDecl:TEnumDecl = ParseEnumDecl( stm )
enumDecl.declImported = True
If CParse("F") Then
enumDecl.isFlags = True
End If
Parse("=")
If _tokeType <> TOKE_STRINGLIT Then
Err "Syntax error - unexpected token '" + _toke + "'"
End If
enumDecl.munged = BmxUnquote(_toke)
_mod.InsertDecl(enumDecl)
Default
If toker._tokeType = TOKE_EOF
Exit
End If
Local a:Int
Local ty:TType = ParseDeclType(a)
If CParse("(") Then
toker.rollback(pos)
toker.NextToke()
Local decl:TFuncDecl = ParseFuncDecl( _toke, 0 )
decl.declImported = True
' an array of function pointers?
If CParse( "&" ) Then
End If
If IstStaticArrayDef() Then
attrs :| DECL_STATIC
End If
While IsArrayDef()
ty = ParseArrayType(ty)
If CParse( "&" ) Then
End If
Wend
If decl.attrs & FUNC_PTR Then
Local fpty:TType = New TFunctionPtrType
TFunctionPtrType(fpty).func = decl
If TArrayType(ty) Then
TArrayType(ty).elemType = fpty
Else
ty = fpty
End If
'Local declInit:TExpr = decl.declInit
'decl.declInit = Null
Local gdecl:TGlobalDecl = New TGlobalDecl.Create( decl.ident,ty, Null, DECL_GLOBAL )
gdecl.munged = decl.munged
_mod.InsertDecl gdecl
gdecl.declImported = True
If CParse( "=" )
If CParse("mem")
If CParse(":")
If CParse("p")
If CParse("(") Then
gdecl.munged = ParseStringLit()
' for function pointers, ensure actual function reference is set too.
'If TFunctionPtrType(gdecl.declTy) Then
' TFunctionPtrType(gdecl.declTy).func.munged = gdecl.munged
'Else If TArrayType(gdecl.declTy) Then
'
'End If
TFunctionPtrType(fpty).func.munged = gdecl.munged
Parse(")")
EndIf
End If
Else
If CParse("(") Then
gdecl.munged = ParseStringLit()
Parse(")")
EndIf
End If
Else
If TStringType(ty)
If CParse("$") Then
gdecl.declInit = ParseUnaryExpr()
End If
Else
' a default value ?
gdecl.declInit = ParseUnaryExpr()
End If
End If
End If
Else
_mod.InsertDecl decl
End If
Else
toker.rollback(pos)
toker.NextToke()
Local decl:TDecl = ParseDecl( _toke, DECL_CONST | DECL_EXTERN)'DECL_GLOBAL | DECL_EXTERN )
_mod.InsertDecl decl
decl.declImported = True
End If
End Select
End Select
line :+ 1
Forever
' semant imported classes
For Local cdecl:TClassDecl = EachIn _mod.decls
cdecl.Semant()
If Not cdecl.args Then
cdecl.FinalizeClass()
End If
Next
Return True
End Method
Method ParseUnaryExpr:TExpr()
SkipEols
Local op$=_toke
Select op
Case "+","-","~~","not"
NextToke
Local expr:TExpr=ParseUnaryExpr()
Return New TUnaryExpr.Create( op,expr )
End Select
Return ParsePrimaryExpr( False )
End Method
Method ParsePrimaryExpr:TExpr( stmt:Int )
Local expr:TExpr
Select _tokeType
'Case TOKE_IDENT
' expr=New TIdentExpr.Create( ParseIdent() )
Case TOKE_INTLIT
expr=New TConstExpr.Create( New TIntType,_toke )
NextToke
Case TOKE_LONGLIT
expr=New TConstExpr.Create( New TLongType,_toke )
NextToke
Case TOKE_FLOATLIT
Local value:String = _toke
NextToke
If CParse("!") Then
expr=New TConstExpr.Create( New TDoubleType,value )
Else
CParse("#")
expr=New TConstExpr.Create( New TFloatType,value )
End If
Case TOKE_STRINGLIT
expr=New TConstExpr.Create( New TStringType,BmxUnquote( _toke ) )
NextToke
Case TOKE_IDENT
If _toke = "nan" Or _toke = "inf" Then
Local value:String
Select _toke
Case "inf"
value = "1.#INF0000"
Case "nan"
value = "-1.#IND0000"
End Select
NextToke
If CParse("!") Then
value :+ "00000000"
expr=New TConstExpr.Create( New TDoubleType,value )
Else
CParse("#")
expr=New TConstExpr.Create( New TFloatType,value )
End If
Else
Err "Syntax error - unexpected token '"+_toke+"'"
End If
Default
Err "Syntax error - unexpected token '"+_toke+"'"
End Select
Return expr
End Method
Method ParseClassDecl:TClassDecl( toke$,attrs:Int )
SetErr
'If toke Parse toke
Local id$=ParseIdent()
Local args:TTemplateArg[]
Local superTy:TIdentType
Local imps:TIdentType[]
If CParse( "^" )
If CParse( "null" )
superTy=Null
Else
superTy=ParseIdentType()
'If superTy.ident <> "Object" Then
' superTy = TIdentType(superTy.Semant())
'EndIf
EndIf
Else
superTy = New TIdentType.Create( "brl.classes.object" )
EndIf
' implements
If CParse("@") Then
Local nimps:Int
Repeat
If imps.Length=nimps imps=imps + New TIdentType[10]
imps[nimps]=ParseIdentType()
nimps:+1
Until Not CParse(",")
imps=imps[..nimps]
End If
Local classDecl:TClassDecl=New TClassDecl.Create( id,args,superTy,imps,attrs )
If classDecl.IsExtern()
classDecl.munged=classDecl.ident
If CParse( "=" ) classDecl.munged=ParseStringLit()
EndIf
'If classDecl.IsTemplateArg() Return classDecl
Local decl_attrs:Int=(attrs & DECL_EXTERN)
Local method_attrs:Int=decl_attrs
If attrs & CLASS_INTERFACE method_attrs:|DECL_ABSTRACT
Repeat
SkipEols
'If IsSpace(Asc(_toker._toke))
' _toker.NextToke
'End If
Select _toker._toke
Case "{"
'_toker.
NextToke
Case "}"
'_toker.
NextToke
Exit
Case "-" ' method
'DebugStop
'_toker.
NextToke
Local decl:TFuncDecl = ParseFuncDecl( _toke,method_attrs|FUNC_METHOD, ,classDecl )
decl.declImported = True
'If decl.IsCtor() decl.retTypeExpr=New TObjectType.Create( classDecl )
classDecl.InsertDecl decl
Case "+" ' function
NextToke
Local decl:TFuncDecl = ParseFuncDecl( _toke,method_attrs )
decl.declImported = True
'If decl.IsCtor() decl.retTypeExpr=New TObjectType.Create( classDecl )
classDecl.InsertDecl decl
Case ".", "@", "~~" ' field
Local d_attrs:Int = decl_attrs | DECL_FIELD
If _toker._toke = "." Then
NextToke
Else
While _toker._toke = "@" Or _toker._toke = "~~"
If _toker._toke = "@" Then
d_attrs :| DECL_READ_ONLY
End If
If _toker._toke = "~~" Then
d_attrs :| DECL_STATIC
End If
NextToke
Wend
End If
Local decl:TDecl= ParseDecl( _toke,d_attrs )
classDecl.InsertDecl decl
Rem
Case "private"
NextToke
decl_attrs=decl_attrs | DECL_PRIVATE
Case "public"
NextToke
decl_attrs=decl_attrs & ~DECL_PRIVATE
Case "const","global","field"
If (attrs & CLASS_INTERFACE) And _toke<>"const"
Err "Interfaces can only contain constants and methods."
EndIf
classDecl.InsertDecls ParseDecls( _toke,decl_attrs )
Case "method"
Local decl:TFuncDecl=ParseFuncDecl( _toke,method_attrs )
If decl.IsCtor() decl.retTypeExpr=New TObjectType.Create( classDecl )
classDecl.InsertDecl decl
Case "function"
If (attrs & CLASS_INTERFACE) And _toke<>"const"
Err "Interfaces can only contain constants and methods."
EndIf
Local decl:TFuncDecl=ParseFuncDecl( _toke,decl_attrs )
classDecl.InsertDecl decl
End Rem
'Default
' Err "Syntax error - expecting class member declaration."
End Select
If _toker._tokeType = TOKE_IDENT Then
' Const / Global?
'NextToke
'decl_attrs :| DECL_CONST
Local decl:TDecl= ParseDecl( _toke,decl_attrs | DECL_CONST)
classDecl.InsertDecl decl
decl.declImported = True
End If
Forever
If toke CParse toke
Return classDecl
End Method
Method ParseEnumDecl:TEnumDecl( toke$ )
SetErr
Local id$=ParseIdent()
Local ty:TType
Local attrs:Int
Parse( "\" )
ty=ParseDeclType(attrs, False)
Local enumDecl:TEnumDecl=New TEnumDecl.Create( id, ty, False, Null )
Local index:Int
Repeat
SkipEols
Select _toker._toke
Case "{"
NextToke
Case "}"
NextToke
Exit
Default
Local decl:TEnumValueDecl = ParseEnumValueDecl(_toke, index, ty)
enumDecl.InsertDecl decl
enumDecl.values :+ [decl]
index :+ 1
End Select
Forever
Return enumDecl
End Method
Method ParseEnumValueDecl:TEnumValueDecl(toke:String, index:Int, enumTy:TType)
Local id:String = ParseIdent()
Parse("=")
Local op:String
If _toke = "-" Then
op = "-"
NextToke
End If
Local expr:TExpr = New TConstExpr.Create( enumTy.Copy(), op + _toke )
Local valDecl:TEnumValueDecl = New TEnumValueDecl.Create(id, index, expr)
NextToke
Return valDecl
End Method
Method Parse( toke$ )
If Not CParse( toke )
Err "Syntax error - expecting '"+toke+"'."
EndIf
End Method
Method ParseIdent$()
Select _toker._toke.tolower()
Case "@" _toker.NextToke
Case "string","___array","object"
Case "?"
Default
If _toker._tokeType<>TOKE_IDENT Err "Syntax error - expecting identifier."
End Select
Local id$=_toker._toke
NextToke
Return id
End Method
Method ParseIdentType:TIdentType()
Local id$=ParseIdent()
While CParse( "." )
id:+"."+ParseIdent()
Wend
Local args:TType[]
If CParse( "<" )
Local nargs:Int
Repeat
Local arg:TType = ParseType()
If args.Length=nargs args=args+ New TType[10]
args[nargs]=arg
nargs:+1
Until Not CParse(",")
args=args[..nargs]
Parse ">"
EndIf
Return New TIdentType.Create( id,args )
End Method
Method NextToke$()
Local toke$=_toke
_tokeSpace=False
Repeat
_toke=_toker.NextToke()
_tokeType=_toker.TokeType()
If _tokeType<>TOKE_SPACE Exit
_tokeSpace=True
Forever
If _tokeType=TOKE_KEYWORD _toke=_toke.ToLower()
If toke="," SkipEols
Return _toke
End Method
Method CParse:Int( toke$ )
If _toker._toke.tolower()<>toke.tolower()
Return False
EndIf
'_toker.
NextToke
Return True
End Method
Method CParseToker:Int( toker:TToker, toke$ )
If toker._toke.ToLower()<>toke
Return False
EndIf
NextTokeToker(toker)
Return True
End Method
Method NextTokeToker$(toker:TToker)
Repeat
toker.NextToke()
Until toker.tokeType()<>TOKE_SPACE
Return toker._toke
End Method
Method SkipEols()
Local found:Int = True
While found
found = False
If CParse( "~n" )
found = True
End If
If CParse("~r")
found = True
End If
Wend
SetErr
End Method
Method ParseStringLit$()
If _toker._tokeType<>TOKE_STRINGLIT Err "Expecting string literal."
Local str$=BmxUnquote( _toker._toke )
'_toker.
NextToke
Return str
End Method
Method ParseFuncDecl:TFuncDecl( toke$,attrs:Int, returnType:TType = Null, classDecl:TClassDecl = Null )
SetErr
'If toke Parse toke
Local id$
Local ty:TType
Local meth:Int = attrs & FUNC_METHOD
If Not returnType Then
If attrs & FUNC_METHOD
If _toker._toke.tolower() = "new"
If attrs & DECL_EXTERN
Err "Extern classes cannot have constructors"
EndIf
id=_toker._toke
NextToke
attrs:|FUNC_CTOR
attrs:&~FUNC_METHOD