tokenizer: possessive quantifiers where the greedy reading matches the same

A possessive repetition of one class at the top level, whose tail in its concatenation is classes
that share no codepoint with it, gives the same matches read as greedy: a shorter run leaves the
tail facing a codepoint it cannot take. Ling-3.0's pre-tokeniser pattern has two such quantifiers.
Everything else stays refused by name.
This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-11-03 19:07:34 +01:00
1 parent 702a0970cc
commit 9c5d4a656c
3 files changed
+88 -7

No files matched your search

+46 -4
View File
@@ -266,6 +266,12 @@ private:
std::vector<size_t> off_; /* byte offset of each codepoint */
size_t i_ = 0;
size_t start_ = 0; /* where the construct being parsed began */
/* Groups open around the construct being parsed: a possessive quantifier is taken only at
* depth 0, where what follows its concatenation is the end of the match. */
int depth_ = 0;
/* Possessive repetitions parse_quant accepted, read as greedy; parse_concat checks that the
* rest of their concatenation cannot take back what they consumed. */
std::vector<int> possessive_;
bool decode_pattern() {
const uint8_t* p = (const uint8_t*)pat_.data();
@@ -382,6 +388,7 @@ private:
if (a < 0) return -1;
items.push_back(a);
}
if (!possessive_tails_ok(items)) return -1;
if (items.empty()) return add(Node{});
if (items.size() == 1) return items[0];
Node n; n.kind = Node::CAT; n.kids = std::move(items);
@@ -448,11 +455,17 @@ private:
else if (!interval_at(i_, &mn, &mx, &len)) return atom;
const bool brace = c == '{';
i_ += len;
bool greedy = true;
bool greedy = true, possessive = false;
if (!at_end() && peek() == '?') { greedy = false; ++i_; }
else if (!at_end() && peek() == '+') {
return failn(brace ? "a quantifier applied to a quantifier (Ruby reads '{n,m}+' as nested repetition)"
: "a possessive quantifier");
if (brace)
return failn("a quantifier applied to a quantifier (Ruby reads '{n,m}+' as nested repetition)");
/* A possessive repetition of one class at the top level is read as greedy, which
* parse_concat proves gives the same matches; anything else has no DFA form. */
if (depth_ > 0 || nodes[atom].kind != Node::SET)
return failn("a possessive quantifier");
possessive = true;
++i_;
}
if (is_quantifier_at(i_)) return failn("a quantifier applied to a quantifier");
if (nodes[atom].kind == Node::ASSERT) return failn("a quantifier on an assertion");
@@ -461,7 +474,35 @@ private:
if (mx < 0 && nullable(nodes, atom))
return failn("unbounded repetition of an expression that can match empty");
Node n; n.kind = Node::REP; n.min = mn; n.max = mx; n.greedy = greedy; n.kids = { atom };
return add(n);
const int r = add(n);
if (possessive) possessive_.push_back(r);
return r;
}
/* A possessive repetition of the class S never gives back, a greedy one gives back only when
* the rest of the match fails after its longest run. At the top level that rest is the tail of
* its concatenation and then the end of the match. When the tail is classes and repetitions of
* classes, none of which can take a codepoint of S, a shorter run leaves the tail facing a
* codepoint of S it cannot take, so no shorter run matches where the longest did not, and the
* greedy reading reports exactly the possessive matches (Ling-3.0's `[^\r\n\p{L}\p{N}]?+\p{L}+`
* and ` ?[^\s\p{L}\p{N}]++[\r\n]*`). Any other tail is refused. */
bool possessive_tails_ok(const std::vector<int>& items) {
for (size_t k = 0; k < items.size(); ++k) {
if (std::find(possessive_.begin(), possessive_.end(), items[k]) == possessive_.end())
continue;
const CpSet& own = sets[nodes[nodes[items[k]].kids[0]].set];
CpSet tail;
for (size_t j = k + 1; j < items.size(); ++j) {
const Node& n = nodes[items[j]];
const Node& m = n.kind == Node::REP ? nodes[n.kids[0]] : n;
if (m.kind != Node::SET)
return fail("a possessive quantifier followed by something other than classes");
tail = set_union(tail, sets[m.set]);
}
if (!set_intersect(own, tail).empty())
return fail("a possessive quantifier whose class the rest of the match can take");
}
return true;
}
int literal(uint32_t c, const Flags& f) {
@@ -494,6 +535,7 @@ private:
int parse_group(const Flags& outer) {
/* `(` already consumed. */
struct Depth { int& d; explicit Depth(int& x) : d(x) { ++d; } ~Depth() { --d; } } depth(depth_);
Flags f = outer;
if (peek() != '?') {
const int r = parse_alt(f);
+3 -1
View File
@@ -18,7 +18,9 @@
* the `onig` crate does.
*
* Anything else is refused at compile time with the construct named: backreferences, atomic
* groups and possessive quantifiers, lookaround wider than one codepoint, unbounded repetition of
* groups, possessive quantifiers except where the greedy reading provably matches the same (one
* class at the top level, followed only by classes it shares no codepoint with, as in Ling-3.0's
* pattern), lookaround wider than one codepoint, unbounded repetition of
* something that can match empty, `(?i)` over a character that folds to several (U+00DF vs "ss"),
* and properties the tables do not carry. There is no fallback matcher; a pattern that does not
* compile fails the vocabulary load.
+39 -2
View File
@@ -463,13 +463,50 @@ TEST(match_at_is_leftmost_first_not_longest) {
/* Every construct outside the dialect is refused by name. The checked phrase is what a reader of
* the load error needs to find the construct in a 200-character pattern. */
/* A possessive repetition is taken only where the greedy reading gives the same matches: one class
* at the top level, its tail classes that cannot take its codepoints. Ling-3.0's pattern is such a
* case, and it splits as its greedy spelling does. */
TEST(possessive_quantifiers_with_a_disjoint_tail_read_as_greedy) {
const char* ling = "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?+\\p{L}+|\\p{N}"
"| ?[^\\s\\p{L}\\p{N}]++[\\r\\n]*|\\s*[\\r\\n]|\\s+(?!\\S)|\\s+";
const char* greedy = "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}"
"| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]|\\s+(?!\\S)|\\s+";
auto a = compile(ling), b = compile(greedy);
REQUIRE(a != nullptr);
REQUIRE(b != nullptr);
const char* texts[] = {
"Hello, world! It's 2026... \"quoted\" spaces\n\nnew lines\r\n--dashes--",
"Die Patientin (67) hat seit 2 h Thoraxschmerzen; RR 158/94 mmHg, SpO\xe2\x82\x82 93 %.",
" ...!!!??? x\t\ty \xe4\xb8\xad\xe6\x96\x87\xe5\xad\x97 ,,; 'LL 'Ve",
};
for (const char* t : texts) {
std::vector<rad::TokSpan> x, y;
a->find_iter(t, x);
b->find_iter(t, y);
REQUIRE_EQ(x.size(), y.size());
for (size_t i = 0; i < x.size(); ++i) {
CHECK_EQ(x[i].begin, y[i].begin);
CHECK_EQ(x[i].end, y[i].end);
}
}
auto plus = compile("a++"), star = compile("[^b]*+b?");
REQUIRE(plus != nullptr);
REQUIRE(star != nullptr);
CHECK_EQ(plus->match_at("aaab", 0), (int64_t)3);
CHECK_EQ(star->match_at("xyzb", 0), (int64_t)4);
}
TEST(constructs_outside_the_dialect_are_refused_by_name) {
const struct { const char* pattern; const char* says; } refused[] = {
{ "(a)\\1", "backreference" },
{ "\\k<n>", "backreference" },
{ "(?>a+)", "atomic group" },
{ "a++", "possessive" },
{ "a*+", "possessive" },
{ "a++a", "possessive" },
{ "[ab]*+b", "possessive" },
{ "(?:a++)", "possessive" },
{ "(a*+)b", "possessive" },
{ "(?:ab)++", "possessive" },
{ "\\s++(?!\\S)", "possessive" },
{ "a{2}{3}", "quantifier applied to a quantifier" },
{ "a{1,2}+", "quantifier applied to a quantifier" },
{ "a**", "quantifier applied to a quantifier" },