summaryrefslogtreecommitdiff
path: root/src/paca.mjs
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 /src/paca.mjs
parentsrc/paca.mjs: Support returning multiple options from `performTransition()` (diff)
downloadpaca-d16864711631747538c156a90eda9878588c1cd2.tar.gz
paca-d16864711631747538c156a90eda9878588c1cd2.tar.xz
src/paca.mjs: Rename buildDFA -> toDFA
Diffstat (limited to 'src/paca.mjs')
-rw-r--r--src/paca.mjs17
1 files changed, 10 insertions, 7 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);