This commit is contained in:
Florian Schroedl
2022-01-20 17:00:00 +01:00
parent aebd570a37
commit 7cfa4ae31d

View File

@@ -2,6 +2,7 @@ import std/options
import std/strutils import std/strutils
import std/strformat import std/strformat
import std/collections/sequtils import std/collections/sequtils
import std/sugar
import results import results
import fusion/matching import fusion/matching
import fp/maybe import fp/maybe
@@ -35,26 +36,26 @@ type
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)
y y
proc `$`*(x: Token): string = proc `$`*(x: Token): string =
&"""Token( &"""Token(
value: {x.value}, value: {x.value},
)""" )"""
proc `$`*(x: ParserState): string = proc `$`*(x: ParserState): string =
&"""ParserState( &"""ParserState(
stream: {x.stream}, stream: "{x.stream}",
position: {x.position}, position: {x.position},
lastPosition: {x.lastPosition}, lastPosition: {x.lastPosition},
)""" )"""
proc `$`*(x: Parser): string = proc `$`*(x: Parser): string =
&"""Parser( &"""Parser(
state: {indentKey($x.state, 4)}, state: {indentKey($x.state, 2)},
tokens: {x.tokens} tokens: {indentKey($x.tokens, 2)},
)""" )"""
proc initParser(str: string): ParserResult = proc initParser(str: string): ParserResult =
Parser( Parser(
@@ -79,13 +80,17 @@ proc ch(parser: Parser, c: char): ParserResult =
tokens: parser.tokens & Token(value: c) tokens: parser.tokens & Token(value: c)
).ok() ).ok()
else: else:
err((parser, "Foo")) err((parser, &"Expected {c}"))
# type R = Result[int, string] # type R = Result[int, string]
# echo R.ok 4 # echo R.ok 4
echo initParser("Foo") echo initParser("Foo")
.flatMap((x: Parser) => ch(x, c = 'F'))
.flatMap((x: Parser) => ch(x, c = 'o'))
.flatMap((x: Parser) => ch(x, c = 'o'))
# proc parseStars # proc parseStars