Use between function to construct anyBetween

This commit is contained in:
Florian Schroedl
2022-08-26 18:19:20 +02:00
parent 214288c1c3
commit 570ffeb100

View File

@@ -241,19 +241,20 @@ proc whitespace*(parser: Parser): ParserResult =
# -- Parsing Helpers # -- Parsing Helpers
# let between* = proc(startParserFn: parserFnT, stopParserFn: parserFnT, betweenParserFn: parserFnT): parserFnT {.closure.} = proc between*(startParserFn: parserFnT, stopParserFn: parserFnT): parserFnT -> parserFnT {.inline.} =
# ## Creates parser function with that matches `betweenParserFn` between `startParserFn` and `stopParserFn`. ## Creates parser function with that matches a `parserFn` between `startParserFn` and `stopParserFn`.
# ## Ignores the delimiters in the tokens. ## Ignores the delimiters in the tokens.
# ## Example: ## Example:
# ## between(ch('('), ch(')')) => Matches (abc) ## between(ch('('), ch(')'))(str("abc")) => Matches (abc)
# ignore(startParserFn) + anyUntil(stopParserFn) + ignore(stopParserFn) return proc(parserFn: parserFnT): parserFnT {.closure.} =
ignore(startParserFn) + parserFn + ignore(stopParserFn)
let anyBetween* = proc(startParserFn: parserFnT, stopParserFn: parserFnT): parserFnT {.closure.} = proc anyBetween*(startParserFn: parserFnT, stopParserFn: parserFnT): parserFnT {.inline.} =
## Creates parser function with that matches anything between `startParserFn` and `stopParserFn`. ## Creates parser function with that matches anything between `startParserFn` and `stopParserFn`.
## Ignores the delimiters in the tokens. ## Ignores the delimiters in the tokens.
## Example: ## Example:
## anyBetween(ch('('), ch(')')) => Matches (abc) ## anyBetween(ch('('), ch(')')) => Matches (abc)
ignore(startParserFn) + anyUntil(stopParserFn) + ignore(stopParserFn) between(startParserFn, stopParserFn)(anyUntil(stopParserFn))
let anyBetweenPair* = proc(parserFn: parserFnT): parserFnT {.closure.} = let anyBetweenPair* = proc(parserFn: parserFnT): parserFnT {.closure.} =
## Creates parser function with that matches anything between matching `parserFns` ## Creates parser function with that matches anything between matching `parserFns`
@@ -321,6 +322,15 @@ when isMainModule:
assert testAbc1Parser.flatMap(following(@[ch('a'), str("bc"), digit])).tokensToString() == "abc1" assert testAbc1Parser.flatMap(following(@[ch('a'), str("bc"), digit])).tokensToString() == "abc1"
block parsingHelpers: block parsingHelpers:
let testParenParser = initParserResult("(123)")
let testQuoteParser = initParserResult("\"123\"")
let testQuote = ch('"')
let testBetweenParen = between(ch('('), ch(')'))
# between
assert testParenParser.flatMap(testBetweenParen(str("123"))).tokensToString() == "123"
# anyBetween # anyBetween
assert initParserResult("(123)").flatMap(anyBetween(ch('('), ch(')'))).tokensToString() == "123" assert testParenParser.flatMap(anyBetween(ch('('), ch(')'))).tokensToString() == "123"
assert initParserResult("\"123\"").flatMap(anyBetweenPair(ch('"'))).tokensToString() == "123" assert testQuoteParser.flatMap(anyBetweenPair(testQuote)).tokensToString() == "123"