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: :PROPERTIES:
:CREATED: [2022-05-16 Mon 22:12] :CREATED: [2022-05-16 Mon 22:12]
:END: :END:
:LOGBOOK:
CLOCK: [2022-06-01 Wed 16:47]
:END:
#+begin_src reason #+begin_src reason
[@bs.module "react"] [@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]] - [[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 ** TODO Headline content parser
:LOGBOOK:
CLOCK: [2022-08-09 Tue 17:51]
:END:
** TODO Store the line numbers :PARSER: ** TODO Store the line numbers :PARSER:
** TODO Tag parser :PARSER: ** TODO Tag parser :PARSER:
** TODO Creating hot reload :DEV_ENVIRONMENT: ** TODO Creating hot reload :DEV_ENVIRONMENT:

View File

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