-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
yarp.c
13156 lines (11170 loc) · 558 KB
/
yarp.c
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
#include "yarp.h"
#include "yarp/version.h"
// The YARP version and the serialization format.
const char *
yp_version(void) {
return YP_VERSION;
}
// In heredocs, tabs automatically complete up to the next 8 spaces. This is
// defined in CRuby as TAB_WIDTH.
#define YP_TAB_WHITESPACE_SIZE 8
// Debugging logging will provide you will additional debugging functions as
// well as automatically replace some functions with their debugging
// counterparts.
#ifndef YP_DEBUG_LOGGING
#define YP_DEBUG_LOGGING 0
#endif
#if YP_DEBUG_LOGGING
/******************************************************************************/
/* Debugging */
/******************************************************************************/
YP_ATTRIBUTE_UNUSED static const char *
debug_context(yp_context_t context) {
switch (context) {
case YP_CONTEXT_BEGIN: return "BEGIN";
case YP_CONTEXT_CLASS: return "CLASS";
case YP_CONTEXT_CASE_IN: return "CASE_IN";
case YP_CONTEXT_CASE_WHEN: return "CASE_WHEN";
case YP_CONTEXT_DEF: return "DEF";
case YP_CONTEXT_DEF_PARAMS: return "DEF_PARAMS";
case YP_CONTEXT_DEFAULT_PARAMS: return "DEFAULT_PARAMS";
case YP_CONTEXT_ENSURE: return "ENSURE";
case YP_CONTEXT_ELSE: return "ELSE";
case YP_CONTEXT_ELSIF: return "ELSIF";
case YP_CONTEXT_EMBEXPR: return "EMBEXPR";
case YP_CONTEXT_BLOCK_BRACES: return "BLOCK_BRACES";
case YP_CONTEXT_BLOCK_KEYWORDS: return "BLOCK_KEYWORDS";
case YP_CONTEXT_FOR: return "FOR";
case YP_CONTEXT_IF: return "IF";
case YP_CONTEXT_MAIN: return "MAIN";
case YP_CONTEXT_MODULE: return "MODULE";
case YP_CONTEXT_PARENS: return "PARENS";
case YP_CONTEXT_POSTEXE: return "POSTEXE";
case YP_CONTEXT_PREDICATE: return "PREDICATE";
case YP_CONTEXT_PREEXE: return "PREEXE";
case YP_CONTEXT_RESCUE: return "RESCUE";
case YP_CONTEXT_RESCUE_ELSE: return "RESCUE_ELSE";
case YP_CONTEXT_SCLASS: return "SCLASS";
case YP_CONTEXT_UNLESS: return "UNLESS";
case YP_CONTEXT_UNTIL: return "UNTIL";
case YP_CONTEXT_WHILE: return "WHILE";
case YP_CONTEXT_LAMBDA_BRACES: return "LAMBDA_BRACES";
case YP_CONTEXT_LAMBDA_DO_END: return "LAMBDA_DO_END";
}
return NULL;
}
YP_ATTRIBUTE_UNUSED static void
debug_contexts(yp_parser_t *parser) {
yp_context_node_t *context_node = parser->current_context;
fprintf(stderr, "CONTEXTS: ");
if (context_node != NULL) {
while (context_node != NULL) {
fprintf(stderr, "%s", debug_context(context_node->context));
context_node = context_node->prev;
if (context_node != NULL) {
fprintf(stderr, " <- ");
}
}
} else {
fprintf(stderr, "NONE");
}
fprintf(stderr, "\n");
}
YP_ATTRIBUTE_UNUSED static void
debug_node(const char *message, yp_parser_t *parser, yp_node_t *node) {
yp_buffer_t buffer;
if (!yp_buffer_init(&buffer)) return;
yp_prettyprint(parser, node, &buffer);
fprintf(stderr, "%s\n%.*s\n", message, (int) buffer.length, buffer.value);
yp_buffer_free(&buffer);
}
YP_ATTRIBUTE_UNUSED static void
debug_lex_mode(yp_parser_t *parser) {
yp_lex_mode_t *lex_mode = parser->lex_modes.current;
bool first = true;
while (lex_mode != NULL) {
if (first) {
first = false;
} else {
fprintf(stderr, " <- ");
}
switch (lex_mode->mode) {
case YP_LEX_DEFAULT: fprintf(stderr, "DEFAULT"); break;
case YP_LEX_EMBEXPR: fprintf(stderr, "EMBEXPR"); break;
case YP_LEX_EMBVAR: fprintf(stderr, "EMBVAR"); break;
case YP_LEX_HEREDOC: fprintf(stderr, "HEREDOC"); break;
case YP_LEX_LIST: fprintf(stderr, "LIST (terminator=%c, interpolation=%d)", lex_mode->as.list.terminator, lex_mode->as.list.interpolation); break;
case YP_LEX_REGEXP: fprintf(stderr, "REGEXP (terminator=%c)", lex_mode->as.regexp.terminator); break;
case YP_LEX_STRING: fprintf(stderr, "STRING (terminator=%c, interpolation=%d)", lex_mode->as.string.terminator, lex_mode->as.string.interpolation); break;
}
lex_mode = lex_mode->prev;
}
fprintf(stderr, "\n");
}
YP_ATTRIBUTE_UNUSED static void
debug_state(yp_parser_t *parser) {
fprintf(stderr, "STATE: ");
bool first = true;
if (parser->lex_state == YP_LEX_STATE_NONE) {
fprintf(stderr, "NONE\n");
return;
}
#define CHECK_STATE(state) \
if (parser->lex_state & state) { \
if (!first) fprintf(stderr, "|"); \
fprintf(stderr, "%s", #state); \
first = false; \
}
CHECK_STATE(YP_LEX_STATE_BEG)
CHECK_STATE(YP_LEX_STATE_END)
CHECK_STATE(YP_LEX_STATE_ENDARG)
CHECK_STATE(YP_LEX_STATE_ENDFN)
CHECK_STATE(YP_LEX_STATE_ARG)
CHECK_STATE(YP_LEX_STATE_CMDARG)
CHECK_STATE(YP_LEX_STATE_MID)
CHECK_STATE(YP_LEX_STATE_FNAME)
CHECK_STATE(YP_LEX_STATE_DOT)
CHECK_STATE(YP_LEX_STATE_CLASS)
CHECK_STATE(YP_LEX_STATE_LABEL)
CHECK_STATE(YP_LEX_STATE_LABELED)
CHECK_STATE(YP_LEX_STATE_FITEM)
#undef CHECK_STATE
fprintf(stderr, "\n");
}
YP_ATTRIBUTE_UNUSED static void
debug_token(yp_token_t * token) {
fprintf(stderr, "%s: \"%.*s\"\n", yp_token_type_to_str(token->type), (int) (token->end - token->start), token->start);
}
#endif
/******************************************************************************/
/* Lex mode manipulations */
/******************************************************************************/
// Returns the incrementor character that should be used to increment the
// nesting count if one is possible.
static inline char
lex_mode_incrementor(const char start) {
switch (start) {
case '(':
case '[':
case '{':
case '<':
return start;
default:
return '\0';
}
}
// Returns the matching character that should be used to terminate a list
// beginning with the given character.
static inline char
lex_mode_terminator(const char start) {
switch (start) {
case '(':
return ')';
case '[':
return ']';
case '{':
return '}';
case '<':
return '>';
default:
return start;
}
}
// Push a new lex state onto the stack. If we're still within the pre-allocated
// space of the lex state stack, then we'll just use a new slot. Otherwise we'll
// allocate a new pointer and use that.
static bool
lex_mode_push(yp_parser_t *parser, yp_lex_mode_t lex_mode) {
lex_mode.prev = parser->lex_modes.current;
parser->lex_modes.index++;
if (parser->lex_modes.index > YP_LEX_STACK_SIZE - 1) {
parser->lex_modes.current = (yp_lex_mode_t *) malloc(sizeof(yp_lex_mode_t));
if (parser->lex_modes.current == NULL) return false;
*parser->lex_modes.current = lex_mode;
} else {
parser->lex_modes.stack[parser->lex_modes.index] = lex_mode;
parser->lex_modes.current = &parser->lex_modes.stack[parser->lex_modes.index];
}
return true;
}
// Push on a new list lex mode.
static inline bool
lex_mode_push_list(yp_parser_t *parser, bool interpolation, char delimiter) {
char incrementor = lex_mode_incrementor(delimiter);
char terminator = lex_mode_terminator(delimiter);
yp_lex_mode_t lex_mode = {
.mode = YP_LEX_LIST,
.as.list = {
.nesting = 0,
.interpolation = interpolation,
.incrementor = incrementor,
.terminator = terminator
}
};
// These are the places where we need to split up the content of the list.
// We'll use strpbrk to find the first of these characters.
char *breakpoints = lex_mode.as.list.breakpoints;
memcpy(breakpoints, "\\ \t\f\r\v\n\0\0\0", sizeof(lex_mode.as.list.breakpoints));
// Now we'll add the terminator to the list of breakpoints.
size_t index = 7;
breakpoints[index++] = terminator;
// If interpolation is allowed, then we're going to check for the #
// character. Otherwise we'll only look for escapes and the terminator.
if (interpolation) {
breakpoints[index++] = '#';
}
// If there is an incrementor, then we'll check for that as well.
if (incrementor != '\0') {
breakpoints[index++] = incrementor;
}
return lex_mode_push(parser, lex_mode);
}
// Push on a new regexp lex mode.
static inline bool
lex_mode_push_regexp(yp_parser_t *parser, char incrementor, char terminator) {
yp_lex_mode_t lex_mode = {
.mode = YP_LEX_REGEXP,
.as.regexp = {
.nesting = 0,
.incrementor = incrementor,
.terminator = terminator
}
};
// These are the places where we need to split up the content of the
// regular expression. We'll use strpbrk to find the first of these
// characters.
char *breakpoints = lex_mode.as.regexp.breakpoints;
memcpy(breakpoints, "\n\\#\0\0", sizeof(lex_mode.as.regexp.breakpoints));
// First we'll add the terminator.
breakpoints[3] = terminator;
// Next, if there is an incrementor, then we'll check for that as well.
if (incrementor != '\0') {
breakpoints[4] = incrementor;
}
return lex_mode_push(parser, lex_mode);
}
// Push on a new string lex mode.
static inline bool
lex_mode_push_string(yp_parser_t *parser, bool interpolation, bool label_allowed, char incrementor, char terminator) {
yp_lex_mode_t lex_mode = {
.mode = YP_LEX_STRING,
.as.string = {
.nesting = 0,
.interpolation = interpolation,
.label_allowed = label_allowed,
.incrementor = incrementor,
.terminator = terminator
}
};
// These are the places where we need to split up the content of the
// string. We'll use strpbrk to find the first of these characters.
char *breakpoints = lex_mode.as.string.breakpoints;
memcpy(breakpoints, "\n\\\0\0\0", sizeof(lex_mode.as.string.breakpoints));
// Now add in the terminator.
size_t index = 2;
breakpoints[index++] = terminator;
// If interpolation is allowed, then we're going to check for the #
// character. Otherwise we'll only look for escapes and the terminator.
if (interpolation) {
breakpoints[index++] = '#';
}
// If we have an incrementor, then we'll add that in as a breakpoint as
// well.
if (incrementor != '\0') {
breakpoints[index++] = incrementor;
}
return lex_mode_push(parser, lex_mode);
}
// Pop the current lex state off the stack. If we're within the pre-allocated
// space of the lex state stack, then we'll just decrement the index. Otherwise
// we'll free the current pointer and use the previous pointer.
static void
lex_mode_pop(yp_parser_t *parser) {
if (parser->lex_modes.index == 0) {
parser->lex_modes.current->mode = YP_LEX_DEFAULT;
} else if (parser->lex_modes.index < YP_LEX_STACK_SIZE) {
parser->lex_modes.index--;
parser->lex_modes.current = &parser->lex_modes.stack[parser->lex_modes.index];
} else {
parser->lex_modes.index--;
yp_lex_mode_t *prev = parser->lex_modes.current->prev;
free(parser->lex_modes.current);
parser->lex_modes.current = prev;
}
}
// This is the equivalent of IS_lex_state is CRuby.
static inline bool
lex_state_p(yp_parser_t *parser, yp_lex_state_t state) {
return parser->lex_state & state;
}
typedef enum {
YP_IGNORED_NEWLINE_NONE = 0,
YP_IGNORED_NEWLINE_ALL,
YP_IGNORED_NEWLINE_PATTERN
} yp_ignored_newline_type_t;
static inline yp_ignored_newline_type_t
lex_state_ignored_p(yp_parser_t *parser) {
bool ignored = lex_state_p(parser, YP_LEX_STATE_BEG | YP_LEX_STATE_CLASS | YP_LEX_STATE_FNAME | YP_LEX_STATE_DOT) && !lex_state_p(parser, YP_LEX_STATE_LABELED);
if (ignored) {
return YP_IGNORED_NEWLINE_ALL;
} else if (parser->lex_state == (YP_LEX_STATE_ARG | YP_LEX_STATE_LABELED)) {
return YP_IGNORED_NEWLINE_PATTERN;
} else {
return YP_IGNORED_NEWLINE_NONE;
}
}
static inline bool
lex_state_beg_p(yp_parser_t *parser) {
return lex_state_p(parser, YP_LEX_STATE_BEG_ANY) || (parser->lex_state == (YP_LEX_STATE_ARG | YP_LEX_STATE_LABELED));
}
static inline bool
lex_state_arg_p(yp_parser_t *parser) {
return lex_state_p(parser, YP_LEX_STATE_ARG_ANY);
}
static inline bool
lex_state_spcarg_p(yp_parser_t *parser, bool space_seen) {
return lex_state_arg_p(parser) && space_seen && !yp_char_is_whitespace(*parser->current.end);
}
static inline bool
lex_state_end_p(yp_parser_t *parser) {
return lex_state_p(parser, YP_LEX_STATE_END_ANY);
}
// This is the equivalent of IS_AFTER_OPERATOR in CRuby.
static inline bool
lex_state_operator_p(yp_parser_t *parser) {
return lex_state_p(parser, YP_LEX_STATE_FNAME | YP_LEX_STATE_DOT);
}
// Set the state of the lexer. This is defined as a function to be able to put a breakpoint in it.
static inline void
lex_state_set(yp_parser_t *parser, yp_lex_state_t state) {
parser->lex_state = state;
}
#if YP_DEBUG_LOGGING
static inline void
debug_lex_state_set(yp_parser_t *parser, yp_lex_state_t state, char const * caller_name, int line_number) {
fprintf(stderr, "Caller: %s:%d\nPrevious: ", caller_name, line_number);
debug_state(parser);
lex_state_set(parser, state);
fprintf(stderr, "Now: ");
debug_state(parser);
fprintf(stderr, "\n");
}
#define lex_state_set(parser, state) debug_lex_state_set(parser, state, __func__, __LINE__)
#endif
/******************************************************************************/
/* Node-related functions */
/******************************************************************************/
// Retrieve the constant pool id for the given location.
static inline yp_constant_id_t
yp_parser_constant_id_location(yp_parser_t *parser, const char *start, const char *end) {
return yp_constant_pool_insert(&parser->constant_pool, start, (size_t) (end - start));
}
// Retrieve the constant pool id for the given token.
static inline yp_constant_id_t
yp_parser_constant_id_token(yp_parser_t *parser, const yp_token_t *token) {
return yp_parser_constant_id_location(parser, token->start, token->end);
}
// Mark any range nodes in this subtree as flipflops.
static void
yp_flip_flop(yp_node_t *node) {
switch (YP_NODE_TYPE(node)) {
case YP_NODE_AND_NODE: {
yp_and_node_t *cast = (yp_and_node_t *) node;
yp_flip_flop(cast->left);
yp_flip_flop(cast->right);
break;
}
case YP_NODE_OR_NODE: {
yp_or_node_t *cast = (yp_or_node_t *) node;
yp_flip_flop(cast->left);
yp_flip_flop(cast->right);
break;
}
case YP_NODE_RANGE_NODE: {
yp_range_node_t *cast = (yp_range_node_t *) node;
cast->flags |= YP_RANGE_NODE_FLAGS_FLIP_FLOP;
break;
}
default:
break;
}
}
// In a lot of places in the tree you can have tokens that are not provided but
// that do not cause an error. For example, in a method call without
// parentheses. In these cases we set the token to the "not provided" type. For
// example:
//
// yp_token_t token;
// not_provided(&token, parser->previous.end);
//
static inline yp_token_t
not_provided(yp_parser_t *parser) {
return (yp_token_t) { .type = YP_TOKEN_NOT_PROVIDED, .start = parser->start, .end = parser->start };
}
#define YP_LOCATION_NULL_VALUE(parser) ((yp_location_t) { .start = parser->start, .end = parser->start })
#define YP_LOCATION_TOKEN_VALUE(token) ((yp_location_t) { .start = (token)->start, .end = (token)->end })
#define YP_LOCATION_NODE_VALUE(node) ((yp_location_t) { .start = (node)->location.start, .end = (node)->location.end })
#define YP_LOCATION_NODE_BASE_VALUE(node) ((yp_location_t) { .start = (node)->base.location.start, .end = (node)->base.location.end })
#define YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE ((yp_location_t) { .start = NULL, .end = NULL })
#define YP_OPTIONAL_LOCATION_TOKEN_VALUE(token) ((token)->type == YP_TOKEN_NOT_PROVIDED ? YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE : YP_LOCATION_TOKEN_VALUE(token))
#define YP_TOKEN_NOT_PROVIDED_VALUE(parser) ((yp_token_t) { .type = YP_TOKEN_NOT_PROVIDED, .start = (parser)->start, .end = (parser)->start })
// This is a special out parameter to the parse_arguments_list function that
// includes opening and closing parentheses in addition to the arguments since
// it's so common. It is handy to use when passing argument information to one
// of the call node creation functions.
typedef struct {
yp_location_t opening_loc;
yp_arguments_node_t *arguments;
yp_location_t closing_loc;
yp_block_node_t *block;
} yp_arguments_t;
#define YP_EMPTY_ARGUMENTS ((yp_arguments_t) { .opening_loc = YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE, .arguments = NULL, .closing_loc = YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE, .block = NULL })
/******************************************************************************/
/* Node creation functions */
/******************************************************************************/
// Parse out the options for a regular expression.
static inline uint32_t
yp_regular_expression_flags_create(const yp_token_t *closing) {
uint32_t flags = 0;
if (closing->type == YP_TOKEN_REGEXP_END) {
for (const char *flag = closing->start + 1; flag < closing->end; flag++) {
switch (*flag) {
case 'i': flags |= YP_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE; break;
case 'm': flags |= YP_REGULAR_EXPRESSION_FLAGS_MULTI_LINE; break;
case 'x': flags |= YP_REGULAR_EXPRESSION_FLAGS_EXTENDED; break;
case 'e': flags |= YP_REGULAR_EXPRESSION_FLAGS_EUC_JP; break;
case 'n': flags |= YP_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT; break;
case 's': flags |= YP_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J; break;
case 'u': flags |= YP_REGULAR_EXPRESSION_FLAGS_UTF_8; break;
case 'o': flags |= YP_REGULAR_EXPRESSION_FLAGS_ONCE; break;
default: assert(false && "unreachable");
}
}
}
return flags;
}
// Allocate and initialize a new StatementsNode node.
static yp_statements_node_t *
yp_statements_node_create(yp_parser_t *parser);
// Append a new node to the given StatementsNode node's body.
static void
yp_statements_node_body_append(yp_statements_node_t *node, yp_node_t *statement);
// This function is here to allow us a place to extend in the future when we
// implement our own arena allocation.
static inline void *
yp_alloc_node(YP_ATTRIBUTE_UNUSED yp_parser_t *parser, size_t size) {
void *memory = calloc(1, size);
if (memory == NULL) {
fprintf(stderr, "Failed to allocate %zu bytes\n", size);
abort();
}
return memory;
}
#define YP_ALLOC_NODE(parser, type) (type *) yp_alloc_node(parser, sizeof(type))
// Allocate a new MissingNode node.
static yp_missing_node_t *
yp_missing_node_create(yp_parser_t *parser, const char *start, const char *end) {
yp_missing_node_t *node = YP_ALLOC_NODE(parser, yp_missing_node_t);
*node = (yp_missing_node_t) {{ .type = YP_NODE_MISSING_NODE, .location = { .start = start, .end = end } }};
return node;
}
// Allocate and initialize a new alias node.
static yp_alias_node_t *
yp_alias_node_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t *new_name, yp_node_t *old_name) {
assert(keyword->type == YP_TOKEN_KEYWORD_ALIAS);
yp_alias_node_t *node = YP_ALLOC_NODE(parser, yp_alias_node_t);
*node = (yp_alias_node_t) {
{
.type = YP_NODE_ALIAS_NODE,
.location = {
.start = keyword->start,
.end = old_name->location.end
},
},
.new_name = new_name,
.old_name = old_name,
.keyword_loc = YP_LOCATION_TOKEN_VALUE(keyword)
};
return node;
}
// Allocate a new AlternationPatternNode node.
static yp_alternation_pattern_node_t *
yp_alternation_pattern_node_create(yp_parser_t *parser, yp_node_t *left, yp_node_t *right, const yp_token_t *operator) {
yp_alternation_pattern_node_t *node = YP_ALLOC_NODE(parser, yp_alternation_pattern_node_t);
*node = (yp_alternation_pattern_node_t) {
{
.type = YP_NODE_ALTERNATION_PATTERN_NODE,
.location = {
.start = left->location.start,
.end = right->location.end
},
},
.left = left,
.right = right,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator)
};
return node;
}
// Allocate and initialize a new and node.
static yp_and_node_t *
yp_and_node_create(yp_parser_t *parser, yp_node_t *left, const yp_token_t *operator, yp_node_t *right) {
yp_and_node_t *node = YP_ALLOC_NODE(parser, yp_and_node_t);
*node = (yp_and_node_t) {
{
.type = YP_NODE_AND_NODE,
.location = {
.start = left->location.start,
.end = right->location.end
},
},
.left = left,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator),
.right = right
};
return node;
}
// Allocate an initialize a new arguments node.
static yp_arguments_node_t *
yp_arguments_node_create(yp_parser_t *parser) {
yp_arguments_node_t *node = YP_ALLOC_NODE(parser, yp_arguments_node_t);
*node = (yp_arguments_node_t) {
{
.type = YP_NODE_ARGUMENTS_NODE,
.location = YP_LOCATION_NULL_VALUE(parser)
},
.arguments = YP_EMPTY_NODE_LIST
};
return node;
}
// Return the size of the given arguments node.
static size_t
yp_arguments_node_size(yp_arguments_node_t *node) {
return node->arguments.size;
}
// Append an argument to an arguments node.
static void
yp_arguments_node_arguments_append(yp_arguments_node_t *node, yp_node_t *argument) {
if (yp_arguments_node_size(node) == 0) {
node->base.location.start = argument->location.start;
}
node->base.location.end = argument->location.end;
yp_node_list_append(&node->arguments, argument);
}
// Allocate and initialize a new ArrayNode node.
static yp_array_node_t *
yp_array_node_create(yp_parser_t *parser, const yp_token_t *opening) {
yp_array_node_t *node = YP_ALLOC_NODE(parser, yp_array_node_t);
*node = (yp_array_node_t) {
{
.type = YP_NODE_ARRAY_NODE,
.location = {
.start = opening->start,
.end = opening->end
},
},
.opening_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(opening),
.closing_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(opening),
.elements = YP_EMPTY_NODE_LIST
};
return node;
}
// Return the size of the given array node.
static inline size_t
yp_array_node_size(yp_array_node_t *node) {
return node->elements.size;
}
// Append an argument to an array node.
static inline void
yp_array_node_elements_append(yp_array_node_t *node, yp_node_t *element) {
if (!node->elements.size && !node->opening_loc.start) {
node->base.location.start = element->location.start;
}
yp_node_list_append(&node->elements, element);
node->base.location.end = element->location.end;
}
// Set the closing token and end location of an array node.
static void
yp_array_node_close_set(yp_array_node_t *node, const yp_token_t *closing) {
assert(closing->type == YP_TOKEN_BRACKET_RIGHT || closing->type == YP_TOKEN_STRING_END || closing->type == YP_TOKEN_MISSING || closing->type == YP_TOKEN_NOT_PROVIDED);
node->base.location.end = closing->end;
node->closing_loc = YP_LOCATION_TOKEN_VALUE(closing);
}
// Allocate and initialize a new array pattern node. The node list given in the
// nodes parameter is guaranteed to have at least two nodes.
static yp_array_pattern_node_t *
yp_array_pattern_node_node_list_create(yp_parser_t *parser, yp_node_list_t *nodes) {
yp_array_pattern_node_t *node = YP_ALLOC_NODE(parser, yp_array_pattern_node_t);
*node = (yp_array_pattern_node_t) {
{
.type = YP_NODE_ARRAY_PATTERN_NODE,
.location = {
.start = nodes->nodes[0]->location.start,
.end = nodes->nodes[nodes->size - 1]->location.end
},
},
.constant = NULL,
.rest = NULL,
.requireds = YP_EMPTY_NODE_LIST,
.posts = YP_EMPTY_NODE_LIST,
.opening_loc = YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE,
.closing_loc = YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE
};
// For now we're going to just copy over each pointer manually. This could be
// much more efficient, as we could instead resize the node list.
bool found_rest = false;
for (size_t index = 0; index < nodes->size; index++) {
yp_node_t *child = nodes->nodes[index];
if (!found_rest && child->type == YP_NODE_SPLAT_NODE) {
node->rest = child;
found_rest = true;
} else if (found_rest) {
yp_node_list_append(&node->posts, child);
} else {
yp_node_list_append(&node->requireds, child);
}
}
return node;
}
// Allocate and initialize a new array pattern node from a single rest node.
static yp_array_pattern_node_t *
yp_array_pattern_node_rest_create(yp_parser_t *parser, yp_node_t *rest) {
yp_array_pattern_node_t *node = YP_ALLOC_NODE(parser, yp_array_pattern_node_t);
*node = (yp_array_pattern_node_t) {
{
.type = YP_NODE_ARRAY_PATTERN_NODE,
.location = rest->location,
},
.constant = NULL,
.rest = rest,
.requireds = YP_EMPTY_NODE_LIST,
.posts = YP_EMPTY_NODE_LIST,
.opening_loc = YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE,
.closing_loc = YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE
};
return node;
}
// Allocate and initialize a new array pattern node from a constant and opening
// and closing tokens.
static yp_array_pattern_node_t *
yp_array_pattern_node_constant_create(yp_parser_t *parser, yp_node_t *constant, const yp_token_t *opening, const yp_token_t *closing) {
yp_array_pattern_node_t *node = YP_ALLOC_NODE(parser, yp_array_pattern_node_t);
*node = (yp_array_pattern_node_t) {
{
.type = YP_NODE_ARRAY_PATTERN_NODE,
.location = {
.start = constant->location.start,
.end = closing->end
},
},
.constant = constant,
.rest = NULL,
.opening_loc = YP_LOCATION_TOKEN_VALUE(opening),
.closing_loc = YP_LOCATION_TOKEN_VALUE(closing),
.requireds = YP_EMPTY_NODE_LIST,
.posts = YP_EMPTY_NODE_LIST
};
return node;
}
// Allocate and initialize a new array pattern node from an opening and closing
// token.
static yp_array_pattern_node_t *
yp_array_pattern_node_empty_create(yp_parser_t *parser, const yp_token_t *opening, const yp_token_t *closing) {
yp_array_pattern_node_t *node = YP_ALLOC_NODE(parser, yp_array_pattern_node_t);
*node = (yp_array_pattern_node_t) {
{
.type = YP_NODE_ARRAY_PATTERN_NODE,
.location = {
.start = opening->start,
.end = closing->end
},
},
.constant = NULL,
.rest = NULL,
.opening_loc = YP_LOCATION_TOKEN_VALUE(opening),
.closing_loc = YP_LOCATION_TOKEN_VALUE(closing),
.requireds = YP_EMPTY_NODE_LIST,
.posts = YP_EMPTY_NODE_LIST
};
return node;
}
static inline void
yp_array_pattern_node_requireds_append(yp_array_pattern_node_t *node, yp_node_t *inner) {
yp_node_list_append(&node->requireds, inner);
}
// Allocate and initialize a new assoc node.
static yp_assoc_node_t *
yp_assoc_node_create(yp_parser_t *parser, yp_node_t *key, const yp_token_t *operator, yp_node_t *value) {
yp_assoc_node_t *node = YP_ALLOC_NODE(parser, yp_assoc_node_t);
const char *end;
if (value != NULL) {
end = value->location.end;
} else if (operator->type != YP_TOKEN_NOT_PROVIDED) {
end = operator->end;
} else {
end = key->location.end;
}
*node = (yp_assoc_node_t) {
{
.type = YP_NODE_ASSOC_NODE,
.location = {
.start = key->location.start,
.end = end
},
},
.key = key,
.operator_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(operator),
.value = value
};
return node;
}
// Allocate and initialize a new assoc splat node.
static yp_assoc_splat_node_t *
yp_assoc_splat_node_create(yp_parser_t *parser, yp_node_t *value, const yp_token_t *operator) {
assert(operator->type == YP_TOKEN_USTAR_STAR);
yp_assoc_splat_node_t *node = YP_ALLOC_NODE(parser, yp_assoc_splat_node_t);
*node = (yp_assoc_splat_node_t) {
{
.type = YP_NODE_ASSOC_SPLAT_NODE,
.location = {
.start = operator->start,
.end = value == NULL ? operator->end : value->location.end
},
},
.value = value,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator)
};
return node;
}
// Allocate a new BackReferenceReadNode node.
static yp_back_reference_read_node_t *
yp_back_reference_read_node_create(yp_parser_t *parser, const yp_token_t *name) {
assert(name->type == YP_TOKEN_BACK_REFERENCE);
yp_back_reference_read_node_t *node = YP_ALLOC_NODE(parser, yp_back_reference_read_node_t);
*node = (yp_back_reference_read_node_t) {
{
.type = YP_NODE_BACK_REFERENCE_READ_NODE,
.location = YP_LOCATION_TOKEN_VALUE(name),
}
};
return node;
}
// Allocate and initialize new a begin node.
static yp_begin_node_t *
yp_begin_node_create(yp_parser_t *parser, const yp_token_t *begin_keyword, yp_statements_node_t *statements) {
yp_begin_node_t *node = YP_ALLOC_NODE(parser, yp_begin_node_t);
*node = (yp_begin_node_t) {
{
.type = YP_NODE_BEGIN_NODE,
.location = {
.start = begin_keyword->start,
.end = statements == NULL ? begin_keyword->end : statements->base.location.end
},
},
.begin_keyword_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(begin_keyword),
.statements = statements,
.end_keyword_loc = YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE
};
return node;
}
// Set the rescue clause, optionally start, and end location of a begin node.
static void
yp_begin_node_rescue_clause_set(yp_begin_node_t *node, yp_rescue_node_t *rescue_clause) {
// If the begin keyword doesn't exist, we set the start on the begin_node
if (!node->begin_keyword_loc.start) {
node->base.location.start = rescue_clause->base.location.start;
}
node->base.location.end = rescue_clause->base.location.end;
node->rescue_clause = rescue_clause;
}
// Set the else clause and end location of a begin node.
static void
yp_begin_node_else_clause_set(yp_begin_node_t *node, yp_else_node_t *else_clause) {
node->base.location.end = else_clause->base.location.end;
node->else_clause = else_clause;
}
// Set the ensure clause and end location of a begin node.
static void
yp_begin_node_ensure_clause_set(yp_begin_node_t *node, yp_ensure_node_t *ensure_clause) {
node->base.location.end = ensure_clause->base.location.end;
node->ensure_clause = ensure_clause;
}
// Set the end keyword and end location of a begin node.
static void
yp_begin_node_end_keyword_set(yp_begin_node_t *node, const yp_token_t *end_keyword) {
assert(end_keyword->type == YP_TOKEN_KEYWORD_END || end_keyword->type == YP_TOKEN_MISSING);
node->base.location.end = end_keyword->end;
node->end_keyword_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(end_keyword);
}
// Allocate and initialize a new BlockArgumentNode node.
static yp_block_argument_node_t *
yp_block_argument_node_create(yp_parser_t *parser, const yp_token_t *operator, yp_node_t *expression) {
yp_block_argument_node_t *node = YP_ALLOC_NODE(parser, yp_block_argument_node_t);
*node = (yp_block_argument_node_t) {
{
.type = YP_NODE_BLOCK_ARGUMENT_NODE,
.location = {
.start = operator->start,
.end = expression == NULL ? operator->end : expression->location.end
},
},
.expression = expression,
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator)
};
return node;
}
// Allocate and initialize a new BlockNode node.
static yp_block_node_t *
yp_block_node_create(yp_parser_t *parser, yp_constant_id_list_t *locals, const yp_token_t *opening, yp_block_parameters_node_t *parameters, yp_node_t *statements, const yp_token_t *closing) {
yp_block_node_t *node = YP_ALLOC_NODE(parser, yp_block_node_t);
*node = (yp_block_node_t) {
{
.type = YP_NODE_BLOCK_NODE,
.location = { .start = opening->start, .end = closing->end },
},
.locals = *locals,
.parameters = parameters,
.statements = statements,
.opening_loc = YP_LOCATION_TOKEN_VALUE(opening),
.closing_loc = YP_LOCATION_TOKEN_VALUE(closing)
};
return node;
}
// Allocate and initialize a new BlockParameterNode node.
static yp_block_parameter_node_t *
yp_block_parameter_node_create(yp_parser_t *parser, const yp_token_t *name, const yp_token_t *operator) {
assert(operator->type == YP_TOKEN_NOT_PROVIDED || operator->type == YP_TOKEN_AMPERSAND);
yp_block_parameter_node_t *node = YP_ALLOC_NODE(parser, yp_block_parameter_node_t);
*node = (yp_block_parameter_node_t) {
{
.type = YP_NODE_BLOCK_PARAMETER_NODE,
.location = {
.start = operator->start,
.end = (name->type == YP_TOKEN_NOT_PROVIDED ? operator->end : name->end)
},
},
.name_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(name),
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator)
};
return node;
}
// Allocate and initialize a new BlockParametersNode node.
static yp_block_parameters_node_t *
yp_block_parameters_node_create(yp_parser_t *parser, yp_parameters_node_t *parameters, const yp_token_t *opening) {
yp_block_parameters_node_t *node = YP_ALLOC_NODE(parser, yp_block_parameters_node_t);
const char *start;
if (opening->type != YP_TOKEN_NOT_PROVIDED) {