Skip to content

Commit

Permalink
Make toForest stack-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
safareli authored and gcanti committed Oct 5, 2020
1 parent f19b679 commit 3a4dd71
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,27 @@ const toTree: (e: DE.DecodeError<string>) => Tree<string> = DE.fold({
Wrap: (error, errors) => make(error, toForest(errors))
})

const toForest: (e: DecodeError) => ReadonlyArray<Tree<string>> = FS.fold(
(value) => [toTree(value)],
(left, right) => toForest(left).concat(toForest(right))
)
const toForest = (e: DecodeError): ReadonlyArray<Tree<string>> => {
const stack = [];
let focus = e;
const res = [];
while (true) {
switch (focus._tag) {
case "Of":
res.push(toTree(focus.value));
if (stack.length === 0) {
return res;
} else {
focus = stack.pop()!;
}
break;
case "Concat":
stack.push(focus.right);
focus = focus.left;
break;
}
}
};

/**
* @since 2.2.7
Expand Down

0 comments on commit 3a4dd71

Please sign in to comment.