Always take initial builder on the first parser/builder [*]

[*] Fixes bug where the builder would just copy the attributes of the
previous element.
This commit is contained in:
Florian Schroedl
2022-05-04 17:00:00 +02:00
parent 178bbed09d
commit 31844ed68d

View File

@@ -168,6 +168,7 @@ proc applyParsersToSingle*[T](
tokenFoldFn: (seq[ParserToken], T) -> T, tokenFoldFn: (seq[ParserToken], T) -> T,
optional = false, optional = false,
initT: T, initT: T,
isFirst: bool,
): BuilderResult[T] = ): BuilderResult[T] =
# Apply the current parsing functions and convert to text tokens wrapped in ParserResult # Apply the current parsing functions and convert to text tokens wrapped in ParserResult
let newParser = ParserResult.ok(Parser( let newParser = ParserResult.ok(Parser(
@@ -188,6 +189,7 @@ proc applyParsersToSingle*[T](
newParser.unsafeGet(), newParser.unsafeGet(),
builder.tree builder.tree
.last() .last()
.filter(x => not isFirst)
.orElse(just(initT)) .orElse(just(initT))
.map((x: T) => @[tokenFoldFn(newTokens, x)]) .map((x: T) => @[tokenFoldFn(newTokens, x)])
.getOrElse(newSeq[T]()) .getOrElse(newSeq[T]())
@@ -211,7 +213,8 @@ proc applyParsersSeqToSingle*[T](
b.parsers, b.parsers,
b.tokenFoldFn, b.tokenFoldFn,
b.ignoreEmpty, b.ignoreEmpty,
initT initT,
isFirst = a == builderResult
)), )),
builderResult builderResult
) )