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/strformat
|
||||||
import std/collections/sequtils
|
import std/collections/sequtils
|
||||||
import std/sugar
|
import std/sugar
|
||||||
|
import std/collections/tables
|
||||||
import results
|
import results
|
||||||
import fusion/matching
|
import fusion/matching
|
||||||
import fp/maybe
|
import fp/maybe
|
||||||
import print
|
import print
|
||||||
|
import types
|
||||||
|
|
||||||
{.experimental: "caseStmtMacros".}
|
{.experimental: "caseStmtMacros".}
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
ParserState* = ref object
|
ParserState* = ref object
|
||||||
stream: string
|
stream: string
|
||||||
position, lastPosition: int
|
position, lastPosition: int
|
||||||
@@ -35,10 +36,11 @@ type
|
|||||||
parser: Parser
|
parser: Parser
|
||||||
ParserResult* = Result[Parser, ParserError]
|
ParserResult* = Result[Parser, ParserError]
|
||||||
|
|
||||||
# Builder[T]* = ref object
|
Builder*[T] = tuple[
|
||||||
# parser: ParserResult,
|
parser: ParserResult,
|
||||||
# tree: seq[T],
|
tree: seq[T]
|
||||||
# BuilderResult* = Result[Builder, (Builder, string)]
|
]
|
||||||
|
BuilderResult*[T] = Result[Builder[T], (Builder[T], string)]
|
||||||
|
|
||||||
# func prettyPrintError*(err: ParserErrror,)
|
# func prettyPrintError*(err: ParserErrror,)
|
||||||
|
|
||||||
@@ -260,6 +262,21 @@ proc foldTokens[T](
|
|||||||
let err = parserResult.error()
|
let err = parserResult.error()
|
||||||
onError(err)
|
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:
|
when isMainModule:
|
||||||
# let fooParser = initParser("FOO___BAR")
|
# let fooParser = initParser("FOO___BAR")
|
||||||
# .parseSeq(@[
|
# .parseSeq(@[
|
||||||
@@ -275,10 +292,44 @@ when isMainModule:
|
|||||||
# xs => xs.foldl(a & b.value, "")
|
# xs => xs.foldl(a & b.value, "")
|
||||||
# )
|
# )
|
||||||
|
|
||||||
echo initParser("BB").parseSeq(@[
|
# echo initParser("BB").parseSeq(@[
|
||||||
choice(@[ch(Digits), ch('B')]),
|
# 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(@[
|
# .parseSeq(@[
|
||||||
# manyUntil(ch('*'), ch(' ')),
|
# manyUntil(ch('*'), ch(' ')),
|
||||||
|
|||||||
Reference in New Issue
Block a user