Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 33 additions & 66 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,28 +633,19 @@ parser_comment(VALUE source, bool freeze, const pm_comment_t *comment) {
return rb_class_new_instance_freeze(1, argv, type, freeze);
}

typedef struct {
VALUE comments;
VALUE source;
bool freeze;
long index;
} parser_comments_each_data_t;

static void
parser_comments_each(const pm_comment_t *comment, void *data) {
parser_comments_each_data_t *each_data = (parser_comments_each_data_t *) data;
VALUE value = parser_comment(each_data->source, each_data->freeze, comment);
rb_ary_store(each_data->comments, each_data->index++, value);
}

/**
* Extract the comments out of the parser into the given array, which was
* created before the source so that the source could hold it.
* created before the source so that the source could hold it. The caller may
* have already resized it to the number of comments, so each comment is stored
* at its index rather than pushed.
*/
static void
parser_comments(const pm_parser_t *parser, VALUE comments, VALUE source, bool freeze) {
parser_comments_each_data_t each_data = { .comments = comments, .source = source, .freeze = freeze };
pm_parser_comments_each(parser, parser_comments_each, &each_data);
long index = 0;
for (const pm_comment_t *comment = pm_parser_comments_first(parser); comment != NULL; comment = pm_comment_next(comment)) {
rb_ary_store(comments, index++, parser_comment(source, freeze, comment));
}

if (freeze) rb_obj_freeze(comments);
}

Expand All @@ -673,28 +664,16 @@ parser_magic_comment(VALUE source, bool freeze, const pm_magic_comment_t *magic_
return rb_class_new_instance_freeze(2, argv, rb_cPrismMagicComment, freeze);
}

typedef struct {
VALUE magic_comments;
VALUE source;
bool freeze;
} parser_magic_comments_each_data_t;

static void
parser_magic_comments_each(const pm_magic_comment_t *magic_comment, void *data) {
parser_magic_comments_each_data_t *each_data = (parser_magic_comments_each_data_t *) data;
VALUE value = parser_magic_comment(each_data->source, each_data->freeze, magic_comment);
rb_ary_push(each_data->magic_comments, value);
}

/**
* Extract the magic comments out of the parser into an array.
*/
static VALUE
parser_magic_comments(const pm_parser_t *parser, VALUE source, bool freeze) {
VALUE magic_comments = rb_ary_new_capa(pm_parser_magic_comments_size(parser));

parser_magic_comments_each_data_t each_data = { magic_comments, source, freeze };
pm_parser_magic_comments_each(parser, parser_magic_comments_each, &each_data);
for (const pm_magic_comment_t *magic_comment = pm_parser_magic_comments_first(parser); magic_comment != NULL; magic_comment = pm_magic_comment_next(magic_comment)) {
rb_ary_push(magic_comments, parser_magic_comment(source, freeze, magic_comment));
}

if (freeze) rb_obj_freeze(magic_comments);
return magic_comments;
Expand All @@ -715,20 +694,14 @@ parser_data_loc(const pm_parser_t *parser, VALUE source, bool freeze) {
}
}

typedef struct {
VALUE errors;
rb_encoding *encoding;
VALUE source;
bool freeze;
} parser_errors_each_data_t;

static void
parser_errors_each(const pm_diagnostic_t *diagnostic, void *data) {
parser_errors_each_data_t *each_data = (parser_errors_each_data_t *) data;

/**
* Build a new ParseError instance from the given error.
*/
static VALUE
parser_error(const pm_diagnostic_t *diagnostic, rb_encoding *encoding, VALUE source, bool freeze) {
VALUE type = ID2SYM(rb_intern(pm_diagnostic_type(diagnostic)));
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), each_data->encoding));
VALUE location = PARSER_LOCATION(each_data->source, each_data->freeze, pm_diagnostic_location(diagnostic));
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), encoding));
VALUE location = PARSER_LOCATION(source, freeze, pm_diagnostic_location(diagnostic));

pm_error_level_t error_level = pm_diagnostic_error_level(diagnostic);
VALUE level = Qnil;
Expand All @@ -748,8 +721,7 @@ parser_errors_each(const pm_diagnostic_t *diagnostic, void *data) {
}

VALUE argv[] = { type, message, location, level };
VALUE value = rb_class_new_instance_freeze(4, argv, rb_cPrismParseError, each_data->freeze);
rb_ary_push(each_data->errors, value);
return rb_class_new_instance_freeze(4, argv, rb_cPrismParseError, freeze);
}

