Skip to content

Commit

Permalink
Add tests for nullable quantifiers in RegExps (tc39#4185)
Browse files Browse the repository at this point in the history
* Add tests for nullable quantifiers in RegExps

The JavaScript semantics for a quantifier matching the empty
string are different from other regex languages.
This adds a test that documents this JavaScript-specific
behavior.

This is part of my work at the SYSTEMF lab at EPFL.

* Update nullable-quantifier.js
  • Loading branch information
Aurele-Barriere authored Aug 5, 2024
1 parent feb400c commit ea37a19
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/built-ins/RegExp/nullable-quantifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2024 Aurèle Barrière. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-runtime-semantics-repeatmatcher-abstract-operation
description: JavaScript nullable quantifiers have special semantics, optional iterations are not allowed to match the empty string. Point 2.b below shows that after each optional (min=0) iteration of a quantifier, if the iteration matched the empty string (y.[[EndIndex]] = x.[[EndIndex]]), then the iteration is discarded. In particular, for (a?b??)* on "ab", it is possible to do two iterations of the star, one matching "a" and the other matching "b".
info: |
RepeatMatcher ( m, min, max, greedy, x, c, parenIndex, parenCount )
2. Let d be a new MatcherContinuation with parameters (y) that captures m, min, max, greedy, x, c, parenIndex, and parenCount and performs the following steps when called:
b. If min = 0 and y.[[EndIndex]] = x.[[EndIndex]], return failure.
author: Aurèle Barrière
---*/

let input = "ab";
let regex = /(a?b??)*/;
let match = regex.exec(input);
let expected = "ab";

assert.sameValue(match[0], expected, "The regex is expected to match the whole string");

0 comments on commit ea37a19

Please sign in to comment.