forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceParser.pb
3917 lines (3250 loc) · 129 KB
/
SourceParser.pb
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) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
;
; Functions for scanning the sourcecode and maintaining a list of source
; tokens for autocomplete, variableviewer, procedurebrowser etc
;
Global Dim FoldStartVT.l(#MaxSizeHT)
Global Dim FoldEndVT.l(#MaxSizeHT)
Global Dim FoldStartVT2.l(#MaxSizeHT) ; indicates the last entry starting with the given char
Global Dim FoldEndVT2.l(#MaxSizeHT)
; Marks the basic keywords that have a fold status (to avoid double scanning)
; 1=fold start
; 2=fold end
Global Dim FoldKeywords.l(#NbBasicKeywords)
Global Dim ForwardMatches.l(#NbBasicKeywords, 0)
Global Dim BackwardMatches.l(#NbBasicKeywords, 0)
Global KeywordMatchesBuild = 0
Global NewList KeywordStack.l()
; --------------------------------------------------------------------
; some global parser state to keep while scanning (for simplicity)
Global Parser_Encoding
Global Parser_CurrentLine
Global *Parser_LineStart
Global *Parser_Array.ParsedLines
Global Parser_IgnoreCommentItems
; current line and line start must be remembered
Structure ParserStack
CurrentLine.l
*LineStart
EndStructure
Global NewList ParserStack.ParserStack()
Procedure Parser_PushState()
AddElement(ParserStack())
ParserStack()\CurrentLine = Parser_CurrentLine
ParserStack()\LineStart = *Parser_LineStart
Parser_IgnoreCommentItems = #True ; true while the stack is pushed (lookahead mode)
EndProcedure
Procedure Parser_PopState()
Parser_CurrentLine = ParserStack()\CurrentLine
*Parser_LineStart = ParserStack()\LineStart
DeleteElement(ParserStack())
If ListSize(ParserStack()) = 0
Parser_IgnoreCommentItems = #False ; record fold marks and comment markers again
EndIf
EndProcedure
; --------------------------------------------------------------------
; These are macros even though they are long, because of speed for large sources
; Used for scanning the parsed data
;
Macro Parser_FirstItem(_Parser, _Item, _Line)
_Line = 0
_Item = _Parser\SourceItemArray\Line[0]\First
While _Item = 0 And _Line < _Parser\SourceItemCount-1
_Line + 1
_Item = _Parser\SourceItemArray\Line[_Line]\First
Wend
EndMacro
Macro Parser_PreviousItem(_Parser, _Item, _Line)
If _Item\Previous
_Item = _Item\Previous
Else
_Item = 0
While _Item = 0 And _Line > 0
_Line - 1
_Item = _Parser\SourceItemArray\Line[_Line]\Last
Wend
EndIf
EndMacro
Macro Parser_NextItem(_Parser, _Item, _Line)
If _Item\Next
_Item = _Item\Next
Else
_Item = 0
While _Item = 0 And _Line < _Parser\SourceItemCount-1
_Line + 1
_Item = _Parser\SourceItemArray\Line[_Line]\First
Wend
EndIf
EndMacro
; --------------------------------------------------------------------
Procedure BuildFoldingVT()
; This needs to be done only once (it MUST be done only once)
If KeywordMatchesBuild = 0
KeywordMatchesBuild = 1
; calculate the matches for every keyword, and the max match
MaxForward = 0
MaxBackward = 0
Restore KeywordMatches
Repeat
Read.l Keyword
Read.l Match
If Keyword <> 0
ForwardMatches(Keyword, 0) + 1
BackwardMatches(Match, 0) + 1
If ForwardMatches(Keyword, 0) > MaxForward
MaxForward = ForwardMatches(Keyword, 0)
EndIf
If BackwardMatches(Match, 0) > MaxBackward
MaxBackward = BackwardMatches(Match, 0)
EndIf
EndIf
Until Keyword = 0
; redim arrays (and reset all counts to 0)
Dim ForwardMatches(#NbBasicKeywords, MaxForward)
Dim BackwardMatches(#NbBasicKeywords, MaxBackward)
; now fill arrays
Restore KeywordMatches
Repeat
Read.l Keyword
Read.l Match
If Keyword <> 0
ForwardMatches(Keyword, 0) + 1
ForwardMatches(Keyword, ForwardMatches(Keyword, 0)) = Match
BackwardMatches(Match, 0) + 1
BackwardMatches(Match, BackwardMatches(Match, 0)) = Keyword
EndIf
Until Keyword = 0
EndIf
IsMacroFolding = 0
For i = 0 To 255
FoldStartVT(i) = 0
FoldEndVT(i) = 0
Next i
For i = 1 To #NbBasicKeywords
FoldKeywords(i) = 0
Next i
SortArray(FoldStart$(), 2, 1, NbFoldStartWords)
k = 0
For i = 1 To NbFoldStartWords
If k <> Asc(LCase(Left(FoldStart$(i), 1)))
FoldStartVT2(k) = i-1
FoldStartVT2(Asc(UCase(Chr(k)))) = i-1
k = Asc(LCase(Left(FoldStart$(i), 1)))
FoldStartVT(k) = i
FoldStartVT(Asc(UCase(Chr(k)))) = i
EndIf
index = IsBasicKeyword(FoldStart$(i))
If index
FoldKeywords(index) = 1
EndIf
Next i
FoldStartVT2(k) = NbFoldStartWords
FoldStartVT2(Asc(UCase(Chr(k)))) = NbFoldStartWords
SortArray(FoldEnd$(), 2, 1, NbFoldEndWords)
k = 0
For i = 1 To NbFoldEndWords
If UCase(FoldEnd$(i)) = "ENDMACRO"
IsMacroFolding = 1 ; special case!
EndIf
If k <> Asc(LCase(Left(FoldEnd$(i), 1)))
FoldEndVT2(k) = i-1
FoldEndVT2(Asc(UCase(Chr(k)))) = i-1
k = Asc(LCase(Left(FoldEnd$(i), 1)))
FoldEndVT(k) = i
FoldEndVT(Asc(UCase(Chr(k)))) = i
EndIf
index = IsBasicKeyword(FoldEnd$(i))
If index
FoldKeywords(index) = 2
EndIf
Next i
FoldEndVT2(k) = NbFoldEndWords
FoldEndVT2(Asc(UCase(Chr(k)))) = NbFoldEndWords
EndProcedure
; frees a linkedlist of *SourceItem structures
Procedure FreeSourceItems(*Item.SourceItem)
While *Item
; DEBUGGING ONLY -- very slow!
; ------------------------------------------
; found = 0
; ForEach SourceItemHeap()
; If @SourceItemHeap() = *Item
; found = 1
; Break
; EndIf
; Next
; If found = 0
; Debug "Trying to free a SourceItem that does not exist!!"
; CallDebugger
; EndIf
; ------------------------------------------
ChangeCurrentElement(SourceItemHeap(), *Item)
*Item = *Item\Next
DeleteElement(SourceItemHeap()) ; frees all contained strings...
Wend
EndProcedure
; frees an entire source items array, including contained lists
Procedure FreeSourceItemArray(*Parser.ParserData)
; protect against 0-var for the first call
If *Parser\SourceItemArray
For i = 0 To *Parser\SourceItemCount-1
FreeSourceItems(*Parser\SourceItemArray\Line[i]\First)
Next i
FreeMemory(*Parser\SourceItemArray)
*Parser\SourceItemArray = 0
EndIf
ClearMap(*Parser\Modules())
; Sorted data no longer valid
*Parser\SortedValid = #False
EndProcedure
; creates and links a new sourceitem structure at the end of the current line
; returns the allocated structure space
Procedure AddSourceItem(Type, Line, Position, Length)
*Item.SourceItem = 0
If AddElement(SourceItemHeap())
*Item = @SourceItemHeap()
*Item\Type = Type
*Item\Position = Position
*Item\Length = Length
If *Parser_Array\Line[Line]\Last = 0
*Parser_Array\Line[Line]\First = *Item
*Parser_Array\Line[Line]\Last = *Item
Else
*Item\Previous = *Parser_Array\Line[Line]\Last
*Parser_Array\Line[Line]\Last\Next = *Item
*Parser_Array\Line[Line]\Last = *Item
EndIf
EndIf
ProcedureReturn *Item
EndProcedure
; compare the 2 sourceitem lists and returns true if they have equal content
; also handles null pointers correctly
Procedure CompareSourceItems(*Item1.SourceItem, *Item2.SourceItem)
; Compare as long as both items are nonzero
;
While *Item1 And *Item2
If *Item1\Type <> *Item2\Type Or *Item1\Position <> *Item2\Position Or *Item1\NumericData <> *Item2\NumericData Or *Item1\Name$ <> *Item2\Name$ Or *Item1\StringData$ <> *Item2\StringData$ Or *Item1\ModulePrefix$ <> *Item2\ModulePrefix$
ProcedureReturn #False
EndIf
*Item1 = *Item1\Next
*Item2 = *Item2\Next
Wend
; One of the two is 0 here. If the other is not 0 as well,
; the lists are different
;
If *Item1 = *Item2
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
; returns true if *Pointer is the end of a line with continuation
; *Pointer must either be at a newline or at a ';' comment start
; *Buffer is the start of the buffer (or line)
;
; NOTE: also used outside of Parser, so don't use global parser state!
Procedure IsLineContinuation(*Buffer, *Pointer.PTR)
; Tokens that cause line continuation:
; ',' '|' '+' And Or Xor
; move away from the newline or comment mark
*LineEnd = *Pointer
*Pointer - 1
; skip whitespace
While *Pointer > *Buffer And (*Pointer\a = ' ' Or *Pointer\a = 9)
*Pointer - 1
Wend
; if a valid word character, skip to start of word
If ValidCharacters(*Pointer\a)
While *Pointer > *Buffer And ValidCharacters(*Pointer\a)
*Pointer - 1
Wend
; move back to first word char
If ValidCharacters(*Pointer\a) = 0
*Pointer + 1
EndIf
EndIf
; check the found token
If *Pointer >= *Buffer
If *Pointer\a = ',' Or *Pointer\a = '|' Or *Pointer\a = '+'
ProcedureReturn #True
ElseIf *LineEnd-*Pointer >= 2 And ValidCharacters(*Pointer\a[2]) = 0
ProcedureReturn Bool(CompareMemoryString(*Pointer, ToAscii("Or"), #PB_String_NoCase, 2, #PB_Ascii) = #PB_String_Equal)
ElseIf *LineEnd-*Pointer >= 3 And ValidCharacters(*Pointer\a[3]) = 0
ProcedureReturn Bool(CompareMemoryString(*Pointer, ToAscii("And"), #PB_String_NoCase, 3, #PB_Ascii) = #PB_String_Equal Or
CompareMemoryString(*Pointer, ToAscii("Xor"), #PB_String_NoCase, 3, #PB_Ascii) = #PB_String_Equal)
EndIf
EndIf
ProcedureReturn #False ; no line continuation
EndProcedure
; returns true if *LineStart is the start of a line with continuation at the end
; *BufferEnd is the end of the allocated buffer
;
; NOTE: also used outside of Parser, so don't use global parser state!
Procedure IsContinuedLineStart(Line$)
*LineStart.Character = @Line$
*BufferEnd = *LineStart + Len(Line$) * #CharSize
*Pointer.Character = *LineStart
FoundChar = #False
; skip the line to a comment star or line end
While *Pointer < *BufferEnd
; detect inline ASM/JS lines
If FoundChar = #False
If *Pointer\c = '!'
; its inline code
ProcedureReturn #False
ElseIf *Pointer\c <> ' ' And *Pointer\c <> 9
; non-whitespace means it cannot be inline code
FoundChar = #True
EndIf
EndIf
If *Pointer\c = '"' ; string
*Pointer + #CharSize
While *Pointer < *BufferEnd And *Pointer\c <> '"'
If *Pointer\c = 13 Or *Pointer\c = 10
ProcedureReturn #False ; something is weird
EndIf
*Pointer + #CharSize
Wend
*Pointer + #CharSize ; skip second "
ElseIf *Pointer\c = 39 ; "'" char const
*Pointer + #CharSize
While *Pointer < *BufferEnd And *Pointer\c <> 39
If *Pointer\c = 13 Or *Pointer\c = 10
ProcedureReturn #False ; something is weird
EndIf
*Pointer + #CharSize
Wend
*Pointer + #CharSize ; skip second "
ElseIf *Pointer\c = '~' And PeekC(*Pointer + #CharSize) = '"' ; escaped string
*Pointer + (2 * #CharSize)
While *Pointer < *BufferEnd And *Pointer\c <> '"'
If *Pointer\c = 13 Or *Pointer\c = 10
ProcedureReturn #False ; something is weird
ElseIf *Pointer\c = '\'
*Pointer + #CharSize ; escape sequence
If *Pointer < *BufferEnd And *Pointer\c <> 13 And *Pointer\c <> 10
*Pointer + #CharSize
EndIf
Else
*Pointer + #CharSize
EndIf
Wend
*Pointer + #CharSize ; skip second "
ElseIf *Pointer\c = 10 Or *Pointer\c = 13 Or *Pointer\c = ';' Or *Pointer\c = 0 ; line end or comment or buffer end
Break
Else
*Pointer + #CharSize
EndIf
Wend
CompilerIf #PB_Compiler_Unicode
; IsLineContinuation() needs ascii input
;
*String = StringToAscii(PeekS(*LineStart, (*Pointer - *LineStart) / #CharSize))
Result = IsLineContinuation(*String, *String + (*Pointer - *LineStart) / #CharSize)
FreeMemory(*String)
ProcedureReturn Result
CompilerElse
; test for line continuation
ProcedureReturn IsLineContinuation(*LineStart, *Pointer)
CompilerEndIf
EndProcedure
; Some helpers for scanning a buffer
; Note: *pCursor is a pointer to the *Cursor from ScanBuffer
;
Macro Parser_SkipSpace(_Cursor)
While _Cursor\b = ' ' Or _Cursor\b = 9
_Cursor + 1
Wend
EndMacro
Macro Parser_SkipString(_Cursor)
_Cursor + 1
While _Cursor\b And _Cursor\b <> '"' And _Cursor\b <> 13 And _Cursor\b <> 10
_Cursor + 1
Wend
If _Cursor\b = '"'
_Cursor + 1
EndIf
EndMacro
Macro Parser_SkipEscapeString(_Cursor)
_Cursor + 2 ; skip ~"
While _Cursor\b And _Cursor\b <> '"' And _Cursor\b <> 13 And _Cursor\b <> 10
If _Cursor\b = '\'
_Cursor + 1
If _Cursor\b And _Cursor\b <> 13 And _Cursor\b <> 10
_Cursor + 1
EndIf
Else
_Cursor + 1
EndIf
Wend
If _Cursor\b = '"'
_Cursor + 1
EndIf
EndMacro
Macro Parser_SkipCharConst(_Cursor)
_Cursor + 1
While _Cursor\b And _Cursor\b <> 39 And _Cursor\b <> 13 And _Cursor\b <> 10
_Cursor + 1
Wend
If _Cursor\b = 39
_Cursor + 1
EndIf
EndMacro
; Skip newline and update CurrentLine count/line start pointer
Macro Parser_Newline(_Cursor)
If _Cursor\b = 13 And _Cursor\b[1] = 10
_Cursor + 2
Else
_Cursor + 1
EndIf
Parser_CurrentLine + 1
*Parser_LineStart = _Cursor
EndMacro
; process comment (and newline)
; use NoItems to suppress fold marks and comment markers
Procedure Parser_Comment(*pCursor.PTR)
Static NewList FoundIssues.FoundIssue() ; static to avoid constant alloc/free
*Cursor.PTR = *pCursor\p ; We could use *pCursor\p\b, but this way should be faster
; Check folding keywords. A Fold can start with a ;
;
If EnableFolding And Parser_IgnoreCommentItems = #False
If FoldStartVT(*Cursor\a)
For i = FoldStartVT(*Cursor\a) To FoldStartVT2(*Cursor\a)
length = Len(FoldStart$(i))
If CompareMemoryString(*Cursor, ToAscii(FoldStart$(i)), 1, length, #PB_Ascii) = 0 And ValidCharacters(PeekA(*Cursor + length)) = 0
AddSourceItem(#ITEM_FoldStart, Parser_CurrentLine, -1, -1)
Break
EndIf
Next i
EndIf
If FoldEndVT(*Cursor\a)
For i = FoldEndVT(*Cursor\a) To FoldEndVT2(*Cursor\a)
length = Len(FoldEnd$(i))
If CompareMemoryString(*Cursor, ToAscii(FoldEnd$(i)), 1, length, #PB_Ascii) = 0 And ValidCharacters(PeekA(*Cursor + length)) = 0
AddSourceItem(#ITEM_FoldEnd, Parser_CurrentLine, -1, -1)
Break
EndIf
Next i
EndIf
EndIf
*Cursor + 1 ; skip comment start
If *Cursor\b = '-' And Parser_IgnoreCommentItems = #False ; marker detected
*Cursor + 1
*Start = *Cursor
While *Cursor\b And *Cursor\b <> 10 And *Cursor\b <> 13
*Cursor + 1
Wend
*Item.SourceItem = AddSourceItem(#ITEM_CommentMark, Parser_CurrentLine, -1, -1)
If *Start < *Cursor
If Parser_Encoding = 1 ; utf8
*Item\Name$ = Trim(PeekS(*Start, *Cursor - *Start, #PB_UTF8))
Else
*Item\Name$ = Trim(PeekS(*Start, *Cursor - *Start, #PB_Ascii))
EndIf
Else
*Item\Name$ = ""
EndIf
Else
; skip to line end
*Start = *Cursor
While *Cursor\b And *Cursor\b <> 10 And *Cursor\b <> 13
*Cursor + 1
Wend
; check issues
If *Start < *Cursor And Parser_IgnoreCommentItems = #False
If Parser_Encoding = 1 ; utf8
Comment$ = PeekS(*Start, *Cursor - *Start, #PB_UTF8)
Else
Comment$ = PeekS(*Start, *Cursor - *Start, #PB_Ascii)
EndIf
If Trim(Comment$) <> ""
ScanCommentIssues(Comment$, FoundIssues(), #False) ; source parser mode
ForEach FoundIssues()
*Item.SourceItem = AddSourceItem(#ITEM_Issue, Parser_CurrentLine, -1, -1)
*Item\Name$ = FoundIssues()\Text$
*Item\Issue = FoundIssues()\Issue
Next FoundIssues()
EndIf
EndIf
EndIf
; process/skip newline
Parser_Newline(*Cursor)
*pCursor\p = *Cursor
EndProcedure
; Generic routine to skip a braced expression
; Returns true if successful, false if brace mismatch
;
Procedure Parser_SkipBraces(*pCursor.PTR, StartBrace, EndBrace)
*Cursor.PTR = *pCursor\p
Result = #True
If *Cursor\b = StartBrace
*Cursor + 1
depth = 1 ; number of ')'s needed to close prototype
Repeat
Select *Cursor\b
Case 0 ; end of data
Result = #False
Break
Case 13, 10 ; newline
If IsLineContinuation(*Parser_LineStart, *Cursor)
Parser_Newline(*Cursor)
Else
Result = #False
Break
EndIf
Case StartBrace
depth + 1
Case EndBrace
depth - 1
If depth = 0
Break
EndIf
Case ';'
If IsLineContinuation(*Parser_LineStart, *Cursor)
Parser_Comment(@*Cursor)
Else
; incorrect code, as ) is missing
Result = #False
Break
EndIf
Case '"' ; strings are possible as default values
*Cursor + 1
While *Cursor\b And *Cursor\b <> '"' And *Cursor\b <> 13 And *Cursor\b <> 10
*Cursor + 1
Wend
If *Cursor\b <> '"'
Result = #False
Break
EndIf
Case '~'
If PeekB(*Cursor + 1) = '"' ; escaped string
*Cursor + 2
While *Cursor\b And *Cursor\b <> '"' And *Cursor\b <> 13 And *Cursor\b <> 10
If *Cursor\b = '\'
*Cursor + 1
If *Cursor\b And *Cursor\b <> 13 And *Cursor\b <> 10
*Cursor + 1
EndIf
Else
*Cursor + 1
EndIf
Wend
If *Cursor\b <> '"'
Result = #False
Break
EndIf
EndIf
Case 39 ; '
*Cursor + 1
While *Cursor\b And *Cursor\b <> 39 And *Cursor\b <> 13 And *Cursor\b <> 10
*Cursor + 1
Wend
If *Cursor\b <> 39
Result = #False
Break
EndIf
EndSelect
*Cursor + 1
ForEver
If *Cursor\b = EndBrace
*Cursor + 1
EndIf
EndIf
*pCursor\p = *Cursor
ProcedureReturn Result
EndProcedure
Procedure Parser_SkipType(*pCursor.PTR)
*Cursor.PTR = *pCursor\p ; We could use *pCursor\p\b, but this way should be faster
Parser_SkipSpace(*Cursor)
If *Cursor\a = '.'
*Cursor + 1
Parser_SkipSpace(*Cursor)
While ValidCharacters(*Cursor\a) ; skip type
*Cursor + 1
Wend
Parser_SkipSpace(*Cursor)
EndIf
; check this even without a ".", as it could be "Var${10}"
; note that the constant expression may be multiline, so use the generic routine
If *Cursor\a = '{'
Parser_SkipBraces(@*Cursor, '{', '}')
EndIf
*pCursor\p = *Cursor
EndProcedure
; cleaned up a (possibly multiline) prototype or {} fixed string expression
Procedure.s Parser_Cleanup(Input$)
; step one:
; - replace entire comments (from line continuation) by chr(1)
; - replace newline and tab and space (outside of string) with chr(1)
; - replace , (outside of string) With Chr(2)
; Note: after Array, List, Map one space must be preserved
*Cursor.PTR = @Input$
PreserveSpace = #False
Repeat
Select *Cursor\c
Case 0
Break
Case '"'
*Cursor + #CharSize
While *Cursor\c And *Cursor\c <> '"' And *Cursor\c <> 13 And *Cursor\c <> 10
*Cursor + #CharSize
Wend
If *Cursor\c = '"'
*Cursor + #CharSize
EndIf
Case '~'
If PeekC(*Cursor + #CharSize) = '"'
*Cursor + (2 * #CharSize)
While *Cursor\c And *Cursor\c <> '"' And *Cursor\c <> 13 And *Cursor\c <> 10
If *Cursor\c = '\'
*Cursor + #CharSize
If *Cursor\c And *Cursor\c <> 13 And *Cursor\c <> 10
*Cursor + 1
EndIf
Else
*Cursor + #CharSize
EndIf
Wend
If *Cursor\c = '"'
*Cursor + #CharSize
EndIf
Else
*Cursor + #CharSize
EndIf
Case 39
*Cursor + #CharSize
While *Cursor\c And *Cursor\c <> 39 And *Cursor\c <> 13 And *Cursor\c <> 10
*Cursor + #CharSize
Wend
If *Cursor\c = 39
*Cursor + #CharSize
EndIf
Case ';'
If PreserveSpace
*Cursor\c = ' '
*Cursor + #CharSize
PreserveSpace = #False
EndIf
While *Cursor\c And *Cursor\c <> 13 And *Cursor\c <> 10
*Cursor\c = 1
*Cursor + #CharSize
Wend
If *Cursor\c = 13 And *Cursor\c[1] = 10
*Cursor\c = 1: *Cursor + #CharSize
*Cursor\c = 1: *Cursor + #CharSize
ElseIf *Cursor\c = 10
*Cursor\c = 1: *Cursor + #CharSize
EndIf
Case 13, 10, 9, ' '
If PreserveSpace
*Cursor\c = ' '
PreserveSpace = #False
Else
*Cursor\c = 1
EndIf
*Cursor + #CharSize
Case ','
*Cursor\c = 2
*Cursor + #CharSize
Default
; handle Array, List, Map
If ValidCharacters(*Cursor\c & $FF) ; must mask for unicode chars!
If CompareMemoryString(*Cursor, @"Array", #PB_String_NoCase, 5) = #PB_String_Equal And ValidCharacters(*Cursor\c[5] & $FF) = 0
CopyMemory(@"Array", *Cursor, 5*#CharSize) ; correct the case
*Cursor + 5*#CharSize
PreserveSpace = #True
ElseIf CompareMemoryString(*Cursor, @"List", #PB_String_NoCase, 4) = #PB_String_Equal And ValidCharacters(*Cursor\c[4] & $FF) = 0
CopyMemory(@"List", *Cursor, 4*#CharSize) ; correct the case
*Cursor + 4*#CharSize
PreserveSpace = #True
ElseIf CompareMemoryString(*Cursor, @"Map", #PB_String_NoCase, 3) = #PB_String_Equal And ValidCharacters(*Cursor\c[3] & $FF) = 0
CopyMemory(@"Map", *Cursor, 3*#CharSize) ; correct the case
*Cursor + 3*#CharSize
PreserveSpace = #True
Else
; skip entire word so we do not detect FooList as a list keyword
While ValidCharacters(*Cursor\c & $FF)
*Cursor + #CharSize
Wend
EndIf
Else
*Cursor + #CharSize
EndIf
EndSelect
ForEver
; step two:
; - remove all stuff marked chr(1)
; - replace the comma (marked chr(2)) with ", " for readability
Result$ = ReplaceString(RemoveString(Input$, Chr(1)), Chr(2), ", ")
; done
ProcedureReturn Result$
EndProcedure
; Name$ is needed to check for a $
;
Procedure.s Parser_GetType(*pCursor.PTR, Name$)
*Cursor.PTR = *pCursor\p ; We could use *pCursor\p\b, but this way should be faster
Parser_SkipSpace(*Cursor)
If Right(Name$, 1) = "$"
If *Cursor\b = '{'
*Start = *Cursor
If Parser_SkipBraces(@*Cursor, '{', '}')
Type$ = "s" + Parser_Cleanup(PeekS(*Start, *Cursor - *Start, #PB_Ascii)) ; store all types as lowercase
EndIf
Else
Type$ = "s"
EndIf
ElseIf *Cursor\b = '.'
*Cursor + 1
Parser_SkipSpace(*Cursor)
*Start = *Cursor
While ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
If *Start < *Cursor
Type$ = LCase(PeekS(*Start, *Cursor - *Start, #PB_Ascii)) ; store in lowercase
EndIf
Parser_SkipSpace(*Cursor)
If *Cursor\b = '{'
; check for fixed string type
*Start = *Cursor
If Parser_SkipBraces(@*Cursor, '{', '}')
Type$ + Parser_Cleanup(PeekS(*Start, *Cursor - *Start, #PB_Ascii))
EndIf
ElseIf *Cursor\b = ':' And *Cursor\b[1] = ':'
; structure name with a module prefix
*Cursor + 2
Parser_SkipSpace(*Cursor)
*Start = *Cursor
While ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
If *Start < *Cursor
Type$ + "::" + LCase(PeekS(*Start, *Cursor - *Start, #PB_Ascii))
EndIf
EndIf
EndIf
*pCursor\p = *Cursor
ProcedureReturn Type$
EndProcedure
; get current word (if any), including possible * and $ chars
;
Procedure.s Parser_GetWord(*pCursor.PTR)
*Cursor.PTR = *pCursor\p ; We could use *pCursor\p\b, but this way should be faster
If ValidCharacters(*Cursor\a) Or (*Cursor\a = '*' And ValidCharacters(*Cursor\a[1]))
*Start = *Cursor
*Cursor + 1
While ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
If *Cursor\a = '$'
*Cursor + 1
EndIf
Word$ = PeekS(*Start, *Cursor - *Start, #PB_Ascii)
EndIf
*pCursor\p = *Cursor
ProcedureReturn Word$
EndProcedure
; get a possible module prefix if a "::" operator is used
; if no such prefix is found, the cursor is not moved
Procedure.s Parser_ModulePrefix(*pCursor.PTR)
*Cursor.PTR = *pCursor\p
If ValidCharacters(*Cursor\a)
Word$ = Parser_GetWord(@*Cursor)
Parser_SkipSpace(*Cursor)
If *Cursor\a = ':' And *Cursor\a[1] = ':'
*Cursor + 2
Parser_SkipSpace(*Cursor)
; update cursor and return module prefix
*pCursor\p = *Cursor
ProcedureReturn Word$
EndIf
EndIf
; do not update *pCursor if no module operator was found
ProcedureReturn ""
EndProcedure
Procedure Parser_SkipPrototype(*pCursor.PTR)
ProcedureReturn Parser_SkipBraces(*pCursor, '(', ')')
EndProcedure
Procedure.s Parser_GetPrototype(*pCursor.PTR)
Parser_SkipSpace(*pCursor\p)
If *pCursor\p\b = '('
*Start = *pCursor\p
Ok = Parser_SkipPrototype(*pCursor)
If Ok And *pCursor\p > *Start And PeekB(*pCursor\p - 1) = ')' ; check if there was no error in the prototype
If Parser_Encoding = 1
Result$ = Parser_Cleanup(PeekS(*Start, *pCursor\p - *Start, #PB_UTF8))
Else
Result$ = Parser_Cleanup(PeekS(*Start, *pCursor\p - *Start, #PB_Ascii))
EndIf
EndIf
EndIf
ProcedureReturn Result$
EndProcedure
; Analyze/add an unknown word (could be a variable)
;
Procedure Parser_GetUnknownWord(*pCursor.PTR, InImport, ModulePrefix$)
*Cursor.PTR = *pCursor\p
Define *Item.SourceItem, ModulePrefix$
; We have any word here that is not a keyword. Check if it is followed by a (, [ or ::
; and else register it as a possible variable.
; Stuff like structure items will be filtered on reading (as it requires walking the items to check for structure/endstructure
;
*Start.BYTE = *Cursor
Word$ = Parser_GetWord(@*Cursor)
length = Len(Word$)
; filter numbers. Note: the .xx part of a number is handled in the '.' handler
If IsDecNumber(*Start, length) = 0 And (*Start\b <> '*' Or IsDecNumber(*Start+1, length-1) = 0)
Parser_SkipSpace(*Cursor)
; Check if this is actually a label (first text on the line, and followed by a ":")
IsLabel = 0
If *Cursor\b = ':'
IsLabel = 1
*BackCursor.BYTE = *Start-1
While *BackCursor >= *Parser_LineStart
If *BackCursor\b <> ' ' And *BackCursor\b <> 9
IsLabel = 0
Break
EndIf
*BackCursor - 1
Wend
EndIf
If IsLabel
*Item = AddSourceItem(#ITEM_Label, Parser_CurrentLine, *Start-*Parser_LineStart, length)
*Item\Name$ = Word$
*Item\IsDefinition = #True
*Item\ModulePrefix$ = ModulePrefix$
Else
Type$ = Parser_GetType(@*Cursor, Word$)
Parser_SkipSpace(*Cursor)