Abstract builder
This commit is contained in:
67
src/test.nim
67
src/test.nim
@@ -3,15 +3,16 @@ import std/strutils
|
||||
import std/strformat
|
||||
import std/collections/sequtils
|
||||
import std/sugar
|
||||
import std/collections/tables
|
||||
import results
|
||||
import fusion/matching
|
||||
import fp/maybe
|
||||
import print
|
||||
import types
|
||||
|
||||
{.experimental: "caseStmtMacros".}
|
||||
|
||||
type
|
||||
|
||||
ParserState* = ref object
|
||||
stream: string
|
||||
position, lastPosition: int
|
||||
@@ -35,10 +36,11 @@ type
|
||||
parser: Parser
|
||||
ParserResult* = Result[Parser, ParserError]
|
||||
|
||||
# Builder[T]* = ref object
|
||||
# parser: ParserResult,
|
||||
# tree: seq[T],
|
||||
# BuilderResult* = Result[Builder, (Builder, string)]
|
||||
Builder*[T] = tuple[
|
||||
parser: ParserResult,
|
||||
tree: seq[T]
|
||||
]
|
||||
BuilderResult*[T] = Result[Builder[T], (Builder[T], string)]
|
||||
|
||||
# func prettyPrintError*(err: ParserErrror,)
|
||||
|
||||
@@ -260,6 +262,21 @@ proc foldTokens[T](
|
||||
let err = parserResult.error()
|
||||
onError(err)
|
||||
|
||||
|
||||
# proc mapTree[T](builder: BuilderResult[T], fn: Builder[T] -> Builder[T]) =
|
||||
# builder.map(proc(b: Builder) =
|
||||
# (@parser, @builder) := builder
|
||||
# b
|
||||
# )
|
||||
|
||||
# proc applyParserTokens[T](builder: BuilderResult, fn: seq[Token] -> T): BuilderResult =
|
||||
# (@parser, @builder) := builder
|
||||
# parser.foldTokens(
|
||||
# proc(x: ParserError): BuilderResult = err(x),
|
||||
# proc(xs: seq[Token]): BuilderResult = ok(parser, builder.map()),
|
||||
# )
|
||||
|
||||
|
||||
when isMainModule:
|
||||
# let fooParser = initParser("FOO___BAR")
|
||||
# .parseSeq(@[
|
||||
@@ -275,10 +292,44 @@ when isMainModule:
|
||||
# xs => xs.foldl(a & b.value, "")
|
||||
# )
|
||||
|
||||
echo initParser("BB").parseSeq(@[
|
||||
choice(@[ch(Digits), ch('B')]),
|
||||
])
|
||||
# echo initParser("BB").parseSeq(@[
|
||||
# choice(@[ch(Digits), ch('B')]),
|
||||
# ])
|
||||
|
||||
let parseHeadingStars = @[
|
||||
manyUntil(ch('*'), ch(' ')),
|
||||
ignore(ch(' '))
|
||||
]
|
||||
# proc buildHeadingStars(stars: ParserResult, builder: OrgElement): OrgElement =
|
||||
# builder.level = stars.get().len
|
||||
|
||||
# let todoKeywords = @[
|
||||
# "TODO",
|
||||
# ]
|
||||
# let todoDoneKeywords = @[
|
||||
# "DONE",
|
||||
# ]
|
||||
# let parseHeadingTodo = @[
|
||||
|
||||
# ]
|
||||
|
||||
# let buildHeadingStars =
|
||||
type StringBuilder = BuilderResult[string]
|
||||
|
||||
let sampleBuilder: StringBuilder = StringBuilder.ok(Builder((
|
||||
parser: initParser(""),
|
||||
tree: newSeq[string](),
|
||||
)))
|
||||
|
||||
echo sampleBuilder
|
||||
# .mapTree((x: string) => x)
|
||||
|
||||
|
||||
# @[
|
||||
# (parseHeadingStars, buildHeadingStars),
|
||||
# (parseHeadingTodo, buildHeadingTodo),
|
||||
# (parseHeadingContent, buildHeadingContent),
|
||||
# ]
|
||||
|
||||
# .parseSeq(@[
|
||||
# manyUntil(ch('*'), ch(' ')),
|
||||
|
||||
Reference in New Issue
Block a user