From f0a759cd9430244bda882856a412ea8ac7468326 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 16 Jul 2025 17:56:08 -0300 Subject: 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. --- src/paca.mjs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src') 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() -- cgit v1.2.3