Helper methods
This commit is contained in:
51
src/test.nim
51
src/test.nim
@@ -34,6 +34,12 @@ type
|
|||||||
|
|
||||||
ParserResult* = Result[Parser, (Parser, string)]
|
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 =
|
proc indentKey(x: string, count: int): string =
|
||||||
var y = x.indent(count)
|
var y = x.indent(count)
|
||||||
y.delete(0..count - 1)
|
y.delete(0..count - 1)
|
||||||
@@ -108,50 +114,15 @@ func str(s: string): (Parser -> ParserResult) {.inline.} =
|
|||||||
p = p.flatMap(ch(c))
|
p = p.flatMap(ch(c))
|
||||||
return p
|
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")
|
||||||
# echo R.ok 4
|
.parseSeq(@[
|
||||||
|
|
||||||
# let i = 3
|
|
||||||
|
|
||||||
# echo $("FOO"[i])
|
|
||||||
|
|
||||||
let fooParser = initParser("FOO_BAR").parseSeq(@[
|
|
||||||
str("FOO"),
|
str("FOO"),
|
||||||
ignore(ch('_')),
|
ignore(ch('_')),
|
||||||
ignore(ch('_')),
|
|
||||||
ignore(ch('_')),
|
|
||||||
ignore(ch('_')),
|
|
||||||
ch('B'),
|
ch('B'),
|
||||||
ch('A'),
|
ch('A'),
|
||||||
ch('R'),
|
ch('R'),
|
||||||
])
|
])
|
||||||
|
|
||||||
echo fooParser
|
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
|
|
||||||
# # )
|
|
||||||
|
|||||||
Reference in New Issue
Block a user