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

Add additional test cases for RegExp modifiers #4321

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Daniel Kwan. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
author: Daniel Kwan
description: >
Adding dotAll (`s`) modifier should not affect alternatives outside.
info: |
Runtime Semantics: CompileAtom
The syntax-directed operation CompileAtom takes arguments direction (forward or backward) and modifiers (a Modifiers Record) and returns a Matcher.

Atom :: `(` `?` RegularExpressionFlags `:` Disjunction `)`
1. Let addModifiers be the source text matched by RegularExpressionFlags.
2. Let removeModifiers be the empty String.
3. Let newModifiers be UpdateModifiers(modifiers, CodePointsToString(addModifiers), removeModifiers).
4. Return CompileSubpattern of Disjunction with arguments direction and newModifiers.

UpdateModifiers ( modifiers, add, remove )
The abstract operation UpdateModifiers takes arguments modifiers (a Modifiers Record), add (a String), and remove (a String) and returns a Modifiers. It performs the following steps when called:

1. Let dotAll be modifiers.[[DotAll]].
2. Let ignoreCase be modifiers.[[IgnoreCase]].
3. Let multiline be modifiers.[[Multiline]].
4. If add contains "s", set dotAll to true.
5. If add contains "i", set ignoreCase to true.
6. If add contains "m", set multiline to true.
7. If remove contains "s", set dotAll to false.
8. If remove contains "i", set ignoreCase to false.
9. If remove contains "m", set multiline to false.
10. Return the Modifiers Record { [[DotAll]]: dotAll, [[IgnoreCase]]: ignoreCase, [[Multiline]]: multiline }.

esid: sec-compileatom
features: [regexp-modifiers]
---*/

var re1 = /a.a|b.b|(?s:c.c)|d.d|e.e/;
assert(!re1.test("a\na"), "Alternative `a.a` should not match newline");
assert(!re1.test("b\nb"), "Alternative `b.b` should not match newline");
assert(re1.test("c\nc"), "Alternative `(?s:c.c)` should match newline in modified group");
assert(!re1.test("d\nd"), "Alternative `d.d` should not match newline");
assert(!re1.test("e\ne"), "Alternative `e.e` should not match newline");

