Getting parser error
This commit is contained in:
@@ -41,6 +41,7 @@ func tokenizeRawText*(kind: orgInlineBlockKind): string -> OrgInlineBuilderT =
|
|||||||
# ## Blocks
|
# ## Blocks
|
||||||
type OrgBuilderT* = OrgBlock
|
type OrgBuilderT* = OrgBlock
|
||||||
type OrgBuilder* = Builder[OrgBuilderT]
|
type OrgBuilder* = Builder[OrgBuilderT]
|
||||||
|
type OrgBuilderError* = BuilderError[OrgBuilderT]
|
||||||
type OrgBuilderResult* = BuilderResult[OrgBuilderT]
|
type OrgBuilderResult* = BuilderResult[OrgBuilderT]
|
||||||
|
|
||||||
func initOrgBuilder*(content: string): OrgBuilderResult =
|
func initOrgBuilder*(content: string): OrgBuilderResult =
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ import fp/[
|
|||||||
]
|
]
|
||||||
import ./org_types
|
import ./org_types
|
||||||
import ./org_builder_api
|
import ./org_builder_api
|
||||||
import ./org_builder_link.nim
|
import ./org_builder_link
|
||||||
|
import ./org_builder_inline_text
|
||||||
import ../parser/parser
|
import ../parser/parser
|
||||||
import ../utils/fp
|
import ../utils/fp
|
||||||
|
|
||||||
@@ -44,12 +45,9 @@ let listStartParser = manyUntil(space, listTypesParser)
|
|||||||
let listTypeParser = listTypesParser + ignore(space)
|
let listTypeParser = listTypesParser + ignore(space)
|
||||||
let listContentParser = anyUntil(newlineOrEol) + ignore(newlineOrEol)
|
let listContentParser = anyUntil(newlineOrEol) + ignore(newlineOrEol)
|
||||||
|
|
||||||
proc buildListItem(builder: OrgBuilder): OrgBuilderResult =
|
let buildListItem = proc(builder: OrgBuilder): OrgBuilderResult {.closure.} =
|
||||||
|
|
||||||
let (parser, tree) = builder
|
let (parser, tree) = builder
|
||||||
|
|
||||||
# let initialParser = ParserResult.ok(parser)
|
|
||||||
|
|
||||||
let beforeSpaces = ParserResult.ok(parser)
|
let beforeSpaces = ParserResult.ok(parser)
|
||||||
.flatMap(listStartParser)
|
.flatMap(listStartParser)
|
||||||
.map(flattenTokens)
|
.map(flattenTokens)
|
||||||
@@ -64,43 +62,62 @@ proc buildListItem(builder: OrgBuilder): OrgBuilderResult =
|
|||||||
.flatMap(listContentParser)
|
.flatMap(listContentParser)
|
||||||
.map(flattenTokens)
|
.map(flattenTokens)
|
||||||
|
|
||||||
let y = case (beforeSpaces, listType, listContent):
|
case (beforeSpaces, listType, listContent):
|
||||||
of (Some(@a), Some(@b), Some(@c)): c.tokens.tokensToString()
|
of (Some(@space), Some(@symbol), Some(@content)):
|
||||||
else: ""
|
let treeResult = tree &
|
||||||
echo y
|
OrgBlock(
|
||||||
|
kind: orgList,
|
||||||
|
listIndentation: 1,
|
||||||
|
listBulletType: '-',
|
||||||
|
listContent: tryTokenizeInline(content.tokens.tokensToString()).unsafeGet().tree
|
||||||
|
)
|
||||||
|
# echo treeResult
|
||||||
|
|
||||||
builder.ok()
|
OrgBuilderResult.ok(OrgBuilder((
|
||||||
|
parser: content,
|
||||||
|
tree: treeResult,
|
||||||
|
)))
|
||||||
|
else: OrgBuilderResult.err(OrgBuilderError(
|
||||||
|
kind: parserError,
|
||||||
|
parser: listContent.error,
|
||||||
|
tree: tree,
|
||||||
|
))
|
||||||
|
|
||||||
discard initOrgBuilder(" 1. foo bar").map(buildListItem)
|
proc buildParagraph*(
|
||||||
|
builder: OrgBuilder,
|
||||||
|
builderFns: seq[proc (builder: OrgBuilder): OrgBuilderResult],
|
||||||
|
stopAtParserFn = endOfStream,
|
||||||
|
): OrgBuilderResult =
|
||||||
|
|
||||||
|
var builderAcc: OrgBuilderResult = OrgBuilderResult.ok(builder)
|
||||||
|
|
||||||
# proc buildParagraph*(
|
while builderAcc.isOk() and builderAcc.tryParserResult(stopAtParserFn).isErr():
|
||||||
# builder: OrgBuilder,
|
var found = false
|
||||||
# builderFns: seq[tuple[
|
|
||||||
# builderFn: OrgBuilder -> OrgBuilderResult,
|
|
||||||
# concatFn: OrgBuilder -> OrgBuilder,
|
|
||||||
# ]],
|
|
||||||
# stopAtParserFn = endOfStream,
|
|
||||||
# ): OrgBuilderResult =
|
|
||||||
|
|
||||||
# var builderAcc = OrgBuilderResult.ok(builder)
|
for builderFn in builderFns:
|
||||||
|
# let (builderFn) = fn
|
||||||
|
let builderResult: OrgBuilderResult = builderAcc.flatMap(builderFn)
|
||||||
|
|
||||||
# while builderAcc.isOk() and builderAcc.applyParser(stopAtParserFn).isErr():
|
if builderResult.isOk():
|
||||||
# var found = false
|
found = true
|
||||||
|
|
||||||
# for fn in builderFns:
|
builderAcc = builderResult
|
||||||
# let builderResult = builderAcc.flatMap(fn)
|
break
|
||||||
|
|
||||||
# if builderResult.isOk():
|
if not found:
|
||||||
# found = true
|
builderAcc = OrgBuilderResult.err(OrgBuilderError(
|
||||||
|
kind: parserError,
|
||||||
|
tree: builder.tree,
|
||||||
|
))
|
||||||
|
break
|
||||||
|
|
||||||
# builderAcc = builderAcc.map((x: OrgBuilder) => x.concat(builderResult.unsafeGet()))
|
builderAcc
|
||||||
# break
|
|
||||||
|
|
||||||
# when isMainModule:
|
let paragraphBuilders: seq[proc (builder: OrgBuilder): OrgBuilderResult] = @[buildListItem]
|
||||||
# block testParsers:
|
|
||||||
# proc testParser(str: string, parser: parserFnT): string =
|
|
||||||
# initParserResult(str).flatMap(parser).tokensToString()
|
|
||||||
|
|
||||||
# assert testParser("- List item", listItemParser) == "- List item"
|
when isMainModule:
|
||||||
# assert testParser(" - List item", listItemParser) == " - List item"
|
block testParsers:
|
||||||
|
let test = initOrgBuilder("""- List item
|
||||||
|
1. List item
|
||||||
|
random stuff""").flatMap((builder: OrgBuilder) => builder.buildParagraph(paragraphBuilders))
|
||||||
|
echo test.isOk()
|
||||||
|
|||||||
@@ -120,6 +120,7 @@ type
|
|||||||
orgBlockKind* = enum
|
orgBlockKind* = enum
|
||||||
orgHeading
|
orgHeading
|
||||||
orgParagraph
|
orgParagraph
|
||||||
|
orgList
|
||||||
orgNewline
|
orgNewline
|
||||||
OrgBlock* = ref object
|
OrgBlock* = ref object
|
||||||
children*: seq[OrgInlineBlock]
|
children*: seq[OrgInlineBlock]
|
||||||
@@ -132,6 +133,10 @@ type
|
|||||||
content*: seq[OrgBlock]
|
content*: seq[OrgBlock]
|
||||||
of orgParagraph:
|
of orgParagraph:
|
||||||
paragraphContent: seq[OrgInlineBlock]
|
paragraphContent: seq[OrgInlineBlock]
|
||||||
|
of orgList:
|
||||||
|
listIndentation*: int
|
||||||
|
listBulletType*: char
|
||||||
|
listContent*: seq[OrgInlineBlock]
|
||||||
of orgNewline: discard
|
of orgNewline: discard
|
||||||
|
|
||||||
## -- OrgBlock.PrettyPrinters
|
## -- OrgBlock.PrettyPrinters
|
||||||
@@ -159,6 +164,13 @@ func pprint*(x: OrgBlock, indent = 0): string =
|
|||||||
("kind", $x.kind, true),
|
("kind", $x.kind, true),
|
||||||
("paragraphContent", $x.paragraphContent, x.paragraphContent.len != 0),
|
("paragraphContent", $x.paragraphContent, x.paragraphContent.len != 0),
|
||||||
]
|
]
|
||||||
|
of orgList:
|
||||||
|
@[
|
||||||
|
("kind", $x.kind, true),
|
||||||
|
("listContent", $x.listContent, x.listContent.len != 0),
|
||||||
|
("listIndentation", $x.listIndentation, true),
|
||||||
|
("listBulletType", $x.listBulletType, true),
|
||||||
|
]
|
||||||
else:
|
else:
|
||||||
@[
|
@[
|
||||||
("kind", $x.kind, true),
|
("kind", $x.kind, true),
|
||||||
|
|||||||
@@ -14,30 +14,6 @@ import ./parser_api
|
|||||||
|
|
||||||
# -- Builder API
|
# -- Builder API
|
||||||
|
|
||||||
proc tryParser*[T](
|
|
||||||
builder: Builder[T],
|
|
||||||
parser: Parser -> ParserResult,
|
|
||||||
): BuilderResult[T] =
|
|
||||||
## Try out a `parser` on a `builder`
|
|
||||||
## When succesful return the original builder, otherwise return an error
|
|
||||||
ParserResult.ok(Parser(
|
|
||||||
state: builder[0].state,
|
|
||||||
tokens: @[]
|
|
||||||
))
|
|
||||||
.flatMap(parser)
|
|
||||||
.foldTokens(
|
|
||||||
(err: ParserError) => BuilderResult[T].err((builder, "Error")),
|
|
||||||
(newTokens: seq[ParserToken]) => BuilderResult[T].ok(builder),
|
|
||||||
)
|
|
||||||
|
|
||||||
proc tryParser*[T](
|
|
||||||
builder: BuilderResult[T],
|
|
||||||
parser: Parser -> ParserResult,
|
|
||||||
): BuilderResult[T] =
|
|
||||||
## Try out a `parser` on a `builder` result
|
|
||||||
## When succesful return the ok builder, otherwise return an error
|
|
||||||
builder.flatMap((x: Builder[T]) => tryParser(x, parser))
|
|
||||||
|
|
||||||
proc tryTokenize*[T](
|
proc tryTokenize*[T](
|
||||||
builder: Builder[T],
|
builder: Builder[T],
|
||||||
builderFns: seq[tuple[
|
builderFns: seq[tuple[
|
||||||
|
|||||||
@@ -65,6 +65,14 @@ proc tryParser*[T](
|
|||||||
(newTokens: seq[ParserToken]) => BuilderResult[T].ok(builder),
|
(newTokens: seq[ParserToken]) => BuilderResult[T].ok(builder),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
proc tryParserResult*[T](
|
||||||
|
builder: BuilderResult[T],
|
||||||
|
parser: Parser -> ParserResult,
|
||||||
|
): BuilderResult[T] =
|
||||||
|
## Try out a `parser` on a `builder` result
|
||||||
|
## When succesful return the ok builder, otherwise return an error
|
||||||
|
builder.flatMap((x: Builder[T]) => tryParser(x, parser))
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
type TestStringBuilderT* = string
|
type TestStringBuilderT* = string
|
||||||
type TestStringBuilder* = Builder[TestStringBuilderT]
|
type TestStringBuilder* = Builder[TestStringBuilderT]
|
||||||
|
|||||||
Reference in New Issue
Block a user