summaryrefslogtreecommitdiff
path: root/src/paca.mjs
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2025-07-16 17:56:08 -0300
committerEuAndreh <eu@euandre.org>2025-07-16 17:56:08 -0300
commitf0a759cd9430244bda882856a412ea8ac7468326 (patch)
treee2965669f5a3bcba26249a1c53d2986a092c6a52 /src/paca.mjs
parentCompress character class when compiling NFA. (diff)
downloadpaca-f0a759cd9430244bda882856a412ea8ac7468326.tar.gz
paca-f0a759cd9430244bda882856a412ea8ac7468326.tar.xz
Support searching in the NFA using the metacharacters.
* src/paca.mjs (searchNFAStep): Now instead of just checking if the node has a transition via a character literal directly, we also check (via the `performTransition()` function) if a metacharacter interpretation allows a transition to happen. (intepretMetacharacter): Add function that "executes" the action representation in the "meta" attribute of the object, when present. It is somewhat ad-hoc now, doing checks that implicitly only exist for "." or "class" metacharacters, but OK for now, given the possibilities. (performTransition): Do the fallback to `interpretMetacharacter()`, giving it an empty object when the node doesn't have the "meta" attribute. * tests/paca.mjs (test_{interpretMetacharacter,performTransition): Add routine test implementation.
Diffstat (limited to '')
-rw-r--r--src/paca.mjs26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/paca.mjs b/src/paca.mjs
index 77a7a4b..216dc2e 100644
--- a/src/paca.mjs
+++ b/src/paca.mjs
@@ -761,10 +761,34 @@ const allDirects = (stateID, nodes) =>
).flat(),
)].toSorted();
+const interpretMetacharacter = ({ op, to, matches, ranges }, char) => {
+ if (!op) {
+ return null;
+ }
+
+ if (op === true) {
+ return to;
+ }
+
+ if (matches.has(char)) {
+ return op === "includes" && to;
+ }
+
+ if (inRange(ranges, char)) {
+ return op === "includes" && to;
+ }
+
+ return op === "excludes" && to;
+};
+
+const performTransition = (nodes, char) => id =>
+ nodes[id].transitions[char] ||
+ interpretMetacharacter(nodes[id].meta || {}, char);
+
const searchNFAStep = nodes => (directStates, char) =>
[...new Set(
directStates
- .map(id => nodes[id].transitions[char])
+ .map(performTransition(nodes, char))
.filter(x => !!x)
.map(id => allDirects(id, nodes))
.flat()