var re2 = /(a.a)|(?:b.b)|(?s:c.c)|(?:d.d)|(e.e)/;
assert(!re2.test("a\na"), "Alternative `(a.a)` should not match newline");
assert(!re2.test("b\nb"), "Alternative `(?:b.b)` should not match newline");
assert(re2.test("c\nc"), "Alternative `(?s:c.c)` should match newline in modified group");
assert(!re2.test("d\nd"), "Alternative `(?:d.d)` should not match newline");
assert(!re2.test("e\ne"), "Alternative `(e.e)` should not match newline");
14 changes: 14 additions & 0 deletions test/built-ins/RegExp/regexp-modifiers/add-dotAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,17 @@ assert(re4.test("\u2028"), "Pattern character '.' should match line terminators
assert(re4.test("\u2029"), "Pattern character '.' should match line terminators in modified group");
assert(re4.test("\uD800"), "Pattern character '.' should match non-line terminators in modified group");
assert(re4.test("\uDFFF"), "Pattern character '.' should match non-line terminators in modified group");

var re5 = /a.(?s:b.b).c/;
assert(re5.test("a,b,b,c"), "Pattern character '.' should match non-line terminators in modified group");
assert(re5.test("a,b\nb,c"), "Pattern character '.' should match line terminators in modified group");
assert(!re5.test("a\nb\nb,c"), "Pattern character '.' should not match line terminators outside modified group");
assert(!re5.test("a,b\nb\nc"), "Pattern character '.' should not match line terminators outside modified group");
assert(!re5.test("a\nb\nb\nc"), "Pattern character '.' should not match line terminators outside modified group");

var re6 = new RegExp("a.(?s:b.b).c");
assert(re6.test("a,b,b,c"), "Pattern character '.' should match non-line terminators in modified group");
assert(re6.test("a,b\nb,c"), "Pattern character '.' should match line terminators in modified group");
assert(!re6.test("a\nb\nb,c"), "Pattern character '.' should not match line terminators outside modified group");
assert(!re6.test("a,b\nb\nc"), "Pattern character '.' should not match line terminators outside modified group");
assert(!re6.test("a\nb\nb\nc"), "Pattern character '.' should not match line terminators outside modified group");
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Daniel Kwan. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
author: Daniel Kwan
description: >
Adding ignoreCase (`i`) modifier should not affect alternatives outside.
info: |
Runtime Semantics: CompileAtom
The syntax-directed operation CompileAtom takes arguments direction (forward or backward) and modifiers (a Modifiers Record) and returns a Matcher.

Atom :: `(` `?` RegularExpressionFlags `:` Disjunction `)`
1. Let addModifiers be the source text matched by RegularExpressionFlags.
2. Let removeModifiers be the empty String.
3. Let newModifiers be UpdateModifiers(modifiers, CodePointsToString(addModifiers), removeModifiers).
4. Return CompileSubpattern of Disjunction with arguments direction and newModifiers.

UpdateModifiers ( modifiers, add, remove )
The abstract operation UpdateModifiers takes arguments modifiers (a Modifiers Record), add (a String), and remove (a String) and returns a Modifiers. It performs the following steps when called:

1. Let dotAll be modifiers.[[DotAll]].
2. Let ignoreCase be modifiers.[[IgnoreCase]].
3. Let multiline be modifiers.[[Multiline]].
4. If add contains "s", set dotAll to true.
5. If add contains "i", set ignoreCase to true.
6. If add contains "m", set multiline to true.
7. If remove contains "s", set dotAll to false.
8. If remove contains "i", set ignoreCase to false.
9. If remove contains "m", set multiline to false.
10. Return the Modifiers Record { [[DotAll]]: dotAll, [[IgnoreCase]]: ignoreCase, [[Multiline]]: multiline }.

esid: sec-compileatom
features: [regexp-modifiers]
---*/

var re1 = /a|b|(?i:c)|d|e/;
assert(!re1.test("A"), "Alternative `a` should not match `A`");
assert(!re1.test("B"), "Alternative `b` should not match `B`");
assert(re1.test("C"), "Alternative `(?i:c)` should match `C` in modified group");
assert(!re1.test("D"), "Alternative `d` should not match `D`");
assert(!re1.test("E"), "Alternative `e` should not match `E`");

var re2 = /(a)|(?:b)|(?i:c)|(?:d)|(e)/;
assert(!re2.test("A"), "Alternative `(a)` should not match `A`");
assert(!re2.test("B"), "Alternative `(?:b)` should not match `B`");
assert(re2.test("C"), "Alternative `(?i:c)` should match `C` in modified group");
assert(!re2.test("D"), "Alternative `(?:d)` should not match `D`");
assert(!re2.test("E"), "Alternative `(e)` should not match `E`");
24 changes: 24 additions & 0 deletions test/built-ins/RegExp/regexp-modifiers/add-ignoreCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,27 @@ var re2 = new RegExp("(?i:a)b");
assert(!re2.test("AB"), "b should not match B in AB");
assert(re2.test("Ab"), "a should match A in AB");
assert(re2.test("ab"), "should match AB");

var re3 = /b(?i:a)/;
assert(!re3.test("BA"), "b should not match B in BA");
assert(re3.test("bA"), "a should match A in BA");
assert(re3.test("ba"), "should match BA");

var re4 = new RegExp("b(?i:a)");
assert(!re4.test("BA"), "b should not match B in BA");
assert(re4.test("bA"), "a should match A in BA");
assert(re4.test("ba"), "should match BA");

var re5 = /a(?i:b)c/;
assert(re5.test("abc"), "b should match b in abc");
assert(re5.test("aBc"), "B should match b in abc");
assert(!re5.test("ABc"), "A should not match a in abc");
assert(!re5.test("aBC"), "C should not match c in abc");
assert(!re5.test("ABC"), "should not match abc");

var re6 = new RegExp("a(?i:b)c");
assert(re6.test("abc"), "b should match b in abc");
assert(re6.test("aBc"), "B should match b in abc");
assert(!re6.test("ABc"), "A should not match a in abc");
assert(!re6.test("aBC"), "C should not match c in abc");
assert(!re6.test("ABC"), "should not match abc");
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Daniel Kwan. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
author: Daniel Kwan
description: >
Adding multiline (`m`) modifier should not affect alternatives outside.
info: |
Runtime Semantics: CompileAtom
The syntax-directed operation CompileAtom takes arguments direction (forward or backward) and modifiers (a Modifiers Record) and returns a Matcher.

Atom :: `(` `?` RegularExpressionFlags `:` Disjunction `)`
1. Let addModifiers be the source text matched by RegularExpressionFlags.
2. Let removeModifiers be the empty String.
3. Let newModifiers be UpdateModifiers(modifiers, CodePointsToString(addModifiers), removeModifiers).
4. Return CompileSubpattern of Disjunction with arguments direction and newModifiers.

UpdateModifiers ( modifiers, add, remove )
The abstract operation UpdateModifiers takes arguments modifiers (a Modifiers Record), add (a String), and remove (a String) and returns a Modifiers. It performs the following steps when called:

1. Let dotAll be modifiers.[[DotAll]].
2. Let ignoreCase be modifiers.[[IgnoreCase]].
3. Let multiline be modifiers.[[Multiline]].
4. If add contains "s", set dotAll to true.
5. If add contains "i", set ignoreCase to true.
6. If add contains "m", set multiline to true.
7. If remove contains "s", set dotAll to false.
8. If remove contains "i", set ignoreCase to false.
9. If remove contains "m", set multiline to false.
10. Return the Modifiers Record { [[DotAll]]: dotAll, [[IgnoreCase]]: ignoreCase, [[Multiline]]: multiline }.

esid: sec-compileatom
features: [regexp-modifiers]
---*/

var re1 = /^a$|^b$|(?m:^c$)|^d$|^e$/;
assert(!re1.test("\na\n"), "Alternative `^a$` should not match newline");
assert(!re1.test("\nb\n"), "Alternative `^b$` should not match newline");
assert(re1.test("\nc\n"), "Alternative `(?m:^c$)` should match newline in modified group");
assert(!re1.test("\nd\n"), "Alternative `^d$` should not match newline");
assert(!re1.test("\ne\n"), "Alternative `^e$` should not match newline");

var re2 = /(^a$)|(?:^b$)|(?m:^c$)|(?:^d$)|(^e$)/;
assert(!re2.test("\na\n"), "Alternative `(^a$)` should not match newline");
assert(!re2.test("\nb\n"), "Alternative `(?:^b$)` should not match newline");
assert(re2.test("\nc\n"), "Alternative `(?m:^c$)` should match newline in modified group");
assert(!re2.test("\nd\n"), "Alternative `(?:^d$)` should not match newline");
assert(!re2.test("\ne\n"), "Alternative `(^e$)` should not match newline");
12 changes: 12 additions & 0 deletions test/built-ins/RegExp/regexp-modifiers/add-multiline.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ assert(re3.test("es\ns"), "$ should match newline in modified group");

var re4 = new RegExp("(?m-:es$)");
assert(re4.test("es\ns"), "$ should match newline in modified group");

var re5 = /^a\n(?m:^b$)\nc$/;
assert(re5.test("a\nb\nc"), "^ and $ should match newline in modified group");
assert(!re5.test("\na\nb\nc"), "^ should not match newline outside modified group");
assert(!re5.test("a\nb\nc\n"), "$ should not match newline outside modified group");
assert(!re5.test("\na\nb\nc\n"), "^ and $ should not match newline outside modified group");

var re6 = new RegExp("^a\\n(?m:^b$)\\nc$");
assert(re6.test("a\nb\nc"), "^ and $ should match newline in modified group");
assert(!re6.test("\na\nb\nc"), "^ should not match newline outside modified group");
assert(!re6.test("a\nb\nc\n"), "$ should not match newline outside modified group");
assert(!re6.test("\na\nb\nc\n"), "^ and $ should not match newline outside modified group");
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 Daniel Kwan. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
author: Daniel Kwan
description: >
Nesting dotAll (`s`) modifier should not affect alternatives outside.
info: |
Runtime Semantics: CompileAtom
The syntax-directed operation CompileAtom takes arguments direction (forward or backward) and modifiers (a Modifiers Record) and returns a Matcher.

Atom :: `(` `?` RegularExpressionFlags `:` Disjunction `)`
1. Let addModifiers be the source text matched by RegularExpressionFlags.
2. Let removeModifiers be the empty String.
3. Let newModifiers be UpdateModifiers(modifiers, CodePointsToString(addModifiers), removeModifiers).
4. Return CompileSubpattern of Disjunction with arguments direction and newModifiers.

Atom :: `(` `?` RegularExpressionFlags `-` RegularExpressionFlags `:` Disjunction `)`
1. Let addModifiers be the source text matched by the first RegularExpressionFlags.
2. Let removeModifiers be the source text matched by the second RegularExpressionFlags.
3. Let newModifiers be UpdateModifiers(modifiers, CodePointsToString(addModifiers), CodePointsToString(removeModifiers)).
4. Return CompileSubpattern of Disjunction with arguments direction and newModifiers.

UpdateModifiers ( modifiers, add, remove )
The abstract operation UpdateModifiers takes arguments modifiers (a Modifiers Record), add (a String), and remove (a String) and returns a Modifiers. It performs the following steps when called:

1. Let dotAll be modifiers.[[DotAll]].
2. Let ignoreCase be modifiers.[[IgnoreCase]].
3. Let multiline be modifiers.[[Multiline]].
4. If add contains "s", set dotAll to true.
5. If add contains "i", set ignoreCase to true.
6. If add contains "m", set multiline to true.
7. If remove contains "s", set dotAll to false.
8. If remove contains "i", set ignoreCase to false.
9. If remove contains "m", set multiline to false.
10. Return the Modifiers Record { [[DotAll]]: dotAll, [[IgnoreCase]]: ignoreCase, [[Multiline]]: multiline }.

esid: sec-compileatom
features: [regexp-modifiers]
---*/

var re1 = /a.a|(?-s:b.b|(?s:c.c)|d.d|(?-s:e.e)|f.f)|g.g|(?s:h.h)|k.k/s;
assert(re1.test("a\na"), "`a.a` should match newline");
assert(!re1.test("b\nb"), "`b.b` should not match newline");
assert(re1.test("c\nc"), "`c.c` should match newline");
assert(!re1.test("d\nd"), "`d.d` should not match newline");
assert(!re1.test("e\ne"), "`e.e` should not match newline");
assert(!re1.test("f\nf"), "`f.f` should not match newline");
assert(re1.test("g\ng"), "`g.g` should match newline");
assert(re1.test("h\nh"), "`h.h` should match newline");
assert(re1.test("k\nk"), "`k.k` should match newline");

var re2 = /a.a|(?s:b.b|(?-s:c.c)|d.d|(?s:e.e)|f.f)|g.g|(?-s:h.h)|k.k/;
assert(!re2.test("a\na"), "`a.a` should not match newline");
assert(re2.test("b\nb"), "`b.b` should match newline");
assert(!re2.test("c\nc"), "`c.c` should not match newline");
assert(re2.test("d\nd"), "`d.d` should match newline");
assert(re2.test("e\ne"), "`e.e` should match newline");
assert(re2.test("f\nf"), "`f.f` should match newline");
assert(!re2.test("g\ng"), "`g.g` should not match newline");
assert(!re2.test("h\nh"), "`h.h` should not match newline");
assert(!re2.test("k\nk"), "`k.k` should not match newline");
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 Daniel Kwan. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
author: Daniel Kwan
description: >
Nesting ignoreCase (`i`) modifier should not affect alternatives outside.
info: |
Runtime Semantics: CompileAtom
The syntax-directed operation CompileAtom takes arguments direction (forward or backward) and modifiers (a Modifiers Record) and returns a Matcher.

Atom :: `(` `?` RegularExpressionFlags `:` Disjunction `)`
1. Let addModifiers be the source text matched by RegularExpressionFlags.
2. Let removeModifiers be the empty String.
3. Let newModifiers be UpdateModifiers(modifiers, CodePointsToString(addModifiers), removeModifiers).
4. Return CompileSubpattern of Disjunction with arguments direction and newModifiers.

Atom :: `(` `?` RegularExpressionFlags `-` RegularExpressionFlags `:` Disjunction `)`
1. Let addModifiers be the source text matched by the first RegularExpressionFlags.
2. Let removeModifiers be the source text matched by the second RegularExpressionFlags.
3. Let newModifiers be UpdateModifiers(modifiers, CodePointsToString(addModifiers), CodePointsToString(removeModifiers)).
4. Return CompileSubpattern of Disjunction with arguments direction and newModifiers.

UpdateModifiers ( modifiers, add, remove )
The abstract operation UpdateModifiers takes arguments modifiers (a Modifiers Record), add (a String), and remove (a String) and returns a Modifiers. It performs the following steps when called:

1. Let dotAll be modifiers.[[DotAll]].
2. Let ignoreCase be modifiers.[[IgnoreCase]].
3. Let multiline be modifiers.[[Multiline]].
4. If add contains "s", set dotAll to true.
5. If add contains "i", set ignoreCase to true.
6. If add contains "m", set multiline to true.
7. If remove contains "s", set dotAll to false.
8. If remove contains "i", set ignoreCase to false.
9. If remove contains "m", set multiline to false.
10. Return the Modifiers Record { [[DotAll]]: dotAll, [[IgnoreCase]]: ignoreCase, [[Multiline]]: multiline }.

esid: sec-compileatom
features: [regexp-modifiers]
---*/

var re1 = /a|(?-i:b|(?i:c)|d|(?-i:e)|f)|g|(?i:h)|k/i;
assert(re1.test("A"), "`a` should match `A`");
assert(!re1.test("B"), "`b` should not match `B`");
assert(re1.test("C"), "`c` should match `C`");
assert(!re1.test("D"), "`d` should not match `D`");
assert(!re1.test("E"), "`e` should not match `E`");
assert(!re1.test("F"), "`f` should not match `F`");
assert(re1.test("G"), "`g` should match `G`");
assert(re1.test("H"), "`h` should match `H`");
assert(re1.test("K"), "`k` should match `K`");

var re2 = /a|(?i:b|(?-i:c)|d|(?i:e)|f)|g|(?-i:h)|k/;
assert(!re2.test("A"), "`a` should not match `A`");
assert(re2.test("B"), "`b` should match `B`");
assert(!re2.test("C"), "`c` should not match `C`");
assert(re2.test("D"), "`d` should match `D`");
assert(re2.test("E"), "`e` should match `E`");
assert(re2.test("F"), "`f` should match `F`");
assert(!re2.test("G"), "`g` should not match `G`");
assert(!re2.test("H"), "`h` should not match `H`");
assert(!re2.test("K"), "`k` should not match `K`");
Loading
Loading