Skip to content

Commit c19b0fa

Browse files
committed
Pass prism parser into regexp parser
1 parent 36c6851 commit c19b0fa

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

fuzz/regexp.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ regexp_name_callback(const pm_string_t *name, void *data) {
77

88
void
99
harness(const uint8_t *input, size_t size) {
10-
pm_regexp_parse(input, size, false, PM_ENCODING_UTF_8_ENTRY, regexp_name_callback, NULL);
10+
pm_parser_t parser;
11+
pm_parser_init(&parser, input, size, NULL);
12+
13+
pm_regexp_parse(&parser, input, size, regexp_name_callback, NULL);
14+
15+
pm_parser_free(&parser);
1116
}

include/prism/regexp.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ typedef void (*pm_regexp_name_callback_t)(const pm_string_t *name, void *data);
2424
/**
2525
* Parse a regular expression.
2626
*
27+
* @param parser The parser that is currently being used.
2728
* @param source The source code to parse.
2829
* @param size The size of the source code.
29-
* @param encoding_changed Whether or not the encoding changed from the default.
30-
* @param encoding The encoding of the source code.
3130
* @param name_callback The callback to call when a named capture group is found.
3231
* @param name_data The data to pass to the name callback.
3332
* @return Whether or not the parsing was successful.
3433
*/
35-
PRISM_EXPORTED_FUNCTION bool pm_regexp_parse(const uint8_t *source, size_t size, bool encoding_changed, const pm_encoding_t *encoding, pm_regexp_name_callback_t name_callback, void *name_data);
34+
PRISM_EXPORTED_FUNCTION bool pm_regexp_parse(pm_parser_t *parser, const uint8_t *source, size_t size, pm_regexp_name_callback_t name_callback, void *name_data);
3635

3736
#endif

src/prism.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20108,7 +20108,7 @@ parse_regular_expression_named_captures(pm_parser_t *parser, const pm_string_t *
2010820108
.names = { 0 }
2010920109
};
2011020110

20111-
pm_regexp_parse(pm_string_source(content), pm_string_length(content), parser->encoding_changed, parser->encoding, parse_regular_expression_named_capture, &callback_data);
20111+
pm_regexp_parse(parser, pm_string_source(content), pm_string_length(content), parse_regular_expression_named_capture, &callback_data);
2011220112
pm_constant_id_list_free(&callback_data.names);
2011320113

2011420114
if (callback_data.match != NULL) {

src/regexp.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* This is the parser that is going to handle parsing regular expressions.
55
*/
66
typedef struct {
7+
/** The parser that is currently being used. */
8+
pm_parser_t *parser;
9+
710
/** A pointer to the start of the source that we are parsing. */
811
const uint8_t *start;
912

@@ -26,22 +29,6 @@ typedef struct {
2629
void *name_data;
2730
} pm_regexp_parser_t;
2831

29-
/**
30-
* This initializes a new parser with the given source.
31-
*/
32-
static void
33-
pm_regexp_parser_init(pm_regexp_parser_t *parser, const uint8_t *start, const uint8_t *end, bool encoding_changed, const pm_encoding_t *encoding, pm_regexp_name_callback_t name_callback, void *name_data) {
34-
*parser = (pm_regexp_parser_t) {
35-
.start = start,
36-
.cursor = start,
37-
.end = end,
38-
.encoding_changed = encoding_changed,
39-
.encoding = encoding,
40-
.name_callback = name_callback,
41-
.name_data = name_data
42-
};
43-
}
44-
4532
/**
4633
* This appends a new string to the list of named captures.
4734
*/
@@ -650,8 +637,17 @@ pm_regexp_parse_pattern(pm_regexp_parser_t *parser) {
650637
* groups.
651638
*/
652639
PRISM_EXPORTED_FUNCTION bool
653-
pm_regexp_parse(const uint8_t *source, size_t size, bool encoding_changed, const pm_encoding_t *encoding, pm_regexp_name_callback_t name_callback, void *name_data) {
654-
pm_regexp_parser_t parser;
655-
pm_regexp_parser_init(&parser, source, source + size, encoding_changed, encoding, name_callback, name_data);
656-
return pm_regexp_parse_pattern(&parser);
640+
pm_regexp_parse(pm_parser_t *parser, const uint8_t *source, size_t size, pm_regexp_name_callback_t name_callback, void *name_data) {
641+
pm_regexp_parser_t regexp_parser = {
642+
.parser = parser,
643+
.start = source,
644+
.cursor = source,
645+
.end = source + size,
646+
.encoding_changed = parser->encoding_changed,
647+
.encoding = parser->encoding,
648+
.name_callback = name_callback,
649+
.name_data = name_data
650+
};
651+
652+
return pm_regexp_parse_pattern(&regexp_parser);
657653
}

0 commit comments

Comments
 (0)