Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unicode property support #1319

Merged
merged 1 commit into from
Apr 24, 2022
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
8 changes: 7 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.11.7 2022-04-25

* [#1319](https://github.com/mbj/mutant/pull/1319)

Fix regexp mapper to do full, ruby version specific unicode property mapping.

# v0.11.6 2022-04-10

* [#1317](https://github.com/mbj/mutant/pull/1317)
Expand Down Expand Up @@ -148,7 +154,7 @@

* [#1234](https://github.com/mbj/mutant/pull/1234)
Add mapping for latin regexp properties to fix crash on mutating
`p{Latin}` regexp nodes.
`\p{Latin}` regexp nodes.

Fix: [#1231](https://github.com/mbj/mutant/issues/1231)

Expand Down
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PATH
remote: .
specs:
mutant (0.11.6)
mutant (0.11.7)
diff-lcs (~> 1.3)
parser (~> 3.1.0)
regexp_parser (~> 2.0, >= 2.0.3)
regexp_parser (~> 2.3)
sorbet-runtime (~> 0.5.0)
unparser (~> 0.6.4)
unparser (~> 0.6.5)

GEM
remote: https://oss:[email protected]/
Expand All @@ -22,7 +22,7 @@ GEM
parser (3.1.1.0)
ast (~> 2.4.1)
rainbow (3.1.1)
regexp_parser (2.2.1)
regexp_parser (2.3.0)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
Expand Down Expand Up @@ -52,9 +52,9 @@ GEM
rubocop-ast (1.16.0)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
sorbet-runtime (0.5.9837)
sorbet-runtime (0.5.9928)
unicode-display_width (2.1.0)
unparser (0.6.4)
unparser (0.6.5)
diff-lcs (~> 1.3)
parser (>= 3.1.0)

Expand Down
51 changes: 41 additions & 10 deletions lib/mutant/ast/regexp/transformer/direct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,52 @@ def call
class ASTToExpression < Transformer::ASTToExpression
include LookupTable

# rubocop:disable Metrics/BlockLength
properties = ::Regexp::Syntax
.version_class("ruby/#{RUBY_VERSION}")
.features.fetch(:property)
.flat_map do |property|
property_specifier = "\\p{#{property}}"
non_property_specifier = "\\P{#{property}}"

begin
property_regex = /#{property_specifier}/
non_property_regex = /#{non_property_specifier}/
# This is probably a regexp_parser bug, ignoring registration of that property
# See: https://github.com/ammar/regexp_parser/issues/84
rescue RegexpError
next
end

[
[
:"regexp_#{property}_property",
[
:property,
property.to_sym,
property_specifier
],
::Regexp::Parser.parse(property_regex).expressions.first.class
],
[
:"regexp_#{property}_nonproperty",
[
:nonproperty,
property.to_sym,
non_property_specifier
],
::Regexp::Parser.parse(non_property_regex).expressions.first.class
]
]
end.compact
# rubocop:enable Metrics/BlockLength

# rubocop:disable Layout/LineLength
TABLE = Table.create(
*properties,
[:regexp_alnum_posixclass, [:posixclass, :alnum, '[:alnum:]'], ::Regexp::Expression::PosixClass],
[:regexp_alpha_posixclass, [:posixclass, :alpha, '[:alpha:]'], ::Regexp::Expression::PosixClass],
[:regexp_alpha_property, [:property, :alpha, '\p{Alpha}'], ::Regexp::Expression::UnicodeProperty::Alpha],
[:regexp_alternation_escape, [:escape, :alternation, '\|'], ::Regexp::Expression::EscapeSequence::Literal],
[:regexp_arabic_property, [:property, :arabic, '\p{Arabic}'], ::Regexp::Expression::UnicodeProperty::Script],
[:regexp_ascii_posixclass, [:posixclass, :ascii, '[:ascii:]'], ::Regexp::Expression::PosixClass],
[:regexp_backspace_escape, [:escape, :backspace, '\b'], ::Regexp::Expression::EscapeSequence::Backspace],
[:regexp_bell_escape, [:escape, :bell, '\a'], ::Regexp::Expression::EscapeSequence::Literal],
Expand All @@ -65,16 +104,10 @@ class ASTToExpression < Transformer::ASTToExpression
[:regexp_graph_posixclass, [:posixclass, :graph, '[:graph:]'], ::Regexp::Expression::PosixClass],
[:regexp_group_close_escape, [:escape, :group_close, '\)'], ::Regexp::Expression::EscapeSequence::Literal],
[:regexp_group_open_escape, [:escape, :group_open, '\('], ::Regexp::Expression::EscapeSequence::Literal],
[:regexp_han_property, [:property, :han, '\p{Han}'], ::Regexp::Expression::UnicodeProperty::Script],
[:regexp_hangul_property, [:property, :hangul, '\p{Hangul}'], ::Regexp::Expression::UnicodeProperty::Script],
[:regexp_hex_type, [:type, :hex, '\h'], ::Regexp::Expression::CharacterType::Hex],
[:regexp_hiragana_property, [:property, :hiragana, '\p{Hiragana}'], ::Regexp::Expression::UnicodeProperty::Script],
[:regexp_interval_close_escape, [:escape, :interval_close, '\}'], ::Regexp::Expression::EscapeSequence::Literal],
[:regexp_interval_open_escape, [:escape, :interval_open, '\{'], ::Regexp::Expression::EscapeSequence::Literal],
[:regexp_katakana_property, [:property, :katakana, '\p{Katakana}'], ::Regexp::Expression::UnicodeProperty::Script],
[:regexp_letter_property, [:property, :letter, '\p{L}'], ::Regexp::Expression::UnicodeProperty::Letter::Any],
[:regexp_linebreak_type, [:type, :linebreak, '\R'], ::Regexp::Expression::CharacterType::Linebreak],
[:regexp_latin_property, [:property, :latin, '\p{Latin}'], ::Regexp::Expression::UnicodeProperty::Script],
[:regexp_lower_posixclass, [:posixclass, :lower, '[:lower:]'], ::Regexp::Expression::PosixClass],
[:regexp_mark_keep, [:keep, :mark, '\K'], ::Regexp::Expression::Keep::Mark],
[:regexp_match_start_anchor, [:anchor, :match_start, '\\G'], ::Regexp::Expression::Anchor::MatchStart],
Expand All @@ -86,10 +119,8 @@ class ASTToExpression < Transformer::ASTToExpression
[:regexp_nonword_type, [:type, :nonword, '\W'], ::Regexp::Expression::CharacterType::NonWord],
[:regexp_one_or_more_escape, [:escape, :one_or_more, '\+'], ::Regexp::Expression::EscapeSequence::Literal],
[:regexp_print_nonposixclass, [:nonposixclass, :print, '[:^print:]'], ::Regexp::Expression::PosixClass],
[:regexp_print_nonproperty, [:nonproperty, :print, '\P{Print}'], ::Regexp::Expression::UnicodeProperty::Print],
[:regexp_print_posixclass, [:posixclass, :print, '[:print:]'], ::Regexp::Expression::PosixClass],
[:regexp_print_posixclass, [:posixclass, :print, '[:print:]'], ::Regexp::Expression::PosixClass],
[:regexp_print_property, [:property, :print, '\p{Print}'], ::Regexp::Expression::UnicodeProperty::Print],
[:regexp_punct_posixclass, [:posixclass, :punct, '[:punct:]'], ::Regexp::Expression::PosixClass],
[:regexp_set_close_escape, [:escape, :set_close, '\]'], ::Regexp::Expression::EscapeSequence::Literal],
[:regexp_set_open_escape, [:escape, :set_open, '\['], ::Regexp::Expression::EscapeSequence::Literal],
Expand Down
Loading