summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/paca.mjs31
1 files changed, 17 insertions, 14 deletions
diff --git a/src/paca.mjs b/src/paca.mjs
index a0e607b..ce973dd 100644
--- a/src/paca.mjs
+++ b/src/paca.mjs
@@ -74,9 +74,15 @@ const PRECEDENCE = {
};
const shouldPush = (stack, token) =>
- stack.length === 0 ||
- token.operator === "(" ||
- stack[0].operator === "(";
+ stack.length === 0 ||
+ token.operator === "(" ||
+ (stack[0].operator === "(" && token.operator !== ")");
+
+const findLowerPrecedenceItem = (stack, token) =>
+ stack.findIndex(el =>
+ el.operator === "(" ||
+ PRECEDENCE[el.operator] < PRECEDENCE[token.operator],
+ );
const toPostfixStep = ({ out, stack }, token, _index, tokens) => {
if (typeof token === "string") {
@@ -109,11 +115,7 @@ const toPostfixStep = ({ out, stack }, token, _index, tokens) => {
};
}
- const idx = stack.findIndex(el =>
- el.operator === "(" ||
- PRECEDENCE[el.operator] < PRECEDENCE[token.operator],
- );
-
+ const idx = findLowerPrecedenceItem(stack, token);
if (idx < 0) {
return {
out: out.concat(stack),
@@ -245,8 +247,7 @@ const zeroOrMore = nfa => {
[nfa.end]: {
...nfa.nodes[nfa.end],
direct: nfa.nodes[nfa.end].direct.concat([
- nfa.start,
- endID,
+ startID,
]),
},
[startID]: {
@@ -340,7 +341,7 @@ const searchNFAStep = nodes => (directStates, char) =>
.filter(x => !!x)
.map(id => allDirects(id, nodes))
.flat()
- )];
+ )].toSorted();
const searchNFAFn = (nfa, string) =>
explode(string).reduce(
@@ -374,8 +375,10 @@ const collectTransitions = (nfaNodes, states) => {
return mapValues(
groupedTransitions,
entry =>
- entry.map(([_, id]) =>
- allDirects(id, nfaNodes)).flat(),
+ [
+ ...new Set(entry.map(([_, id]) =>
+ allDirects(id, nfaNodes)).flat()),
+ ].toSorted(),
);
};
@@ -383,7 +386,7 @@ const computePending = (pending, transitions) =>
[...new Set(
pending.slice(1).concat(transitions)
.map(nodeID))
- ].map(unNodeID);
+ ].toSorted().map(unNodeID);
const buildDFANodes = (end, nfaNodes, pending, dfaNodes) => {
if (pending.length === 0) {