Start parse

This commit is contained in:
Florian Schroedl
2022-08-09 17:57:32 +02:00
parent 99b587813d
commit e0722edb57
2 changed files with 39 additions and 23 deletions

View File

@@ -14,9 +14,6 @@
:PROPERTIES:
:CREATED: [2022-05-16 Mon 22:12]
:END:
:LOGBOOK:
CLOCK: [2022-06-01 Wed 16:47]
:END:
#+begin_src reason
[@bs.module "react"]
@@ -30,6 +27,9 @@ CLOCK: [2022-06-01 Wed 16:47]
- [[https://blog.bitsrc.io/build-our-own-react-redux-using-usereducer-and-usecontext-hooks-a5574b526475][Build Your Own React-Redux Using useReducer and useContext Hooks | by Chidume Nnamdi | Bits and Pieces]]
** TODO Headline content parser
:LOGBOOK:
CLOCK: [2022-08-09 Tue 17:51]
:END:
** TODO Store the line numbers :PARSER:
** TODO Tag parser :PARSER:
** TODO Creating hot reload :DEV_ENVIRONMENT:

View File

@@ -93,7 +93,7 @@ proc tryBuildHeading(builder: OrgBuilderResult): OrgBuilderResult =
(parseContentText, buildHeadlineChildren, true),
(parseHeadlineNewline, buildHeadlineNewline, true),
# (parseHeadlineNewline, buildHeadlineNewline, true),
]
)
@@ -102,7 +102,7 @@ let headingParser = choice(@[
endOfStream,
])
let newlineParser: seq[Parser -> ParserResult] = @[newline + newline]
let newlineParser = @[newline + newline]
let buildNewline = func(tokens: seq[ParserToken], org: OrgBlock): OrgBlock {.closure.} =
OrgBlock(
@@ -111,6 +111,10 @@ let buildNewline = func(tokens: seq[ParserToken], org: OrgBlock): OrgBlock {.clo
proc concatOrgBlock(xs: seq[OrgBlock], ys: seq[OrgBlock]): seq[OrgBlock] = xs & ys
let parseBlockText = @[
anyUntil(choice(@[endOfStream, str("\n")])),
]
let contentParsersSeq: seq[tuple[
parsers: seq[Parser -> ParserResult],
tokenFoldFn: (seq[ParserToken], OrgBlock) -> OrgBlock,
@@ -118,37 +122,48 @@ let contentParsersSeq: seq[tuple[
# concatFn: (seq[OrgBlock], seq[OrgBlock]) -> seq[OrgBlock],
]] = @[
(newlineParser, buildNewline, false),
(parseBlockText, buildNewline, true),
# (newlineParser, buildNewline, false),
]
proc parseHeadlineChildren(builder: OrgBuilder): OrgBuilderResult =
var headingBlock = builder.tree[0]
var builderAcc: OrgBuilderResult = OrgBuilderResult.ok(builder)
echo "Builder " & $builder
# var builderAcc: OrgBuilderResult = OrgBuilderResult.ok(builder)
var builderAcc: OrgBuilderResult = OrgBuilderResult.ok(OrgBuilder(
(
parser: builder.parser,
tree: @[],
)
))
while builderAcc.isOk() and builderAcc.tryParser(headingParser).isErr():
builderAcc = builderAcc
.applyParsersSeqToSeq(
contentParsersSeq
)
# echo builderAcc
builderAcc
# let content = builderAcc.fold(
# (_err) => "",
# (builder: OrgBuilder) => builder.tree,
# )
let content = builderAcc.fold(
(err) => newSeq[OrgBlock](),
(builder: OrgBuilder) => builder.tree,
)
echo "content" & $content
# headingBlock.content = content
# let res = builderAcc.fold(
# (_err) => OrgBuilderResult.err((builder, "Could not parse content")),
# (builder: OrgBuilder) => OrgBuilderResult.ok((
# parser: builder.parser.emptyTokens(),
# tree: @[headingBlock],
# ))
# )
let res = builderAcc.fold(
(err) => OrgBuilderResult.err((builder, "Could not parse content")),
(builder: OrgBuilder) => OrgBuilderResult.ok((
parser: builder.parser.emptyTokens(),
tree: @[headingBlock],
))
)
# res
res
proc makeOrg*(x: string): OrgBuilderResult =
var acc = initOrgBuilder(x)
@@ -181,14 +196,15 @@ proc foldOrg*(x: OrgBuilderResult): string =
# echo acc.unsafeGet().tree[^1]
when isMainModule:
let test1 = """* TODO Level 1
Some text inbetween
let test1 = """* TODO Level 1"""
let test2 = """* TODO Level 1
Foo
** DONE Level 2
Some more content
"""
let acc = makeOrg(test1).foldOrg()
let acc = makeOrg(test2).foldOrg()
echo acc