Trying to parse list type
This commit is contained in:
@@ -39,15 +39,15 @@ func tokenizeRawText*(kind: orgInlineBlockKind): string -> OrgInlineBuilderT =
|
||||
)
|
||||
|
||||
# ## Blocks
|
||||
# type OrgBuilderT* = OrgBlock
|
||||
# type OrgBuilder* = Builder[OrgBuilderT]
|
||||
# type OrgBuilderResult* = BuilderResult[OrgBuilderT]
|
||||
type OrgBuilderT* = OrgBlock
|
||||
type OrgBuilder* = Builder[OrgBuilderT]
|
||||
type OrgBuilderResult* = BuilderResult[OrgBuilderT]
|
||||
|
||||
# func initOrgBuilder*(content: string): OrgBuilderResult =
|
||||
# return OrgBuilderResult.ok(OrgBuilder((
|
||||
# parser: initParser(content),
|
||||
# tree: newSeq[OrgBuilderT](),
|
||||
# )))
|
||||
func initOrgBuilder*(content: string): OrgBuilderResult =
|
||||
return OrgBuilderResult.ok(OrgBuilder((
|
||||
parser: initParser(content),
|
||||
tree: newSeq[OrgBuilderT](),
|
||||
)))
|
||||
|
||||
# # proc orgBuilderConcat*(typeInfo: OrgBuilderT, concatFn: (ParserToken, OrgBuilderT) -> OrgBuilderT):
|
||||
# # (seq[ParserToken], OrgBuilderT) -> OrgBuilderT =
|
||||
|
||||
@@ -77,24 +77,24 @@ let buildStars = func(tokens: seq[ParserToken], org: OrgBlock): OrgBlock {.closu
|
||||
# let buildHeadlineNewline = func(tokens: seq[ParserToken], org: OrgBlock): OrgBlock {.closure.}=
|
||||
# org
|
||||
|
||||
# proc tryBuildHeading(builder: OrgBuilderResult): OrgBuilderResult =
|
||||
# # echo builder
|
||||
# builder
|
||||
# .applyParsersSeqToSingle(
|
||||
# OrgBlock(kind: orgHeading),
|
||||
# @[
|
||||
# (parseHeadingStars, buildStars, false),
|
||||
proc tryBuildHeading(builder: OrgBuilderResult): OrgBuilderResult =
|
||||
# echo builder
|
||||
builder
|
||||
.applyParsersSeqToSingle(
|
||||
OrgBlock(kind: orgHeading),
|
||||
@[
|
||||
(parseHeadingStars, buildStars, false),
|
||||
|
||||
# (@[optional(parseTodoKeyword)], buildTodo, true),
|
||||
# (@[optional(parseDoneKeyword)], buildTodo, true),
|
||||
(@[optional(parseTodoKeyword)], buildTodo, true),
|
||||
(@[optional(parseDoneKeyword)], buildTodo, true),
|
||||
|
||||
# (parseHeadingText, buildHeadlineContent, false),
|
||||
(parseHeadingText, buildHeadlineContent, false),
|
||||
|
||||
# # (parseContentText, buildHeadlineChildren, true),
|
||||
# (parseContentText, buildHeadlineChildren, true),
|
||||
|
||||
# (parseHeadlineNewline, buildHeadlineNewline, false),
|
||||
# ]
|
||||
# )
|
||||
(parseHeadlineNewline, buildHeadlineNewline, false),
|
||||
]
|
||||
)
|
||||
|
||||
# let headingParser = choice(@[
|
||||
# newline + ch('*'),
|
||||
|
||||
99
src_v2/org/org_builder_paragraph.nim
Normal file
99
src_v2/org/org_builder_paragraph.nim
Normal file
@@ -0,0 +1,99 @@
|
||||
import std/[
|
||||
collections/sequtils,
|
||||
sugar,
|
||||
]
|
||||
import fp/[
|
||||
option,
|
||||
resultM,
|
||||
]
|
||||
import ./org_types
|
||||
import ./org_builder_api
|
||||
import ./org_builder_link.nim
|
||||
import ../parser/parser
|
||||
|
||||
# let lineTokenizer = (builder: OrgInlineBuilder) => tryTokenize(
|
||||
# builder = builder,
|
||||
# builderFns = styledTextTokenizers,
|
||||
# defaultTokenizerFn = tryTokenizeRawText,
|
||||
# )
|
||||
|
||||
# OrgDocucment
|
||||
|
||||
# -> Seq [Builder -> Builder]
|
||||
|
||||
# One of OrgBlock
|
||||
|
||||
# @[
|
||||
|
||||
# OrgBuilderResult -> OrgBuilderResult
|
||||
# ]
|
||||
# with default OrgBuilder -> OrgBuilder
|
||||
# until Eol
|
||||
|
||||
let listTypesParser = choice(@[
|
||||
ch('-'),
|
||||
ch('+'),
|
||||
choice(@[letter, digit]) + choice(@[ch('.'), ch(')')]),
|
||||
])
|
||||
|
||||
let listStartParser = manyUntil(space, listTypesParser)
|
||||
let listTypeParser = listTypesParser + ignore(space)
|
||||
let listContentParser = anyUntil(newlineOrEol) + ignore(newlineOrEol)
|
||||
|
||||
proc buildListItem(builder: OrgBuilder): OrgBuilderResult =
|
||||
|
||||
let (parser, tree) = builder
|
||||
|
||||
# let initialParser = ParserResult.ok(parser)
|
||||
|
||||
let beforeSpaces = ParserResult.ok(parser)
|
||||
.flatMap(listStartParser)
|
||||
.map(flattenTokens)
|
||||
|
||||
let listType = beforeSpaces
|
||||
.map(emptyTokens)
|
||||
.flatMap(listTypeParser)
|
||||
.map(flattenTokens)
|
||||
|
||||
let listContent = listType
|
||||
.map(emptyTokens)
|
||||
.flatMap(listContentParser)
|
||||
.map(flattenTokens)
|
||||
|
||||
echo listContent
|
||||
|
||||
builder.ok()
|
||||
|
||||
discard initOrgBuilder(" 1. foo bar").map(buildListItem)
|
||||
|
||||
|
||||
# proc buildParagraph*(
|
||||
# builder: OrgBuilder,
|
||||
# builderFns: seq[tuple[
|
||||
# builderFn: OrgBuilder -> OrgBuilderResult,
|
||||
# concatFn: OrgBuilder -> OrgBuilder,
|
||||
# ]],
|
||||
# stopAtParserFn = endOfStream,
|
||||
# ): OrgBuilderResult =
|
||||
|
||||
# var builderAcc = OrgBuilderResult.ok(builder)
|
||||
|
||||
# while builderAcc.isOk() and builderAcc.applyParser(stopAtParserFn).isErr():
|
||||
# var found = false
|
||||
|
||||
# for fn in builderFns:
|
||||
# let builderResult = builderAcc.flatMap(fn)
|
||||
|
||||
# if builderResult.isOk():
|
||||
# found = true
|
||||
|
||||
# builderAcc = builderAcc.map((x: OrgBuilder) => x.concat(builderResult.unsafeGet()))
|
||||
# break
|
||||
|
||||
# when isMainModule:
|
||||
# block testParsers:
|
||||
# proc testParser(str: string, parser: parserFnT): string =
|
||||
# initParserResult(str).flatMap(parser).tokensToString()
|
||||
|
||||
# assert testParser("- List item", listItemParser) == "- List item"
|
||||
# assert testParser(" - List item", listItemParser) == " - List item"
|
||||
@@ -122,6 +122,11 @@ func flattenTokens*(parser: Parser): Parser =
|
||||
tokens: @[token]
|
||||
)
|
||||
|
||||
func flattenTokensResult*(parser: Parser): ParserResult =
|
||||
parser
|
||||
.flattenTokens()
|
||||
.ok()
|
||||
|
||||
func emptyTokens*(parser: Parser): Parser =
|
||||
## Empty `parser` tokens.
|
||||
Parser(
|
||||
|
||||
Reference in New Issue
Block a user