-
Notifications
You must be signed in to change notification settings - Fork 4
/
code-parser.jc
4484 lines (4444 loc) · 137 KB
/
code-parser.jc
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
import "system.jc"
import "gui2d.jc"
import "javascript.jc"
import "encoding.jc"
import "text-box.jc"
import System.Math.*
import System.Algorithm.*
import System.Console.*
import Gui2D.detail.*
import Javascript.*
import TextBox.*
import TextBox.detail.*
import Encoding.*
ENABLE_EXPERIMENTAL_FEATURES=0
MAX_WORD_LENGTH_TEXT=64
/////////////////////////////////////
//parser utility from compiler code
class CUniqueIDProvider(TTraits)
traits=TTraits()
int[] a
iptr n
auto clear()
this.a=int[].NULL
this.n=0L
auto _grow_to(iptr sz)
auto a2=new int[sz?sz*2:8L]
auto a=this.a
auto mask=a2.n-1
for i=0:sz-1
id=a[i]
if !id:continue
slot=-1L
b=traits.getHash(id)
b&=mask
delta=1L
for(;;)
//Write('.')
if !a2[b]:
slot=b
break
b+=delta++
b&=mask
a2[slot]=id
this.a=a2
auto _find(const CREATE_NEW,traits.TKey key)
auto a=this.a
auto grown=0
auto sz=(a?a.n:0L)
if !CREATE_NEW:
if !sz:return -1
else
if !sz:
_grow_to(sz)
a=this.a
sz=(a?a.n:0L)
grown=1
for(;;)
slot=-1L
h=iptr(traits.computeHash(key))
mask=sz-1
b=(iptr(h)&mask)
delta=1L
for(;;)
//Write('#')
pi=a[b]
if !pi:
slot=b
break
if traits.isKeyEqual(pi,key):
//found
return pi
b+=delta++
b&=mask
if !CREATE_NEW:
return -1
if !grown&&(this.n*4+1>=sz*3||sz-this.n<sz>>2):
_grow_to(sz)
a=this.a
sz=(a?a.n:0L)
grown=1
continue
break
__rc_barrier()
this.a[slot]=traits.createNew(key,h)
this.n++
return this.a[slot]
//////////////////////////////////////
//symbol table
struct TSymbol
iptr p_name,n
iptr hash
int user_slot
int decl_slot
//pointers for global key decl item, initialized to be self-pointing
int gkd_w,gkd_x
class TTraitSymbol
TKey=string
inline getHash(int id)
return g_symbols[id].hash
inline computeHash(string key)
return iptr(key.__hash__())
inline isKeyEqual(int id,string key)
//could do: __optimize__ rule for this
//return getIdString(id)==key
sym=g_symbols[id]
return sym.n==key.n&&__basic_api.memcmp(__pointer(g_strings.d+sym.p_name),__pointer(key.d),sym.n)==0
inline createNew(string key,iptr hash)
p=g_strings.n
n=key.n
//Writeln(g_strings.n,' ',g_strings.destructor_nbound_or_slice_reference)
g_strings.push(key)
ret=int(g_symbols.n)
g_symbols.push(TSymbol(){'hash':hash,'p_name':p,"n":n,"gkd_w":~ret,"gkd_x":~ret})
return ret
g_strings=new string
g_symbols=[TSymbol()]
g_id_provider=new CUniqueIDProvider(TTraitSymbol)
auto getid(string s)
return int(g_id_provider._find(1,s))
auto getIdString(int id)
sym=g_symbols[id]
return new(g_strings[sym.p_name:sym.p_name+sym.n-1])
//////////////////////////////////////
//tokenization
module CharSet
auto charset(string e)
ok=new u32[8]
for i=0:7
ok[i]=0
inv=0
s=0
if e[0]=='^':
inv=1;s++
for(;s<e.n;s++)
if s+1<e.n&&e[s+1]=='-':
for(i=u32(u8(e[s]));i<=u32(u8(e[s+2]));i++)
ok[i>>5]|=(1u<<int(i&31u));
s+=2
else
ok[e[s]>>5]|=(1u<<int(u32(u8(e[s]))&31u));
if inv:
for i=0:7
ok[i]=~ok[i]
return ok
digits=charset("0-9")
digdot=charset("0-9.")
hexdigit=charset("0-9a-fA-F")
idhead=charset("_A-Za-z\200-\377")
idbody=charset("_0-9A-Za-z\200-\377")
spaces=charset("\r\t ")
spaces_newline=charset("\r\n\t ")
qi_typename=charset("~$._[]0-9A-Za-z\200-\377")
qi_separator=charset("\r\n,)")
//qi_call_ptn_separator=charset(",;)]}")
newlines=charset("\r\n")
inline has(u32[] ok,int c)
return !(c&0xffffff00)&&((ok[c>>5]>>(c&31))&1u);
TOK_TYPE=0x20000000
TOK_TYPE_MASK=-TOK_TYPE
TOK_CONST=1*TOK_TYPE
TOK_ID=2*TOK_TYPE
TOK_STRING=3*TOK_TYPE
TOK_EOF=4*TOK_TYPE
TOK_FLAG_COMBO_PARAM_STRING=(TOK_TYPE>>1)
TOK_EQ=int('a')
TOK_NE=int('b')
TOK_LE=int('c')
TOK_GE=int('d')
TOK_AA=int('e')
TOK_OO=int('f')
TOK_LL=int('g')
TOK_GG=int('h')
TOK_ADD_ADD=int('i')
TOK_SUB_SUB=int('j')
TOK_ADD_EQ=int('k')
TOK_SUB_EQ=int('l')
TOK_MUL_EQ=int('m')
TOK_DIV_EQ=int('n')
////////////////
TOK_MOD_EQ=int('o')
TOK_OR_EQ=int('p')
TOK_AND_EQ=int('q')
TOK_XOR_EQ=int('r')
TOK_LSHIFT_EQ=int('s')
TOK_RSHIFT_EQ=int('t')
////////////////
TOK_COMMENT=int('u')
TOK_COMBO_SLOT=int('v') //not a real token
g_id_include=getid("#include")
g_id_define=getid("#define")
g_id_pragma=getid("#pragma")
g_id_pragma_mark=getid("#pragma mark - ")
g_id_typedef=getid("typedef")
g_id_require=getid("require")
g_id_import=getid("import")
g_id_in=getid("in")
g_id_this=getid("this")
g_id_home=getid("~")
g_id_1=getid("1")
g_id_slash=getid("-")
g_id_return=getid("return")
g_id_const=getid("const")
g_id_default=getid("__default")
g_id___c_function=getid("__c_function")
g_id___generate_json=getid("__generate_json")
g_id_ProjectAddHeader=getid("ProjectAddHeader")
g_id_ProjectAddSource=getid("ProjectAddSource")
g_id_ImportCFunction=getid("ImportCFunction")
g_sta_tr0=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,4,-1,-1,-1,4,10,-1,3,-1,4,5,-1,6,-1,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,7,9,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,-1,-1,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,11,-1,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
struct TToken
int tok
int epos0,epos1
struct TParsedTag
int id_side
int epos
class CTag
int id,idfn
int epos
auto tokenize(string sdata,int g_ignore_indents,int allow_unpaired_brackets)
auto feed=sdata[0:].ConvertToAsBinary(u8)
//we want feed to be zero-terminated
//merge all files to one big vector, add zeroes, and pass in the starting location
inline cite_raw(iptr epos0,iptr epos1)
assert(epos1>0)
return feed[epos0:epos1-1].ConvertToAsBinary(char)
ptr_start=0
ptr=ptr_start
inline skipchars(u32[] cs)
while ptr<feed.n
ch=int(u8(feed[ptr]))
if !CharSet.has(cs,ch):return
ptr++
inline parseOperator()
ptr0=ptr
state=int(g_sta_tr0[int(feed[ptr])]);ptr++
switch state{
default:
ptr=ptr0
return
case 1:
ch=int(feed[ptr]);ptr++
if ch==']':return
ptr=ptr0
return
case 2:
return;
case 3:
//operator() should be allowed
ch=int(feed[ptr]);ptr++
if ch==')':return
ptr=ptr0
return
case 4:
ch=int(feed[ptr]);ptr++
if ch=='=':return
ptr--;return;
case 5:
ch=int(feed[ptr]);ptr++
if ch=='+':return
if ch=='=':return
ptr--;return;
case 6:
ch=int(feed[ptr]);ptr++
if ch=='-':return
if ch=='=':return
ptr--;return;
case 7:
ch=int(feed[ptr]);ptr++
if ch=='<':{state=4;break}
if ch=='=':return
ptr--;return;
case 8:
ch=int(feed[ptr]);ptr++
if ch=='=':return
if ch=='>':{state=4;break}
ptr--;return;
case 9:
ch=int(feed[ptr]);ptr++
if ch=='=':return
ptr--;return;
case 10:
ch=int(feed[ptr]);ptr++
if ch=='&':return
if ch=='=':return
ptr--;return;
case 11:
ch=int(feed[ptr]);ptr++
if ch=='=':return
if ch=='|':return
ptr--;return;
}
assert(state==4)
ch=int(feed[ptr]);ptr++
if ch=='=':return
ptr--;
return;
//////////////////////////
struct TBracket
int ch
iptr pos
inds=new int[]
brastk=new TBracket[]
toks=new TToken[]
ignore_indents=g_ignore_indents
last_significant_newline_position=-1L
last_line_comment_position=-1L
inline left_bracket(int ch0)
brastk.push(TBracket(){ch:ch0,pos:iptr(ptr)})
if ch0=='{':
ignore_indents=g_ignore_indents
if !ignore_indents:
if inds.n:
ind0=inds.back()
else
ind0=0
inds.push(ind0|0x80000000)
else
ignore_indents=1
inline push_token(int tok,int epos0,int epos1)
toks.push(TToken(){tok:tok,epos0:epos0,epos1:epos1})
inline pop_indents()
//add } accordingly
//ignore the last dedent
while inds.n&&!(inds.back()&0x80000000)
if !(inds.back()&0x40000000):
push_token(int('}'),int(ptr),int(ptr))
inds.pop()
if inds.n:
inds.pop()
auto right_bracket(int ch0)
matcher=int(ch0==')'?'(':(ch0=='}'?'{':(ch0==']'?'[':'{')))
errored=0
err_pos=0
estr=string.NULL
while brastk.n&&brastk.back().ch!=matcher:
//compensate for the unmatching bracket
if !errored:
//if ch0==0:
// //EOF
// error(ETYPE_ERROR,ptr,ptr,"the opening '@1' is not properly closed".Replace(["@1",string(char(brastk.back().ch))]))
//else
// error(ETYPE_ERROR,ptr,ptr+1,"the opening '@1' doesn't match the closing '@2'".Replace(["@1",string(char(brastk.back().ch)),"@2",string(char(ch0))]))
err_pos=int(brastk.back().pos)
estr=new string
errored=1
ch_poped=brastk.back().ch
matcher_ch_poped=char(ch_poped=='('?')':(ch_poped=='{'?'}':']'))
estr.push(matcher_ch_poped)
push_token(int(matcher_ch_poped),int(ptr),int(ptr)+1)
brastk.pop()
if ch_poped=='{'&&!g_ignore_indents:
pop_indents()
if errored:
if brastk.n:
epos_pop_to=brastk.back().pos+1
else
epos_pop_to=ptr_start
//error(ETYPE_NOTE,ptr,ptr,"inserting '@1'".Replace(["@1",estr]))
//if brastk.n:
// error(ETYPE_NOTE,err_pos,err_pos+1,"the opening '@1' was here".Replace(["@1",string(char(brastk.back().ch))]))
//else
// error(ETYPE_NOTE,err_pos,err_pos+1,"the opening bracket was not found")
//error(ETYPE_NOTE,epos_pop_to,epos_pop_to,FormatAsText("automatically matching dangling opening brackets until here"))
if brastk.n:
ch_poped=brastk.back().ch
brastk.pop()
if ch_poped=='{'&&!g_ignore_indents:
pop_indents()
else
if !errored&&ch0:
//error(ETYPE_ERROR,ptr,ptr+1,"the closing '@1' does not close any opening bracket".Replace(["@1",string(char(ch0))]))
return 0
if brastk.n&&brastk.back().ch!='{':
ignore_indents=1
else
ignore_indents=g_ignore_indents
return 1
s_include="include"
s_define="define"
s_pragma="pragma"
s_mark="mark"
for(;;)
skipchars(ignore_indents?CharSet.spaces_newline:CharSet.spaces)
ch=int(feed[ptr])
//numbers
if CharSet.has(CharSet.digits,ch):
s=ptr
ptr++
skipchars(CharSet.idbody)
id=getid(cite_raw(s,ptr))
push_token(TOK_CONST+id,int(s),int(ptr))
continue
//id
if CharSet.has(CharSet.idhead,ch):
s=ptr
ptr++
skipchars(CharSet.idbody)
if ptr-s>=8:
if cite_raw(ptr-8,ptr)=="operator":
//operator state machine
parseOperator()
else if ptr-s==8&&cite_raw(ptr-8,ptr)=="function"&&int(feed[ptr])==int('*'):
//ES6 function*
ptr++
id=getid(cite_raw(s,ptr))
push_token(TOK_ID+id,int(s),int(ptr))
continue
ch0=ch
if !ch0:break
epos0_ch0=ptr
ptr++;ch=int(feed[ptr])
//is_at_str=0
switch ch0{
default:
break;//nothing
case '[','(','{':
left_bracket(ch0)
break;
case ')',']','}':
if !right_bracket(ch0):
//we shouldn't put it in
continue
break;
case '=':
if(ch=='=')
ptr++
ch0=TOK_EQ;
break;
case '+':
if(ch=='='){ptr++;ch0=TOK_ADD_EQ;}else
if(ch=='+'){ptr++;ch0=TOK_ADD_ADD;}
break
case '-':
if(ch=='='){ptr++;ch0=TOK_SUB_EQ;}else
if(ch=='-'){ptr++;ch0=TOK_SUB_SUB;}else
if(ch=='>'){ptr++;ch0=int('.');}
break
case '*':
if(ch=='='){ptr++;ch0=TOK_MUL_EQ;}
break
case '<':
if(ch=='='){ptr++;ch0=TOK_LE;}else
if(ch=='<')
ptr++;ch0=TOK_LL;
if feed[ptr]==u8('='):
ptr++;ch0=TOK_LSHIFT_EQ;
break;
case '>':
if(ch=='='){ptr++;ch0=TOK_GE;}else\
if(ch=='>')
ptr++;ch0=TOK_GG;
if feed[ptr]==u8('='):
ptr++;ch0=TOK_RSHIFT_EQ;
break;
case '!':{if(ch=='='){ptr++;ch0=TOK_NE;}break;}
case ':':{if(ch==':'){ptr++;ch0=int('.');}break;}
case '&':
if(ch=='&'){ptr++;ch0=TOK_AA;}
if(ch=='='){ptr++;ch0=TOK_AND_EQ;}
break;
case '|':
if(ch=='|'){ptr++;ch0=TOK_OO;}
if(ch=='='){ptr++;ch0=TOK_OR_EQ;}
break;
case '%':
if(ch=='='){ptr++;ch0=TOK_MOD_EQ;}
break
case '^':
if(ch=='='){ptr++;ch0=TOK_XOR_EQ;}
break
//indent stuff
case '\\':
//line-escape
if(ch=='\n'||ch=='\r')
skipchars(CharSet.newlines)
continue
break;
case '\n':
//just a normal space when gettext AND no indent
assert(!ignore_indents)
epos0=ptr
app=0
ind=0
for(ind=0;;)
ch=int(feed[ptr]);
if !CharSet.has(CharSet.spaces_newline,ch):break
ptr++
if ch=='\r':
//nothing
else if ch=='\n':
ind=0;app=0
else
if ch=='\t':{app|=1}else app|=2;
ind++;
if app==3:
app=7
//error(ETYPE_ERROR,int(epos0),ptr,"space and tab can't be mixed in indentation")
if !feed[ptr]:
ind=0
if int(feed[ptr])=='}':
//auto-pop to 0x80000000 during the latter }
//pop_indents()
continue
else
ind0=(inds.n?(inds.back()&0x3fffffff):0)
if ind0!=ind:
if ind0<ind:
if inds.n&&(inds.back()&0x80000000)&&toks.back().tok==int('{'):
//ignore the first indent immediately following a {
inds.push(ind|0x40000000)
last_significant_newline_position=toks.n
continue
inds.push(ind)
ch0=int('{')
epos0_ch0=ptr
else
//don't pop past the {
while inds.n&&(inds.back()&0xbfffffff)>ind:
inds.pop();
push_token(int('}'),int(ptr),int(ptr))
if !inds.n:
if ind:
//error(ETYPE_ERROR,epos0_ch0,ptr,FormatAsText("indentation mismatch - this line is indented less than the first line in the file"))
else if (inds.back()&0x3fffffff)!=ind:
//indentation mismatch
//error(ETYPE_ERROR,epos0_ch0,ptr,FormatAsText("indentation mismatch - @1 expected but @2 provided").Replace(["@1",string(inds.back()),"@2",string(ind)]))
last_significant_newline_position=toks.n
continue
else
ch0=int(';')
last_significant_newline_position=toks.n
if toks.n:
ch_lastline=toks.back().tok
if ch_lastline==int(';')||ch_lastline==int(',')||ch_lastline==int('{'):
continue
if toks.n==last_line_comment_position:
//line comments shouldn't generate ;, but they should generate {}
continue
last_significant_newline_position=toks.n+1
break
//char/string literal
case '"','\'','`':
//is_python_str=0
//if ch==ch0:
// if ptr<feed.n-1&&(int)feed[ptr+1]==(int)ch0:
// //python string
// is_python_str=1
// ptr+=2
efeed0=ptr
c0=ch0
isrecover=0
//str=new string
for(;;)
c=int(feed[ptr]);ptr++
if c==0:
if isrecover:
ptr--
break
ptr--
//error(ETYPE_ERROR,int(efeed0),ptr,"this string is not properly enclosed")
ptr=efeed0
isrecover=1
//str.clear()
continue
if c=='\\'://&&!is_at_str:
ch=int(feed[ptr])
if ch=='\r'||ch=='\n':
ptr++;ch=int(feed[ptr])
if ch=='\r'||ch=='\n':
ptr++;ch=int(feed[ptr])
continue
c=ch;ptr++;
switch c{
case 'n':
c=int('\n');break;
case 'r':
c=int('\r');break;
case 't':
c=int('\t');break;
case 'b':
c=int('\b');break;
case 'e':
c=27;
break;
case 'x','u':
chu=0
for j=0:(c=='u'?3:1)
chj=int(feed[ptr])
if !CharSet.has(CharSet.hexdigit,chj):
break
if chj:ptr++
si=((chj-'0')&0x1f)
if si>=0x10:si-=7
chu=chu*16+(si&0xf)
if c=='x':
//str.push(char(chu))
else
if chu>=2048:
//str.push(char(((chu>>12)&0xf)+0xe0))
//str.push(char(0x80+((chu>>6)&63)))
//str.push(char(0x80+(chu&63)))
else if chu>=128:
//str.push(char((chu>>6)+0xc0))
//str.push(char(0x80+(chu&63)))
else
//str.push(char(chu))
continue
default:
if CharSet.has(CharSet.digits,c):
ptr--
s=ptr
skipchars(CharSet.digits)
c=0
for(;s!=ptr;s++)
si=int(feed[s])
c=c*8+(si-'0')
break
}
else
if c==c0:
//if is_python_str:
// if ptr<=feed.n-2&&feed[ptr]==c0&&feed[ptr+1]==c0:
// ptr+=2
// break
// else
// goto goodchar0
break;
if isrecover&&(c=='\r'||c=='\n'):break
//:goodchar0
//str.push(char(c))
//if ch0=='\''&&str.n==1:
// //char
// toks.push(TToken(){'tok':TOK_CONST+getid_const(const_type(CTYPE_INT,8),i64(str[0])),'epos0':int(efeed0)-1,'epos1':int(ptr)})
//else
//toks.push(TToken(){'tok':TOK_STRING+getid(str),'epos0':int(efeed0)-1,'epos1':int(ptr)})
push_token(TOK_STRING,int(efeed0),int(ptr)-1)
//coulddo: python tri-quote strings
continue
//comments
case '/':
if(ch=='='){ptr++;ch0=TOK_DIV_EQ;break;}
if ch=='/':
ptr++
efeed0=ptr
if ptr<feed.n-6&&__basic_api.memcmp(__pointer(feed.d+ptr),"@qpad",5)==0&&(feed[ptr+5]==u8(' ')||feed[ptr+5]==u8('#')):
if feed[ptr+5]==u8(' '):
ptr+=6
push_token(TOK_COMMENT,int(efeed0-2),int(ptr))
else if feed[ptr+5]==u8('#'):
//@qpad# tag_name
ptr+=6
push_token(TOK_ID+g_id_pragma_mark,int(efeed0-2),int(ptr))
else
for(;;)
ch=int(feed[ptr])
if ch==0||ch=='\n':break
ptr++
if last_significant_newline_position==toks.n:
last_line_comment_position=toks.n
//merge comments to avoid perf issues during token neib lookup
can_merge=1
if ENABLE_EXPERIMENTAL_FEATURES:
for j=efeed0:min(ptr,efeed0+3)-1
if int(feed[j])==int('@'):
can_merge=0
break
if can_merge&&toks.n>0&&toks.back().tok==TOK_COMMENT:
toks[toks.n-1].epos1=int(ptr)
else
push_token(TOK_COMMENT,int(efeed0),int(ptr))
//parseComment(feed,efeed0,ptr+1)
continue
if ch=='*':{
ptr++
efeed0=ptr
auto c=0;
if ENABLE_EXPERIMENTAL_FEATURES&&ptr<feed.n-6&&__basic_api.memcmp(__pointer(feed.d+ptr),"@qpad ",6)==0:
ptr+=6
push_token(TOK_COMMENT,int(efeed0-2),int(ptr))
else
for(;;)
ch=int(feed[ptr])
if ch==0:break
ptr++
if ch=='/'&&c=='*':break;
c=ch;
//merge comments to avoid perf issues during token neib lookup
can_merge=1
if ENABLE_EXPERIMENTAL_FEATURES:
for j=efeed0:min(ptr,efeed0+3)-1
if int(feed[j])==int('@'):
can_merge=0
break
if can_merge&&toks.n>0&&toks.back().tok==TOK_COMMENT:
toks[toks.n-1].epos1=int(ptr-2)
else
push_token(TOK_COMMENT,int(efeed0),int(ptr-2))
//parseComment(feed,efeed0,ptr)
continue
}
break;
case '#':
//# as comment for Python, shell and C/C++
pword=-1L
in_slash=0
efeed0=ptr
for(;;)
ch=int(feed[ptr])
if ch==0||!in_slash&&ch=='\n':break
if in_slash:
in_slash=0
else if ch=='\\':
in_slash=1
if pword<0&&ch!=' '&&ch!='\t':
pword=ptr
ptr++
//parseComment(feed,efeed0,ptr)
if last_significant_newline_position==toks.n:
last_line_comment_position=toks.n
//C macro recognition: #define, #include
//make into token lists
if feed.n-pword>=8:
bk_ptr=ptr
if __basic_api.memcmp(__pointer(feed.d+pword),__pointer(s_include.d),s_include.n)==0:
//#include
push_token(TOK_ID+g_id_include,int(pword),int(pword+s_include.n))
//skip spaces
ptr=pword+s_include.n
skipchars(CharSet.spaces)
if ptr<feed.n:
c0=int(feed[ptr])
ptr++
if c0=='<':c0=int('>')
if c0=='>'||c0=='"':
//just tokens for now
efeed0=ptr
str=new string
while ptr<bk_ptr
c=int(feed[ptr]);ptr++
if c=='\\':
ch=int(feed[ptr])
if ch=='\r'||ch=='\n':
ptr++;ch=int(feed[ptr])
if ch=='\r'||ch=='\n':
ptr++;ch=int(feed[ptr])
continue
c=ch;ptr++;
switch c{
case 'n':
c=int('\n');break;
case 'r':
c=int('\r');break;
case 't':
c=int('\t');break;
case 'b':
c=int('\b');break;
case 'e':
c=27;
break;
case 'x','u':
chu=0
for j=0:(c=='u'?3:1)
chj=int(feed[ptr])
if !CharSet.has(CharSet.hexdigit,chj):
break
if chj:ptr++
si=((chj-'0')&0x1f)
if si>=0x10:si-=7
chu=chu*16+(si&0xf)
if c=='x':
str.push(char(chu))
else
if chu>=2048:
str.push(char(((chu>>12)&0xf)+0xe0))
str.push(char(0x80+((chu>>6)&63)))
str.push(char(0x80+(chu&63)))
else if chu>=128:
str.push(char((chu>>6)+0xc0))
str.push(char(0x80+(chu&63)))
else
str.push(char(chu))
continue
default:
if CharSet.has(CharSet.digits,c):
ptr--
s=ptr
skipchars(CharSet.digits)
c=0
for(;s!=ptr;s++)
si=int(feed[s])
c=c*8+(si-'0')
break
}
else
if c==c0:break
str.push(char(c))
//need the id for include
push_token(TOK_STRING+getid(str),int(efeed0),int(ptr)-1)
push_token(int(';'),int(bk_ptr-1),int(bk_ptr))
else if __basic_api.memcmp(__pointer(feed.d+pword),__pointer(s_define.d),s_define.n)==0:
//#define
push_token(TOK_ID+g_id_define,int(pword),int(pword+s_define.n))
//grab the id, test for ()
ptr=pword+s_define.n
skipchars(CharSet.spaces)
s=ptr
skipchars(CharSet.idbody)
//Writeln('#define ',pword,' ',s,' ',ptr,' ',cite_raw(pword,ptr))
id=getid(cite_raw(s,ptr))
push_token(TOK_ID+id,int(s),int(ptr))
if ptr<feed.n&&int(feed[ptr])=='(':
push_token(int('('),int(ptr),int(ptr))
push_token(int(')'),int(ptr),int(ptr))
push_token(int(';'),int(bk_ptr-1),int(bk_ptr))
else if __basic_api.memcmp(__pointer(feed.d+pword),__pointer(s_pragma.d),s_pragma.n)==0:
ptr=pword+s_pragma.n
skipchars(CharSet.spaces)
if __basic_api.memcmp(__pointer(feed.d+ptr),__pointer(s_mark.d),s_mark.n)==0:
ptr+=s_mark.n
skipchars(CharSet.spaces)
if ptr<feed.n&&int(feed[ptr])=='-':
ptr++
push_token(TOK_ID+g_id_pragma_mark,int(pword),int(ptr))
skipchars(CharSet.spaces)
id=getid(cite_raw(ptr,bk_ptr))
push_token(TOK_ID+id,int(ptr),int(bk_ptr))
push_token(int(';'),int(bk_ptr),int(bk_ptr))
ptr=bk_ptr
continue
}
//toks.push(TToken(){'tok':ch0,'epos0':int(epos0_ch0),'epos1':int(ptr)})
push_token(ch0,int(epos0_ch0),int(ptr))
//////////////////
if !allow_unpaired_brackets:
while brastk.n:
right_bracket(0)
pop_indents()
//toks.push(TToken(){'tok':TOK_EOF,'epos0':int(ptr),'epos1':int(ptr)})
push_token(TOK_EOF,int(ptr),int(ptr))
return toks
/*
auto parseComment(u8[] feed,int ptr0,int ptr_end)
//find the convention strings
inline skipchars(u32[] cs)
while ptr<feed.n
ch=int(u8(feed[ptr]))
if !CharSet.has(cs,ch):return
ptr++
is_first=0
ptr=ptr0
toks=new int[]
for(;ptr<ptr_end;)
skipchars(is_first?CharSet.spaces_newline:CharSet.spaces)
ch=int(feed[ptr])
//id
if CharSet.has(CharSet.idbody,ch):
s=ptr
ptr++
skipchars(CharSet.idbody)
if is_first:
if ptr==s+2&&feed[s]=='w'&&feed[s+1]=='e':
//trigger word: we
else
break
id=getid(feed[s:ptr-1].ConvertToAsBinary(char))
toks.push(TOK_ID+id)
continue
ch0=ch
if !ch0:break
epos0_ch0=ptr
ptr++;ch=int(feed[ptr])
switch ch0{
default:
break;//nothing
case ':':
ptr++
skipchars(CharSet.spaces)
toks.push(TOK_STRING+ptr)
continue;
case '\n':
ch0=';'
break;
}
toks.push(int(ch0))
if toks.n:
//generate tokens for function correspondence
parseConvention(toks)
class CConventionList
auto parseConvention(int[] toks)
//for each line, search key word
//ignore we say
*/
//////////////////////////////////////////////////////////////////
/*
permissive parser:
make sure the brackets match
do we even attempt to deduce types?
maybe not
use the old code?
not broken, don't fix
the data structure was messy...
copy paste from there
comment parser - English text
old version:
cursor history not working
make the .name func() system an independent pass
per-file PPM models
*/
//global by-last-symbol model (for plain text)
struct TDeclItem
int epos_scope
int id
int epos0
struct TPrototypeItem
int id
iptr ptr_prototype
iptr ptr_fidx
struct TDocedVariable
int epos_scope
int id
int2 pdoc
struct TIDBrief
int epos_comment
int id
int2 pdoc
struct TIDBriefWithFile
iptr ptr_fidx
int epos_comment
int id
int2 pdoc
///////////////
//qinfo-related things
struct TQIType
int id
int2[] annotations
struct TQIObject
int id
TQIType t
//params can be in-out
QIPARAM_IN=0
QIPARAM_OUT=0x80000000
struct TQIParamDesc
int id_inout
TQIType t
inline GetID()
return (id_inout&0x7fffffff)
QITypeToJS=inline(JSContext JS,TQIType t){
auto ret=JS.New()
ret["*"]=getIdString(t.id)
if t.annotations:
for ai in t.annotations
ret[getIdString(ai.x)]=getIdString(ai.y)
return ret
}
class CQIFunc
//we want to index methods by effect... and by applicability
//id and epos are fine to include though
//params / call pattern
int epos
TQIParamDesc[] m_params
int[] call_ptn
int[][] optional_prefixes
int[][] prefix_param_ids
int id
string s_brief
////////////
int[] s_param_id
int p_sig
int id_sig
int p_index
int p_index_call_ptn
int2[] tmp_param_match_slots
////////////
JS_toJSON=function(JSContext JS){
//coulddo: give some documentation to aid the user
auto ret=JS.New()
ret["id"]=getIdString(id)
auto params=JS.NewArray()
for p,I in m_params
param_i=JS.New()
params[I]=param_i
param_i["id"]=getIdString(p.GetID())
if p.id_inout&QIPARAM_OUT:
param_i["is_out"]=1
obj_t=QITypeToJS(JS,p.t)
param_i["t"]=obj_t
ret["m_params"]=params