summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2025-07-20 09:54:38 -0300
committerEuAndreh <eu@euandre.org>2025-07-20 09:54:38 -0300
commitd16864711631747538c156a90eda9878588c1cd2 (patch)
tree9bee02b0c9f1d2d668d9cfeb210509555f179389
parentsrc/paca.mjs: Support returning multiple options from `performTransition()` (diff)
downloadpaca-d16864711631747538c156a90eda9878588c1cd2.tar.gz
paca-d16864711631747538c156a90eda9878588c1cd2.tar.xz
src/paca.mjs: Rename buildDFA -> toDFA
-rw-r--r--src/paca.mjs17
-rw-r--r--tests/paca.mjs30
2 files changed, 25 insertions, 22 deletions
diff --git a/src/paca.mjs b/src/paca.mjs
index f1e3e50..38100ff 100644
--- a/src/paca.mjs
+++ b/src/paca.mjs
@@ -874,7 +874,7 @@ const computePending = (pending, transitions) =>
.map(nodeID))
].toSorted().map(unNodeID);
-const buildDFANodes = (end, nfaNodes, pending, dfaNodes) => {
+const toDFANodes = (end, nfaNodes, pending, dfaNodes) => {
if (pending.length === 0) {
return dfaNodes;
}
@@ -882,12 +882,12 @@ const buildDFANodes = (end, nfaNodes, pending, dfaNodes) => {
const states = pending[0];
const stateID = nodeID(states);
if (!!dfaNodes[stateID]) {
- return buildDFANodes(end, nfaNodes, pending.slice(1), dfaNodes);
+ return toDFANodes(end, nfaNodes, pending.slice(1), dfaNodes);
}
const transitions = collectTransitions(nfaNodes, states);
const newPending = computePending(pending, Object.values(transitions));
- return buildDFANodes(end, nfaNodes, newPending, {
+ return toDFANodes(end, nfaNodes, newPending, {
...dfaNodes,
[stateID]: {
end: states.includes(end),
@@ -896,11 +896,11 @@ const buildDFANodes = (end, nfaNodes, pending, dfaNodes) => {
});
};
-const buildDFA = nfa => {
+const toDFA = nfa => {
const start = allDirects(nfa.start, nfa.nodes);
return {
start: nodeID(start),
- nodes: buildDFANodes(nfa.end, nfa.nodes, [start], {}),
+ nodes: toDFANodes(nfa.end, nfa.nodes, [start], {}),
};
};
@@ -913,11 +913,14 @@ const searchDFAFn = (dfa, string) =>
const searchDFA = (dfa, string) =>
!!dfa.nodes[searchDFAFn(dfa, string)]?.end
+const parse = regex =>
+ toPostfix(tokenizeRegex(explode(regex)));
+
const compileNFA = regex =>
- buildNFA(toPostfix(tokenizeRegex(explode(regex))));
+ buildNFA(parse(regex));
const compileDFA = regex =>
- buildDFA(compileNFA(regex));
+ toDFA(compileNFA(regex));
export const compile = regex => {
const nfa = compileDFA(regex);
diff --git a/tests/paca.mjs b/tests/paca.mjs
index 6913b9e..3469129 100644
--- a/tests/paca.mjs
+++ b/tests/paca.mjs
@@ -50,8 +50,8 @@ import {
unNodeID,
collectTransitions,
computePending,
- buildDFANodes,
- buildDFA,
+ toDFANodes,
+ toDFA,
searchDFAStep,
searchDFAFn,
searchDFA,
@@ -3121,11 +3121,11 @@ const test_computePending = t => {
});
};
-const test_buildDFANodes = t => {
- t.start("buildDFANodes()");
+const test_toDFANodes = t => {
+ t.start("toDFANodes()");
t.testing("empty values", () => {
- t.assertEq(buildDFANodes(null, null, [], "RET"), "RET");
+ t.assertEq(toDFANodes(null, null, [], "RET"), "RET");
});
t.testing("ignore states that were already built", () => {
@@ -3133,7 +3133,7 @@ const test_buildDFANodes = t => {
"1-2-3": true,
};
const states = [[1, 2, 3], [3, 2, 1]];
- t.assertEq(buildDFANodes(null, null, states, nodes), nodes);
+ t.assertEq(toDFANodes(null, null, states, nodes), nodes);
});
t.testing("recursively build the DFA", () => {
@@ -3181,14 +3181,14 @@ const test_buildDFANodes = t => {
},
};
t.assertEq(
- buildDFANodes(end, nfaNodes, pending, dfaNodes),
+ toDFANodes(end, nfaNodes, pending, dfaNodes),
expected,
);
});
};
-const test_buildDFA = t => {
- t.start("buildDFA()");
+const test_toDFA = t => {
+ t.start("toDFA()");
t.testing("minimal NFA", () => {
const nfa = {
@@ -3214,7 +3214,7 @@ const test_buildDFA = t => {
},
},
};
- t.assertEq(buildDFA(nfa), expected);
+ t.assertEq(toDFA(nfa), expected);
});
t.testing("sample regex NFA", () => {
@@ -3282,7 +3282,7 @@ const test_buildDFA = t => {
},
};
t.assertEq(nfa, compileNFA("(a|b)*c"));
- t.assertEq(buildDFA(nfa), expected);
+ t.assertEq(toDFA(nfa), expected);
});
};
@@ -3335,7 +3335,7 @@ const test_searchDFAFn = t => {
t.testing("on composed sample regex", () => {
const regex = "(a|b)*c";
- const dfa = buildDFA(
+ const dfa = toDFA(
buildNFA(toPostfix(tokenizeRegex(explode(regex)))),
);
t.assertEq(searchDFAFn(dfa, "a"), "1-3-9");
@@ -3354,7 +3354,7 @@ const test_searchDFA = t => {
t.testing("on composed sample regex", () => {
const regex = "(a|b)*c";
- const dfa = buildDFA(
+ const dfa = toDFA(
buildNFA(toPostfix(tokenizeRegex(explode(regex)))),
);
t.assertEq(searchDFA(dfa, "a"), false);
@@ -3663,8 +3663,8 @@ runTests([
test_unNodeID,
test_collectTransitions,
test_computePending,
- test_buildDFANodes,
- test_buildDFA,
+ test_toDFANodes,
+ test_toDFA,
test_searchDFAStep,
test_searchDFAFn,
test_searchDFA,