Ruby 4.1.0dev (2026-12-03 revision da234ef6cade3309d163e0cbeaaf616f3367b377)
regexp.h
1#ifndef PRISM_INTERNAL_REGEXP_H
2#define PRISM_INTERNAL_REGEXP_H
3
4#include "prism/ast.h"
6#include "prism/internal/parser.h"
7
8/* The name in the preprocessed pattern and its range in the original source. */
9typedef struct {
10 const uint8_t *name;
11 size_t length;
12 pm_location_t location;
14
15/* Captures saved until the parser has read the right operand of =~. */
16typedef struct pm_regexp_match {
17 const pm_node_t *receiver;
18 pm_regexp_capture_t *captures;
19 size_t size;
20} pm_regexp_match_t;
21
22/*
23 * Append a place to a list of places. When the list is full, it moves to a
24 * larger array in the scratch arena, which releases the old one when parsing
25 * finishes.
26 */
27void pm_regexp_offsets_append(pm_parser_t *parser, pm_regexp_offsets_t *offsets, pm_regexp_offset_t offset);
28
29/*
30 * Record that the given offset in the content is at the given offset in the
31 * source, if the distance between them changed since the last place. The base
32 * is the offset in the source of the start of the content, which gives the
33 * distance before the first place.
34 *
35 * Text that is dropped, like a line continuation, leaves two places at the
36 * same content offset. Only the second one is kept, so the places stay in
37 * order with no two at the same offset.
38 */
39static PRISM_INLINE void
40pm_regexp_offsets_record(pm_parser_t *parser, pm_regexp_offsets_t *offsets, uint32_t content, uint32_t source, uint32_t base) {
41 pm_regexp_offset_t *last = offsets->size == 0 ? NULL : &offsets->values[offsets->size - 1];
42
43 /* Each distance is a source offset minus a content offset. Both fit in 32
44 * bits, so two distances differ by less than 2^32, and their subtractions,
45 * which may wrap, are equal exactly when the distances are. */
46 uint32_t previous = (last == NULL) ? base : (last->source - last->content);
47 if (source - content == previous) return;
48
49 if (last != NULL && last->content == content) {
50 last->source = source;
51 } else {
52 pm_regexp_offsets_append(parser, offsets, (pm_regexp_offset_t) { .content = content, .source = source });
53 }
54}
55
56/*
57 * Parse a regular expression and validate its encoding. The offsets are where
58 * the unescaped content of the node stops lining up with the source, as the
59 * lexer recorded them. Returns the encoding flags to set on the node.
60 */
61PRISM_EXPORTED_FUNCTION pm_node_flags_t pm_regexp_parse(pm_parser_t *parser, pm_regular_expression_node_t *node, const pm_regexp_offsets_t *offsets);
62
63/*
64 * Validate a regular expression with interpolation. If its value is known,
65 * validate the joined pattern and clear the static literal flag when each
66 * evaluation needs a fresh regexp.
67 */
68void pm_regexp_parse_interpolated(pm_parser_t *parser, pm_interpolated_regular_expression_node_t *node);
69
70#endif
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition exported.h:20
#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
This struct represents a slice in the source code, defined by an offset and a length.
Definition ast.h:579