Helper methods
This commit is contained in:
61
src/test.nim
61
src/test.nim
@@ -34,6 +34,12 @@ type
|
||||
|
||||
ParserResult* = Result[Parser, (Parser, string)]
|
||||
|
||||
func getOrElse*[T, E](self: Result[T, E], otherwise: T): T =
|
||||
if self.isOk():
|
||||
self.v
|
||||
else:
|
||||
otherwise
|
||||
|
||||
proc indentKey(x: string, count: int): string =
|
||||
var y = x.indent(count)
|
||||
y.delete(0..count - 1)
|
||||
@@ -108,50 +114,15 @@ func str(s: string): (Parser -> ParserResult) {.inline.} =
|
||||
p = p.flatMap(ch(c))
|
||||
return p
|
||||
|
||||
# func str(s1: string): (Parser -> ParserResult) {.inline.} =
|
||||
# return func(parser: Parser): ParserResult =
|
||||
# return parser.flatMap(x => parseSeq(@[ch('F')]))
|
||||
|
||||
# type R = Result[int, string]
|
||||
when isMainModule:
|
||||
let fooParser = initParser("FOO_BAR")
|
||||
.parseSeq(@[
|
||||
str("FOO"),
|
||||
ignore(ch('_')),
|
||||
ch('B'),
|
||||
ch('A'),
|
||||
ch('R'),
|
||||
])
|
||||
|
||||
# echo R.ok 4
|
||||
|
||||
# let i = 3
|
||||
|
||||
# echo $("FOO"[i])
|
||||
|
||||
let fooParser = initParser("FOO_BAR").parseSeq(@[
|
||||
str("FOO"),
|
||||
ignore(ch('_')),
|
||||
ignore(ch('_')),
|
||||
ignore(ch('_')),
|
||||
ignore(ch('_')),
|
||||
ch('B'),
|
||||
ch('A'),
|
||||
ch('R'),
|
||||
])
|
||||
|
||||
echo fooParser
|
||||
|
||||
|
||||
# echo initParser("FUo")
|
||||
# .flatMap(ch('F'))
|
||||
# .flatMap(ch('o'))
|
||||
# .flatMap(ch('o'))
|
||||
# .flatMap((x: Parser) => ch(x, c = 'o'))
|
||||
# .flatMap((x: Parser) => ch(x, c = 'o'))
|
||||
|
||||
# proc parseStars
|
||||
|
||||
# proc parseHeadline(parser: ParserResult[OrgElement]): ParserResult[OrgElement] =
|
||||
# parser
|
||||
# .flatMap(parseA)
|
||||
# # flatMapOptionalToken(
|
||||
# # content =
|
||||
# # )
|
||||
# # flatMapToken(
|
||||
# # content =
|
||||
# # )
|
||||
# # flatMapToken(
|
||||
# # tags
|
||||
# # )
|
||||
echo fooParser
|
||||
|
||||
Reference in New Issue
Block a user