Rename newline and whitespace parsers as they include end of stream

This commit is contained in:
Florian Schroedl
2022-08-26 18:37:01 +02:00
parent 570ffeb100
commit 178aeec565

View File

@@ -220,27 +220,6 @@ proc following*(parserFns: seq[parserFnT]): parserFnT {.inline.} =
return proc(parser: Parser): ParserResult {.closure.} =
parserFns.foldl(a.flatMap(b), parser.ok)
# -- Parsing Aliases
let newlineParser = choice(@[
ch(NewLines),
endOfStream,
])
proc newline*(parser: Parser): ParserResult =
newlineParser(parser)
.mapErr((x: ParserError) => x.setErrorExpectedField("Newline"))
let whitespaceParser = choice(@[
ch(Whitespace),
newlineParser,
])
proc whitespace*(parser: Parser): ParserResult =
whitespaceParser(parser)
.mapErr((x: ParserError) => x.setErrorExpectedField("Whitespace"))
# -- Parsing Helpers
proc between*(startParserFn: parserFnT, stopParserFn: parserFnT): parserFnT -> parserFnT {.inline.} =
## Creates parser function with that matches a `parserFn` between `startParserFn` and `stopParserFn`.
## Ignores the delimiters in the tokens.
@@ -263,6 +242,26 @@ let anyBetweenPair* = proc(parserFn: parserFnT): parserFnT {.closure.} =
## anyBetweenPair(ch('"')) => Matches "abc"
anyBetween(parserFn, parserFn)
# -- Parsing Aliases
const newlineEolExpectedErr = "NewlineEol"
let newlineOrEolParser = choice(@[
ch(NewLines),
endOfStream,
])
proc newlineOrEol*(parser: Parser): ParserResult =
newlineOrEolParser(parser)
.mapErr((x: ParserError) => x.setErrorExpectedField(newlineEolExpectedErr))
const whitespaceEolExpectedErr = "WhitespaceEol"
let whitespaceOrEolParser = choice(@[
ch(Whitespace),
newlineOrEolParser,
])
proc whitespaceOrEol*(parser: Parser): ParserResult =
whitespaceOrEolParser(parser)
.mapErr((x: ParserError) => x.setErrorExpectedField(whitespaceEolExpectedErr))
# -- Tests
when isMainModule:
@@ -334,3 +333,9 @@ when isMainModule:
# anyBetween
assert testParenParser.flatMap(anyBetween(ch('('), ch(')'))).tokensToString() == "123"
assert testQuoteParser.flatMap(anyBetweenPair(testQuote)).tokensToString() == "123"
block parsingAliases:
assert initParserResult("").flatMap(newlineOrEol).isOk() == true
assert initParserResult("abc ").flatMap(str("abc") + newlineOrEol).error().expected == newlineEolExpectedErr
assert initParserResult("").flatMap(whitespaceOrEol).isOk() == true
assert initParserResult("abc ").flatMap(str("abc") + whitespaceOrEol + whitespaceOrEol).tokensToString() == "abc "