Ruby 4.1.0dev (2026-11-25 revision 2a3f7e1f2ddfee0bfd40492fdd6e4290aac8fc58)
parser.h
1#ifndef PRISM_INTERNAL_PARSER_H
2#define PRISM_INTERNAL_PARSER_H
3
7
8#include "prism/internal/arena.h"
9#include "prism/internal/buffer.h"
10#include "prism/internal/comments.h"
11#include "prism/internal/constant_pool.h"
12#include "prism/internal/encoding.h"
13#include "prism/internal/list.h"
14#include "prism/internal/node.h"
15#include "prism/internal/options.h"
16#include "prism/internal/static_literals.h"
17#include "prism/internal/strpbrk.h"
18
19#include "prism/ast.h"
21#include "prism/parser.h"
22
23#include <stdbool.h>
24#include <stddef.h>
25#include <stdint.h>
26
27/*
28 * A place in the unescaped content of a regular expression where it stops
29 * lining up with the source, because the lexer dropped or rewrote an escape
30 * just before it. From here up to the next such place, each byte of the content
31 * is the byte at the same distance into the source.
32 */
33typedef struct {
34 /* The offset of the place in the unescaped content. */
35 uint32_t content;
36
37 /* The offset in the source of the byte at that place. */
38 uint32_t source;
40
41/*
42 * The places where the unescaped content of a regular expression stops lining
43 * up with the source, in order. Before the first one, each byte of the content
44 * is the byte at the same distance from the start of the content in the source.
45 * The values are allocated in the scratch arena, since they are only read while
46 * the regular expression is validated.
47 */
48typedef struct {
49 /* The places, in order of their offsets in the content. */
50 pm_regexp_offset_t *values;
51
52 /* The number of places. */
53 size_t size;
54
55 /* The number of places that fit in the values before they must grow. */
56 size_t capacity;
58
59/*
60 * This enum provides various bits that represent different kinds of states that
61 * the lexer can track. This is used to determine which kind of token to return
62 * based on the context of the parser.
63 */
64typedef enum {
65 PM_LEX_STATE_BIT_BEG,
66 PM_LEX_STATE_BIT_END,
67 PM_LEX_STATE_BIT_ENDARG,
68 PM_LEX_STATE_BIT_ENDFN,
69 PM_LEX_STATE_BIT_ARG,
70 PM_LEX_STATE_BIT_CMDARG,
71 PM_LEX_STATE_BIT_MID,
72 PM_LEX_STATE_BIT_FNAME,
73 PM_LEX_STATE_BIT_DOT,
74 PM_LEX_STATE_BIT_CLASS,
75 PM_LEX_STATE_BIT_LABEL,
76 PM_LEX_STATE_BIT_LABELED,
77 PM_LEX_STATE_BIT_FITEM
78} pm_lex_state_bit_t;
79
80/*
81 * This enum combines the various bits from the above enum into individual
82 * values that represent the various states of the lexer.
83 */
84typedef enum {
85 PM_LEX_STATE_NONE = 0,
86 PM_LEX_STATE_BEG = (1 << PM_LEX_STATE_BIT_BEG),
87 PM_LEX_STATE_END = (1 << PM_LEX_STATE_BIT_END),
88 PM_LEX_STATE_ENDARG = (1 << PM_LEX_STATE_BIT_ENDARG),
89 PM_LEX_STATE_ENDFN = (1 << PM_LEX_STATE_BIT_ENDFN),
90 PM_LEX_STATE_ARG = (1 << PM_LEX_STATE_BIT_ARG),
91 PM_LEX_STATE_CMDARG = (1 << PM_LEX_STATE_BIT_CMDARG),
92 PM_LEX_STATE_MID = (1 << PM_LEX_STATE_BIT_MID),
93 PM_LEX_STATE_FNAME = (1 << PM_LEX_STATE_BIT_FNAME),
94 PM_LEX_STATE_DOT = (1 << PM_LEX_STATE_BIT_DOT),
95 PM_LEX_STATE_CLASS = (1 << PM_LEX_STATE_BIT_CLASS),
96 PM_LEX_STATE_LABEL = (1 << PM_LEX_STATE_BIT_LABEL),
97 PM_LEX_STATE_LABELED = (1 << PM_LEX_STATE_BIT_LABELED),
98 PM_LEX_STATE_FITEM = (1 << PM_LEX_STATE_BIT_FITEM),
99 PM_LEX_STATE_BEG_ANY = PM_LEX_STATE_BEG | PM_LEX_STATE_MID | PM_LEX_STATE_CLASS,
100 PM_LEX_STATE_ARG_ANY = PM_LEX_STATE_ARG | PM_LEX_STATE_CMDARG,
101 PM_LEX_STATE_END_ANY = PM_LEX_STATE_END | PM_LEX_STATE_ENDARG | PM_LEX_STATE_ENDFN
102} pm_lex_state_t;
103
104/*
105 * The type of quote that a heredoc uses.
106 */
107typedef enum {
108 PM_HEREDOC_QUOTE_NONE,
109 PM_HEREDOC_QUOTE_SINGLE = '\'',
110 PM_HEREDOC_QUOTE_DOUBLE = '"',
111 PM_HEREDOC_QUOTE_BACKTICK = '`',
112} pm_heredoc_quote_t;
113
114/*
115 * The type of indentation that a heredoc uses.
116 */
117typedef enum {
118 PM_HEREDOC_INDENT_NONE,
119 PM_HEREDOC_INDENT_DASH,
120 PM_HEREDOC_INDENT_TILDE,
121} pm_heredoc_indent_t;
122
123/*
124 * All of the information necessary to store to lexing a heredoc.
125 */
126typedef struct {
127 /* A pointer to the start of the heredoc identifier. */
128 const uint8_t *ident_start;
129
130 /* The length of the heredoc identifier. */
131 size_t ident_length;
132
133 /* The type of quote that the heredoc uses. */
134 pm_heredoc_quote_t quote;
135
136 /* The type of indentation that the heredoc uses. */
137 pm_heredoc_indent_t indent;
139
140/*
141 * When lexing Ruby source, the lexer has a small amount of state to tell which
142 * kind of token it is currently lexing. For example, when we find the start of
143 * a string, the first token that we return is a TOKEN_STRING_BEGIN token. After
144 * that the lexer is now in the PM_LEX_STRING mode, and will return tokens that
145 * are found as part of a string.
146 */
147typedef struct pm_lex_mode {
148 /* The type of this lex mode. */
149 enum {
150 /* This state is used when any given token is being lexed. */
151 PM_LEX_DEFAULT,
152
153 /*
154 * This state is used when we're lexing as normal but inside an embedded
155 * expression of a string.
156 */
157 PM_LEX_EMBEXPR,
158
159 /*
160 * This state is used when we're lexing a variable that is embedded
161 * directly inside of a string with the # shorthand.
162 */
163 PM_LEX_EMBVAR,
164
165 /* This state is used when you are inside the content of a heredoc. */
166 PM_LEX_HEREDOC,
167
168 /*
169 * This state is used when we are lexing a list of tokens, as in a %w
170 * word list literal or a %i symbol list literal.
171 */
172 PM_LEX_LIST,
173
174 /*
175 * This state is used when a regular expression has been begun and we
176 * are looking for the terminator.
177 */
178 PM_LEX_REGEXP,
179
180 /*
181 * This state is used when we are lexing a string or a string-like
182 * token, as in string content with either quote or an xstring.
183 */
184 PM_LEX_STRING
185 } mode;
186
187 /* The data associated with this type of lex mode. */
188 union {
189 struct {
190 /* This keeps track of the nesting level of the list. */
191 size_t nesting;
192
193 /* Whether or not interpolation is allowed in this list. */
194 bool interpolation;
195
196 /*
197 * Whether any token has been emitted from this list. A word
198 * separator delimits the opener from the first word, so one
199 * without source characters is emitted when the list does not
200 * start with whitespace.
201 */
202 bool started;
203
204 /*
205 * Whether the previously emitted token was a word separator. A
206 * word separator delimits the last word from the terminator, so
207 * one without source characters is emitted when the list does
208 * not end with whitespace.
209 */
210 bool separated;
211
212 /*
213 * When lexing a list, it takes into account balancing the
214 * terminator if the terminator is one of (), [], {}, or <>.
215 */
216 uint8_t incrementor;
217
218 /* This is the terminator of the list literal. */
219 uint8_t terminator;
220
221 /*
222 * This is the character set that should be used to delimit the
223 * tokens within the list.
224 */
225 uint8_t breakpoints[PM_STRPBRK_CACHE_SIZE];
226 } list;
227
228 struct {
229 /*
230 * This keeps track of the nesting level of the regular expression.
231 */
232 size_t nesting;
233
234 /*
235 * When lexing a regular expression, it takes into account balancing
236 * the terminator if the terminator is one of (), [], {}, or <>.
237 */
238 uint8_t incrementor;
239
240 /* This is the terminator of the regular expression. */
241 uint8_t terminator;
242
243 /*
244 * This is the character set that should be used to delimit the
245 * tokens within the regular expression.
246 */
247 uint8_t breakpoints[PM_STRPBRK_CACHE_SIZE];
248 } regexp;
249
250 struct {
251 /* This keeps track of the nesting level of the string. */
252 size_t nesting;
253
254 /* Whether or not interpolation is allowed in this string. */
255 bool interpolation;
256
257 /*
258 * Whether or not at the end of the string we should allow a :,
259 * which would indicate this was a dynamic symbol instead of a
260 * string.
261 */
262 bool label_allowed;
263
264 /* Whether the final content token joined text after the last
265 * breakpoint, where this string used to split at EOF. */
266 bool joined_at_eof;
267
268 /*
269 * When lexing a string, it takes into account balancing the
270 * terminator if the terminator is one of (), [], {}, or <>.
271 */
272 uint8_t incrementor;
273
274 /*
275 * This is the terminator of the string. It is typically either a
276 * single or double quote.
277 */
278 uint8_t terminator;
279
280 /*
281 * This is the character set that should be used to delimit the
282 * tokens within the string.
283 */
284 uint8_t breakpoints[PM_STRPBRK_CACHE_SIZE];
285 } string;
286
287 struct {
288 /*
289 * All of the data necessary to lex a heredoc.
290 */
292
293 /*
294 * This is the pointer to the character where lexing should resume
295 * once the heredoc has been completely processed.
296 */
297 const uint8_t *next_start;
298
299 /*
300 * This is used to track the amount of common whitespace on each
301 * line so that we know how much to dedent each line in the case of
302 * a tilde heredoc.
303 */
304 size_t *common_whitespace;
305
306 /* True if the previous token ended with a line continuation. */
307 bool line_continuation;
308 } heredoc;
309 } as;
310
311 /* The previous lex state so that it knows how to pop. */
312 struct pm_lex_mode *prev;
313} pm_lex_mode_t;
314
315/*
316 * While parsing, we keep track of a stack of contexts. This is helpful for
317 * error recovery so that we can pop back to a previous context when we hit a
318 * token that is understood by a parent context but not by the current context.
319 */
320typedef enum {
321 /* a null context, used for returning a value from a function */
322 PM_CONTEXT_NONE = 0,
323
324 /* a begin statement */
325 PM_CONTEXT_BEGIN,
326
327 /* an ensure statement with an explicit begin */
328 PM_CONTEXT_BEGIN_ENSURE,
329
330 /* a rescue else statement with an explicit begin */
331 PM_CONTEXT_BEGIN_ELSE,
332
333 /* a rescue statement with an explicit begin */
334 PM_CONTEXT_BEGIN_RESCUE,
335
336 /* expressions in block arguments using braces */
337 PM_CONTEXT_BLOCK_BRACES,
338
339 /* expressions in block arguments using do..end */
340 PM_CONTEXT_BLOCK_KEYWORDS,
341
342 /* an ensure statement within a do..end block */
343 PM_CONTEXT_BLOCK_ENSURE,
344
345 /* a rescue else statement within a do..end block */
346 PM_CONTEXT_BLOCK_ELSE,
347
348 /* expressions in block parameters `foo do |...| end ` */
349 PM_CONTEXT_BLOCK_PARAMETERS,
350
351 /* a rescue statement within a do..end block */
352 PM_CONTEXT_BLOCK_RESCUE,
353
354 /* a case when statements */
355 PM_CONTEXT_CASE_WHEN,
356
357 /* a case in statements */
358 PM_CONTEXT_CASE_IN,
359
360 /* a class declaration */
361 PM_CONTEXT_CLASS,
362
363 /* an ensure statement within a class statement */
364 PM_CONTEXT_CLASS_ENSURE,
365
366 /* a rescue else statement within a class statement */
367 PM_CONTEXT_CLASS_ELSE,
368
369 /* a rescue statement within a class statement */
370 PM_CONTEXT_CLASS_RESCUE,
371
372 /* a method definition */
373 PM_CONTEXT_DEF,
374
375 /* an ensure statement within a method definition */
376 PM_CONTEXT_DEF_ENSURE,
377
378 /* a rescue else statement within a method definition */
379 PM_CONTEXT_DEF_ELSE,
380
381 /* a rescue statement within a method definition */
382 PM_CONTEXT_DEF_RESCUE,
383
384 /* a method definition's parameters */
385 PM_CONTEXT_DEF_PARAMS,
386
387 /* a defined? expression */
388 PM_CONTEXT_DEFINED,
389
390 /* a method definition's default parameter */
391 PM_CONTEXT_DEFAULT_PARAMS,
392
393 /* an else clause */
394 PM_CONTEXT_ELSE,
395
396 /* an elsif clause */
397 PM_CONTEXT_ELSIF,
398
399 /* an interpolated expression */
400 PM_CONTEXT_EMBEXPR,
401
402 /* a for loop */
403 PM_CONTEXT_FOR,
404
405 /* a for loop's index */
406 PM_CONTEXT_FOR_INDEX,
407
408 /* an if statement */
409 PM_CONTEXT_IF,
410
411 /* a lambda expression with braces */
412 PM_CONTEXT_LAMBDA_BRACES,
413
414 /* a lambda expression with do..end */
415 PM_CONTEXT_LAMBDA_DO_END,
416
417 /* an ensure statement within a lambda expression */
418 PM_CONTEXT_LAMBDA_ENSURE,
419
420 /* a rescue else statement within a lambda expression */
421 PM_CONTEXT_LAMBDA_ELSE,
422
423 /* expressions in lambda parameters */
424 PM_CONTEXT_LAMBDA_PARAMETERS,
425
426 /* a rescue statement within a lambda expression */
427 PM_CONTEXT_LAMBDA_RESCUE,
428
429 /* the predicate clause of a loop statement */
430 PM_CONTEXT_LOOP_PREDICATE,
431
432 /* the top level context */
433 PM_CONTEXT_MAIN,
434
435 /* a module declaration */
436 PM_CONTEXT_MODULE,
437
438 /* an ensure statement within a module statement */
439 PM_CONTEXT_MODULE_ENSURE,
440
441 /* a rescue else statement within a module statement */
442 PM_CONTEXT_MODULE_ELSE,
443
444 /* a rescue statement within a module statement */
445 PM_CONTEXT_MODULE_RESCUE,
446
447 /* a multiple target expression */
448 PM_CONTEXT_MULTI_TARGET,
449
450 /* a parenthesized expression */
451 PM_CONTEXT_PARENS,
452
453 /* an END block */
454 PM_CONTEXT_POSTEXE,
455
456 /* a predicate inside an if/elsif/unless statement */
457 PM_CONTEXT_PREDICATE,
458
459 /* a BEGIN block */
460 PM_CONTEXT_PREEXE,
461
462 /* the exception list and assignment target of a rescue clause */
463 PM_CONTEXT_RESCUE_HEADER,
464
465 /* a modifier rescue clause */
466 PM_CONTEXT_RESCUE_MODIFIER,
467
468 /* a singleton class definition */
469 PM_CONTEXT_SCLASS,
470
471 /* an ensure statement with a singleton class */
472 PM_CONTEXT_SCLASS_ENSURE,
473
474 /* a rescue else statement with a singleton class */
475 PM_CONTEXT_SCLASS_ELSE,
476
477 /* a rescue statement with a singleton class */
478 PM_CONTEXT_SCLASS_RESCUE,
479
480 /* a ternary expression */
481 PM_CONTEXT_TERNARY,
482
483 /* an unless statement */
484 PM_CONTEXT_UNLESS,
485
486 /* an until statement */
487 PM_CONTEXT_UNTIL,
488
489 /* a while statement */
490 PM_CONTEXT_WHILE,
491
492 /* the number of contexts, which is not itself a context */
493 PM_CONTEXT_MAXIMUM,
494} pm_context_t;
495
496/* This is a node in a linked list of contexts. */
497typedef struct pm_context_node {
498 /* The context that this node represents. */
499 pm_context_t context;
500
501 /* A pointer to the previous context in the linked list. */
502 struct pm_context_node *prev;
503
504 /* One bit set per context in this list, including one for this node. */
505 uint64_t mask;
506} pm_context_node_t;
507
508/* The type of shareable constant value that can be set. */
509typedef uint8_t pm_shareable_constant_value_t;
510static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_NONE = 0x0;
511static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_LITERAL = PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL;
512static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_EVERYTHING = PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING;
513static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_COPY = PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY;
514
515/*
516 * This tracks an individual local variable in a certain lexical context, as
517 * well as the number of times is it read. The locals of every live scope are
518 * kept on a single stack on the parser, since a local is only ever declared in
519 * the innermost scope and is discarded when that scope is popped.
520 */
521typedef struct {
522 /* The name of the local variable. */
523 pm_constant_id_t name;
524
525 /* The location of the local variable in the source. */
526 pm_location_t location;
527
528 /* The number of times the local variable is read. */
529 uint32_t reads;
530
531 /* The level of the scope that declares the local variable. */
532 uint32_t level;
533
534 /*
535 * The index in the locals stack of the local with the same name that this
536 * local hides while its scope is live, or 0 if it hides nothing. It is
537 * restored as the binding of the name when the scope is popped.
538 */
539 uint32_t shadowed;
540} pm_local_t;
541
542/* The flags about scope parameters that can be set. */
543typedef uint8_t pm_scope_parameters_t;
544static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NONE = 0x0;
545static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_POSITIONALS = 0x1;
546static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_KEYWORDS = 0x2;
547static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_BLOCK = 0x4;
548static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_ALL = 0x8;
549static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_IMPLICIT_DISALLOWED = 0x10;
550static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NUMBERED_INNER = 0x20;
551static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NUMBERED_FOUND = 0x40;
552
553/*
554 * This struct represents a node in a linked list of scopes. Some scopes can see
555 * into their parent scopes, while others cannot.
556 */
557typedef struct pm_scope {
558 /* A pointer to the previous scope in the linked list. */
559 struct pm_scope *previous;
560
561 /*
562 * The index in the locals stack of the first local of this scope. The
563 * locals of this scope run from here to the top of the stack.
564 */
565 uint32_t locals_start;
566
567 /*
568 * The position of this scope in the scope stack, where the outermost scope
569 * is at level 1.
570 */
571 uint32_t level;
572
573 /*
574 * The locals_start of the nearest closed scope at or above this one. Every
575 * local below this index in the locals stack belongs to a scope that this
576 * scope cannot see into.
577 */
578 uint32_t floor;
579
580 /*
581 * This is a list of the implicit parameters contained within the block.
582 * These will be processed after the block is parsed to determine the kind
583 * of parameters node that should be used and to check if any errors need to
584 * be added.
585 */
586 pm_scratch_node_list_t implicit_parameters;
587
588 /*
589 * This is a bitfield that indicates the parameters that are being used in
590 * this scope. It is a combination of the PM_SCOPE_PARAMETERS_* constants.
591 * There are three different kinds of parameters that can be used in a
592 * scope:
593 *
594 * - Ordinary parameters (e.g., def foo(bar); end)
595 * - Numbered parameters (e.g., def foo; _1; end)
596 * - The it parameter (e.g., def foo; it; end)
597 *
598 * If ordinary parameters are being used, then certain parameters can be
599 * forwarded to another method/structure. Those are indicated by four
600 * additional bits in the params field. For example, some combinations of:
601 *
602 * - def foo(*); end
603 * - def foo(**); end
604 * - def foo(&); end
605 * - def foo(...); end
606 */
607 pm_scope_parameters_t parameters;
608
609 /*
610 * The current state of constant shareability for this scope. This is
611 * changed by magic shareable_constant_value comments.
612 */
613 pm_shareable_constant_value_t shareable_constant;
614
615 /*
616 * A boolean indicating whether or not this scope can see into its parent.
617 * If closed is true, then the scope cannot see into its parent.
618 */
619 bool closed;
620} pm_scope_t;
621
622/*
623 * A stack of boolean values with no fixed depth limit. The newest frames live
624 * in an inline word, so pushing and popping are usually a shift and a compare.
625 * When that word fills, it moves to the spill array and the inline word starts
626 * over empty. The spill array is only allocated once the nesting is more than
627 * 64 frames deep.
628 */
629typedef struct {
630 /*
631 * The newest frames, with the top of the stack in bit 0. It holds between
632 * 1 and 64 frames while the stack is not empty, and is 0 when it is.
633 */
634 uint64_t top;
635
636 /*
637 * The number of frames on the whole stack. The inline word holds the
638 * frames above the last multiple of 64, so the spill array holds
639 * (depth - 1) / 64 words.
640 */
641 uint32_t depth;
642
643 /* The number of words the spill array has room for. */
644 uint32_t capacity;
645
646 /*
647 * Full words of 64 older frames each, oldest first, allocated from the
648 * scratch arena.
649 */
650 uint64_t *spill;
652
653/*
654 * This struct represents the overall parser. It contains a reference to the
655 * source file, as well as pointers that indicate where in the source it's
656 * currently parsing. It also contains the most recent and current token that
657 * it's considering.
658 */
660 /* The arena used for all AST-lifetime allocations. Caller-owned. */
661 pm_arena_t *arena;
662
663 /*
664 * The arena used for parser metadata (comments, diagnostics, etc.), which
665 * lives as long as the parser.
666 */
667 pm_arena_t metadata_arena;
668
669 /*
670 * The arena used for state that is only needed while parsing, such as
671 * scopes, locals, and contexts. It is released when pm_parse returns, so
672 * nothing allocated in it can be reachable from the tree or from the
673 * parser's metadata.
674 */
675 pm_arena_t scratch_arena;
676
677 /*
678 * The next node identifier that will be assigned. This is a unique
679 * identifier used to track nodes such that the syntax tree can be dropped
680 * but the node can be found through another parse.
681 */
682 uint32_t node_id;
683
684 /*
685 * A single-entry cache for pm_parser_constant_id_raw. Avoids redundant
686 * constant pool lookups when the same token is resolved multiple times
687 * (e.g., once during lexing for local variable detection, and again
688 * during parsing for node creation).
689 */
690 struct {
691 const uint8_t *start;
692 const uint8_t *end;
694 } constant_cache;
695
696 /* The current state of the lexer. */
697 pm_lex_state_t lex_state;
698
699 /* Tracks the current nesting of (), [], and {}. */
700 int enclosure_nesting;
701
702 /*
703 * Used to temporarily track the nesting of enclosures to determine if a {
704 * is the beginning of a lambda following the parameters of a lambda.
705 */
706 int lambda_enclosure_nesting;
707
708 /*
709 * Used to track the nesting of braces to ensure we get the correct value
710 * when we are interpolating blocks with braces.
711 */
712 int brace_nesting;
713
714 /*
715 * The stack used to determine if a do keyword belongs to the predicate of a
716 * while, until, or for loop.
717 */
718 pm_state_stack_t do_loop_stack;
719
720 /*
721 * The stack used to determine if a do keyword belongs to the beginning of a
722 * block.
723 */
724 pm_state_stack_t accepts_block_stack;
725
726 /* A stack of lex modes, linked through their prev pointers. */
727 struct {
728 /* The current mode of the lexer. */
729 pm_lex_mode_t *current;
730
731 /*
732 * The mode at the bottom of the stack, which is never popped. Every
733 * other mode is allocated from the scratch arena.
734 */
735 pm_lex_mode_t base;
736
737 /*
738 * Lex modes that have been popped, kept for reuse by the next push.
739 * They are allocated from the scratch arena, so they are never freed
740 * individually.
741 */
742 pm_lex_mode_t *free;
743 } lex_modes;
744
745 /*
746 * The chunks that open node lists are built in. They are carved from the
747 * scratch arena, so they are never freed individually.
748 */
749 pm_node_chunks_t node_chunks;
750
751 /* The pointer to the start of the source. */
752 const uint8_t *start;
753
754 /* The pointer to the end of the source. */
755 const uint8_t *end;
756
757 /* The previous token we were considering. */
758 pm_token_t previous;
759
760 /* The current token we're considering. */
761 pm_token_t current;
762
763 /*
764 * This is a special field set on the parser when we need the parser to jump
765 * to a specific location when lexing the next token, as opposed to just
766 * using the end of the previous token. Normally this is NULL.
767 */
768 const uint8_t *next_start;
769
770 /*
771 * This field indicates the end of a heredoc whose identifier was found on
772 * the current line. If another heredoc is found on the same line, then this
773 * will be moved forward to the end of that heredoc. If no heredocs are
774 * found on a line then this is NULL.
775 */
776 const uint8_t *heredoc_end;
777
778 /* The list of comments that have been found while parsing. */
779 pm_list_t comment_list;
780
781 /*
782 * The attachments of comments to the nodes that own them, populated after
783 * parsing when the attach_comments option is set.
784 */
785 pm_comment_attachments_t comment_attachments;
786
787 /* The list of magic comments that have been found while parsing. */
788 pm_list_t magic_comment_list;
789
790 /*
791 * An optional location that represents the location of the __END__ marker
792 * and the rest of the content of the file. This content is loaded into the
793 * DATA constant when the file being parsed is the main file being executed.
794 */
795 pm_location_t data_loc;
796
797 /* The list of warnings that have been found while parsing. */
798 pm_list_t warning_list;
799
800 /* The list of errors that have been found while parsing. */
801 pm_list_t error_list;
802
803 /* The current local scope. */
804 pm_scope_t *current_scope;
805
806 /*
807 * The locals of every live scope, in the order they were declared. Index 0
808 * is never used, so that 0 can mean the absence of a local.
809 */
810 pm_local_t *locals;
811
812 /* The number of slots in use in locals, including the unused slot 0. */
813 uint32_t locals_size;
814
815 /* The number of slots allocated in locals. */
816 uint32_t locals_capacity;
817
818 /*
819 * The index in the locals stack of the innermost live local with each
820 * constant id as its name, or 0 if there is none. A local that is declared
821 * in a scope replaces the binding of its name, and the binding it replaced
822 * is restored when the scope is popped. Constant ids at or beyond the
823 * capacity have never been declared as locals, so they are unbound.
824 */
825 uint32_t *bindings;
826
827 /* The number of slots allocated in bindings. */
828 uint32_t bindings_capacity;
829
830 /*
831 * Scopes that have been popped, kept for reuse by the next push. Scopes
832 * are allocated from the scratch arena, so they are never freed
833 * individually.
834 */
835 pm_scope_t *free_scopes;
836
837 /* The current parsing context. */
838 pm_context_node_t *current_context;
839
840 /*
841 * Context nodes that have been popped, kept for reuse by the next push.
842 * Context nodes are allocated from the scratch arena, so they are never
843 * freed individually.
844 */
845 pm_context_node_t *free_contexts;
846
847 /*
848 * The encoding functions for the current file is attached to the parser as
849 * it's parsing so that it can change with a magic comment.
850 */
851 const pm_encoding_t *encoding;
852
853 /*
854 * When the encoding that is being used to parse the source is changed by
855 * prism, we provide the ability here to call out to a user-defined
856 * function.
857 */
858 pm_encoding_changed_callback_t encoding_changed_callback;
859
860 /*
861 * This pointer indicates where a comment must start if it is to be
862 * considered an encoding comment.
863 */
864 const uint8_t *encoding_comment_start;
865
866 /*
867 * When you are lexing through a file, the lexer needs all of the information
868 * that the parser additionally provides (for example, the local table). So if
869 * you want to properly lex Ruby, you need to actually lex it in the context of
870 * the parser. In order to provide this functionality, we optionally allow a
871 * struct to be attached to the parser that calls back out to a user-provided
872 * callback when each token is lexed.
873 */
874 struct {
875 /*
876 * This is the callback that is called when a token is lexed. It is
877 * passed the opaque data pointer, the parser, and the token that was
878 * lexed.
879 */
880 pm_lex_callback_t callback;
881
882 /*
883 * This opaque pointer is used to provide whatever information the user
884 * deemed necessary to the callback. In our case we use it to pass the
885 * array that the tokens get appended into.
886 */
887 void *data;
888 } lex_callback;
889
890 /*
891 * This is the path of the file being parsed. We use the filepath when
892 * constructing SourceFileNodes.
893 */
894 pm_string_t filepath;
895
896 /*
897 * This constant pool keeps all of the constants defined throughout the file
898 * so that we can reference them later.
899 */
900 pm_constant_pool_t constant_pool;
901
902 /* This is the list of line offsets in the source file. */
903 pm_line_offset_list_t line_offsets;
904
905 /*
906 * State communicated from the lexer to the parser for integer tokens.
907 */
908 struct {
909 /*
910 * A flag indicating the base of the integer (binary, octal, decimal,
911 * hexadecimal). Set during lexing and read during node creation.
912 */
913 pm_node_flags_t base;
914
915 /*
916 * When lexing a decimal integer of at most 19 digits, which always
917 * fits in a uint64_t, or a plain 0, we compute the value during
918 * lexing to avoid re-scanning the digits during parsing. If start is
919 * set, this holds the result for the token that begins there and
920 * pm_integer_parse can be skipped.
921 */
922 uint64_t value;
923
924 /*
925 * The start of the token whose value is held in value, or NULL before
926 * any has been. The parser creates the node for a token only after
927 * lexing the next one, which may be another integer that replaces the
928 * value, so node creation checks that it belongs to the token at hand.
929 */
930 const uint8_t *start;
931 } integer;
932
933 /*
934 * This string is used to pass information from the lexer to the parser. It
935 * is particularly necessary because of escape sequences.
936 */
937 pm_string_t current_string;
938
939 /* Reusable workspace for rewriting escaped token content. */
940 pm_buffer_t lex_buffer;
941
942 /*
943 * Where current_string stops lining up with the source, when it is the
944 * content of a regular expression.
945 */
946 pm_regexp_offsets_t current_regexp_offsets;
947
948 /* Named captures from the regexp immediately before a pending =~. */
949 const struct pm_regexp_match *regexp_match;
950
951 /*
952 * The line number at the start of the parse. This will be used to offset
953 * the line numbers of all of the locations.
954 */
955 int32_t start_line;
956
957 /*
958 * When a string-like expression is being lexed, any byte or escape sequence
959 * that resolves to a value whose top bit is set (i.e., >= 0x80) will
960 * explicitly set the encoding to the same encoding as the source.
961 * Alternatively, if a unicode escape sequence is used (e.g., \\u{80}) that
962 * resolves to a value whose top bit is set, then the encoding will be
963 * explicitly set to UTF-8.
964 *
965 * The _next_ time this happens, if the encoding that is about to become the
966 * explicitly set encoding does not match the previously set explicit
967 * encoding, a mixed encoding error will be emitted.
968 *
969 * When the expression is finished being lexed, the explicit encoding
970 * controls the encoding of the expression. For the most part this means
971 * that the expression will either be encoded in the source encoding or
972 * UTF-8. This holds for all encodings except US-ASCII. If the source is
973 * US-ASCII and an explicit encoding was set that was _not_ UTF-8, then the
974 * expression will be encoded as ASCII-8BIT.
975 *
976 * Note that if the expression is a list, different elements within the same
977 * list can have different encodings, so this will get reset between each
978 * element. Furthermore all of this only applies to lists that support
979 * interpolation, because otherwise escapes that could change the encoding
980 * are ignored.
981 *
982 * At first glance, it may make more sense for this to live on the lexer
983 * mode, but we need it here to communicate back to the parser for character
984 * literals that do not push a new lexer mode.
985 */
986 const pm_encoding_t *explicit_encoding;
987
988 /*
989 * When parsing block exits (e.g., break, next, redo), we need to validate
990 * that they are in correct contexts. For the most part we can do this by
991 * looking at our parent contexts. However, modifier while and until
992 * expressions can change that context to make block exits valid. In these
993 * cases, we need to keep track of the block exits and then validate them
994 * after the expression has been parsed.
995 *
996 * We use a pointer here because we don't want to keep a whole list attached
997 * since this will only be used in the context of begin/end expressions.
998 */
999 pm_scratch_node_list_t *current_block_exits;
1000
1001 /*
1002 * The address of the C stack when pm_parse was entered. The stack budget
1003 * is measured as the distance from this address.
1004 */
1005 uintptr_t stack_base;
1006
1007 /*
1008 * The number of bytes of C stack below stack_base that the recursive
1009 * descent may use before it reports that the stack is exhausted. SIZE_MAX
1010 * when no budget was given, so the check never fires.
1011 */
1012 size_t stack_budget;
1013
1014 /* The version of prism that we should use to parse. */
1015 pm_options_version_t version;
1016
1017 /* The command line flags given from the options. */
1018 uint8_t command_line;
1019
1020 /*
1021 * Whether or not we have found a frozen_string_literal magic comment with
1022 * a true or false value.
1023 * May be:
1024 * - PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED
1025 * - PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED
1026 * - PM_OPTIONS_FROZEN_STRING_LITERAL_UNSET
1027 */
1028 int8_t frozen_string_literal;
1029
1030 /*
1031 * Whether or not we are parsing an eval string. This impacts whether or not
1032 * we should evaluate if block exits/yields are valid.
1033 */
1034 bool parsing_eval;
1035
1036 /*
1037 * Whether or not we are parsing a "partial" script, which is a script that
1038 * will be evaluated in the context of another script, so we should not
1039 * check jumps (next/break/etc.) for validity.
1040 */
1041 bool partial_script;
1042
1043 /*
1044 * Whether or not comments should be attached to their owning nodes once
1045 * parsing has finished.
1046 */
1047 bool attach_comments;
1048
1049 /* Whether or not we're at the beginning of a command. */
1050 bool command_start;
1051
1052 /*
1053 * Whether or not we're currently parsing the body of an endless method
1054 * definition. In this context, PM_TOKEN_KEYWORD_DO_BLOCK should not be
1055 * consumed by commands (it should bubble up to the outer context).
1056 */
1057 bool in_endless_def_body;
1058
1059 /* Whether or not we're currently recovering from a syntax error. */
1060 bool recovering;
1061
1062 /*
1063 * Whether or not the source being parsed could become valid if more input
1064 * were appended. This is set to false when the parser encounters a token
1065 * that is definitively wrong (e.g., a stray `end` or `]`) as opposed to
1066 * merely incomplete.
1067 */
1068 bool continuable;
1069
1070 /*
1071 * This is very specialized behavior for when you want to parse in a context
1072 * that does not respect encoding comments. Its main use case is translating
1073 * into the whitequark/parser AST which re-encodes source files in UTF-8
1074 * before they are parsed and ignores encoding comments.
1075 */
1076 bool encoding_locked;
1077
1078 /*
1079 * Whether or not the encoding has been changed by a magic comment. We use
1080 * this to provide a fast path for the lexer instead of going through the
1081 * function pointer.
1082 */
1083 bool encoding_changed;
1084
1085 /* In bare method parameters and patterns, a newline after a label
1086 * terminates the expression. Braced hash patterns clear this flag. */
1087 bool in_keyword_arg;
1088
1089 /*
1090 * Whether or not the parser has seen a token that has semantic meaning
1091 * (i.e., a token that is not a comment or whitespace).
1092 */
1093 bool semantic_token_seen;
1094
1095 /*
1096 * By default, Ruby always warns about mismatched indentation. This can be
1097 * toggled with a magic comment.
1098 */
1099 bool warn_mismatched_indentation;
1100
1101 /*
1102 * Whether the stack exhausted error has been reported. Once the budget is
1103 * used up, every recursion that unwinds past it would report the error
1104 * again, so it is only reported the first time.
1105 */
1106 bool stack_exhausted_reported;
1107
1108#if defined(PRISM_HAS_NEON) || defined(PRISM_HAS_SSE2) || defined(PRISM_HAS_SWAR)
1109 /* Breakpoint lookup tables shared by the ASCII and encoding-aware scanners. */
1110 struct {
1111 /* The NUL-padded charset used to build the lookup tables. */
1112 uint8_t charset[PM_STRPBRK_CACHE_SIZE];
1113
1114 /* Nibble-based low lookup table for SIMD matching. */
1115 uint8_t low_lut[16];
1116
1117 /* Nibble-based high lookup table for SIMD matching. */
1118 uint8_t high_lut[16];
1119
1120 /* One bit per byte for scalar breakpoint checks. */
1121 uint64_t table[4];
1122 } strpbrk_cache;
1123#endif
1124};
1125
1126/*
1127 * Initialize a parser with the given start and end pointers.
1128 */
1129void pm_parser_init(pm_arena_t *arena, pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options);
1130
1131/*
1132 * Free the memory held by the given parser.
1133 *
1134 * This does not free the `pm_options_t` object that was used to initialize the
1135 * parser.
1136 */
1137void pm_parser_cleanup(pm_parser_t *parser);
1138
1139/*
1140 * Returns true if the bytes between start and end spell a keyword, using the
1141 * same lookup that the lexer does.
1142 */
1143bool pm_keyword_p(const uint8_t *start, const uint8_t *end);
1144
1145/*
1146 * Returns true if parsing has used more C stack since pm_parse was entered than
1147 * the stack budget allows. Every recursion that runs inside pm_parse checks
1148 * this alongside its own depth limit, so that the budget bounds all of them.
1149 * The distance is taken without assuming which direction the stack grows. With
1150 * no budget the limit is SIZE_MAX, so this never fires.
1151 */
1152static PRISM_INLINE bool
1153pm_parser_stack_exhausted(const pm_parser_t *parser) {
1154 uintptr_t current = pm_stack_address();
1155 uintptr_t used = current < parser->stack_base ? parser->stack_base - current : current - parser->stack_base;
1156 return used > parser->stack_budget;
1157}
1158
1159/*
1160 * Reports that the stack budget has been used up, at the given location. This
1161 * is a separate error from the nesting depth limit, with its own error level,
1162 * because the source may be valid and parse with more stack. It is reported
1163 * once per parse.
1164 */
1165void pm_parser_err_stack_exhausted(pm_parser_t *parser, uint32_t start, uint32_t length);
1166
1167#endif
uint32_t pm_constant_id_t
A constant id is a unique identifier for a constant in the constant pool.
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition inline.h:12
A list of byte offsets of newlines in a string.
The parser used to parse Ruby source.
void(* pm_lex_callback_t)(pm_parser_t *parser, pm_token_t *token, void *data)
This is the callback that is called when a token is lexed.
Definition parser.h:59
void(* pm_encoding_changed_callback_t)(pm_parser_t *parser)
When the encoding that is being used to parse the source is changed by prism, we provide the ability ...
Definition parser.h:53
static PRISM_INLINE uintptr_t pm_stack_address(void)
Returns the current position of the C stack, for measuring how much of it has been consumed since an ...
Definition stack.h:34
C99 shim for <stdbool.h>.
A list of offsets of the start of lines in a string.
This struct represents a slice in the source code, defined by an offset and a length.
Definition ast.h:579
A view of a sequence of bytes.
Definition stringy.h:23
This struct represents a token in the Ruby source.
Definition ast.h:551