Skip to content

Commit 20b6cc7

Browse files
committed
[GR-32618] Add truffle-regex-ignore-atomic-groups option.
PullRequest: truffleruby/3242
2 parents a5b5504 + 920830c commit 20b6cc7

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
slow:The --truffle-regex-ignore-atomic-groups option works
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 2.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
11+
describe "The --truffle-regex-ignore-atomic-groups option" do
12+
it "works" do
13+
out = ruby_exe('/foo(?>(?:A)+)Abar/ =~ "fooAAAAAbar"', options: "--experimental-options --truffle-regex-ignore-atomic-groups --compare-regex-engines", args: "2>&1")
14+
out.should == <<~OUT
15+
match_in_region(/foo(?>(?:A)+)Abar/, "fooAAAAAbar"@UTF-8, 0, 11, false, 0) enc=? gave
16+
0 - 11
17+
but we expected
18+
NO MATCH
19+
OUT
20+
end
21+
end

src/main/java/org/truffleruby/core/regexp/TRegexCache.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,12 @@ private static Object compileTRegex(RubyContext context, RubyRegexp regexp, bool
174174
return null;
175175
}
176176

177-
String regex = "Flavor=Ruby,Encoding=" + tRegexEncoding + "/" + processedRegexpSource + "/" + flags;
177+
String ignoreAtomicGroups = context.getOptions().TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS
178+
? ",IgnoreAtomicGroups=true"
179+
: "";
180+
181+
String regex = "Flavor=Ruby,Encoding=" + tRegexEncoding + ignoreAtomicGroups + "/" + processedRegexpSource +
182+
"/" + flags;
178183
Source regexSource = Source
179184
.newBuilder("regex", regex, "Regexp")
180185
.mimeType("application/tregex")

