Ruby 4.1.0dev (2026-11-30 revision 83557d59f9eab9c7fa6004de6c51482b967e68e2)
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
496typedef struct {
497 pm_context_t context;
498
499 /* Popping the outermost occurrence clears this context's membership bit. */
500 bool outermost;
502
503/* The type of shareable constant value that can be set. */
504typedef uint8_t pm_shareable_constant_value_t;
505static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_NONE = 0x0;
506static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_LITERAL = PM_SHAREABLE_CONSTANT_NODE_FLAGS_LITERAL;
507static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_EVERYTHING = PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_EVERYTHING;
508static const pm_shareable_constant_value_t PM_SCOPE_SHAREABLE_CONSTANT_EXPERIMENTAL_COPY = PM_SHAREABLE_CONSTANT_NODE_FLAGS_EXPERIMENTAL_COPY;
509
510/*
511 * This tracks an individual local variable in a certain lexical context, as
512 * well as the number of times is it read. The locals of every live scope are
513 * kept on a single stack on the parser, since a local is only ever declared in
514 * the innermost scope and is discarded when that scope is popped.
515 */
516typedef struct {
517 /* The name of the local variable. */
518 pm_constant_id_t name;
519
520 /* The location of the local variable in the source. */
521 pm_location_t location;
522
523 /* The number of times the local variable is read. */
524 uint32_t reads;
525
526 /* The level of the scope that declares the local variable. */
527 uint32_t level;
528
529 /*
530 * The index in the locals stack of the local with the same name that this
531 * local hides while its scope is live, or 0 if it hides nothing. It is
532 * restored as the binding of the name when the scope is popped.
533 */
534 uint32_t shadowed;
535} pm_local_t;
536
537/* The flags about scope parameters that can be set. */
538typedef uint8_t pm_scope_parameters_t;
539static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NONE = 0x0;
540static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_POSITIONALS = 0x1;
541static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_KEYWORDS = 0x2;
542static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_BLOCK = 0x4;
543static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_FORWARDING_ALL = 0x8;
544static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_IMPLICIT_DISALLOWED = 0x10;
545static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NUMBERED_INNER = 0x20;
546static const pm_scope_parameters_t PM_SCOPE_PARAMETERS_NUMBERED_FOUND = 0x40;
547
548/*
549 * This struct represents a node in a linked list of scopes. Some scopes can see
550 * into their parent scopes, while others cannot.
551 */
552typedef struct pm_scope {
553 /* A pointer to the previous scope in the linked list. */
554 struct pm_scope *previous;
555
556 /*
557 * The index in the locals stack of the first local of this scope. The
558 * locals of this scope run from here to the top of the stack.
559 */
560 uint32_t locals_start;
561
562 /*
563 * The position of this scope in the scope stack, where the outermost scope
564 * is at level 1.
565 */
566 uint32_t level;
567
568 /*
569 * The locals_start of the nearest closed scope at or above this one. Every
570 * local below this index in the locals stack belongs to a scope that this
571 * scope cannot see into.
572 */
573 uint32_t floor;
574
575 /*
576 * This is a list of the implicit parameters contained within the block.
577 * These will be processed after the block is parsed to determine the kind
578 * of parameters node that should be used and to check if any errors need to
579 * be added.
580 */
581 pm_scratch_node_list_t implicit_parameters;
582
583 /*
584 * This is a bitfield that indicates the parameters that are being used in
585 * this scope. It is a combination of the PM_SCOPE_PARAMETERS_* constants.
586 * There are three different kinds of parameters that can be used in a
587 * scope:
588 *
589 * - Ordinary parameters (e.g., def foo(bar); end)
590 * - Numbered parameters (e.g., def foo; _1; end)
591 * - The it parameter (e.g., def foo; it; end)
592 *
593 * If ordinary parameters are being used, then certain parameters can be
594 * forwarded to another method/structure. Those are indicated by four
595 * additional bits in the params field. For example, some combinations of:
596 *
597 * - def foo(*); end
598 * - def foo(**); end
599 * - def foo(&); end
600 * - def foo(...); end
601 */
602 pm_scope_parameters_t parameters;
603
604 /*
605 * The current state of constant shareability for this scope. This is
606 * changed by magic shareable_constant_value comments.
607 */
608 pm_shareable_constant_value_t shareable_constant;
609
610 /*
611 * A boolean indicating whether or not this scope can see into its parent.
612 * If closed is true, then the scope cannot see into its parent.
613 */
614 bool closed;
615} pm_scope_t;
616
617/*
618 * A stack of boolean values with no fixed depth limit. The newest frames live
619 * in an inline word, so pushing and popping are usually a shift and a compare.
620 * When that word fills, it moves to the spill array and the inline word starts
621 * over empty. The spill array is only allocated once the nesting is more than
622 * 64 frames deep.
623 */
624typedef struct {
625 /*
626 * The newest frames, with the top of the stack in bit 0. It holds between
627 * 1 and 64 frames while the stack is not empty, and is 0 when it is.
628 */
629 uint64_t top;
630
631 /*
632 * The number of frames on the whole stack. The inline word holds the
633 * frames above the last multiple of 64, so the spill array holds
634 * (depth - 1) / 64 words.
635 */
636 uint32_t depth;
637
638 /* The number of words the spill array has room for. */
639 uint32_t capacity;
640
641 /*
642 * Full words of 64 older frames each, oldest first, allocated from the
643 * scratch arena.
644 */
645 uint64_t *spill;
647
648/*
649 * This struct represents the overall parser. It contains a reference to the
650 * source file, as well as pointers that indicate where in the source it's
651 * currently parsing. It also contains the most recent and current token that
652 * it's considering.
653 */
655 /* The arena used for all AST-lifetime allocations. Caller-owned. */
656 pm_arena_t *arena;
657
658 /*
659 * The arena used for parser metadata (comments, diagnostics, etc.), which
660 * lives as long as the parser.
661 */
662 pm_arena_t metadata_arena;
663
664 /*
665 * The arena used for state that is only needed while parsing, such as
666 * scopes, locals, and contexts. It is released when pm_parse returns, so
667 * nothing allocated in it can be reachable from the tree or from the
668 * parser's metadata.
669 */
670 pm_arena_t scratch_arena;
671
672 /*
673 * The next node identifier that will be assigned. This is a unique
674 * identifier used to track nodes such that the syntax tree can be dropped
675 * but the node can be found through another parse.
676 */
677 uint32_t node_id;
678
679 /*
680 * A single-entry cache for pm_parser_constant_id_raw. Avoids redundant
681 * constant pool lookups when the same token is resolved multiple times
682 * (e.g., once during lexing for local variable detection, and again
683 * during parsing for node creation).
684 */
685 struct {
686 const uint8_t *start;
687 const uint8_t *end;
689 } constant_cache;
690
691 /* The current state of the lexer. */
692 pm_lex_state_t lex_state;
693
694 /* Tracks the current nesting of (), [], and {}. */
695 int enclosure_nesting;
696
697 /*
698 * Used to temporarily track the nesting of enclosures to determine if a {
699 * is the beginning of a lambda following the parameters of a lambda.
700 */
701 int lambda_enclosure_nesting;
702
703 /*
704 * Used to track the nesting of braces to ensure we get the correct value
705 * when we are interpolating blocks with braces.
706 */
707 int brace_nesting;
708
709 /*
710 * The stack used to determine if a do keyword belongs to the predicate of a
711 * while, until, or for loop.
712 */
713 pm_state_stack_t do_loop_stack;
714
715 /*
716 * The stack used to determine if a do keyword belongs to the beginning of a
717 * block.
718 */
719 pm_state_stack_t accepts_block_stack;
720
721 /* A stack of lex modes, linked through their prev pointers. */
722 struct {
723 /* The current mode of the lexer. */
724 pm_lex_mode_t *current;
725
726 /*
727 * The mode at the bottom of the stack, which is never popped. Every
728 * other mode is allocated from the scratch arena.
729 */
730 pm_lex_mode_t base;
731
732 /*
733 * Lex modes that have been popped, kept for reuse by the next push.
734 * They are allocated from the scratch arena, so they are never freed
735 * individually.
736 */
737 pm_lex_mode_t *free;
738 } lex_modes;
739
740 /*
741 * The chunks that open node lists are built in. They are carved from the
742 * scratch arena, so they are never freed individually.
743 */
744 pm_node_chunks_t node_chunks;
745
746 /* The pointer to the start of the source. */
747 const uint8_t *start;
748
749 /* The end of the full input, including the data section. */
750 const uint8_t *end;
751
752 /* The end of the Ruby code, at the start of the __END__ marker if present. */
753 const uint8_t *source_end;
754
755 /* The previous token we were considering. */
756 pm_token_t previous;
757
758 /* The current token we're considering. */
759 pm_token_t current;
760
761 /*
762 * This is a special field set on the parser when we need the parser to jump
763 * to a specific location when lexing the next token, as opposed to just
764 * using the end of the previous token. Normally this is NULL.
765 */
766 const uint8_t *next_start;
767
768 /*
769 * This field indicates the end of a heredoc whose identifier was found on
770 * the current line. If another heredoc is found on the same line, then this
771 * will be moved forward to the end of that heredoc. If no heredocs are
772 * found on a line then this is NULL.
773 */
774 const uint8_t *heredoc_end;
775
776 /* The list of comments that have been found while parsing. */
777 pm_list_t comment_list;
778
779 /*
780 * The attachments of comments to the nodes that own them, populated after
781 * parsing when the attach_comments option is set.
782 */
783 pm_comment_attachments_t comment_attachments;
784
785 /* The list of magic comments that have been found while parsing. */
786 pm_list_t magic_comment_list;
787
788 /*
789 * An optional location that represents the location of the __END__ marker
790 * and the rest of the content of the file. This content is loaded into the
791 * DATA constant when the file being parsed is the main file being executed.
792 */
793 pm_location_t data_loc;
794
795 /* The list of warnings that have been found while parsing. */
796 pm_list_t warning_list;
797
798 /* The list of errors that have been found while parsing. */
799 pm_list_t error_list;
800
801 /* The current local scope. */
802 pm_scope_t *current_scope;
803
804 /*
805 * The locals of every live scope, in the order they were declared. Index 0
806 * is never used, so that 0 can mean the absence of a local.
807 */
808 pm_local_t *locals;
809
810 /* The number of slots in use in locals, including the unused slot 0. */
811 uint32_t locals_size;
812
813 /* The number of slots allocated in locals. */
814 uint32_t locals_capacity;
815
816 /*
817 * The index in the locals stack of the innermost live local with each
818 * constant id as its name, or 0 if there is none. A local that is declared
819 * in a scope replaces the binding of its name, and the binding it replaced
820 * is restored when the scope is popped. Constant ids at or beyond the
821 * capacity have never been declared as locals, so they are unbound.
822 */
823 uint32_t *bindings;
824
825 /* The number of slots allocated in bindings. */
826 uint32_t bindings_capacity;
827
828 /*
829 * Scopes that have been popped, kept for reuse by the next push. Scopes
830 * are allocated from the scratch arena, so they are never freed
831 * individually.
832 */
833 pm_scope_t *free_scopes;
834
835 /* Slot 0 holds PM_CONTEXT_NONE. The depth indexes the current frame. */
836 pm_context_frame_t *contexts;
837 uint32_t context_depth;
838 uint32_t context_capacity;
839
840 /* One bit per context present anywhere on the stack. */
841 uint64_t context_mask;
842
843 /*
844 * The encoding functions for the current file is attached to the parser as
845 * it's parsing so that it can change with a magic comment.
846 */
847 const pm_encoding_t *encoding;
848
849 /*
850 * When the encoding that is being used to parse the source is changed by
851 * prism, we provide the ability here to call out to a user-defined
852 * function.
853 */
854 pm_encoding_changed_callback_t encoding_changed_callback;
855
856 /*
857 * Start of the source line that can contain an encoding comment, or NULL
858 * if there is no eligible line.
859 */
860 const uint8_t *encoding_comment_start;
861
862 /*
863 * When you are lexing through a file, the lexer needs all of the information
864 * that the parser additionally provides (for example, the local table). So if
865 * you want to properly lex Ruby, you need to actually lex it in the context of
866 * the parser. In order to provide this functionality, we optionally allow a
867 * struct to be attached to the parser that calls back out to a user-provided
868 * callback when each token is lexed.
869 */
870 struct {
871 /*
872 * This is the callback that is called when a token is lexed. It is
873 * passed the opaque data pointer, the parser, and the token that was
874 * lexed.
875 */
876 pm_lex_callback_t callback;
877
878 /*
879 * This opaque pointer is used to provide whatever information the user
880 * deemed necessary to the callback. In our case we use it to pass the
881 * array that the tokens get appended into.
882 */
883 void *data;
884 } lex_callback;
885
886 /*
887 * This is the path of the file being parsed. We use the filepath when
888 * constructing SourceFileNodes.
889 */
890 pm_string_t filepath;
891
892 /*
893 * This constant pool keeps all of the constants defined throughout the file
894 * so that we can reference them later.
895 */
896 pm_constant_pool_t constant_pool;
897
898 /* This is the list of line offsets in the source file. */
899 pm_line_offset_list_t line_offsets;
900
901 /*
902 * State communicated from the lexer to the parser for integer tokens.
903 */
904 struct {
905 /*
906 * A flag indicating the base of the integer (binary, octal, decimal,
907 * hexadecimal). Set during lexing and read during node creation.
908 */
909 pm_node_flags_t base;
910
911 /*
912 * When lexing a decimal integer of at most 19 digits, which always
913 * fits in a uint64_t, or a plain 0, we compute the value during
914 * lexing to avoid re-scanning the digits during parsing. If start is
915 * set, this holds the result for the token that begins there and
916 * pm_integer_parse can be skipped.
917 */
918 uint64_t value;
919
920 /*
921 * The start of the token whose value is held in value, or NULL before
922 * any has been. The parser creates the node for a token only after
923 * lexing the next one, which may be another integer that replaces the
924 * value, so node creation checks that it belongs to the token at hand.
925 */
926 const uint8_t *start;
927 } integer;
928
929 /*
930 * This string is used to pass information from the lexer to the parser. It
931 * is particularly necessary because of escape sequences.
932 */
933 pm_string_t current_string;
934
935 /* Reusable workspace for rewriting escaped token content. */
936 pm_buffer_t lex_buffer;
937
938 /*
939 * Where current_string stops lining up with the source, when it is the
940 * content of a regular expression.
941 */
942 pm_regexp_offsets_t current_regexp_offsets;
943
944 /* Named captures from the regexp immediately before a pending =~. */
945 const struct pm_regexp_match *regexp_match;
946
947 /*
948 * The line number at the start of the parse. This will be used to offset
949 * the line numbers of all of the locations.
950 */
951 int32_t start_line;
952
953 /*
954 * When a string-like expression is being lexed, any byte or escape sequence
955 * that resolves to a value whose top bit is set (i.e., >= 0x80) will
956 * explicitly set the encoding to the same encoding as the source.
957 * Alternatively, if a unicode escape sequence is used (e.g., \\u{80}) that
958 * resolves to a value whose top bit is set, then the encoding will be
959 * explicitly set to UTF-8.
960 *
961 * The _next_ time this happens, if the encoding that is about to become the
962 * explicitly set encoding does not match the previously set explicit
963 * encoding, a mixed encoding error will be emitted.
964 *
965 * When the expression is finished being lexed, the explicit encoding
966 * controls the encoding of the expression. For the most part this means
967 * that the expression will either be encoded in the source encoding or
968 * UTF-8. This holds for all encodings except US-ASCII. If the source is
969 * US-ASCII and an explicit encoding was set that was _not_ UTF-8, then the
970 * expression will be encoded as ASCII-8BIT.
971 *
972 * Note that if the expression is a list, different elements within the same
973 * list can have different encodings, so this will get reset between each
974 * element. Furthermore all of this only applies to lists that support
975 * interpolation, because otherwise escapes that could change the encoding
976 * are ignored.
977 *
978 * At first glance, it may make more sense for this to live on the lexer
979 * mode, but we need it here to communicate back to the parser for character
980 * literals that do not push a new lexer mode.
981 */
982 const pm_encoding_t *explicit_encoding;
983
984 /*
985 * When parsing block exits (e.g., break, next, redo), we need to validate
986 * that they are in correct contexts. For the most part we can do this by
987 * looking at our parent contexts. However, modifier while and until
988 * expressions can change that context to make block exits valid. In these
989 * cases, we need to keep track of the block exits and then validate them
990 * after the expression has been parsed.
991 *
992 * We use a pointer here because we don't want to keep a whole list attached
993 * since this will only be used in the context of begin/end expressions.
994 */
995 pm_scratch_node_list_t *current_block_exits;
996
997 /*
998 * The address of the C stack when pm_parse was entered. The stack budget
999 * is measured as the distance from this address.
1000 */
1001 uintptr_t stack_base;
1002
1003 /*
1004 * The number of bytes of C stack below stack_base that the recursive
1005 * descent may use before it reports that the stack is exhausted. SIZE_MAX
1006 * when no budget was given, so the check never fires.
1007 */
1008 size_t stack_budget;
1009
1010 /* The version of prism that we should use to parse. */
1011 pm_options_version_t version;
1012
1013 /* The command line flags given from the options. */
1014 uint8_t command_line;
1015
1016 /*
1017 * Whether or not we have found a frozen_string_literal magic comment with
1018 * a true or false value.
1019 * May be:
1020 * - PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED
1021 * - PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED
1022 * - PM_OPTIONS_FROZEN_STRING_LITERAL_UNSET
1023 */
1024 int8_t frozen_string_literal;
1025
1026 /*
1027 * Whether or not we are parsing an eval string. This impacts whether or not
1028 * we should evaluate if block exits/yields are valid.
1029 */
1030 bool parsing_eval;
1031
1032 /*
1033 * Whether or not we are parsing a "partial" script, which is a script that
1034 * will be evaluated in the context of another script, so we should not
1035 * check jumps (next/break/etc.) for validity.
1036 */
1037 bool partial_script;
1038
1039 /*
1040 * Whether or not comments should be attached to their owning nodes once
1041 * parsing has finished.
1042 */
1043 bool attach_comments;
1044
1045 /* Whether or not we're at the beginning of a command. */
1046 bool command_start;
1047
1048 /*
1049 * Whether or not we're currently parsing the body of an endless method
1050 * definition. In this context, PM_TOKEN_KEYWORD_DO_BLOCK should not be
1051 * consumed by commands (it should bubble up to the outer context).
1052 */
1053 bool in_endless_def_body;
1054
1055 /* Whether or not we're currently recovering from a syntax error. */
1056 bool recovering;
1057
1058 /*
1059 * Whether or not the source being parsed could become valid if more input
1060 * were appended. This is set to false when the parser encounters a token
1061 * that is definitively wrong (e.g., a stray `end` or `]`) as opposed to
1062 * merely incomplete.
1063 */
1064 bool continuable;
1065
1066 /*
1067 * This is very specialized behavior for when you want to parse in a context
1068 * that does not respect encoding comments. Its main use case is translating
1069 * into the whitequark/parser AST which re-encodes source files in UTF-8
1070 * before they are parsed and ignores encoding comments.
1071 */
1072 bool encoding_locked;
1073
1074 /*
1075 * Whether or not the encoding has been changed by a magic comment. We use
1076 * this to provide a fast path for the lexer instead of going through the
1077 * function pointer.
1078 */
1079 bool encoding_changed;
1080
1081 /* In bare method parameters and patterns, a newline after a label
1082 * terminates the expression. Braced hash patterns clear this flag. */
1083 bool in_keyword_arg;
1084
1085 /*
1086 * Whether or not the parser has seen a token that has semantic meaning
1087 * (i.e., a token that is not a comment or whitespace).
1088 */
1089 bool semantic_token_seen;
1090
1091 /*
1092 * By default, Ruby always warns about mismatched indentation. This can be
1093 * toggled with a magic comment.
1094 */
1095 bool warn_mismatched_indentation;
1096
1097 /*
1098 * Whether the stack exhausted error has been reported. Once the budget is
1099 * used up, every recursion that unwinds past it would report the error
1100 * again, so it is only reported the first time.
1101 */
1102 bool stack_exhausted_reported;
1103
1104#if defined(PRISM_HAS_NEON) || defined(PRISM_HAS_SSE2) || defined(PRISM_HAS_SWAR)
1105 /* Breakpoint lookup tables shared by the ASCII and encoding-aware scanners. */
1106 struct {
1107 /* The NUL-padded charset used to build the lookup tables. */
1108 uint8_t charset[PM_STRPBRK_CACHE_SIZE];
1109
1110 /* Nibble-based low lookup table for SIMD matching. */
1111 uint8_t low_lut[16];
1112
1113 /* Nibble-based high lookup table for SIMD matching. */
1114 uint8_t high_lut[16];
1115
1116 /* One bit per byte for scalar breakpoint checks. */
1117 uint64_t table[4];
1118 } strpbrk_cache;
1119#endif
1120};
1121
1122/*
1123 * Initialize a parser with the given start and end pointers.
1124 */
1125void pm_parser_init(pm_arena_t *arena, pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options);
1126
1127/*
1128 * Free the memory held by the given parser.
1129 *
1130 * This does not free the `pm_options_t` object that was used to initialize the
1131 * parser.
1132 */
1133void pm_parser_cleanup(pm_parser_t *parser);
1134
1135/*
1136 * Returns true if the bytes between start and end spell a keyword, using the
1137 * same lookup that the lexer does.
1138 */
1139bool pm_keyword_p(const uint8_t *start, const uint8_t *end);
1140
1141/*
1142 * Returns true if parsing has used more C stack since pm_parse was entered than
1143 * the stack budget allows. Every recursion that runs inside pm_parse checks
1144 * this alongside its own depth limit, so that the budget bounds all of them.
1145 * The distance is taken without assuming which direction the stack grows. With
1146 * no budget the limit is SIZE_MAX, so this never fires.
1147 */
1148static PRISM_INLINE bool
1149pm_parser_stack_exhausted(const pm_parser_t *parser) {
1150 uintptr_t current = pm_stack_address();
1151 uintptr_t used = current < parser->stack_base ? parser->stack_base - current : current - parser->stack_base;
1152 return used > parser->stack_budget;
1153}
1154
1155/*
1156 * Reports that the stack budget has been used up, at the given location. This
1157 * is a separate error from the nesting depth limit, with its own error level,
1158 * because the source may be valid and parse with more stack. It is reported
1159 * once per parse.
1160 */
1161void pm_parser_err_stack_exhausted(pm_parser_t *parser, uint32_t start, uint32_t length);
1162
1163#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