From 671ddb0022c9459344513f6ed89f57acdc98b32e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 20 Oct 2026 11:34:47 -0400 Subject: [PATCH] Add a first/next API for our iterables This is as opposed to the *_each APIs we have been using, because that adds the overhead of a passed around callback. --- ext/prism/extension.c | 99 ++++++------------ include/prism/comments.h | 9 ++ include/prism/diagnostic.h | 10 ++ include/prism/magic_comments.h | 10 ++ include/prism/parser.h | 70 ++++--------- rust/ruby-prism-sys/build/main.rs | 11 +- rust/ruby-prism-sys/src/bindings.rs | 119 ++++++++++------------ rust/ruby-prism-sys/tests/parser_tests.rs | 39 +++---- rust/ruby-prism/src/parse_result/mod.rs | 54 +++------- src/parser.c | 72 ++++++------- templates/src/diagnostic.c.erb | 8 ++ 11 files changed, 213 insertions(+), 288 deletions(-) diff --git a/ext/prism/extension.c b/ext/prism/extension.c index b840799be1..1aabfd253e 100644 --- a/ext/prism/extension.c +++ b/ext/prism/extension.c @@ -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); } @@ -673,19 +664,6 @@ 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. */ @@ -693,8 +671,9 @@ 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; @@ -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; @@ -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); } /** @@ -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; @@ -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); } /** @@ -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; diff --git a/include/prism/comments.h b/include/prism/comments.h index 77e31e4e4f..e4f5cb9dc2 100644 --- a/include/prism/comments.h +++ b/include/prism/comments.h @@ -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 diff --git a/include/prism/diagnostic.h b/include/prism/diagnostic.h index f7585dcdb4..202a3e782a 100644 --- a/include/prism/diagnostic.h +++ b/include/prism/diagnostic.h @@ -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 diff --git a/include/prism/magic_comments.h b/include/prism/magic_comments.h index 485da7861b..13cbe26a9f 100644 --- a/include/prism/magic_comments.h +++ b/include/prism/magic_comments.h @@ -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 diff --git a/include/prism/parser.h b/include/prism/parser.h index aeeffab079..0a05776983 100644 --- a/include/prism/parser.h +++ b/include/prism/parser.h @@ -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 @@ -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. @@ -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 diff --git a/rust/ruby-prism-sys/build/main.rs b/rust/ruby-prism-sys/build/main.rs index 45ec1011ee..acd4b14244 100644 --- a/rust/ruby-prism-sys/build/main.rs +++ b/rust/ruby-prism-sys/build/main.rs @@ -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") @@ -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") diff --git a/rust/ruby-prism-sys/src/bindings.rs b/rust/ruby-prism-sys/src/bindings.rs index 8e415c7191..c05e1e2525 100644 --- a/rust/ruby-prism-sys/src/bindings.rs +++ b/rust/ruby-prism-sys/src/bindings.rs @@ -2583,28 +2583,6 @@ pub type pm_x_string_node_t = pm_x_string_node; @extends pm_node_t */ pub type pm_yield_node_t = pm_yield_node; -/** A callback function that can be used to process comments found while parsing. -*/ -pub type pm_comment_callback_t = ::std::option::Option< - unsafe extern "C" fn(comment: *const pm_comment_t, data: *mut ::std::os::raw::c_void), ->; -/** A callback function that can be used to process magic comments found while parsing. -*/ -pub type pm_magic_comment_callback_t = ::std::option::Option< - unsafe extern "C" fn( - magic_comment: *const pm_magic_comment_t, - data: *mut ::std::os::raw::c_void, - ), ->; -/** A callback function that can be used to process diagnostics found while - parsing. -*/ -pub type pm_diagnostic_callback_t = ::std::option::Option< - unsafe extern "C" fn( - diagnostic: *const pm_diagnostic_t, - data: *mut ::std::os::raw::c_void, - ), ->; /** A callback function that can be used to process constants found while parsing. */ @@ -11728,6 +11706,16 @@ unsafe extern "C" { pub fn pm_diagnostic_warning_level( diagnostic: *const pm_diagnostic_t, ) -> pm_warning_level_t; + /** 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. +*/ + pub fn pm_diagnostic_next( + diagnostic: *const pm_diagnostic_t, + ) -> *const pm_diagnostic_t; /** Returns the location associated with the given comment. @param comment the comment whose location we want to get @@ -11741,6 +11729,13 @@ unsafe extern "C" { PM_COMMENT_INLINE or PM_COMMENT_EMBDOC. */ pub fn pm_comment_type(comment: *const pm_comment_t) -> pm_comment_type_t; + /** 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 +*/ + pub fn pm_comment_next(comment: *const pm_comment_t) -> *const pm_comment_t; /** Returns the line and column of the given offset. If the offset is not in the list, the line and column of the closest offset less than the given offset are returned. @@ -11771,6 +11766,16 @@ unsafe extern "C" { pub fn pm_magic_comment_value( magic_comment: *const pm_magic_comment_t, ) -> pm_location_t; + /** 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 +*/ + pub fn pm_magic_comment_next( + magic_comment: *const pm_magic_comment_t, + ) -> *const pm_magic_comment_t; /** Allocate a new options struct. If the options struct cannot be allocated, this function aborts the process. @@ -11972,42 +11977,34 @@ unsafe extern "C" { @returns the number of comments associated with the given parser */ pub fn pm_parser_comments_size(parser: *const pm_parser_t) -> usize; - /** 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 */ - pub fn pm_parser_comments_each( - parser: *const pm_parser_t, - callback: pm_comment_callback_t, - data: *mut ::std::os::raw::c_void, - ); + pub fn pm_parser_comments_first(parser: *const pm_parser_t) -> *const pm_comment_t; /** Returns the number of magic comments associated with the given parser. @param parser the parser whose magic comments we want to get the size of @returns the number of magic comments associated with the given parser */ pub fn pm_parser_magic_comments_size(parser: *const pm_parser_t) -> usize; - /** 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 */ - pub fn pm_parser_magic_comments_each( + pub fn pm_parser_magic_comments_first( parser: *const pm_parser_t, - callback: pm_magic_comment_callback_t, - data: *mut ::std::os::raw::c_void, - ); + ) -> *const pm_magic_comment_t; /** Returns the number of errors associated with the given parser. @param parser the parser whose errors we want to get the size of @@ -12020,36 +12017,22 @@ unsafe extern "C" { @returns the number of warnings associated with the given parser */ pub fn pm_parser_warnings_size(parser: *const pm_parser_t) -> usize; - /** 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 */ - pub fn pm_parser_errors_each( - parser: *const pm_parser_t, - callback: pm_diagnostic_callback_t, - data: *mut ::std::os::raw::c_void, - ); - /** Iterates over the warnings associated with the given parser and calls the - given callback for each warning. + pub fn pm_parser_errors_first(parser: *const pm_parser_t) -> *const pm_diagnostic_t; + /** 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 */ - pub fn pm_parser_warnings_each( + pub fn pm_parser_warnings_first( parser: *const pm_parser_t, - callback: pm_diagnostic_callback_t, - data: *mut ::std::os::raw::c_void, - ); + ) -> *const pm_diagnostic_t; /** Returns the number of constants in the constant pool associated with the given parser. diff --git a/rust/ruby-prism-sys/tests/parser_tests.rs b/rust/ruby-prism-sys/tests/parser_tests.rs index 0cfc234de2..edf71ad085 100644 --- a/rust/ruby-prism-sys/tests/parser_tests.rs +++ b/rust/ruby-prism-sys/tests/parser_tests.rs @@ -2,23 +2,11 @@ use std::ffi::{CStr, CString}; use std::path::Path; use ruby_prism_sys::{ - pm_arena_free, pm_arena_new, pm_comment_location, pm_comment_type, pm_comment_type_t, pm_diagnostic_location, - pm_diagnostic_message, pm_parse, pm_parser_comments_each, pm_parser_errors_each, pm_parser_free, pm_parser_new, + pm_arena_free, pm_arena_new, pm_comment_location, pm_comment_next, pm_comment_type, pm_comment_type_t, + pm_diagnostic_location, pm_diagnostic_message, pm_diagnostic_next, pm_parse, pm_parser_comments_first, + pm_parser_errors_first, pm_parser_errors_size, pm_parser_free, pm_parser_new, }; -unsafe extern "C" fn collect_comment(comment: *const ruby_prism_sys::pm_comment_t, data: *mut std::ffi::c_void) { - let vec = &mut *(data.cast::>()); - vec.push(comment); -} - -unsafe extern "C" fn collect_diagnostic( - diagnostic: *const ruby_prism_sys::pm_diagnostic_t, - data: *mut std::ffi::c_void, -) { - let vec = &mut *(data.cast::>()); - vec.push(diagnostic); -} - fn ruby_file_contents() -> (CString, usize) { let rust_path = Path::new(env!("CARGO_MANIFEST_DIR")); let ruby_file_path = rust_path.join("../../lib/prism.rb").canonicalize().unwrap(); @@ -56,11 +44,9 @@ fn comments_test() { ); let _node = pm_parse(parser); - let mut comments: Vec<*const ruby_prism_sys::pm_comment_t> = Vec::new(); - pm_parser_comments_each(parser, Some(collect_comment), (&raw mut comments).cast()); - - assert_eq!(comments.len(), 1); - let comment = comments[0]; + let comment = pm_parser_comments_first(parser); + assert!(!comment.is_null()); + assert!(pm_comment_next(comment).is_null()); assert_eq!(pm_comment_type(comment), pm_comment_type_t::PM_COMMENT_INLINE); let location = pm_comment_location(comment); @@ -85,11 +71,16 @@ fn diagnostics_test() { ); let _node = pm_parse(parser); - let mut errors: Vec<*const ruby_prism_sys::pm_diagnostic_t> = Vec::new(); - pm_parser_errors_each(parser, Some(collect_diagnostic), (&raw mut errors).cast()); + let error = pm_parser_errors_first(parser); + assert!(!error.is_null()); - assert!(!errors.is_empty()); - let error = errors[0]; + let mut count = 0; + let mut current = error; + while !current.is_null() { + count += 1; + current = pm_diagnostic_next(current); + } + assert_eq!(count, pm_parser_errors_size(parser)); let message = CStr::from_ptr(pm_diagnostic_message(error)); assert_eq!( diff --git a/rust/ruby-prism/src/parse_result/mod.rs b/rust/ruby-prism/src/parse_result/mod.rs index 82cae4b731..3f3abe962d 100644 --- a/rust/ruby-prism/src/parse_result/mod.rs +++ b/rust/ruby-prism/src/parse_result/mod.rs @@ -6,8 +6,8 @@ mod diagnostics; use std::ptr::NonNull; use ruby_prism_sys::{ - pm_arena_free, pm_arena_t, pm_comment_t, pm_diagnostic_t, pm_line_offset_list_line_column, pm_location_t, pm_magic_comment_t, pm_node_t, pm_parser_comments_each, pm_parser_comments_size, pm_parser_data_loc, pm_parser_errors_each, pm_parser_errors_size, pm_parser_free, - pm_parser_frozen_string_literal, pm_parser_line_offsets, pm_parser_magic_comments_each, pm_parser_magic_comments_size, pm_parser_start, pm_parser_start_line, pm_parser_t, pm_parser_warnings_each, pm_parser_warnings_size, + pm_arena_free, pm_arena_t, pm_comment_next, pm_diagnostic_next, pm_line_offset_list_line_column, pm_location_t, pm_magic_comment_next, pm_node_t, pm_parser_comments_first, pm_parser_comments_size, pm_parser_data_loc, pm_parser_errors_first, pm_parser_errors_size, pm_parser_free, + pm_parser_frozen_string_literal, pm_parser_line_offsets, pm_parser_magic_comments_first, pm_parser_magic_comments_size, pm_parser_start, pm_parser_start_line, pm_parser_t, pm_parser_warnings_first, pm_parser_warnings_size, }; pub use self::comments::{Comment, CommentType, Comments, MagicComment, MagicComments}; @@ -140,22 +140,16 @@ impl std::fmt::Debug for Location<'_> { } } -// C callback that collects comment pointers into a Vec -unsafe extern "C" fn collect_comment(comment: *const pm_comment_t, data: *mut std::ffi::c_void) { - let vec = &mut *(data.cast::>()); - vec.push(comment); -} - -// C callback that collects magic comment pointers into a Vec -unsafe extern "C" fn collect_magic_comment(comment: *const pm_magic_comment_t, data: *mut std::ffi::c_void) { - let vec = &mut *(data.cast::>()); - vec.push(comment); -} - -// C callback that collects diagnostic pointers into a Vec -unsafe extern "C" fn collect_diagnostic(diagnostic: *const pm_diagnostic_t, data: *mut std::ffi::c_void) { - let vec = &mut *(data.cast::>()); - vec.push(diagnostic); +// Collects the items of one of the parser's lists into a Vec, starting from the +// first item and following each item's successor until it is null. +unsafe fn collect(size: usize, first: *const T, next: unsafe extern "C" fn(*const T) -> *const T) -> Vec<*const T> { + let mut ptrs = Vec::with_capacity(size); + let mut current = first; + while !current.is_null() { + ptrs.push(current); + current = next(current); + } + ptrs } /// The result of parsing a source string. @@ -207,11 +201,7 @@ impl<'pr> ParseResult<'pr> { /// parse result. #[must_use] pub fn errors(&self) -> Diagnostics<'_> { - let size = unsafe { pm_parser_errors_size(self.parser) }; - let mut ptrs: Vec<*const pm_diagnostic_t> = Vec::with_capacity(size); - unsafe { - pm_parser_errors_each(self.parser, Some(collect_diagnostic), (&raw mut ptrs).cast()); - } + let ptrs = unsafe { collect(pm_parser_errors_size(self.parser), pm_parser_errors_first(self.parser), pm_diagnostic_next) }; Diagnostics::new(ptrs, self.parser) } @@ -219,11 +209,7 @@ impl<'pr> ParseResult<'pr> { /// parse result. #[must_use] pub fn warnings(&self) -> Diagnostics<'_> { - let size = unsafe { pm_parser_warnings_size(self.parser) }; - let mut ptrs: Vec<*const pm_diagnostic_t> = Vec::with_capacity(size); - unsafe { - pm_parser_warnings_each(self.parser, Some(collect_diagnostic), (&raw mut ptrs).cast()); - } + let ptrs = unsafe { collect(pm_parser_warnings_size(self.parser), pm_parser_warnings_first(self.parser), pm_diagnostic_next) }; Diagnostics::new(ptrs, self.parser) } @@ -231,11 +217,7 @@ impl<'pr> ParseResult<'pr> { /// parse result. #[must_use] pub fn comments(&self) -> Comments<'_> { - let size = unsafe { pm_parser_comments_size(self.parser) }; - let mut ptrs: Vec<*const pm_comment_t> = Vec::with_capacity(size); - unsafe { - pm_parser_comments_each(self.parser, Some(collect_comment), (&raw mut ptrs).cast()); - } + let ptrs = unsafe { collect(pm_parser_comments_size(self.parser), pm_parser_comments_first(self.parser), pm_comment_next) }; Comments::new(ptrs, self.parser) } @@ -243,11 +225,7 @@ impl<'pr> ParseResult<'pr> { /// parse result. #[must_use] pub fn magic_comments(&self) -> MagicComments<'_> { - let size = unsafe { pm_parser_magic_comments_size(self.parser) }; - let mut ptrs: Vec<*const pm_magic_comment_t> = Vec::with_capacity(size); - unsafe { - pm_parser_magic_comments_each(self.parser, Some(collect_magic_comment), (&raw mut ptrs).cast()); - } + let ptrs = unsafe { collect(pm_parser_magic_comments_size(self.parser), pm_parser_magic_comments_first(self.parser), pm_magic_comment_next) }; MagicComments::new(ptrs, self.parser) } diff --git a/src/parser.c b/src/parser.c index 2d98581114..b9103d7c25 100644 --- a/src/parser.c +++ b/src/parser.c @@ -185,6 +185,14 @@ pm_comment_type(const pm_comment_t *comment) { return comment->type; } +/** + * Returns the comment that follows the given comment. + */ +const pm_comment_t * +pm_comment_next(const pm_comment_t *comment) { + return (const pm_comment_t *) comment->node.next; +} + /** * Returns the number of comments associated with the given parser. */ @@ -194,17 +202,11 @@ pm_parser_comments_size(const pm_parser_t *parser) { } /** - * 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. */ -void -pm_parser_comments_each(const pm_parser_t *parser, pm_comment_callback_t callback, void *data) { - const pm_list_node_t *current = parser->comment_list.head; - while (current != NULL) { - const pm_comment_t *comment = (const pm_comment_t *) current; - callback(comment, data); - current = current->next; - } +const pm_comment_t * +pm_parser_comments_first(const pm_parser_t *parser) { + return (const pm_comment_t *) parser->comment_list.head; } /** @@ -270,6 +272,14 @@ pm_magic_comment_value(const pm_magic_comment_t *magic_comment) { return magic_comment->value; } +/** + * Returns the magic comment that follows the given magic comment. + */ +const pm_magic_comment_t * +pm_magic_comment_next(const pm_magic_comment_t *magic_comment) { + return (const pm_magic_comment_t *) magic_comment->node.next; +} + /** * Returns the number of magic comments associated with the given parser. */ @@ -279,17 +289,11 @@ pm_parser_magic_comments_size(const pm_parser_t *parser) { } /** - * 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. */ -void -pm_parser_magic_comments_each(const pm_parser_t *parser, pm_magic_comment_callback_t callback, void *data) { - const pm_list_node_t *current = parser->magic_comment_list.head; - while (current != NULL) { - const pm_magic_comment_t *magic_comment = (const pm_magic_comment_t *) current; - callback(magic_comment, data); - current = current->next; - } +const pm_magic_comment_t * +pm_parser_magic_comments_first(const pm_parser_t *parser) { + return (const pm_magic_comment_t *) parser->magic_comment_list.head; } /** @@ -308,32 +312,20 @@ pm_parser_warnings_size(const pm_parser_t *parser) { return parser->warning_list.size; } -static inline void -pm_parser_diagnostics_each(const pm_list_t *list, pm_diagnostic_callback_t callback, void *data) { - const pm_list_node_t *current = list->head; - while (current != NULL) { - const pm_diagnostic_t *diagnostic = (const pm_diagnostic_t *) current; - callback(diagnostic, data); - current = current->next; - } -} - /** - * 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. */ -void -pm_parser_errors_each(const pm_parser_t *parser, pm_diagnostic_callback_t callback, void *data) { - pm_parser_diagnostics_each(&parser->error_list, callback, data); +const pm_diagnostic_t * +pm_parser_errors_first(const pm_parser_t *parser) { + return (const pm_diagnostic_t *) parser->error_list.head; } /** - * 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. */ -void -pm_parser_warnings_each(const pm_parser_t *parser, pm_diagnostic_callback_t callback, void *data) { - pm_parser_diagnostics_each(&parser->warning_list, callback, data); +const pm_diagnostic_t * +pm_parser_warnings_first(const pm_parser_t *parser) { + return (const pm_diagnostic_t *) parser->warning_list.head; } /** diff --git a/templates/src/diagnostic.c.erb b/templates/src/diagnostic.c.erb index 0683978084..d8e7c1c289 100644 --- a/templates/src/diagnostic.c.erb +++ b/templates/src/diagnostic.c.erb @@ -503,6 +503,14 @@ pm_diagnostic_warning_level(const pm_diagnostic_t *diagnostic) { return (pm_warning_level_t) pm_diagnostic_id_level(diagnostic->diag_id); } +/** + * Get the diagnostic that follows the given diagnostic. + */ +const pm_diagnostic_t * +pm_diagnostic_next(const pm_diagnostic_t *diagnostic) { + return (const pm_diagnostic_t *) diagnostic->node.next; +} + /** * Append an error to the given list of diagnostic. */