src/main/java/org/truffleruby/options/Options.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ public class Options {
136136
public final boolean WARN_TRUFFLE_REGEX_COMPILE_FALLBACK;
137137
/** --warn-truffle-regex-match-fallback=false */
138138
public final boolean WARN_TRUFFLE_REGEX_MATCH_FALLBACK;
139+
/** --truffle-regex-ignore-atomic-groups=false */
140+
public final boolean TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS;
139141
/** --argv-globals=false */
140142
public final boolean ARGV_GLOBALS;
141143
/** --chomp-loop=false */
@@ -264,6 +266,7 @@ public Options(Env env, OptionValues options, LanguageOptions languageOptions) {
264266
USE_TRUFFLE_REGEX = options.get(OptionsCatalog.USE_TRUFFLE_REGEX_KEY);
265267
WARN_TRUFFLE_REGEX_COMPILE_FALLBACK = options.get(OptionsCatalog.WARN_TRUFFLE_REGEX_COMPILE_FALLBACK_KEY);
266268
WARN_TRUFFLE_REGEX_MATCH_FALLBACK = options.get(OptionsCatalog.WARN_TRUFFLE_REGEX_MATCH_FALLBACK_KEY);
269+
TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS = options.get(OptionsCatalog.TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS_KEY);
267270
ARGV_GLOBALS = options.get(OptionsCatalog.ARGV_GLOBALS_KEY);
268271
CHOMP_LOOP = options.get(OptionsCatalog.CHOMP_LOOP_KEY);
269272
GETS_LOOP = options.get(OptionsCatalog.GETS_LOOP_KEY);
@@ -415,6 +418,8 @@ public Object fromDescriptor(OptionDescriptor descriptor) {
415418
return WARN_TRUFFLE_REGEX_COMPILE_FALLBACK;
416419
case "ruby.warn-truffle-regex-match-fallback":
417420
return WARN_TRUFFLE_REGEX_MATCH_FALLBACK;
421+
case "ruby.truffle-regex-ignore-atomic-groups":
422+
return TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS;
418423
case "ruby.argv-globals":
419424
return ARGV_GLOBALS;
420425
case "ruby.chomp-loop":

src/options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ EXPERT:
148148
USE_TRUFFLE_REGEX: [use-truffle-regex, boolean, true, 'Use the Truffle regular expression engine when possible and fallback to Joni otherwise']
149149
WARN_TRUFFLE_REGEX_COMPILE_FALLBACK: [warn-truffle-regex-compile-fallback, boolean, false, 'Warn when a Ruby Regexp could not be compiled to a Truffle Regex and Joni is used instead']
150150
WARN_TRUFFLE_REGEX_MATCH_FALLBACK: [warn-truffle-regex-match-fallback, boolean, false, 'Warn every time Truffle Regex cannot be used for a Regexp match (and instead Joni is used)']
151+
TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS: [truffle-regex-ignore-atomic-groups, boolean, false, 'Treat atomic groups (?>...) as ordinary groups (?:...) with Truffle Regex.']
151152

152153
INTERNAL: # Options for debugging the TruffleRuby implementation
153154
EXPERIMENTAL:

src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public class OptionsCatalog {
8686
public static final OptionKey<Boolean> USE_TRUFFLE_REGEX_KEY = new OptionKey<>(true);
8787
public static final OptionKey<Boolean> WARN_TRUFFLE_REGEX_COMPILE_FALLBACK_KEY = new OptionKey<>(false);
8888
public static final OptionKey<Boolean> WARN_TRUFFLE_REGEX_MATCH_FALLBACK_KEY = new OptionKey<>(false);
89+
public static final OptionKey<Boolean> TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS_KEY = new OptionKey<>(false);
8990
public static final OptionKey<Boolean> ARGV_GLOBALS_KEY = new OptionKey<>(false);
9091
public static final OptionKey<Boolean> CHOMP_LOOP_KEY = new OptionKey<>(false);
9192
public static final OptionKey<Boolean> GETS_LOOP_KEY = new OptionKey<>(false);
@@ -691,6 +692,14 @@ public class OptionsCatalog {
691692
.usageSyntax("")
692693
.build();
693694

695+
public static final OptionDescriptor TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS = OptionDescriptor
696+
.newBuilder(TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS_KEY, "ruby.truffle-regex-ignore-atomic-groups")
697+
.help("Treat atomic groups (?>...) as ordinary groups (?:...) with Truffle Regex.")
698+
.category(OptionCategory.EXPERT)
699+
.stability(OptionStability.EXPERIMENTAL)
700+
.usageSyntax("")
701+
.build();
702+
694703
public static final OptionDescriptor ARGV_GLOBALS = OptionDescriptor
695704
.newBuilder(ARGV_GLOBALS_KEY, "ruby.argv-globals")
696705
.help("Parse options in script argv into global variables (configured by the -s Ruby option)")
@@ -1433,6 +1442,8 @@ public static OptionDescriptor fromName(String name) {
14331442
return WARN_TRUFFLE_REGEX_COMPILE_FALLBACK;
14341443
case "ruby.warn-truffle-regex-match-fallback":
14351444
return WARN_TRUFFLE_REGEX_MATCH_FALLBACK;
1445+
case "ruby.truffle-regex-ignore-atomic-groups":
1446+
return TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS;
14361447
case "ruby.argv-globals":
14371448
return ARGV_GLOBALS;
14381449
case "ruby.chomp-loop":
@@ -1658,6 +1669,7 @@ public static OptionDescriptor[] allDescriptors() {
16581669
USE_TRUFFLE_REGEX,
16591670
WARN_TRUFFLE_REGEX_COMPILE_FALLBACK,
16601671
WARN_TRUFFLE_REGEX_MATCH_FALLBACK,
1672+
TRUFFLE_REGEX_IGNORE_ATOMIC_GROUPS,
16611673
ARGV_GLOBALS,
16621674
CHOMP_LOOP,
16631675
GETS_LOOP,

0 commit comments

Comments
 (0)