Rename to anyBetween & add tests
This commit is contained in:
@@ -241,11 +241,26 @@ proc whitespace*(parser: Parser): ParserResult =
|
|||||||
|
|
||||||
# -- Parsing Helpers
|
# -- Parsing Helpers
|
||||||
|
|
||||||
let parseBetweenDelimiter* = proc(start: parserFnT, stop: parserFnT): parserFnT {.closure.} =
|
# let between* = proc(startParserFn: parserFnT, stopParserFn: parserFnT, betweenParserFn: parserFnT): parserFnT {.closure.} =
|
||||||
ignore(start) + anyUntil(stop + whitespace) + ignore(start)
|
# ## 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.} =
|
let anyBetween* = proc(startParserFn: parserFnT, stopParserFn: parserFnT): parserFnT {.closure.} =
|
||||||
parseBetweenDelimiter(delimiterParser, delimiterParser)
|
## 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
|
# -- Tests
|
||||||
|
|
||||||
@@ -304,3 +319,8 @@ when isMainModule:
|
|||||||
|
|
||||||
# parse
|
# parse
|
||||||
assert testAbc1Parser.flatMap(following(@[ch('a'), str("bc"), digit])).tokensToString() == "abc1"
|
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"
|
||||||
|
|||||||
Reference in New Issue
Block a user