@@ -201,9 +201,24 @@ build_options_i(VALUE key, VALUE value, VALUE argument) {
201201 const char * version = check_string (value );
202202
203203 if (RSTRING_LEN (value ) == 7 && strncmp (version , "current" , 7 ) == 0 ) {
204- const char * current_version = RSTRING_PTR (rb_const_get (rb_cObject , rb_intern ("RUBY_VERSION" )));
205- if (!pm_options_version_set (options , current_version , 3 )) {
206- rb_exc_raise (rb_exc_new_cstr (rb_cPrismCurrentVersionError , current_version ));
204+ const char * ruby_version = RSTRING_PTR (rb_const_get (rb_cObject , rb_intern ("RUBY_VERSION" )));
205+ if (!pm_options_version_set (options , ruby_version , 3 )) {
206+ rb_exc_raise (rb_exc_new_cstr (rb_cPrismCurrentVersionError , ruby_version ));
207+ }
208+ } else if (RSTRING_LEN (value ) == 7 && strncmp (version , "nearest" , 7 ) == 0 ) {
209+ const char * ruby_version = RSTRING_PTR (rb_const_get (rb_cObject , rb_intern ("RUBY_VERSION" )));
210+ const char * nearest_version ;
211+
212+ if (ruby_version [0 ] < '3' || (ruby_version [0 ] == '3' && ruby_version [2 ] < '3' )) {
213+ nearest_version = "3.3" ;
214+ } else if (ruby_version [0 ] > '4' || (ruby_version [0 ] == '4' && ruby_version [2 ] > '1' )) {
215+ nearest_version = "4.1" ;
216+ } else {
217+ nearest_version = ruby_version ;
218+ }
219+
220+ if (!pm_options_version_set (options , nearest_version , 3 )) {
221+ rb_raise (rb_eArgError , "invalid nearest version: %s" , nearest_version );
207222 }
208223 } else if (!pm_options_version_set (options , version , RSTRING_LEN (value ))) {
209224 rb_raise (rb_eArgError , "invalid version: %" PRIsVALUE , value );
@@ -894,8 +909,10 @@ parse_input(pm_string_t *input, const pm_options_t *options) {
894909 * version of Ruby syntax (which you can trigger with `nil` or
895910 * `"latest"`). You may also restrict the syntax to a specific version of
896911 * Ruby, e.g., with `"3.3.0"`. To parse with the same syntax version that
897- * the current Ruby is running use `version: "current"`. Raises
898- * ArgumentError if the version is not currently supported by Prism.
912+ * the current Ruby is running use `version: "current"`. To parse with the
913+ * nearest version to the current Ruby that is running, use
914+ * `version: "nearest"`. Raises ArgumentError if the version is not
915+ * currently supported by Prism.
899916 */
900917static VALUE
901918parse (int argc , VALUE * argv , VALUE self ) {
0 commit comments