[ruby/prism] Accept version shorthand like 3.4

https://github.com/ruby/prism/commit/098f1c4607
This commit is contained in:
Kevin Newton 2024-09-24 08:41:03 -04:00 committed by git
parent e02a6097e6
commit 414a848cc6
5 changed files with 55 additions and 42 deletions

View File

@ -416,9 +416,9 @@ module Prism
case version
when nil, "latest"
0
when /\A3\.3\.\d+\z/
when /\A3\.3(\.\d+)?\z/
1
when /\A3\.4\.\d+\z/
when /\A3\.4(\.\d+)?\z/
0
else
raise ArgumentError, "invalid version: #{version}"

View File

@ -1,5 +1,4 @@
#include "prism/options.h"
#include "prism/util/pm_char.h"
/**
* Set the shebang callback option on the given options struct.
@ -58,7 +57,11 @@ pm_options_command_line_set(pm_options_t *options, uint8_t command_line) {
options->command_line = command_line;
}
static bool is_number(const char *string, size_t length) {
/**
* Checks if the given slice represents a number.
*/
static inline bool
is_number(const char *string, size_t length) {
return pm_strspn_decimal_digit((const uint8_t *) string, (ptrdiff_t) length) == length;
}
@ -74,6 +77,20 @@ pm_options_version_set(pm_options_t *options, const char *version, size_t length
return true;
}
if (length == 3) {
if (strncmp(version, "3.3", 3) == 0) {
options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
return true;
}
if (strncmp(version, "3.4", 3) == 0) {
options->version = PM_OPTIONS_VERSION_LATEST;
return true;
}
return false;
}
if (length >= 4) {
if (strncmp(version, "3.3.", 4) == 0 && is_number(version + 4, length - 4)) {
options->version = PM_OPTIONS_VERSION_CRUBY_3_3;

View File

@ -7,6 +7,7 @@
#define PRISM_OPTIONS_H
#include "prism/defines.h"
#include "prism/util/pm_char.h"
#include "prism/util/pm_string.h"
#include <stdbool.h>

View File

@ -104,6 +104,39 @@ module Prism
assert Prism.parse_success?("yield", partial_script: true)
end
def test_version
assert Prism.parse_success?("1 + 1", version: "3.3")
assert Prism.parse_success?("1 + 1", version: "3.3.0")
assert Prism.parse_success?("1 + 1", version: "3.3.1")
assert Prism.parse_success?("1 + 1", version: "3.3.9")
assert Prism.parse_success?("1 + 1", version: "3.3.10")
assert Prism.parse_success?("1 + 1", version: "3.4")
assert Prism.parse_success?("1 + 1", version: "3.4.0")
assert Prism.parse_success?("1 + 1", version: "3.4.9")
assert Prism.parse_success?("1 + 1", version: "3.4.10")
assert Prism.parse_success?("1 + 1", version: "latest")
# Test edge case
error = assert_raise(ArgumentError) { Prism.parse("1 + 1", version: "latest2") }
assert_equal "invalid version: latest2", error.message
assert_raise ArgumentError do
Prism.parse("1 + 1", version: "3.3.a")
end
# Not supported version (too old)
assert_raise ArgumentError do
Prism.parse("1 + 1", version: "3.2.0")
end
# Not supported version (too new)
assert_raise ArgumentError do
Prism.parse("1 + 1", version: "3.5.0")
end
end
private
def find_source_file_node(program)

View File

@ -7,43 +7,5 @@ module Prism
def test_prism_version_is_set
refute_nil VERSION
end
def test_syntax_versions
assert Prism.parse("1 + 1", version: "3.3.0").success?
assert Prism.parse("1 + 1", version: "3.3.1").success?
assert Prism.parse("1 + 1", version: "3.3.9").success?
assert Prism.parse("1 + 1", version: "3.3.10").success?
assert Prism.parse("1 + 1", version: "3.4.0").success?
assert Prism.parse("1 + 1", version: "3.4.9").success?
assert Prism.parse("1 + 1", version: "3.4.10").success?
assert Prism.parse("1 + 1", version: "latest").success?
# Test edge case
error = assert_raise ArgumentError do
Prism.parse("1 + 1", version: "latest2")
end
assert_equal "invalid version: latest2", error.message
assert_raise ArgumentError do
Prism.parse("1 + 1", version: "3.3.a")
end
# Not supported version syntax
assert_raise ArgumentError do
Prism.parse("1 + 1", version: "3.3")
end
# Not supported version (too old)
assert_raise ArgumentError do
Prism.parse("1 + 1", version: "3.2.0")
end
# Not supported version (too new)
assert_raise ArgumentError do
Prism.parse("1 + 1", version: "3.5.0")
end
end
end
end