forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructureFunctions.pb
949 lines (809 loc) · 31.4 KB
/
StructureFunctions.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
;--------------------------------------------------------------------------------------------
; 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.
;--------------------------------------------------------------------------------------------
;
; Helper functions for managing structures/interfaces in the user's code
; Used mainly by AutoComplete
;
; Parse a Structure content (without Structure XXX / EndStructure)
; And fill Output() with the separated items
; (Output() is not cleared, for better handling of Extends by chaining ParseStructure calls)
;
; Note:
; As structure/interface items can be names like keywords or PB functions,
; the usual parsed SourceItem data is not enough to recover a structure's content,
; so it must be parsed explicitly from a buffer
;
; Note: Extends must be handled separately
;
; *Buffer must point AFTER any 'Extends' parts, but may be before any
; 'Align' part.
;
Procedure ParseStructure(*Buffer, Length, List Output.s())
*BufferEnd = *Buffer + Length
*Cursor.PTR = *Buffer
; skip whitespace
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
; Properly skip any 'Align' part
If *BufferEnd-*Cursor >= 6 And CompareMemoryString(*Cursor, ToAscii("Align"), #PB_String_NoCase, 5, #PB_Ascii) = #PB_String_Equal And ValidCharacters(*Cursor\a[5]) = 0
*Cursor + 5
; the align part is an expression which may be multiline
While *Cursor < *BufferEnd
; Comment
If *Cursor\b = ';'
Continuation = IsLineContinuation(*Buffer, *Cursor)
While *Cursor < *BufferEnd And *Cursor\b <> 10 And *Cursor\b <> 13
*Cursor + 1
Wend
If *Cursor < *BufferEnd-1 And *Cursor\b = 13 And *Cursor\b[1] = 10
*Cursor + 2
Else
*Cursor + 1
EndIf
; If not a line continuation, we have skipped the expression so break
If Continuation = #False
Break
EndIf
; String
ElseIf *Cursor\b = '"'
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor\b <> '"'
If *Cursor\b = 13 Or *Cursor\b = 10
Break 2 ; something is weird
EndIf
*Cursor + 1
Wend
*Cursor + 1 ; skip second "
; Escape Strings
ElseIf *Cursor\b = '~' And *Cursor < (*BufferEnd - 1) And *Cursor\b[1] = '"'
*Cursor + 2
While *Cursor < *BufferEnd And *Cursor\b <> '"'
If *Cursor\b = 13 Or *Cursor\b = 10
Break 2 ; something is weird
ElseIf *Cursor\b = '\'
*Cursor + 1
If *Cursor < *BufferEnd And *Cursor\b <> 13 Or *Cursor\b <> 10
*Cursor + 1
EndIf
Else
*Cursor + 1
EndIf
Wend
*Cursor + 1 ; skip second "
; Character constant
ElseIf *Cursor\b = 39
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor\b <> 39
If *Cursor\b = 13 Or *Cursor\b = 10
Break 2 ; something is weird
EndIf
*Cursor + 1
Wend
*Cursor + 1 ; skip second '
; ':' is the end of the align expression
ElseIf *Cursor\b = ':'
Break
; newline
ElseIf *Cursor\b = 13 Or *Cursor\b = 10
Continuation = IsLineContinuation(*Buffer, *Cursor)
If *Cursor < *BufferEnd-1 And *Cursor\b = 13 And *Cursor\b[1] = 10
*Cursor + 2
Else
*Cursor + 1
EndIf
; break only if not a continuation char
If Continuation = #False
Break
EndIf
Else
*Cursor + 1
EndIf
Wend
EndIf
; finally start scanning for structure items
;
While *Cursor < *BufferEnd
Prefix$ = ""
If *Cursor\b = ';' ; comment
While *Cursor < *BufferEnd And *Cursor\b <> 10 And *Cursor\b <> 13
*Cursor + 1
Wend
ElseIf ValidCharacters(*Cursor\a) Or *Cursor\a = '*'
*Start = *Cursor
; Check for Array, List, Map def
; Note: "List .l" is a valid structure entry, so check for this
;
If *Cursor < *BufferEnd-6 And (PeekB(*Cursor+5) = ' ' Or PeekB(*Cursor+5) = 9) And CompareMemoryString(*Cursor, ToAscii("Array"), #PB_String_NoCase, 5, #PB_Ascii) = #PB_String_Equal
*Cursor2.BYTE = *Cursor+5
While *Cursor2 < *BufferEnd And (*Cursor2\b = ' ' Or *Cursor2\b = 9)
*Cursor2 + 1
Wend
If *Cursor2 < *BufferEnd And *Cursor2\b <> '.'
; its a proper array def
Prefix$ = "Array "
*Cursor = *Cursor2 ; skip keyword and whitespace
*Start = *Cursor
EndIf
ElseIf *Cursor < *BufferEnd-5 And (PeekB(*Cursor+4) = ' ' Or PeekB(*Cursor+4) = 9) And CompareMemoryString(*Cursor, ToAscii("List"), #PB_String_NoCase, 4, #PB_Ascii) = #PB_String_Equal
*Cursor2.BYTE = *Cursor+4
While *Cursor2 < *BufferEnd And (*Cursor2\b = ' ' Or *Cursor2\b = 9)
*Cursor2 + 1
Wend
If *Cursor2 < *BufferEnd And *Cursor2\b <> '.'
; its a proper list def
Prefix$ = "List "
*Cursor = *Cursor2 ; skip keyword and whitespace
*Start = *Cursor
EndIf
ElseIf *Cursor < *BufferEnd-4 And (PeekB(*Cursor+3) = ' ' Or PeekB(*Cursor+3) = 9) And CompareMemoryString(*Cursor, ToAscii("Map"), #PB_String_NoCase, 3, #PB_Ascii) = #PB_String_Equal
*Cursor2.BYTE = *Cursor+3
While *Cursor2 < *BufferEnd And (*Cursor2\b = ' ' Or *Cursor2\b = 9)
*Cursor2 + 1
Wend
If *Cursor2 < *BufferEnd And *Cursor2\b <> '.'
; its a proper map def
Prefix$ = "Map "
*Cursor = *Cursor2 ; skip keyword and whitespace
*Start = *Cursor
EndIf
EndIf
If *Cursor\b = '*' ; yes, there can be whitespace after the *
*Cursor + 1
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
EndIf
; get the name
While *Cursor < *BufferEnd And ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
If *Cursor < *BufferEnd And *Cursor\b = '$'
*Cursor + 1
EndIf
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
; get the type
If *Cursor < *BufferEnd And *Cursor\b = '.'
*Cursor + 1
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
While *Cursor < *BufferEnd And ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
; possible module prefix
If *Cursor < *BufferEnd - 2 And *Cursor\b = ':' And *Cursor\b[1] = ':'
*Cursor + 2
While *Cursor < *BufferEnd And ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
EndIf
EndIf
; get a possible fixed string size
If *Cursor < *BufferEnd And *Cursor\b = '{'
Depth = 1
*Cursor + 1
While *Cursor < *BufferEnd And Depth > 0
Select *Cursor\b
Case 10, 13, ':', ';': Break ; problem
Case '{': Depth + 1
Case '}': Depth - 1
Case 39 ; ' is possible here (though unlikely)
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor <> 39
*Cursor + 1
Wend
EndSelect
*Cursor + 1
Wend
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
EndIf
; get a possible array, list, map content
If *Cursor < *BufferEnd And (*Cursor\b = '[' Or *Cursor\b = '(')
If *Cursor\b = '['
CharOpen = '['
CharClose = ']'
Else
CharOpen = '('
CharClose = ')'
EndIf
Depth = 1
*Cursor + 1
While *Cursor < *BufferEnd And Depth > 0
Select *Cursor\b
Case 10, 13, ':', ';': Break ; problem
Case CharOpen: Depth + 1
Case CharClose: Depth - 1
Case 39 ; ' is possible here (though unlikely)
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor <> 39
*Cursor + 1
Wend
EndSelect
*Cursor + 1
Wend
EndIf
; finally done
If *Cursor <= *BufferEnd
Field$ = PeekS(*Start, *Cursor - *Start, #PB_Ascii)
Field$ = RemoveString(Field$, " ") ; cut whitespace
Field$ = RemoveString(Field$, Chr(9))
Select UCase(Field$) ; filter some stuff
Case "STRUCTUREUNION", "ENDSTRUCTUREUNION", "COMPILERIF", "COMPILERELSE", "COMPILERENDIF", "COMPILERSELECT", "COMPILERCASE", "COMPILERDEFAULT", "COMPILERENDSELECT", "", "*"
; skip to newline or : to ignore anything after compiler directives
While *Cursor < *BufferEnd
Select *Cursor\b
Case 10, 13 ; we can continue parsing here
Break
Case ':' ; can be module start or : separator
If *Cursor < *BufferEnd - 1 And *Cursor\b[1] = ':'
*Cursor + 2
Else
Break
EndIf
Case ';' ; have to skip the line, then continue
While *Cursor < *BufferEnd And *Cursor\b <> 13 And *Cursor\b <> 10
*Cursor + 1
Wend
Break
Case 39 ; '
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor <> 39
*Cursor + 1
Wend
Case '"'
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor <> '"'
*Cursor + 1
Wend
Case '~'
If (*Cursor + 1) < *BufferEnd And *Cursor\b[1] = '"'
*Cursor + 2
While *Cursor < *BufferEnd And *Cursor <> '"'
If *Cursor\b = '\'
*Cursor + 2
Else
*Cursor + 1
EndIf
Wend
EndIf
EndSelect
*Cursor + 1
Wend
Default
; Seems to be a valid field
AddElement(Output())
Output() = Prefix$ + Field$
EndSelect
EndIf
Else ; anything invalid, just skip it (including newline, separators etc
*Cursor + 1
EndIf
Wend
EndProcedure
; Same for Interfaces
;
Procedure ParseInterface(*Buffer, Length, List Output.s())
*BufferEnd = *Buffer + Length
*Cursor.PTR = *Buffer
While *Cursor < *BufferEnd
If *Cursor\b = ';' ; comment
While *Cursor < *BufferEnd And *Cursor\b <> 10 And *Cursor\b <> 13
*Cursor + 1
Wend
ElseIf ValidCharacters(*Cursor\a) ; interfaces cannot have pointer type ?
*Start = *Cursor
; get the name
While *Cursor < *BufferEnd And ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
Name$ = PeekS(*Start, *Cursor - *Start, #PB_Ascii)
If *Cursor < *BufferEnd And *Cursor\b = '$'
*Cursor + 1
EndIf
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
; get the type
If *Cursor < *BufferEnd And *Cursor\b = '.'
*Cursor + 1
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
While *Cursor < *BufferEnd And ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
; possible module prefix
If *Cursor < *BufferEnd - 2 And *Cursor\b = ':' And *Cursor\b[1] = ':'
*Cursor + 2
While *Cursor < *BufferEnd And ValidCharacters(*Cursor\a)
*Cursor + 1
Wend
While *Cursor < *BufferEnd And (*Cursor\b = ' ' Or *Cursor\b = 9)
*Cursor + 1
Wend
EndIf
EndIf
; get the prototype
If *Cursor < *BufferEnd And *Cursor\b = '('
Depth = 1
*Cursor + 1
While *Cursor < *BufferEnd And Depth > 0
Select *Cursor\b
Case 10, 13, ':', ';'
Break ; problem
Case '('
Depth + 1
Case ')'
Depth - 1
If Depth = 0
Break
EndIf
Case '"' ; strings are possible as default values
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor\b <> '"'
*Cursor + 1
Wend
Case '~'
If (*Cursor + 1) < *BufferEnd And *Cursor\b[1] = '"'
*Cursor + 2
While *Cursor < *BufferEnd And *Cursor <> '"'
If *Cursor\b = '\'
*Cursor + 2
Else
*Cursor + 1
EndIf
Wend
EndIf
Case 39 ; ' is possible here too
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor\b <> 39
*Cursor + 1
Wend
EndSelect
*Cursor + 1
Wend
If *Cursor < *BufferEnd And *Cursor\b = ')'
*Cursor + 1
; again, filter compiler directives
Select UCase(Name$)
Case "COMPILERIF", "COMPILERELSE", "COMPILERENDIF", "COMPILERSELECT", "COMPILERCASE", "COMPILERDEFAULT", "COMPILERENDSELECT", ""
; ignore until : ; or newline
While *Cursor < *BufferEnd
Select *Cursor\b
Case 10, 13 ; we can continue parsing here
Break
Case ':' ; can be module start or : separator
If *Cursor < *BufferEnd - 1 And *Cursor\b[1] = ':'
*Cursor + 2
Else
Break
EndIf
Case ';' ; have to skip the line, then continue
While *Cursor < *BufferEnd And *Cursor\b <> 13 And *Cursor\b <> 10
*Cursor + 1
Wend
Break
Case 39 ; '
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor <> 39
*Cursor + 1
Wend
Case '"'
*Cursor + 1
While *Cursor < *BufferEnd And *Cursor <> '"'
*Cursor + 1
Wend
Case '~'
If (*Cursor + 1) < *BufferEnd And *Cursor\b[1] = '"'
*Cursor + 2
While *Cursor < *BufferEnd And *Cursor <> '"'
If *Cursor\b = '\'
*Cursor + 2
Else
*Cursor + 1
EndIf
Wend
EndIf
EndSelect
*Cursor + 1
Wend
Default
; done
AddElement(Output())
Output() = Trim(PeekS(*Start, *Cursor - *Start, #PB_Ascii))
EndSelect
EndIf
EndIf
Else ; anything invalid, just skip it (including newline, separators etc
*Cursor + 1
EndIf
Wend
EndProcedure
; If needed, fully resolve the type of Field$ with module prefix
; if not needed, just return Field$ as is
;
Procedure.s ResolveStructureFieldType(*Parser.ParserData, *Item.SourceItem, Field$)
Index1 = FindString(Field$, ".")
If Index1 = 0
ProcedureReturn Field$
EndIf
Index2 = FindString(Field$, "(", Index1)
If Index2 = 0: Index2 = FindString(Field$, "[", Index1): EndIf
If Index2 = 0: Index2 = FindString(Field$, "{", Index1): EndIf
If Index2 = 0: Index2 = Len(Field$) + 1: EndIf
; split up the line
Prefix$ = Left(Field$, Index1)
Type$ = Mid(Field$, Index1 + 1, Index2-Index1-1)
Postfix$ = Mid(Field$, Index2)
; clean up the type
Type$ = RemoveString(RemoveString(Type$, " "), Chr(9))
; resolve the type
Type$ = ResolveStructureType(*Parser, *Item, *Item\SortedLine, Type$)
If Type$ = ""
ProcedureReturn Field$
EndIf
; return the final thing
ProcedureReturn Prefix$ + Type$ + Postfix$
EndProcedure
; Find structure/interface in an open Source
;
Procedure FindStructureInterfaceFromSource(*Source.SourceFile, Name$, ModuleName$, Type, List Output.s(), Recursion)
Success = #False
Bucket = GetBucket(@Name$)
; walk all Structures or interfaces in this source
*Item.SourceItem = *Source\Parser\Modules(UCase(ModuleName$))\Indexed[Type]\Bucket[Bucket]
While *Item
If CompareMemoryString(@Name$, @*Item\Name$, #PB_String_NoCase) = #PB_String_Equal
Break
EndIf
*Item = *Item\NextSorted
Wend
; check also the module body (if any)
If *Item = 0 And ModuleName$ <> ""
*Item.SourceItem = *Source\Parser\Modules("IMPL::" + UCase(ModuleName$))\Indexed[Type]\Bucket[Bucket]
While *Item
If CompareMemoryString(@Name$, @*Item\Name$, #PB_String_NoCase) = #PB_String_Equal
Break
EndIf
*Item = *Item\NextSorted
Wend
EndIf
If *Item
; we need to parse the content from the actual gadget
StartIndex = ScintillaSendMessage(*Source\EditorGadget, #SCI_POSITIONFROMLINE, *Item\SortedLine) + *Item\Position + *Item\FullLength ; FullLength includes Extends keyword
EndIndex = -1
Success = #False
; If this structure/interface extends another, get that first
; We do not care about the result here. If the Base is not found, its items
; will simply be missing
;
ExtendBase$ = StringField(*Item\Content$, 1, Chr(10)) ; holds the base struct, if any
ExtendBase$ = ResolveStructureType(@*Source\Parser, *Item, *Item\SortedLine, ExtendBase$)
If ExtendBase$ <> ""
FindStructureInterface(ExtendBase$, Type, Output(), Recursion + 1)
EndIf
If Type = #ITEM_Structure
EndKeyword = #KEYWORD_EndStructure
Else
EndKeyword = #KEYWORD_EndInterface
EndIf
Line = *Item\SortedLine
*EndItem.SourceItem = *Item
While *EndItem
If *EndItem\Type = #ITEM_Keyword And *EndItem\Keyword = EndKeyword
EndIndex = ScintillaSendMessage(*Source\EditorGadget, #SCI_POSITIONFROMLINE, Line) + *EndItem\Position
Break
EndIf
Parser_NextItem(*Source\Parser, *EndItem, Line)
Wend
If EndIndex > StartIndex
*Buffer = AllocateMemory(EndIndex - StartIndex + 1)
If *Buffer
Range.SCTextRange
Range\chrg\cpMin = StartIndex
Range\chrg\cpMax = EndIndex
Range\lpstrText = *Buffer
If ScintillaSendMessage(*Source\EditorGadget, #SCI_GETTEXTRANGE, 0, @Range)
If Type = #ITEM_Structure
; need to resolve the field types of structures (can be from different module)
Protected NewList TempList.s()
ParseStructure(*Buffer, EndIndex-StartIndex, TempList())
ForEach TempList()
AddElement(Output())
Output() = ResolveStructureFieldType(@*Source\Parser, *Item, TempList())
Next TempList()
Else
ParseInterface(*Buffer, EndIndex-StartIndex, Output())
EndIf
Success = #True
EndIf
FreeMemory(*Buffer)
EndIf
EndIf
EndIf
ProcedureReturn Success
EndProcedure
; Find a structure or interface in all open/project sources or residents
; IF the structure/interface is inside a module, the name MUST include the module prefix!
; Returns true/false
;
; Keep a recursion count to detect infinite recursions "Test extends Test".
; We could also build a list of already seen names, but this way is simpler to catch
; this very rare case
;
Procedure FindStructureInterface(Name$, Type, List Output.s(), Recursion)
ClearList(Output())
; Limit recursion to prevent stack overflow on "Test extends Test"
; More than 50 "Extends" sounds pretty insane anyway
If Recursion > 50
ProcedureReturn #False
EndIf
If Type <> #ITEM_Structure And Type <> #ITEM_Interface Or Name$ = ""
ProcedureReturn #False
EndIf
If FindString(Name$, "::")
ModuleName$ = StringField(Name$, 1, "::")
Name$ = StringField(Name$, 2, "::")
Else
ModuleName$ = ""
EndIf
; Check predefined structures (only if there is a loaded compiler)
; This is not needed if a module prefix is given
If CompilerReady And ModuleName$ = ""
c = Asc(UCase(Left(Name$, 1)))
If c = '_'
c = 27
ElseIf c >= 'A' And c <= 'Z'
c - 'A' + 1
Else
c = 0
EndIf
If Type = #ITEM_Structure
For i = StructureHT(c, 0) To StructureHT(c, 1) ; will be "0 To -1" if no entry
If CompareMemoryString(@Name$, @StructureList(i), #PB_String_NoCase) = #PB_String_Equal
; Request structure content
ForceDefaultCompiler()
CompilerWrite("STRUCTURE"+Chr(9)+Trim(Name$))
Repeat
Response$ = CompilerRead()
If Response$ = "OUTPUT"+Chr(9)+"COMPLETE"
Break
Else
AddElement(Output())
Output() = Response$
EndIf
ForEver
ProcedureReturn #True
EndIf
Next i
Else
For i = InterfaceHT(c, 0) To InterfaceHT(c, 1)
If CompareMemoryString(@Name$, @InterfaceList(i), #PB_String_NoCase) = #PB_String_Equal
; Request structure content
ForceDefaultCompiler()
CompilerWrite("INTERFACE"+Chr(9)+Trim(Name$))
Repeat
Response$ = CompilerRead()
If Response$ = "OUTPUT"+Chr(9)+"COMPLETE"
Break
Else
AddElement(Output())
Output() = Response$
EndIf
ForEver
ProcedureReturn #True
EndIf
Next i
EndIf
EndIf
; Always check the active source first for priority
;
SortParserData(@*ActiveSource\Parser, *ActiveSource)
If FindStructureInterfaceFromSource(*ActiveSource, Name$, ModuleName$, Type, Output(), Recursion)
ProcedureReturn #True
EndIf
; check all open files that are included in autocomplete listing
; All files have sorted data now (as we just sorted the active source above)
;
; Note: this can be called inside another function which loops on FileList() too!
; (also for recursive searches!)
Current = ListIndex(FileList())
ForEach FileList()
If @FileList() <> *ProjectInfo And @FileList() <> *ActiveSource
If AutoCompleteAllFiles Or (AutoCompleteProject And *ActiveSource\ProjectFile And FileList()\ProjectFile)
; A recursive FindStructureInterface (for Extends support) could change this, so store the filelist element
If FindStructureInterfaceFromSource(@FileList(), Name$, ModuleName$, Type, Output(), Recursion)
SelectElement(FileList(), Current) ; important
ProcedureReturn #True
EndIf
EndIf
EndIf
Next FileList()
SelectElement(FileList(), Current)
; Check all project files that are not open
; These should have the Structure content as a Chr(10) separated list in the SourceItem
;
Bucket = GetBucket(@Name$)
If AutoCompleteProject And *ActiveSource\ProjectFile
Current = ListIndex(ProjectFiles())
ForEach ProjectFiles()
If ProjectFiles()\Source = 0
; walk all Structures in this source
*Item.SourceItem = ProjectFiles()\Parser\Modules(UCase(ModuleName$))\Indexed[Type]\Bucket[Bucket]
While *Item
If CompareMemoryString(@Name$, @*Item\Name$, #PB_String_NoCase) = #PB_String_Equal
Break
EndIf
*Item = *Item\NextSorted
Wend
; check also the module impl
If *Item = 0 And ModuleName$ <> ""
*Item.SourceItem = ProjectFiles()\Parser\Modules("IMPL::" + UCase(ModuleName$))\Indexed[Type]\Bucket[Bucket]
While *Item
If CompareMemoryString(@Name$, @*Item\Name$, #PB_String_NoCase) = #PB_String_Equal
Break
EndIf
*Item = *Item\NextSorted
Wend
EndIf
If *Item
; If this structure/interface extends another, get that first
; We do not care about the result here. If the Base is not found, its items
; will simply be missing
;
ExtendBase$ = StringField(*Item\Content$, 1, Chr(10)) ; holds the base struct, if any
ExtendBase$ = ResolveStructureType(@ProjectFiles()\Parser, *Item, *Item\SortedLine, ExtendBase$)
If ExtendBase$ <> ""
FindStructureInterface(ExtendBase$, Type, Output(), Recursion + 1)
EndIf
If Len(*Item\Content$) > Len(ExtendBase$)+1
Count = CountString(*Item\Content$, Chr(10))
For i = 2 To Count+1 ; The first slot is the Extends base structure always (even if empty)
AddElement(Output())
Entry$ = StringField(*Item\Content$, i, Chr(10))
If Type = #ITEM_Structure
Output() = ResolveStructureFieldType(@ProjectFiles()\Parser, *Item, Entry$)
Else
Output() = Entry$
EndIf
Next i
EndIf
SelectElement(ProjectFiles(), Current)
ProcedureReturn #True
EndIf
EndIf
Next ProjectFiles()
SelectElement(ProjectFiles(), Current)
EndIf
ProcedureReturn #False
EndProcedure
Procedure FindStructure(Name$, List Output.s())
ProcedureReturn FindStructureInterface(Name$, #ITEM_Structure, Output(), 0)
EndProcedure
Procedure FindInterface(Name$, List Output.s())
ProcedureReturn FindStructureInterface(Name$, #ITEM_Interface, Output(), 0)
EndProcedure
; return "Array", "List", "Map" or "" (in this case)
Procedure.s StructureFieldKind(Entry$)
Entry$ = Trim(ReplaceString(Entry$, Chr(9), " ")) ; do not remove yet for the List/Array/Map check
; Test also for the space, as it could be "ArraySomething.l" else
If CompareMemoryString(@Entry$, @"Array ", #PB_String_NoCase, 6) = #PB_String_Equal
ProcedureReturn "Array"
ElseIf CompareMemoryString(@Entry$, @"List ", #PB_String_NoCase, 5) = #PB_String_Equal
ProcedureReturn "List"
ElseIf CompareMemoryString(@Entry$, @"Map ", #PB_String_NoCase, 4) = #PB_String_Equal
ProcedureReturn "Map"
Else
ProcedureReturn ""
EndIf
EndProcedure
Procedure.s StructureFieldName(Entry$)
Entry$ = Trim(ReplaceString(Entry$, Chr(9), " ")) ; do not remove yet for the List/Array/Map check
If CompareMemoryString(@Entry$, @"Array ", #PB_String_NoCase, 6) = #PB_String_Equal
Entry$ = Right(Entry$, Len(Entry$)-6)
ElseIf CompareMemoryString(@Entry$, @"List ", #PB_String_NoCase, 5) = #PB_String_Equal
Entry$ = Right(Entry$, Len(Entry$)-5)
ElseIf CompareMemoryString(@Entry$, @"Map ", #PB_String_NoCase, 4) = #PB_String_Equal
Entry$ = Right(Entry$, Len(Entry$)-4)
EndIf
Entry$ = RemoveString(Entry$, " ") ; now cut whitespace
Entry$ = RemoveString(Entry$, "*") ; * is not present when accessing pointer structure items!
Entry$ = StringField(Entry$, 1, "[") ; cut array stuff
Entry$ = StringField(Entry$, 1, "(") ; cut dynamic array stuff
Entry$ = StringField(Entry$, 1, "{") ; cut fixed string stuff
ProcedureReturn StringField(Entry$, 1, ".")
EndProcedure
Procedure.s StructureFieldType(Entry$)
Entry$ = RemoveString(RemoveString(Entry$, Chr(9)), " ") ; cut whitespace (messes up "List Something.l()", but we only want the type anyway)
Entry$ = StringField(Entry$, 1, "[") ; cut array stuff
Entry$ = StringField(Entry$, 1, "(") ; cut dynamic array stuff
Entry$ = StringField(Entry$, 1, "{") ; cut fixed string stuff
ProcedureReturn StringField(Entry$, 2, ".")
EndProcedure
Procedure.s InterfaceFieldName(Entry$)
Entry$ = RemoveString(RemoveString(Entry$, Chr(9)), " ") ; cut whitespace
Entry$ = StringField(Entry$, 1, "(") ; cut prototype
ProcedureReturn StringField(Entry$, 1, ".") ; cut any type info
EndProcedure
Procedure FindPrototypeInModule(Name$, ModuleName$)
Bucket = GetBucket(@Name$)
; check all open files that are included in autocomplete listing
; All files have sorted data now (as we just sorted the active source above)
; Note: This function can be called inside another that loops on FileList(),
; so do not use *ActiveSource to restore the position!
Current = ListIndex(FileList())
ForEach FileList()
If @FileList() <> *ProjectInfo
If AutoCompleteAllFiles Or @FileList() = *ActiveSource Or (AutoCompleteProject And *ActiveSource\ProjectFile And FileList()\ProjectFile)
; walk all Structures or interfaces in this source
*Item.SourceItem = FileList()\Parser\Modules(UCase(ModuleName$))\Sorted\Prototypes[Bucket]
While *Item
If CompareMemoryString(@Name$, @*Item\Name$, #PB_String_NoCase) = #PB_String_Equal
ChangeCurrentElement(FileList(), *ActiveSource) ; important
ProcedureReturn *Item
EndIf
*Item = *Item\NextSorted
Wend
EndIf
EndIf
Next FileList()
SelectElement(FileList(), Current)
; Check Project files
;
If AutoCompleteProject And *ActiveSource\ProjectFile
Current = ListIndex(ProjectFiles())
ForEach ProjectFiles()
If ProjectFiles()\Source = 0
; walk all Structures in this source
*Item.SourceItem = ProjectFiles()\Parser\Modules(UCase(ModuleName$))\Sorted\Prototypes[Bucket]
While *Item
If CompareMemoryString(@Name$, @*Item\Name$, #PB_String_NoCase) = #PB_String_Equal
ProcedureReturn *Item
EndIf
*Item = *Item\NextSorted
Wend
EndIf
Next ProjectFiles()
SelectElement(ProjectFiles(), Current)
EndIf
EndProcedure
; Return the SourceItem that defines the given prototype. Null if not found
; Input must include a module prefix if a module should be searched
Procedure FindPrototype(Name$)
If Name$ = ""
ProcedureReturn 0
EndIf
; Prototypes are always global scope, so we can find them easily in the sorted data
;
SortParserData(@*ActiveSource\Parser, *ActiveSource)
; split module prefix
If FindString(Name$, "::")
ModulePrefix$ = StringField(Name$, 1, "::")
Name$ = StringField(Name$, 2, "::")
; search module declaration and implementation (in this order)
*ProtoItem = FindPrototypeInModule(Name$, ModulePrefix$)
If *ProtoItem = 0
*ProtoItem = FindPrototypeInModule(Name$, "IMPL::" + ModulePrefix$)
EndIf
ProcedureReturn *ProtoItem
Else
ProcedureReturn FindPrototypeInModule(Name$, "") ; search global space
EndIf
EndProcedure