/**
Expand All @@ -759,27 +731,22 @@ static VALUE
parser_errors(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) {
VALUE errors = rb_ary_new_capa(pm_parser_errors_size(parser));

parser_errors_each_data_t each_data = { errors, encoding, source, freeze };
pm_parser_errors_each(parser, parser_errors_each, &each_data);
for (const pm_diagnostic_t *error = pm_parser_errors_first(parser); error != NULL; error = pm_diagnostic_next(error)) {
rb_ary_push(errors, parser_error(error, encoding, source, freeze));
}

if (freeze) rb_obj_freeze(errors);
return errors;
}

typedef struct {
VALUE warnings;
rb_encoding *encoding;
VALUE source;
bool freeze;
} parser_warnings_each_data_t;

static void
parser_warnings_each(const pm_diagnostic_t *diagnostic, void *data) {
parser_warnings_each_data_t *each_data = (parser_warnings_each_data_t *) data;

/**
* Build a new ParseWarning instance from the given warning.
*/
static VALUE
parser_warning(const pm_diagnostic_t *diagnostic, rb_encoding *encoding, VALUE source, bool freeze) {
VALUE type = ID2SYM(rb_intern(pm_diagnostic_type(diagnostic)));
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), each_data->encoding));
VALUE location = PARSER_LOCATION(each_data->source, each_data->freeze, pm_diagnostic_location(diagnostic));
VALUE message = rb_obj_freeze(rb_enc_str_new_cstr(pm_diagnostic_message(diagnostic), encoding));
VALUE location = PARSER_LOCATION(source, freeze, pm_diagnostic_location(diagnostic));

pm_warning_level_t warning_level = pm_diagnostic_warning_level(diagnostic);
VALUE level = Qnil;
Expand All @@ -796,8 +763,7 @@ parser_warnings_each(const pm_diagnostic_t *diagnostic, void *data) {
}

VALUE argv[] = { type, message, location, level };
VALUE value = rb_class_new_instance_freeze(4, argv, rb_cPrismParseWarning, each_data->freeze);
rb_ary_push(each_data->warnings, value);
return rb_class_new_instance_freeze(4, argv, rb_cPrismParseWarning, freeze);
}

/**
Expand All @@ -807,8 +773,9 @@ static VALUE
parser_warnings(const pm_parser_t *parser, rb_encoding *encoding, VALUE source, bool freeze) {
VALUE warnings = rb_ary_new_capa(pm_parser_warnings_size(parser));

parser_warnings_each_data_t each_data = { warnings, encoding, source, freeze };
pm_parser_warnings_each(parser, parser_warnings_each, &each_data);
for (const pm_diagnostic_t *warning = pm_parser_warnings_first(parser); warning != NULL; warning = pm_diagnostic_next(warning)) {
rb_ary_push(warnings, parser_warning(warning, encoding, source, freeze));
}

if (freeze) rb_obj_freeze(warnings);
return warnings;
Expand Down
9 changes: 9 additions & 0 deletions include/prism/comments.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ PRISM_EXPORTED_FUNCTION pm_location_t pm_comment_location(const pm_comment_t *co
*/
PRISM_EXPORTED_FUNCTION pm_comment_type_t pm_comment_type(const pm_comment_t *comment) PRISM_NONNULL(1);

