Finish builder
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import std/sugar
|
import std/sugar
|
||||||
import std/collections/sequtils
|
import std/collections/sequtils
|
||||||
|
import std/strformat
|
||||||
|
import std/strutils
|
||||||
import results
|
import results
|
||||||
import fusion/matching
|
import fusion/matching
|
||||||
import ./org_types
|
import ./org_types
|
||||||
@@ -40,7 +42,7 @@ type OrgBuilderT* = OrgElement
|
|||||||
type OrgBuilder* = Builder[OrgBuilderT]
|
type OrgBuilder* = Builder[OrgBuilderT]
|
||||||
type OrgBuilderResult* = BuilderResult[OrgBuilderT]
|
type OrgBuilderResult* = BuilderResult[OrgBuilderT]
|
||||||
|
|
||||||
proc makeBoldTokens*(content: string): OrgBuilderT =
|
proc makeBoldToken*(content: string): OrgBuilderT =
|
||||||
OrgBuilderT(
|
OrgBuilderT(
|
||||||
kind: orgBoldText,
|
kind: orgBoldText,
|
||||||
content: content,
|
content: content,
|
||||||
@@ -50,7 +52,7 @@ proc makeOrgToken*(orgTokenFn: string -> OrgBuilderT): (seq[ParserToken], seq[Or
|
|||||||
return proc(parserTokens: seq[ParserToken], builderTokens: seq[OrgBuilderT]): seq[OrgBuilderT] =
|
return proc(parserTokens: seq[ParserToken], builderTokens: seq[OrgBuilderT]): seq[OrgBuilderT] =
|
||||||
return builderTokens & parserTokens.foldl(a & b.tokenStringValue(), "").orgTokenFn()
|
return builderTokens & parserTokens.foldl(a & b.tokenStringValue(), "").orgTokenFn()
|
||||||
|
|
||||||
proc textParser[T](
|
proc parseText[T](
|
||||||
builder: Builder[T],
|
builder: Builder[T],
|
||||||
builderFns: seq[tuple[
|
builderFns: seq[tuple[
|
||||||
parserFn: Parser -> ParserResult,
|
parserFn: Parser -> ParserResult,
|
||||||
@@ -60,10 +62,10 @@ proc textParser[T](
|
|||||||
): BuilderResult[T] =
|
): BuilderResult[T] =
|
||||||
let (parser, tree) = builder
|
let (parser, tree) = builder
|
||||||
|
|
||||||
var parserAcc: ParserResult = parser.ok()
|
var parserAcc: ParserResult = ParserResult.ok(parser)
|
||||||
var builderAcc: Builder = builder
|
var builderAcc: Builder[T] = builder
|
||||||
|
|
||||||
while parser.isOk() and parserAcc.flatMap(stopFn).isErr():
|
while parserAcc.isOk() and parserAcc.flatMap(stopFn).isErr():
|
||||||
# Empty the parser tokens as we want to seperate them for the next parser in the sequence
|
# Empty the parser tokens as we want to seperate them for the next parser in the sequence
|
||||||
let emptyParser = parserAcc.map(emptyTokens)
|
let emptyParser = parserAcc.map(emptyTokens)
|
||||||
|
|
||||||
@@ -87,3 +89,23 @@ proc textParser[T](
|
|||||||
|
|
||||||
if not found:
|
if not found:
|
||||||
parserAcc = parserAcc.flatMap(anyCh)
|
parserAcc = parserAcc.flatMap(anyCh)
|
||||||
|
|
||||||
|
BuilderResult[T].ok(builderAcc)
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
let foo = OrgBuilderResult.ok(OrgBuilder((
|
||||||
|
parser: initParser("Just *some* text"),
|
||||||
|
tree: newSeq[OrgBuilderT](),
|
||||||
|
)))
|
||||||
|
.flatMap((builder: OrgBuilder) => parseText(
|
||||||
|
builder = builder,
|
||||||
|
builderFns = @[
|
||||||
|
(boldParser, makeOrgToken(makeBoldToken)),
|
||||||
|
]
|
||||||
|
))
|
||||||
|
# .foldBuilder(
|
||||||
|
# err => &"Error Parsing: {err}",
|
||||||
|
# xs => $xs
|
||||||
|
# )
|
||||||
|
|
||||||
|
echo foo
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import std/strformat
|
||||||
|
|
||||||
type
|
type
|
||||||
orgElementKind* = enum
|
orgElementKind* = enum
|
||||||
orgRawText,
|
orgRawText,
|
||||||
@@ -11,3 +13,15 @@ type
|
|||||||
of orgRawText: discard
|
of orgRawText: discard
|
||||||
of orgText: discard
|
of orgText: discard
|
||||||
of orgBoldText: discard
|
of orgBoldText: discard
|
||||||
|
|
||||||
|
proc `$`*(x: orgElementKind): string =
|
||||||
|
case x:
|
||||||
|
of orgRawText: "Text (Raw)"
|
||||||
|
of orgText: "Text"
|
||||||
|
of orgBoldText: "Text (Bold)"
|
||||||
|
|
||||||
|
proc `$`*(x: OrgElement): string =
|
||||||
|
&"""OrgElement(
|
||||||
|
content: {x.content}
|
||||||
|
kind: {x.kind}
|
||||||
|
)"""
|
||||||
|
|||||||
Reference in New Issue
Block a user