-
Notifications
You must be signed in to change notification settings - Fork 0
/
prism.c
14639 lines (12468 loc) · 612 KB
/
prism.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 "prism.h"
// The prism version and the serialization format.
const char *
pm_version(void) {
return PRISM_VERSION;
}
// In heredocs, tabs automatically complete up to the next 8 spaces. This is
// defined in CRuby as TAB_WIDTH.
#define PM_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 PM_DEBUG_LOGGING
#define PM_DEBUG_LOGGING 0
#endif
#if PM_DEBUG_LOGGING
/******************************************************************************/
/* Debugging */
/******************************************************************************/
PRISM_ATTRIBUTE_UNUSED static const char *
debug_context(pm_context_t context) {
switch (context) {
case PM_CONTEXT_BEGIN: return "BEGIN";
case PM_CONTEXT_CLASS: return "CLASS";
case PM_CONTEXT_CASE_IN: return "CASE_IN";
case PM_CONTEXT_CASE_WHEN: return "CASE_WHEN";
case PM_CONTEXT_DEF: return "DEF";
case PM_CONTEXT_DEF_PARAMS: return "DEF_PARAMS";
case PM_CONTEXT_DEFAULT_PARAMS: return "DEFAULT_PARAMS";
case PM_CONTEXT_ENSURE: return "ENSURE";
case PM_CONTEXT_ELSE: return "ELSE";
case PM_CONTEXT_ELSIF: return "ELSIF";
case PM_CONTEXT_EMBEXPR: return "EMBEXPR";
case PM_CONTEXT_BLOCK_BRACES: return "BLOCK_BRACES";
case PM_CONTEXT_BLOCK_KEYWORDS: return "BLOCK_KEYWORDS";
case PM_CONTEXT_FOR: return "FOR";
case PM_CONTEXT_IF: return "IF";
case PM_CONTEXT_MAIN: return "MAIN";
case PM_CONTEXT_MODULE: return "MODULE";
case PM_CONTEXT_PARENS: return "PARENS";
case PM_CONTEXT_POSTEXE: return "POSTEXE";
case PM_CONTEXT_PREDICATE: return "PREDICATE";
case PM_CONTEXT_PREEXE: return "PREEXE";
case PM_CONTEXT_RESCUE: return "RESCUE";
case PM_CONTEXT_RESCUE_ELSE: return "RESCUE_ELSE";
case PM_CONTEXT_SCLASS: return "SCLASS";
case PM_CONTEXT_UNLESS: return "UNLESS";
case PM_CONTEXT_UNTIL: return "UNTIL";
case PM_CONTEXT_WHILE: return "WHILE";
case PM_CONTEXT_LAMBDA_BRACES: return "LAMBDA_BRACES";
case PM_CONTEXT_LAMBDA_DO_END: return "LAMBDA_DO_END";
}
return NULL;
}
PRISM_ATTRIBUTE_UNUSED static void
debug_contexts(pm_parser_t *parser) {
pm_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");
}
PRISM_ATTRIBUTE_UNUSED static void
debug_node(const char *message, pm_parser_t *parser, pm_node_t *node) {
pm_buffer_t buffer;
if (!pm_buffer_init(&buffer)) return;
pm_prettyprint(parser, node, &buffer);
fprintf(stderr, "%s\n%.*s\n", message, (int) buffer.length, buffer.value);
pm_buffer_free(&buffer);
}
PRISM_ATTRIBUTE_UNUSED static void
debug_lex_mode(pm_parser_t *parser) {
pm_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 PM_LEX_DEFAULT: fprintf(stderr, "DEFAULT"); break;
case PM_LEX_EMBEXPR: fprintf(stderr, "EMBEXPR"); break;
case PM_LEX_EMBVAR: fprintf(stderr, "EMBVAR"); break;
case PM_LEX_HEREDOC: fprintf(stderr, "HEREDOC"); break;
case PM_LEX_LIST: fprintf(stderr, "LIST (terminator=%c, interpolation=%d)", lex_mode->as.list.terminator, lex_mode->as.list.interpolation); break;
case PM_LEX_REGEXP: fprintf(stderr, "REGEXP (terminator=%c)", lex_mode->as.regexp.terminator); break;
case PM_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");
}
PRISM_ATTRIBUTE_UNUSED static void
debug_state(pm_parser_t *parser) {
fprintf(stderr, "STATE: ");
bool first = true;
if (parser->lex_state == PM_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(PM_LEX_STATE_BEG)
CHECK_STATE(PM_LEX_STATE_END)
CHECK_STATE(PM_LEX_STATE_ENDARG)
CHECK_STATE(PM_LEX_STATE_ENDFN)
CHECK_STATE(PM_LEX_STATE_ARG)
CHECK_STATE(PM_LEX_STATE_CMDARG)
CHECK_STATE(PM_LEX_STATE_MID)
CHECK_STATE(PM_LEX_STATE_FNAME)
CHECK_STATE(PM_LEX_STATE_DOT)
CHECK_STATE(PM_LEX_STATE_CLASS)
CHECK_STATE(PM_LEX_STATE_LABEL)
CHECK_STATE(PM_LEX_STATE_LABELED)
CHECK_STATE(PM_LEX_STATE_FITEM)
#undef CHECK_STATE
fprintf(stderr, "\n");
}
PRISM_ATTRIBUTE_UNUSED static void
debug_token(pm_token_t * token) {
fprintf(stderr, "%s: \"%.*s\"\n", pm_token_type_to_str(token->type), (int) (token->end - token->start), token->start);
}
#endif
/* Macros for min/max. */
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
/******************************************************************************/
/* Lex mode manipulations */
/******************************************************************************/
// Returns the incrementor character that should be used to increment the
// nesting count if one is possible.
static inline uint8_t
lex_mode_incrementor(const uint8_t 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 uint8_t
lex_mode_terminator(const uint8_t 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(pm_parser_t *parser, pm_lex_mode_t lex_mode) {
lex_mode.prev = parser->lex_modes.current;
parser->lex_modes.index++;
if (parser->lex_modes.index > PM_LEX_STACK_SIZE - 1) {
parser->lex_modes.current = (pm_lex_mode_t *) malloc(sizeof(pm_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(pm_parser_t *parser, bool interpolation, uint8_t delimiter) {
uint8_t incrementor = lex_mode_incrementor(delimiter);
uint8_t terminator = lex_mode_terminator(delimiter);
pm_lex_mode_t lex_mode = {
.mode = PM_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.
uint8_t *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(pm_parser_t *parser, uint8_t incrementor, uint8_t terminator) {
pm_lex_mode_t lex_mode = {
.mode = PM_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.
uint8_t *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(pm_parser_t *parser, bool interpolation, bool label_allowed, uint8_t incrementor, uint8_t terminator) {
pm_lex_mode_t lex_mode = {
.mode = PM_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.
uint8_t *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(pm_parser_t *parser) {
if (parser->lex_modes.index == 0) {
parser->lex_modes.current->mode = PM_LEX_DEFAULT;
} else if (parser->lex_modes.index < PM_LEX_STACK_SIZE) {
parser->lex_modes.index--;
parser->lex_modes.current = &parser->lex_modes.stack[parser->lex_modes.index];
} else {
parser->lex_modes.index--;
pm_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(pm_parser_t *parser, pm_lex_state_t state) {
return parser->lex_state & state;
}
typedef enum {
PM_IGNORED_NEWLINE_NONE = 0,
PM_IGNORED_NEWLINE_ALL,
PM_IGNORED_NEWLINE_PATTERN
} pm_ignored_newline_type_t;
static inline pm_ignored_newline_type_t
lex_state_ignored_p(pm_parser_t *parser) {
bool ignored = lex_state_p(parser, PM_LEX_STATE_BEG | PM_LEX_STATE_CLASS | PM_LEX_STATE_FNAME | PM_LEX_STATE_DOT) && !lex_state_p(parser, PM_LEX_STATE_LABELED);
if (ignored) {
return PM_IGNORED_NEWLINE_ALL;
} else if ((parser->lex_state & ~((unsigned int) PM_LEX_STATE_LABEL)) == (PM_LEX_STATE_ARG | PM_LEX_STATE_LABELED)) {
return PM_IGNORED_NEWLINE_PATTERN;
} else {
return PM_IGNORED_NEWLINE_NONE;
}
}
static inline bool
lex_state_beg_p(pm_parser_t *parser) {
return lex_state_p(parser, PM_LEX_STATE_BEG_ANY) || (parser->lex_state == (PM_LEX_STATE_ARG | PM_LEX_STATE_LABELED));
}
static inline bool
lex_state_arg_p(pm_parser_t *parser) {
return lex_state_p(parser, PM_LEX_STATE_ARG_ANY);
}
static inline bool
lex_state_spcarg_p(pm_parser_t *parser, bool space_seen) {
if (parser->current.end >= parser->end) {
return false;
}
return lex_state_arg_p(parser) && space_seen && !pm_char_is_whitespace(*parser->current.end);
}
static inline bool
lex_state_end_p(pm_parser_t *parser) {
return lex_state_p(parser, PM_LEX_STATE_END_ANY);
}
// This is the equivalent of IS_AFTER_OPERATOR in CRuby.
static inline bool
lex_state_operator_p(pm_parser_t *parser) {
return lex_state_p(parser, PM_LEX_STATE_FNAME | PM_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(pm_parser_t *parser, pm_lex_state_t state) {
parser->lex_state = state;
}
#if PM_DEBUG_LOGGING
static inline void
debug_lex_state_set(pm_parser_t *parser, pm_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
/******************************************************************************/
/* Diagnostic-related functions */
/******************************************************************************/
// Append an error to the list of errors on the parser.
static inline void
pm_parser_err(pm_parser_t *parser, const uint8_t *start, const uint8_t *end, pm_diagnostic_id_t diag_id) {
pm_diagnostic_list_append(&parser->error_list, start, end, diag_id);
}
// Append an error to the list of errors on the parser using the location of the
// current token.
static inline void
pm_parser_err_current(pm_parser_t *parser, pm_diagnostic_id_t diag_id) {
pm_parser_err(parser, parser->current.start, parser->current.end, diag_id);
}
// Append an error to the list of errors on the parser using the given location.
static inline void
pm_parser_err_location(pm_parser_t *parser, const pm_location_t *location, pm_diagnostic_id_t diag_id) {
pm_parser_err(parser, location->start, location->end, diag_id);
}
// Append an error to the list of errors on the parser using the location of the
// given node.
static inline void
pm_parser_err_node(pm_parser_t *parser, const pm_node_t *node, pm_diagnostic_id_t diag_id) {
pm_parser_err(parser, node->location.start, node->location.end, diag_id);
}
// Append an error to the list of errors on the parser using the location of the
// previous token.
static inline void
pm_parser_err_previous(pm_parser_t *parser, pm_diagnostic_id_t diag_id) {
pm_parser_err(parser, parser->previous.start, parser->previous.end, diag_id);
}
// Append an error to the list of errors on the parser using the location of the
// given token.
static inline void
pm_parser_err_token(pm_parser_t *parser, const pm_token_t *token, pm_diagnostic_id_t diag_id) {
pm_parser_err(parser, token->start, token->end, diag_id);
}
// Append a warning to the list of warnings on the parser.
static inline void
pm_parser_warn(pm_parser_t *parser, const uint8_t *start, const uint8_t *end, pm_diagnostic_id_t diag_id) {
pm_diagnostic_list_append(&parser->warning_list, start, end, diag_id);
}
// Append a warning to the list of warnings on the parser using the location of
// the given token.
static inline void
pm_parser_warn_token(pm_parser_t *parser, const pm_token_t *token, pm_diagnostic_id_t diag_id) {
pm_parser_warn(parser, token->start, token->end, diag_id);
}
/******************************************************************************/
/* Node-related functions */
/******************************************************************************/
// Retrieve the constant pool id for the given location.
static inline pm_constant_id_t
pm_parser_constant_id_location(pm_parser_t *parser, const uint8_t *start, const uint8_t *end) {
return pm_constant_pool_insert_shared(&parser->constant_pool, start, (size_t) (end - start));
}
// Retrieve the constant pool id for the given string.
static inline pm_constant_id_t
pm_parser_constant_id_owned(pm_parser_t *parser, const uint8_t *start, size_t length) {
return pm_constant_pool_insert_owned(&parser->constant_pool, start, length);
}
// Retrieve the constant pool id for the given static literal C string.
static inline pm_constant_id_t
pm_parser_constant_id_static(pm_parser_t *parser, const char *start, size_t length) {
uint8_t *owned_copy;
if (length > 0) {
owned_copy = malloc(length);
memcpy(owned_copy, start, length);
} else {
owned_copy = malloc(1);
owned_copy[0] = '\0';
}
return pm_constant_pool_insert_owned(&parser->constant_pool, owned_copy, length);
// Does not work because the static literal cannot be serialized as an offset of source
// return pm_constant_pool_insert_shared(&parser->constant_pool, start, length);
}
// Retrieve the constant pool id for the given token.
static inline pm_constant_id_t
pm_parser_constant_id_token(pm_parser_t *parser, const pm_token_t *token) {
return pm_parser_constant_id_location(parser, token->start, token->end);
}
// Retrieve the constant pool id for the given token. If the token is not
// provided, then return 0.
static inline pm_constant_id_t
pm_parser_optional_constant_id_token(pm_parser_t *parser, const pm_token_t *token) {
return token->type == PM_TOKEN_NOT_PROVIDED ? 0 : pm_parser_constant_id_token(parser, token);
}
// The predicate of conditional nodes can change what would otherwise be regular
// nodes into specialized nodes. For example:
//
// if foo .. bar => RangeNode becomes FlipFlopNode
// if foo and bar .. baz => RangeNode becomes FlipFlopNode
// if /foo/ => RegularExpressionNode becomes MatchLastLineNode
// if /foo #{bar}/ => InterpolatedRegularExpressionNode becomes InterpolatedMatchLastLineNode
//
static void
pm_conditional_predicate(pm_node_t *node) {
switch (PM_NODE_TYPE(node)) {
case PM_AND_NODE: {
pm_and_node_t *cast = (pm_and_node_t *) node;
pm_conditional_predicate(cast->left);
pm_conditional_predicate(cast->right);
break;
}
case PM_OR_NODE: {
pm_or_node_t *cast = (pm_or_node_t *) node;
pm_conditional_predicate(cast->left);
pm_conditional_predicate(cast->right);
break;
}
case PM_PARENTHESES_NODE: {
pm_parentheses_node_t *cast = (pm_parentheses_node_t *) node;
if ((cast->body != NULL) && PM_NODE_TYPE_P(cast->body, PM_STATEMENTS_NODE)) {
pm_statements_node_t *statements = (pm_statements_node_t *) cast->body;
if (statements->body.size == 1) pm_conditional_predicate(statements->body.nodes[0]);
}
break;
}
case PM_RANGE_NODE: {
pm_range_node_t *cast = (pm_range_node_t *) node;
if (cast->left) {
pm_conditional_predicate(cast->left);
}
if (cast->right) {
pm_conditional_predicate(cast->right);
}
// Here we change the range node into a flip flop node. We can do
// this since the nodes are exactly the same except for the type.
// We're only asserting against the size when we should probably
// assert against the entire layout, but we'll assume tests will
// catch this.
assert(sizeof(pm_range_node_t) == sizeof(pm_flip_flop_node_t));
node->type = PM_FLIP_FLOP_NODE;
break;
}
case PM_REGULAR_EXPRESSION_NODE:
// Here we change the regular expression node into a match last line
// node. We can do this since the nodes are exactly the same except
// for the type.
assert(sizeof(pm_regular_expression_node_t) == sizeof(pm_match_last_line_node_t));
node->type = PM_MATCH_LAST_LINE_NODE;
break;
case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE:
// Here we change the interpolated regular expression node into an
// interpolated match last line node. We can do this since the nodes
// are exactly the same except for the type.
assert(sizeof(pm_interpolated_regular_expression_node_t) == sizeof(pm_interpolated_match_last_line_node_t));
node->type = PM_INTERPOLATED_MATCH_LAST_LINE_NODE;
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:
//
// pm_token_t token;
// not_provided(&token, parser->previous.end);
//
static inline pm_token_t
not_provided(pm_parser_t *parser) {
return (pm_token_t) { .type = PM_TOKEN_NOT_PROVIDED, .start = parser->start, .end = parser->start };
}
#define PM_LOCATION_NULL_VALUE(parser) ((pm_location_t) { .start = parser->start, .end = parser->start })
#define PM_LOCATION_TOKEN_VALUE(token) ((pm_location_t) { .start = (token)->start, .end = (token)->end })
#define PM_LOCATION_NODE_VALUE(node) ((pm_location_t) { .start = (node)->location.start, .end = (node)->location.end })
#define PM_LOCATION_NODE_BASE_VALUE(node) ((pm_location_t) { .start = (node)->base.location.start, .end = (node)->base.location.end })
#define PM_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE ((pm_location_t) { .start = NULL, .end = NULL })
#define PM_OPTIONAL_LOCATION_TOKEN_VALUE(token) ((token)->type == PM_TOKEN_NOT_PROVIDED ? PM_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE : PM_LOCATION_TOKEN_VALUE(token))
// 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 {
pm_location_t opening_loc;
pm_arguments_node_t *arguments;
pm_location_t closing_loc;
pm_node_t *block;
} pm_arguments_t;
#define PM_EMPTY_ARGUMENTS ((pm_arguments_t) { \
.opening_loc = PM_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE, \
.arguments = NULL, \
.closing_loc = PM_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE, \
.block = NULL, \
})
// Check that we're not about to attempt to attach a brace block to a call that
// has arguments without parentheses.
static void
pm_arguments_validate_block(pm_parser_t *parser, pm_arguments_t *arguments, pm_block_node_t *block) {
// First, check that we have arguments and that we don't have a closing
// location for them.
if (arguments->arguments == NULL || arguments->closing_loc.start != NULL) {
return;
}
// Next, check that we don't have a single parentheses argument. This would
// look like:
//
// foo (1) {}
//
// In this case, it's actually okay for the block to be attached to the
// call, even though it looks like it's attached to the argument.
if (arguments->arguments->arguments.size == 1 && PM_NODE_TYPE_P(arguments->arguments->arguments.nodes[0], PM_PARENTHESES_NODE)) {
return;
}
// If we didn't hit a case before this check, then at this point we need to
// add a syntax error.
pm_parser_err_node(parser, (pm_node_t *) block, PM_ERR_ARGUMENT_UNEXPECTED_BLOCK);
}
/******************************************************************************/
/* Scope node functions */
/******************************************************************************/
// Generate a scope node from the given node.
void
pm_scope_node_init(pm_node_t *node, pm_scope_node_t *scope) {
scope->base.type = PM_SCOPE_NODE;
scope->base.location.start = node->location.start;
scope->base.location.end = node->location.end;
scope->parameters = NULL;
scope->body = NULL;
pm_constant_id_list_init(&scope->locals);
switch (PM_NODE_TYPE(node)) {
case PM_BLOCK_NODE: {
pm_block_node_t *cast = (pm_block_node_t *) node;
if (cast->parameters) scope->parameters = cast->parameters->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_CLASS_NODE: {
pm_class_node_t *cast = (pm_class_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_DEF_NODE: {
pm_def_node_t *cast = (pm_def_node_t *) node;
scope->parameters = cast->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_LAMBDA_NODE: {
pm_lambda_node_t *cast = (pm_lambda_node_t *) node;
if (cast->parameters) scope->parameters = cast->parameters->parameters;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_MODULE_NODE: {
pm_module_node_t *cast = (pm_module_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
case PM_PROGRAM_NODE: {
pm_program_node_t *cast = (pm_program_node_t *) node;
scope->body = (pm_node_t *) cast->statements;
scope->locals = cast->locals;
break;
}
case PM_SINGLETON_CLASS_NODE: {
pm_singleton_class_node_t *cast = (pm_singleton_class_node_t *) node;
scope->body = cast->body;
scope->locals = cast->locals;
break;
}
default:
assert(false && "unreachable");
break;
}
}
/******************************************************************************/
/* Node creation functions */
/******************************************************************************/
// Parse the decimal number represented by the range of bytes. returns
// UINT32_MAX if the number fails to parse. This function assumes that the range
// of bytes has already been validated to contain only decimal digits.
static uint32_t
parse_decimal_number(pm_parser_t *parser, const uint8_t *start, const uint8_t *end) {
ptrdiff_t diff = end - start;
assert(diff > 0 && ((unsigned long) diff < SIZE_MAX));
size_t length = (size_t) diff;
char *digits = calloc(length + 1, sizeof(char));
memcpy(digits, start, length);
digits[length] = '\0';
char *endptr;
errno = 0;
unsigned long value = strtoul(digits, &endptr, 10);
if ((digits == endptr) || (*endptr != '\0') || (errno == ERANGE)) {
pm_parser_err(parser, start, end, PM_ERR_INVALID_NUMBER_DECIMAL);
value = UINT32_MAX;
}
free(digits);
if (value > UINT32_MAX) {
pm_parser_err(parser, start, end, PM_ERR_INVALID_NUMBER_DECIMAL);
value = UINT32_MAX;
}
return (uint32_t) value;
}
// Parse out the options for a regular expression.
static inline pm_node_flags_t
pm_regular_expression_flags_create(const pm_token_t *closing) {
pm_node_flags_t flags = 0;
if (closing->type == PM_TOKEN_REGEXP_END) {
for (const uint8_t *flag = closing->start + 1; flag < closing->end; flag++) {
switch (*flag) {
case 'i': flags |= PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE; break;
case 'm': flags |= PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE; break;
case 'x': flags |= PM_REGULAR_EXPRESSION_FLAGS_EXTENDED; break;
case 'e': flags |= PM_REGULAR_EXPRESSION_FLAGS_EUC_JP; break;
case 'n': flags |= PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT; break;
case 's': flags |= PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J; break;
case 'u': flags |= PM_REGULAR_EXPRESSION_FLAGS_UTF_8; break;
case 'o': flags |= PM_REGULAR_EXPRESSION_FLAGS_ONCE; break;
default: assert(false && "unreachable");
}
}
}
return flags;
}
// Allocate and initialize a new StatementsNode node.
static pm_statements_node_t *
pm_statements_node_create(pm_parser_t *parser);
// Append a new node to the given StatementsNode node's body.
static void
pm_statements_node_body_append(pm_statements_node_t *node, pm_node_t *statement);
// Get the length of the given StatementsNode node's body.
static size_t
pm_statements_node_body_length(pm_statements_node_t *node);
// This function is here to allow us a place to extend in the future when we
// implement our own arena allocation.
static inline void *
pm_alloc_node(PRISM_ATTRIBUTE_UNUSED pm_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 PM_ALLOC_NODE(parser, type) (type *) pm_alloc_node(parser, sizeof(type))
// Allocate a new MissingNode node.
static pm_missing_node_t *
pm_missing_node_create(pm_parser_t *parser, const uint8_t *start, const uint8_t *end) {
pm_missing_node_t *node = PM_ALLOC_NODE(parser, pm_missing_node_t);
*node = (pm_missing_node_t) {{ .type = PM_MISSING_NODE, .location = { .start = start, .end = end } }};
return node;
}
// Allocate and initialize a new AliasGlobalVariableNode node.
static pm_alias_global_variable_node_t *
pm_alias_global_variable_node_create(pm_parser_t *parser, const pm_token_t *keyword, pm_node_t *new_name, pm_node_t *old_name) {
assert(keyword->type == PM_TOKEN_KEYWORD_ALIAS);
pm_alias_global_variable_node_t *node = PM_ALLOC_NODE(parser, pm_alias_global_variable_node_t);
*node = (pm_alias_global_variable_node_t) {
{
.type = PM_ALIAS_GLOBAL_VARIABLE_NODE,
.location = {
.start = keyword->start,
.end = old_name->location.end
},
},
.new_name = new_name,
.old_name = old_name,
.keyword_loc = PM_LOCATION_TOKEN_VALUE(keyword)
};
return node;
}
// Allocate and initialize a new AliasMethodNode node.
static pm_alias_method_node_t *
pm_alias_method_node_create(pm_parser_t *parser, const pm_token_t *keyword, pm_node_t *new_name, pm_node_t *old_name) {
assert(keyword->type == PM_TOKEN_KEYWORD_ALIAS);
pm_alias_method_node_t *node = PM_ALLOC_NODE(parser, pm_alias_method_node_t);
*node = (pm_alias_method_node_t) {
{
.type = PM_ALIAS_METHOD_NODE,
.location = {
.start = keyword->start,
.end = old_name->location.end
},
},
.new_name = new_name,
.old_name = old_name,
.keyword_loc = PM_LOCATION_TOKEN_VALUE(keyword)
};
return node;
}
// Allocate a new AlternationPatternNode node.
static pm_alternation_pattern_node_t *
pm_alternation_pattern_node_create(pm_parser_t *parser, pm_node_t *left, pm_node_t *right, const pm_token_t *operator) {
pm_alternation_pattern_node_t *node = PM_ALLOC_NODE(parser, pm_alternation_pattern_node_t);
*node = (pm_alternation_pattern_node_t) {
{
.type = PM_ALTERNATION_PATTERN_NODE,
.location = {
.start = left->location.start,
.end = right->location.end
},
},
.left = left,
.right = right,
.operator_loc = PM_LOCATION_TOKEN_VALUE(operator)
};
return node;
}
// Allocate and initialize a new and node.
static pm_and_node_t *
pm_and_node_create(pm_parser_t *parser, pm_node_t *left, const pm_token_t *operator, pm_node_t *right) {
pm_and_node_t *node = PM_ALLOC_NODE(parser, pm_and_node_t);
*node = (pm_and_node_t) {
{
.type = PM_AND_NODE,
.location = {
.start = left->location.start,
.end = right->location.end
},
},
.left = left,
.operator_loc = PM_LOCATION_TOKEN_VALUE(operator),
.right = right
};
return node;
}
// Allocate an initialize a new arguments node.
static pm_arguments_node_t *
pm_arguments_node_create(pm_parser_t *parser) {
pm_arguments_node_t *node = PM_ALLOC_NODE(parser, pm_arguments_node_t);
*node = (pm_arguments_node_t) {
{
.type = PM_ARGUMENTS_NODE,
.location = PM_LOCATION_NULL_VALUE(parser)
},
.arguments = PM_EMPTY_NODE_LIST
};
return node;
}
// Return the size of the given arguments node.
static size_t
pm_arguments_node_size(pm_arguments_node_t *node) {
return node->arguments.size;
}
// Append an argument to an arguments node.
static void
pm_arguments_node_arguments_append(pm_arguments_node_t *node, pm_node_t *argument) {
if (pm_arguments_node_size(node) == 0) {
node->base.location.start = argument->location.start;
}
node->base.location.end = argument->location.end;
pm_node_list_append(&node->arguments, argument);
}
// Allocate and initialize a new ArrayNode node.
static pm_array_node_t *
pm_array_node_create(pm_parser_t *parser, const pm_token_t *opening) {
pm_array_node_t *node = PM_ALLOC_NODE(parser, pm_array_node_t);
*node = (pm_array_node_t) {
{
.type = PM_ARRAY_NODE,
.flags = PM_NODE_FLAG_STATIC_LITERAL,
.location = PM_LOCATION_TOKEN_VALUE(opening)
},
.opening_loc = PM_OPTIONAL_LOCATION_TOKEN_VALUE(opening),
.closing_loc = PM_OPTIONAL_LOCATION_TOKEN_VALUE(opening),
.elements = PM_EMPTY_NODE_LIST
};
return node;
}
// Return the size of the given array node.
static inline size_t
pm_array_node_size(pm_array_node_t *node) {
return node->elements.size;
}
// Append an argument to an array node.
static inline void
pm_array_node_elements_append(pm_array_node_t *node, pm_node_t *element) {
if (!node->elements.size && !node->opening_loc.start) {
node->base.location.start = element->location.start;
}
pm_node_list_append(&node->elements, element);
node->base.location.end = element->location.end;
// If the element is not a static literal, then the array is not a static
// literal. Turn that flag off.
if (PM_NODE_TYPE_P(element, PM_ARRAY_NODE) || PM_NODE_TYPE_P(element, PM_HASH_NODE) || PM_NODE_TYPE_P(element, PM_RANGE_NODE) || (element->flags & PM_NODE_FLAG_STATIC_LITERAL) == 0) {
node->base.flags &= (pm_node_flags_t) ~PM_NODE_FLAG_STATIC_LITERAL;
}
}
// Set the closing token and end location of an array node.
static void
pm_array_node_close_set(pm_array_node_t *node, const pm_token_t *closing) {
assert(closing->type == PM_TOKEN_BRACKET_RIGHT || closing->type == PM_TOKEN_STRING_END || closing->type == PM_TOKEN_MISSING || closing->type == PM_TOKEN_NOT_PROVIDED);
node->base.location.end = closing->end;
node->closing_loc = PM_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 pm_array_pattern_node_t *
pm_array_pattern_node_node_list_create(pm_parser_t *parser, pm_node_list_t *nodes) {
pm_array_pattern_node_t *node = PM_ALLOC_NODE(parser, pm_array_pattern_node_t);
*node = (pm_array_pattern_node_t) {
{
.type = PM_ARRAY_PATTERN_NODE,
.location = {