/**
* Returns the comment that follows the given comment among the comments
* associated with the parser that found it.
*
* @param comment the comment whose successor we want to get
* @returns the next comment, or NULL if the given comment is the last one
*/
PRISM_EXPORTED_FUNCTION const pm_comment_t * pm_comment_next(const pm_comment_t *comment) PRISM_NONNULL(1);

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions include/prism/diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ PRISM_EXPORTED_FUNCTION pm_error_level_t pm_diagnostic_error_level(const pm_diag
*/
PRISM_EXPORTED_FUNCTION pm_warning_level_t pm_diagnostic_warning_level(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);

/**
* Get the diagnostic that follows the given diagnostic among the errors or the
* warnings of the parser that produced it.
*
* @param diagnostic The diagnostic to get the successor of.
* @returns The next diagnostic of the same kind, or NULL if the given
* diagnostic is the last one.
*/
PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_diagnostic_next(const pm_diagnostic_t *diagnostic) PRISM_NONNULL(1);

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions include/prism/magic_comments.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ PRISM_EXPORTED_FUNCTION pm_location_t pm_magic_comment_key(const pm_magic_commen
*/
PRISM_EXPORTED_FUNCTION pm_location_t pm_magic_comment_value(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1);

/**
* Returns the magic comment that follows the given magic comment among the
* magic comments associated with the parser that found it.
*
* @param magic_comment the magic comment whose successor we want to get
* @returns the next magic comment, or NULL if the given magic comment is the
* last one
*/
PRISM_EXPORTED_FUNCTION const pm_magic_comment_t * pm_magic_comment_next(const pm_magic_comment_t *magic_comment) PRISM_NONNULL(1);

#ifdef __cplusplus
}
#endif
Expand Down
70 changes: 22 additions & 48 deletions include/prism/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,19 @@ PRISM_EXPORTED_FUNCTION int pm_parser_lex_state(const pm_parser_t *parser) PRISM
PRISM_EXPORTED_FUNCTION size_t pm_parser_comments_size(const pm_parser_t *parser) PRISM_NONNULL(1);

/**
* A callback function that can be used to process comments found while parsing.
*/
typedef void (*pm_comment_callback_t)(const pm_comment_t *comment, void *data);

/**
* Iterates over the comments associated with the given parser and calls the
* given callback for each comment.
* Returns the first comment associated with the given parser. Iterate over the
* rest with pm_comment_next, as in:
*
* ```c
* for (const pm_comment_t *comment = pm_parser_comments_first(parser); comment != NULL; comment = pm_comment_next(comment)) {
* pm_location_t location = pm_comment_location(comment);
* }
* ```
*
* @param parser the parser whose comments we want to iterate over
* @param callback the callback function to call for each comment. This function
* will be passed a pointer to the comment and the data parameter passed to
* this function.
* @param data the data to pass to the callback function for each comment. This
* can be NULL if no data needs to be passed to the callback function.
* @returns the first comment, or NULL if there are no comments
*/
PRISM_EXPORTED_FUNCTION void pm_parser_comments_each(const pm_parser_t *parser, pm_comment_callback_t callback, void *data) PRISM_NONNULL(1);
PRISM_EXPORTED_FUNCTION const pm_comment_t * pm_parser_comments_first(const pm_parser_t *parser) PRISM_NONNULL(1);

/**
* Returns the number of comments owned by the given node. This is 0 if the
Expand Down Expand Up @@ -313,22 +310,13 @@ PRISM_EXPORTED_FUNCTION pm_comment_range_t pm_parser_comment_range(const pm_pars
PRISM_EXPORTED_FUNCTION size_t pm_parser_magic_comments_size(const pm_parser_t *parser) PRISM_NONNULL(1);

/**
* A callback function that can be used to process magic comments found while parsing.
*/
typedef void (*pm_magic_comment_callback_t)(const pm_magic_comment_t *magic_comment, void *data);

/**
* Iterates over the magic comments associated with the given parser and calls the
* given callback for each magic comment.
* Returns the first magic comment associated with the given parser. Iterate
* over the rest with pm_magic_comment_next.
*
* @param parser the parser whose magic comments we want to iterate over
* @param callback the callback function to call for each magic comment. This
* function will be passed a pointer to the magic comment and the data
* parameter passed to this function.
* @param data the data to pass to the callback function for each magic comment.
* This can be NULL if no data needs to be passed to the callback function.
* @returns the first magic comment, or NULL if there are no magic comments
*/
PRISM_EXPORTED_FUNCTION void pm_parser_magic_comments_each(const pm_parser_t *parser, pm_magic_comment_callback_t callback, void *data) PRISM_NONNULL(1);
PRISM_EXPORTED_FUNCTION const pm_magic_comment_t * pm_parser_magic_comments_first(const pm_parser_t *parser) PRISM_NONNULL(1);

/**
* Returns the number of errors associated with the given parser.
Expand All @@ -347,36 +335,22 @@ PRISM_EXPORTED_FUNCTION size_t pm_parser_errors_size(const pm_parser_t *parser)
PRISM_EXPORTED_FUNCTION size_t pm_parser_warnings_size(const pm_parser_t *parser) PRISM_NONNULL(1);

/**
* A callback function that can be used to process diagnostics found while
* parsing.
*/
typedef void (*pm_diagnostic_callback_t)(const pm_diagnostic_t *diagnostic, void *data);

/**
* Iterates over the errors associated with the given parser and calls the
* given callback for each error.
* Returns the first error associated with the given parser. Iterate over the
* rest with pm_diagnostic_next.
*
* @param parser the parser whose errors we want to iterate over
* @param callback the callback function to call for each error. This function
* will be passed a pointer to the error and the data parameter passed to
* this function.
* @param data the data to pass to the callback function for each error. This
* can be NULL if no data needs to be passed to the callback function.
* @returns the first error, or NULL if there are no errors
*/
PRISM_EXPORTED_FUNCTION void pm_parser_errors_each(const pm_parser_t *parser, pm_diagnostic_callback_t callback, void *data) PRISM_NONNULL(1);
PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_parser_errors_first(const pm_parser_t *parser) PRISM_NONNULL(1);

/**
* Iterates over the warnings associated with the given parser and calls the
* given callback for each warning.
* Returns the first warning associated with the given parser. Iterate over the
* rest with pm_diagnostic_next.
*
* @param parser the parser whose warnings we want to iterate over
* @param callback the callback function to call for each warning. This function
* will be passed a pointer to the warning and the data parameter passed to
* this function.
* @param data the data to pass to the callback function for each warning. This
* can be NULL if no data needs to be passed to the callback function.
* @returns the first warning, or NULL if there are no warnings
*/
PRISM_EXPORTED_FUNCTION void pm_parser_warnings_each(const pm_parser_t *parser, pm_diagnostic_callback_t callback, void *data) PRISM_NONNULL(1);
PRISM_EXPORTED_FUNCTION const pm_diagnostic_t * pm_parser_warnings_first(const pm_parser_t *parser) PRISM_NONNULL(1);

/**
* Returns the number of constants in the constant pool associated with the
Expand Down
11 changes: 7 additions & 4 deletions rust/ruby-prism-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,19 @@ fn generate_bindings(ruby_include_path: &Path, clang_args: &[String], layout_tes
.allowlist_function("pm_arena_free")
.allowlist_function("pm_arena_new")
.allowlist_function("pm_comment_location")
.allowlist_function("pm_comment_next")
.allowlist_function("pm_comment_type")
.allowlist_function("pm_constant_length")
.allowlist_function("pm_constant_start")
.allowlist_function("pm_diagnostic_error_level")
.allowlist_function("pm_diagnostic_location")
.allowlist_function("pm_diagnostic_message")
.allowlist_function("pm_diagnostic_next")
.allowlist_function("pm_diagnostic_type")
.allowlist_function("pm_diagnostic_warning_level")
.allowlist_function("pm_line_offset_list_line_column")
.allowlist_function("pm_magic_comment_key")
.allowlist_function("pm_magic_comment_next")
.allowlist_function("pm_magic_comment_value")
.allowlist_function("pm_options_command_line_set")
.allowlist_function("pm_options_encoding_locked_set")
Expand All @@ -213,23 +216,23 @@ fn generate_bindings(ruby_include_path: &Path, clang_args: &[String], layout_tes
.allowlist_function("pm_options_scopes_init")
.allowlist_function("pm_options_version_set")
.allowlist_function("pm_parse")
.allowlist_function("pm_parser_comments_each")
.allowlist_function("pm_parser_comments_first")
.allowlist_function("pm_parser_comments_size")
.allowlist_function("pm_parser_constant")
.allowlist_function("pm_parser_constants_each")
.allowlist_function("pm_parser_constants_size")
.allowlist_function("pm_parser_data_loc")
.allowlist_function("pm_parser_errors_each")
.allowlist_function("pm_parser_errors_first")
.allowlist_function("pm_parser_errors_size")
.allowlist_function("pm_parser_free")
.allowlist_function("pm_parser_frozen_string_literal")
.allowlist_function("pm_parser_line_offsets")
.allowlist_function("pm_parser_magic_comments_each")
.allowlist_function("pm_parser_magic_comments_first")
.allowlist_function("pm_parser_magic_comments_size")
.allowlist_function("pm_parser_new")
.allowlist_function("pm_parser_start")
.allowlist_function("pm_parser_start_line")
.allowlist_function("pm_parser_warnings_each")
.allowlist_function("pm_parser_warnings_first")
.allowlist_function("pm_parser_warnings_size")
.allowlist_function("pm_size_to_native")
.allowlist_function("pm_string_constant_init")
Expand Down
Loading
Loading