Rename to anyBetween & add tests

This commit is contained in:
Florian Schroedl
2022-08-26 17:56:54 +02:00
parent c3389abb59
commit 214288c1c3

View File

@@ -241,11 +241,26 @@ proc whitespace*(parser: Parser): ParserResult =
# -- Parsing Helpers
let parseBetweenDelimiter* = proc(start: parserFnT, stop: parserFnT): parserFnT {.closure.} =
ignore(start) + anyUntil(stop + whitespace) + ignore(start)
# let between* = proc(startParserFn: parserFnT, stopParserFn: parserFnT, betweenParserFn: parserFnT): parserFnT {.closure.} =
# ## Creates parser function with that matches `betweenParserFn` between `startParserFn` and `stopParserFn`.
# ## Ignores the delimiters in the tokens.
# ## Example:
# ## between(ch('('), ch(')')) => Matches (abc)
# ignore(startParserFn) + anyUntil(stopParserFn) + ignore(stopParserFn)
let parseBetweenPair* = proc(delimiterParser: parserFnT): parserFnT {.closure.} =
parseBetweenDelimiter(delimiterParser, delimiterParser)
let anyBetween* = proc(startParserFn: parserFnT, stopParserFn: parserFnT): parserFnT {.closure.} =
## Creates parser function with that matches anything between `startParserFn` and `stopParserFn`.
## Ignores the delimiters in the tokens.
## Example:
## anyBetween(ch('('), ch(')')) => Matches (abc)
ignore(startParserFn) + anyUntil(stopParserFn) + ignore(stopParserFn)
let anyBetweenPair* = proc(parserFn: parserFnT): parserFnT {.closure.} =
## Creates parser function with that matches anything between matching `parserFns`
## Ignores the delimiters in the tokens.
## Example:
## anyBetweenPair(ch('"')) => Matches "abc"
anyBetween(parserFn, parserFn)
# -- Tests
@@ -304,3 +319,8 @@ when isMainModule:
# parse
assert testAbc1Parser.flatMap(following(@[ch('a'), str("bc"), digit])).tokensToString() == "abc1"
block parsingHelpers:
# anyBetween
assert initParserResult("(123)").flatMap(anyBetween(ch('('), ch(')'))).tokensToString() == "123"
assert initParserResult("\"123\"").flatMap(anyBetweenPair(ch('"'))).tokensToString() == "123"