-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.cpp
5559 lines (4574 loc) · 146 KB
/
parser.cpp
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
#ifdef GEN_INTELLISENSE_DIRECTIVES
#pragma once
#include "gen/etoktype.cpp"
#include "parser_case_macros.cpp"
#include "interface.upfront.cpp"
#include "lexer.cpp"
#endif
// TODO(Ed) : Rename ETok_Capture_Start, ETok_Capture_End to Open_Parenthesis adn Close_Parenthesis
constexpr bool lex_dont_skip_formatting = false;
constexpr bool lex_skip_formatting = true;
void parser_push( ParseContext* ctx, StackNode* node )
{
node->Prev = ctx->Scope;
ctx->Scope = node;
#if 0 && GEN_BUILD_DEBUG
log_fmt("\tEntering _ctx->parser: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr );
#endif
}
void parser_pop(ParseContext* ctx)
{
#if 0 && GEN_BUILD_DEBUG
log_fmt("\tPopping _ctx->parser: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr );
#endif
ctx->Scope = ctx->Scope->Prev;
}
StrBuilder parser_to_strbuilder(ParseContext ctx)
{
StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(4) );
Token scope_start = * ctx.Scope->Start;
Token last_valid = ctx.Tokens.Idx >= array_num(ctx.Tokens.Arr) ? ctx.Tokens.Arr[array_num(ctx.Tokens.Arr) -1] : (* lex_current(& ctx.Tokens, true));
sptr length = scope_start.Text.Len;
char const* current = scope_start.Text.Ptr + length;
while ( current <= array_back( ctx.Tokens.Arr)->Text.Ptr && (* current) != '\n' && length < 74 )
{
current++;
length++;
}
Str scope_str = { scope_start.Text.Ptr, length };
StrBuilder line = strbuilder_make_str( _ctx->Allocator_Temp, scope_str );
strbuilder_append_fmt( & result, "\tScope : %s\n", line );
strbuilder_free(& line);
sptr dist = (sptr)last_valid.Text.Ptr - (sptr)scope_start.Text.Ptr + 2;
sptr length_from_err = dist;
Str err_str = { last_valid.Text.Ptr, length_from_err };
StrBuilder line_from_err = strbuilder_make_str( _ctx->Allocator_Temp, err_str );
if ( length_from_err < 100 )
strbuilder_append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' );
else
strbuilder_append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column );
StackNode* curr_scope = ctx.Scope;
s32 level = 0;
do
{
if ( curr_scope->Name.Ptr ) {
strbuilder_append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Len, curr_scope->Name.Ptr );
}
else {
strbuilder_append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr );
}
curr_scope = curr_scope->Prev;
level++;
}
while ( curr_scope );
return result;
}
bool lex__eat(TokArray* self, TokType type )
{
if ( array_num(self->Arr) - self->Idx <= 0 ) {
log_failure( "No tokens left.\n%s", parser_to_strbuilder(_ctx->parser) );
return false;
}
Token at_idx = self->Arr[ self->Idx ];
if ( ( at_idx.Type == Tok_NewLine && type != Tok_NewLine )
|| ( at_idx.Type == Tok_Comment && type != Tok_Comment ) )
{
self->Idx ++;
}
b32 not_accepted = at_idx.Type != type;
b32 is_identifier = at_idx.Type == Tok_Identifier;
if ( not_accepted )
{
Macro* macro = lookup_macro(at_idx.Text);
b32 accept_as_identifier = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Identifier );
not_accepted = type == Tok_Identifier && accept_as_identifier ? false : true;
}
if ( not_accepted )
{
Token tok = * lex_current( self, lex_skip_formatting );
log_failure( "Parse Error, TokArray::eat, Expected: ' %s ' not ' %.*s ' (%d, %d)`\n%s"
, toktype_to_str(type).Ptr
, at_idx.Text.Len, at_idx.Text.Ptr
, tok.Line
, tok.Column
, parser_to_strbuilder(_ctx->parser)
);
GEN_DEBUG_TRAP();
return false;
}
#if 0 && GEN_BUILD_DEBUG
log_fmt("Ate: %SB\n", self->Arr[Idx].to_strbuilder() );
#endif
self->Idx ++;
return true;
}
internal
void parser_init()
{
_ctx->Lexer_Tokens = array_init_reserve(Token, _ctx->Allocator_DyanmicContainers, _ctx->InitSize_LexerTokens );
}
internal
void parser_deinit()
{
Array(Token) null_array = { nullptr };
_ctx->Lexer_Tokens = null_array;
}
#pragma region Helper Macros
#define check_parse_args( def ) _check_parse_args(def, stringize(_func_) )
bool _check_parse_args( Str def, char const* func_name )
{
if ( def.Len <= 0 )
{
log_failure( c_str_fmt_buf("gen::%s: length must greater than 0", func_name) );
parser_pop(& _ctx->parser);
return false;
}
if ( def.Ptr == nullptr )
{
log_failure( c_str_fmt_buf("gen::%s: def was null", func_name) );
parser_pop(& _ctx->parser);
return false;
}
return true;
}
# define currtok_noskip (* lex_current( & _ctx->parser.Tokens, lex_dont_skip_formatting ))
# define currtok (* lex_current( & _ctx->parser.Tokens, lex_skip_formatting ))
# define peektok (* lex_peek(_ctx->parser.Tokens, lex_skip_formatting))
# define prevtok (* lex_previous( _ctx->parser.Tokens, lex_dont_skip_formatting))
# define nexttok (* lex_next( _ctx->parser.Tokens, lex_skip_formatting ))
# define nexttok_noskip (* lex_next( _ctx->parser.Tokens, lex_dont_skip_formatting))
# define eat( Type_ ) lex__eat( & _ctx->parser.Tokens, Type_ )
# define left ( array_num(_ctx->parser.Tokens.Arr) - _ctx->parser.Tokens.Idx )
#if GEN_COMPILER_CPP
# define def_assign( ... ) { __VA_ARGS__ }
#else
# define def_assign( ... ) __VA_ARGS__
#endif
#ifdef check
#define CHECK_WAS_DEFINED
#pragma push_macro("check")
#undef check
#endif
# define check_noskip( Type_ ) ( left && currtok_noskip.Type == Type_ )
# define check( Type_ ) ( left && currtok.Type == Type_ )
# define push_scope() \
Str null_name = {}; \
StackNode scope = { nullptr, lex_current( & _ctx->parser.Tokens, lex_dont_skip_formatting ), null_name, txt( __func__ ) }; \
parser_push( & _ctx->parser, & scope )
#pragma endregion Helper Macros
// Procedure Forwards ( Entire parser internal parser interface )
internal Code parse_array_decl ();
internal CodeAttributes parse_attributes ();
internal CodeComment parse_comment ();
internal Code parse_complicated_definition ( TokType which );
internal CodeBody parse_class_struct_body ( TokType which, Token name );
internal Code parse_class_struct ( TokType which, bool inplace_def );
internal Code parse_expression ();
internal Code parse_forward_or_definition ( TokType which, bool is_inplace );
internal CodeFn parse_function_after_name ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type, Token name );
internal Code parse_function_body ();
internal CodeBody parse_global_nspace ( CodeType which );
internal Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers );
internal Token parse_identifier ( bool* possible_member_function );
internal CodeInclude parse_include ();
internal Code parse_macro_as_definiton ( CodeAttributes attributes, CodeSpecifiers specifiers );
internal CodeOperator parse_operator_after_ret_type ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename ret_type );
internal Code parse_operator_function_or_variable( bool expects_function, CodeAttributes attributes, CodeSpecifiers specifiers );
internal CodePragma parse_pragma ();
internal CodeParams parse_params ( bool use_template_capture );
internal CodePreprocessCond parse_preprocess_cond ();
internal Code parse_simple_preprocess ( TokType which );
internal Code parse_static_assert ();
internal void parse_template_args ( Token* token );
internal CodeVar parse_variable_after_name ( ModuleFlag mflags, CodeAttributes attributes, CodeSpecifiers specifiers, CodeTypename type, Str name );
internal CodeVar parse_variable_declaration_list ();
internal CodeClass parser_parse_class ( bool inplace_def );
internal CodeConstructor parser_parse_constructor ( CodeSpecifiers specifiers );
internal CodeDefine parser_parse_define ();
internal CodeDestructor parser_parse_destructor ( CodeSpecifiers specifiers );
internal CodeEnum parser_parse_enum ( bool inplace_def );
internal CodeBody parser_parse_export_body ();
internal CodeBody parser_parse_extern_link_body();
internal CodeExtern parser_parse_extern_link ();
internal CodeFriend parser_parse_friend ();
internal CodeFn parser_parse_function ();
internal CodeNS parser_parse_namespace ();
internal CodeOpCast parser_parse_operator_cast ( CodeSpecifiers specifiers );
internal CodeStruct parser_parse_struct ( bool inplace_def );
internal CodeVar parser_parse_variable ();
internal CodeTemplate parser_parse_template ();
internal CodeTypename parser_parse_type ( bool from_template, bool* is_function );
internal CodeTypedef parser_parse_typedef ();
internal CodeUnion parser_parse_union ( bool inplace_def );
internal CodeUsing parser_parse_using ();
constexpr bool parser_inplace_def = true;
constexpr bool parser_not_inplace_def = false;
constexpr bool parser_dont_consume_braces = true;
constexpr bool parser_consume_braces = false;
constexpr bool parser_not_from_template = false;
constexpr bool parser_use_parenthesis = false;
// Internal parsing functions
constexpr bool parser_strip_formatting_dont_preserve_newlines = false;
/*
This function was an attempt at stripping formatting from any c++ code.
It has edge case failures that prevent it from being used in function bodies.
*/
internal
StrBuilder parser_strip_formatting( Str raw_text, bool preserve_newlines )
{
StrBuilder content = strbuilder_make_reserve( _ctx->Allocator_Temp, raw_text.Len );
if ( raw_text.Len == 0 )
return content;
#define cut_length ( scanner - raw_text.Ptr - last_cut )
#define cut_ptr ( raw_text.Ptr + last_cut )
#define pos ( rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr ) )
#define move_fwd() do { scanner++; tokleft--; } while(0)
s32 tokleft = raw_text.Len;
sptr last_cut = 0;
char const* scanner = raw_text.Ptr;
if ( scanner[0] == ' ' ) {
move_fwd();
last_cut = 1;
}
bool within_string = false;
bool within_char = false;
bool must_keep_newline = false;
while ( tokleft )
{
// Skip over the content of string literals
if ( scanner[0] == '"' )
{
move_fwd();
while ( tokleft && ( scanner[0] != '"' || *( scanner - 1 ) == '\\' ) )
{
if ( scanner[0] == '\\' && tokleft > 1 )
{
scanner += 2;
tokleft -= 2;
}
else
{
move_fwd();
}
}
// Skip the closing "
if ( tokleft )
move_fwd();
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
last_cut = rcast(sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
// Skip over the content of character literals
if ( scanner[0] == '\'' )
{
move_fwd();
while ( tokleft
&& ( scanner[0] != '\''
|| ( *(scanner -1 ) == '\\' )
) )
{
move_fwd();
}
// Skip the closing '
if ( tokleft )
move_fwd();
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
// Block comments
if ( tokleft > 1 && scanner[0] == '/' && scanner[1] == '*' )
{
while ( tokleft > 1 && !(scanner[0] == '*' && scanner[1] == '/') )
move_fwd();
scanner += 2;
tokleft -= 2;
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
// Line comments
if ( tokleft > 1 && scanner[0] == '/' && scanner[1] == '/' )
{
must_keep_newline = true;
scanner += 2;
tokleft -= 2;
while ( tokleft && scanner[ 0 ] != '\n' )
move_fwd();
if (tokleft)
move_fwd();
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
// Tabs
if (scanner[0] == '\t')
{
if (pos > last_cut)
strbuilder_append_c_str_len( & content, cut_ptr, cut_length);
if ( * strbuilder_back( content ) != ' ' )
strbuilder_append_char( & content, ' ' );
move_fwd();
last_cut = rcast( sptr, scanner) - rcast( sptr, raw_text.Ptr);
continue;
}
if ( tokleft > 1 && scanner[0] == '\r' && scanner[1] == '\n' )
{
if ( must_keep_newline || preserve_newlines )
{
must_keep_newline = false;
scanner += 2;
tokleft -= 2;
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
if ( pos > last_cut )
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
// Replace with a space
if ( * strbuilder_back( content ) != ' ' )
strbuilder_append_char( & content, ' ' );
scanner += 2;
tokleft -= 2;
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
if ( scanner[0] == '\n' )
{
if ( must_keep_newline || preserve_newlines )
{
must_keep_newline = false;
move_fwd();
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
if ( pos > last_cut )
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
// Replace with a space
if ( * strbuilder_back( content ) != ' ' )
strbuilder_append_char( & content, ' ' );
move_fwd();
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
// Escaped newlines
if ( scanner[0] == '\\' )
{
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
s32 amount_to_skip = 1;
if ( tokleft > 1 && scanner[1] == '\n' )
{
amount_to_skip = 2;
}
else if ( tokleft > 2 && scanner[1] == '\r' && scanner[2] == '\n' )
{
amount_to_skip = 3;
}
if ( amount_to_skip > 1 && pos == last_cut )
{
scanner += amount_to_skip;
tokleft -= amount_to_skip;
}
else
move_fwd();
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
continue;
}
// Consectuive spaces
if ( tokleft > 1 && char_is_space( scanner[0] ) && char_is_space( scanner[ 1 ] ) )
{
strbuilder_append_c_str_len( & content, cut_ptr, cut_length );
do
{
move_fwd();
}
while ( tokleft && char_is_space( scanner[0] ) );
last_cut = rcast( sptr, scanner ) - rcast( sptr, raw_text.Ptr );
// Preserve only 1 space of formattting
char* last = strbuilder_back(content);
if ( last == nullptr || * last != ' ' )
strbuilder_append_char( & content, ' ' );
continue;
}
move_fwd();
}
if ( last_cut < raw_text.Len ) {
strbuilder_append_c_str_len( & content, cut_ptr, raw_text.Len - last_cut );
}
#undef cut_ptr
#undef cut_length
#undef pos
#undef move_fwd
return content;
}
internal
Code parse_array_decl()
{
push_scope();
if ( check( Tok_Operator ) && currtok.Text.Ptr[0] == '[' && currtok.Text.Ptr[1] == ']' )
{
Code array_expr = untyped_str( txt(" ") );
eat( Tok_Operator );
// []
parser_pop(& _ctx->parser);
return array_expr;
}
if ( check( Tok_BraceSquare_Open ) )
{
eat( Tok_BraceSquare_Open );
// [
if ( left == 0 )
{
log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", parser_to_strbuilder(_ctx->parser) );
parser_pop(& _ctx->parser);
return InvalidCode;
}
if ( currtok.Type == Tok_BraceSquare_Close )
{
log_failure( "Error, empty array expression in definition\n%s", parser_to_strbuilder(_ctx->parser) );
parser_pop(& _ctx->parser);
return InvalidCode;
}
Token untyped_tok = currtok;
while ( left && currtok.Type != Tok_BraceSquare_Close )
{
eat( currtok.Type );
}
untyped_tok.Text.Len = ( (sptr)prevtok.Text.Ptr + prevtok.Text.Len ) - (sptr)untyped_tok.Text.Ptr;
Code array_expr = untyped_str( untyped_tok.Text );
// [ <Content>
if ( left == 0 )
{
log_failure( "Error, unexpected end of array declaration, expected ]\n%s", parser_to_strbuilder(_ctx->parser) );
parser_pop(& _ctx->parser);
return InvalidCode;
}
if ( currtok.Type != Tok_BraceSquare_Close )
{
log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", toktype_to_str( currtok.Type ), parser_to_strbuilder(_ctx->parser) );
parser_pop(& _ctx->parser);
return InvalidCode;
}
eat( Tok_BraceSquare_Close );
// [ <Content> ]
// Its a multi-dimensional array
if ( check( Tok_BraceSquare_Open ))
{
Code adjacent_arr_expr = parse_array_decl();
// [ <Content> ][ <Content> ]...
array_expr->Next = adjacent_arr_expr;
}
parser_pop(& _ctx->parser);
return array_expr;
}
parser_pop(& _ctx->parser);
return NullCode;
}
internal inline
CodeAttributes parse_attributes()
{
push_scope();
Token start = currtok;
s32 len = 0;
// There can be more than one attribute. If there is flatten them to a single string.
// TODO(Ed): Support chaining attributes (Use parameter linkage pattern)
while ( left && tok_is_attribute(currtok) )
{
if ( check( Tok_Attribute_Open ) )
{
eat( Tok_Attribute_Open );
// [[
while ( left && currtok.Type != Tok_Attribute_Close )
{
eat( currtok.Type );
}
// [[ <Content>
eat( Tok_Attribute_Close );
// [[ <Content> ]]
len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )start.Text.Ptr;
}
else if ( check( Tok_Decl_GNU_Attribute ) )
{
eat( Tok_Decl_GNU_Attribute );
eat( Tok_Paren_Open );
eat( Tok_Paren_Open );
// __attribute__((
while ( left && currtok.Type != Tok_Paren_Close )
{
eat( currtok.Type );
}
// __attribute__(( <Content>
eat( Tok_Paren_Close );
eat( Tok_Paren_Close );
// __attribute__(( <Content> ))
len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )start.Text.Ptr;
}
else if ( check( Tok_Decl_MSVC_Attribute ) )
{
eat( Tok_Decl_MSVC_Attribute );
eat( Tok_Paren_Open );
// __declspec(
while ( left && currtok.Type != Tok_Paren_Close )
{
eat( currtok.Type );
}
// __declspec( <Content>
eat( Tok_Paren_Close );
// __declspec( <Content> )
len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )start.Text.Ptr;
}
else if ( tok_is_attribute(currtok) )
{
eat( currtok.Type );
// <Attribute>
// If its a macro based attribute, this could be a functional macro such as Unreal's UE_DEPRECATED(...)
if ( check( Tok_Paren_Open))
{
eat( Tok_Paren_Open );
s32 level = 0;
while (left && currtok.Type != Tok_Paren_Close && level == 0)
{
if (currtok.Type == Tok_Paren_Open)
++ level;
if (currtok.Type == Tok_Paren_Close)
--level;
eat(currtok.Type);
}
eat(Tok_Paren_Close);
}
len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )start.Text.Ptr;
// <Attribute> ( ... )
}
}
if ( len > 0 )
{
Str attribute_txt = { start.Text.Ptr, len };
parser_pop(& _ctx->parser);
StrBuilder name_stripped = parser_strip_formatting( attribute_txt, parser_strip_formatting_dont_preserve_newlines );
Code result = make_code();
result->Type = CT_PlatformAttributes;
result->Name = cache_str( strbuilder_to_str(name_stripped) );
result->Content = result->Name;
// result->Token =
return ( CodeAttributes )result;
}
parser_pop(& _ctx->parser);
return NullCode;
}
internal
Code parse_class_struct( TokType which, bool inplace_def )
{
if ( which != Tok_Decl_Class && which != Tok_Decl_Struct ) {
log_failure( "Error, expected class or struct, not %s\n%s", toktype_to_str( which ), parser_to_strbuilder(_ctx->parser) );
return InvalidCode;
}
Token name = NullToken;
AccessSpec access = AccessSpec_Default;
CodeTypename parent = { nullptr };
CodeBody body = { nullptr };
CodeAttributes attributes = { nullptr };
ModuleFlag mflags = ModuleFlag_None;
Code result = InvalidCode;
if ( check(Tok_Module_Export) ) {
mflags = ModuleFlag_Export;
eat( Tok_Module_Export );
}
// <ModuleFlags>
eat( which );
// <ModuleFlags> <class/struct>
attributes = parse_attributes();
// <ModuleFlags> <class/struct> <Attributes>
if ( check( Tok_Identifier ) ) {
name = parse_identifier(nullptr);
_ctx->parser.Scope->Name = name.Text;
}
// <ModuleFlags> <class/struct> <Attributes> <Name>
local_persist
char interface_arr_mem[ kilobytes(4) ] = {0};
Array(CodeTypename) interfaces; {
Arena arena = arena_init_from_memory( interface_arr_mem, kilobytes(4) );
interfaces = array_init_reserve(CodeTypename, arena_allocator_info(& arena), 4 );
}
// TODO(Ed) : Make an AST_DerivedType, we'll store any arbitary derived type into there as a linear linked list of them.
if ( check( Tok_Assign_Classifer ) )
{
eat( Tok_Assign_Classifer );
// <ModuleFlags> <class/struct> <Attributes> <Name> :
if ( tok_is_access_specifier(currtok) ) {
access = tok_to_access_specifier(currtok);
// <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier>
eat( currtok.Type );
}
Token parent_tok = parse_identifier(nullptr);
parent = def_type( parent_tok.Text );
// <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier> <Parent/Interface Name>
while ( check(Tok_Comma) )
{
eat( Tok_Comma );
// <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier> <Name>,
if ( tok_is_access_specifier(currtok) ) {
eat(currtok.Type);
}
Token interface_tok = parse_identifier(nullptr);
array_append( interfaces, def_type( interface_tok.Text ) );
// <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier> <Name>, ...
}
}
if ( check( Tok_BraceCurly_Open ) ) {
body = parse_class_struct_body( which, name );
}
// <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier> <Name>, ... { <Body> }
CodeComment inline_cmt = NullCode;
if ( ! inplace_def )
{
Token stmt_end = currtok;
eat( Tok_Statement_End );
// <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier> <Name>, ... { <Body> };
if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line )
inline_cmt = parse_comment();
// <ModuleFlags> <class/struct> <Attributes> <Name> : <Access Specifier> <Name>, ... { <Body> }; <InlineCmt>
}
if ( which == Tok_Decl_Class )
result = cast(Code, def_class( name.Text, def_assign( body, parent, access, attributes, interfaces, scast(s32, array_num(interfaces)), mflags ) ));
else
result = cast(Code, def_struct( name.Text, def_assign( body, (CodeTypename)parent, access, attributes, interfaces, scast(s32, array_num(interfaces)), mflags ) ));
if ( inline_cmt )
result->InlineCmt = cast(Code, inline_cmt);
array_free(interfaces);
return result;
}
internal neverinline
CodeBody parse_class_struct_body( TokType which, Token name )
{
push_scope();
eat( Tok_BraceCurly_Open );
// {
CodeBody
result = (CodeBody) make_code();
if ( which == Tok_Decl_Class )
result->Type = CT_Class_Body;
else
result->Type = CT_Struct_Body;
while ( left && currtok_noskip.Type != Tok_BraceCurly_Close )
{
Code member = Code_Invalid;
CodeAttributes attributes = { nullptr };
CodeSpecifiers specifiers = { nullptr };
bool expects_function = false;
// _ctx->parser.Scope->Start = currtok_noskip;
if ( currtok_noskip.Type == Tok_Preprocess_Hash )
eat( Tok_Preprocess_Hash );
switch ( currtok_noskip.Type )
{
case Tok_Statement_End: {
// TODO(Ed): Convert this to a general warning procedure
log_fmt("Dangling end statement found %SB\n", tok_to_strbuilder(currtok_noskip));
eat( Tok_Statement_End );
continue;
}
case Tok_NewLine: {
member = fmt_newline;
eat( Tok_NewLine );
break;
}
case Tok_Comment: {
member = cast(Code, parse_comment());
break;
}
case Tok_Access_Public: {
member = access_public;
eat( Tok_Access_Public );
eat( Tok_Assign_Classifer );
// public:
break;
}
case Tok_Access_Protected: {
member = access_protected;
eat( Tok_Access_Protected );
eat( Tok_Assign_Classifer );
// protected:
break;
}
case Tok_Access_Private: {
member = access_private;
eat( Tok_Access_Private );
eat( Tok_Assign_Classifer );
// private:
break;
}
case Tok_Decl_Class: {
member = parse_complicated_definition( Tok_Decl_Class );
// class
break;
}
case Tok_Decl_Enum: {
member = parse_complicated_definition( Tok_Decl_Enum );
// enum
break;
}
case Tok_Decl_Friend: {
member = cast(Code, parser_parse_friend());
// friend
break;
}
case Tok_Decl_Operator: {
member = cast(Code, parser_parse_operator_cast(NullCode));
// operator <Type>()
break;
}
case Tok_Decl_Struct: {
member = parse_complicated_definition( Tok_Decl_Struct );
// struct
break;
}
case Tok_Decl_Template: {
member = cast(Code, parser_parse_template());
// template< ... >
break;
}
case Tok_Decl_Typedef: {
member = cast(Code, parser_parse_typedef());
// typedef
break;
}
case Tok_Decl_Union: {
member = parse_complicated_definition( Tok_Decl_Union );
// union
break;
}
case Tok_Decl_Using: {
member = cast(Code, parser_parse_using());
// using
break;
}
case Tok_Operator:
{
//if ( currtok.Text[0] != '~' )
//{
// log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_strbuilder(_ctx->parser) );
// return InvalidCode;
//}
member = cast(Code, parser_parse_destructor(NullCode));
// ~<Name>()
break;
}
case Tok_Preprocess_Define: {
member = cast(Code, parser_parse_define());
// #define
break;
}
case Tok_Preprocess_Include:
{
member = cast(Code, parse_include());
// #include
break;
}
case Tok_Preprocess_If:
case Tok_Preprocess_IfDef:
case Tok_Preprocess_IfNotDef:
case Tok_Preprocess_ElIf:
member = cast(Code, parse_preprocess_cond());
// #<Condition>
break;
case Tok_Preprocess_Else: {
member = cast(Code, preprocess_else);
eat( Tok_Preprocess_Else );
// #else
break;
}
case Tok_Preprocess_EndIf: {
member = cast(Code, preprocess_endif);
eat( Tok_Preprocess_EndIf );
// #endif
break;
}
case Tok_Preprocess_Macro_Stmt: {
member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Macro_Stmt ));
break;
}
case Tok_Preprocess_Macro_Expr: {
log_failure("Unbounded macro expression residing in class/struct body\n%S", parser_to_strbuilder(_ctx->parser));
return InvalidCode;
}
// case Tok_Preprocess_Macro:
// // <Macro>
// macro_found = true;
// goto Preprocess_Macro_Bare_In_Body;
// break;
case Tok_Preprocess_Pragma: {
member = cast(Code, parse_pragma());
// #pragma
break;
}
case Tok_Preprocess_Unsupported: {
member = cast(Code, parse_simple_preprocess( Tok_Preprocess_Unsupported ));
// #<UNKNOWN>
break;
}
case Tok_StaticAssert: {
member = parse_static_assert();
// static_assert
break;
}
case Tok_Attribute_Open:
case Tok_Decl_GNU_Attribute:
case Tok_Decl_MSVC_Attribute:
#define Entry( attribute, str ) case attribute:
GEN_DEFINE_ATTRIBUTE_TOKENS
#undef Entry
{
attributes = parse_attributes();
// <Attributes>
}
//! Fallthrough intended
GEN_PARSER_CLASS_STRUCT_BODY_ALLOWED_MEMBER_TOK_SPECIFIER_CASES:
{
Specifier specs_found[16] = { Spec_NumSpecifiers };
s32 NumSpecifiers = 0;
while ( left && tok_is_specifier(currtok) )
{
Specifier spec = str_to_specifier( currtok.Text );
b32 ignore_spec = false;