summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2025-07-15 14:11:18 -0300
committerEuAndreh <eu@euandre.org>2025-07-15 14:15:24 -0300
commit36689acb6bb577b27e546b9d166a40f503a47eac (patch)
treef6d2f1d4f07876c03fb77dc7ab3556111a7a103b
parentSupport tokenizing character class expressions [a-z] (diff)
downloadpaca-36689acb6bb577b27e546b9d166a40f503a47eac.tar.gz
paca-36689acb6bb577b27e546b9d166a40f503a47eac.tar.xz
Use `shouldConcat()` in decision of `escapingStateSte()`
* src/paca.mjs (escapingStateStep): Use `shouldConcat()` instead of only checking if we're on the last char. We abuse it a bit by passing `null` as the first argument, since it is being escaped. (nonConcatOperators, shouldConcat): Hoist the definition of both above `escapingStateStep()`, so that they're defined before being used. * tests/paca.mjs (test_shouldConcat): Add test case where `null` is explicitly passed as the first argument.
-rw-r--r--src/paca.mjs22
-rw-r--r--tests/paca.mjs4
2 files changed, 15 insertions, 11 deletions
diff --git a/src/paca.mjs b/src/paca.mjs
index b92bcdc..598c751 100644
--- a/src/paca.mjs
+++ b/src/paca.mjs
@@ -15,6 +15,15 @@ const ConcatStep = {
CLASS: "class",
};
+const nonConcatOperators = new Set(["*", "+", "?", "|", ")"]);
+
+const shouldConcat = (char, next) =>
+ next !== undefined &&
+ char !== "(" &&
+ char !== "|" &&
+ char !== "{" &&
+ !nonConcatOperators.has(next);
+
const numFromDigits = digits =>
digits.length === 0
? -1
@@ -23,7 +32,7 @@ const numFromDigits = digits =>
const escapingStateStep = ({ out, _state, context }, char, _index, next) => ({
out: out.concat(
char,
- next !== undefined ? {operator: "concat"} : [],
+ shouldConcat(null, next) ? [{ operator: "concat" }] : [],
),
state: ConcatStep.ACCEPTING,
context,
@@ -265,15 +274,6 @@ const TRANSITION_FNS = {
const stateTransitionOperators = new Set(Object.keys(TRANSITION_FNS));
-const nonConcatOperators = new Set(["*", "+", "?", "|", ")"]);
-
-const shouldConcat = (char, next) =>
- next !== undefined &&
- char !== "(" &&
- char !== "|" &&
- char !== "{" &&
- !nonConcatOperators.has(next);
-
const isOperator = char =>
nonConcatOperators.has(char) || char == "(";
@@ -302,7 +302,7 @@ const tokenizeRegexStep = chars => ({ out, state, context }, char, index) => {
return {
out: out.concat(
op,
- shouldConcat(char, next) ? [{operator: "concat"}] : [],
+ shouldConcat(char, next) ? [{ operator: "concat" }] : [],
),
state,
context,
diff --git a/tests/paca.mjs b/tests/paca.mjs
index e9c3a6b..2c44f9e 100644
--- a/tests/paca.mjs
+++ b/tests/paca.mjs
@@ -511,6 +511,10 @@ const test_shouldConcat = t => {
t.assertEq(shouldConcat("a", "|"), false);
t.assertEq(shouldConcat("a", ")"), false);
});
+
+ t.testing("only consider the `next` char", () => {
+ t.assertEq(shouldConcat(null, "\\"), true);
+ });
};
const test_isOperator = t => {