Implement Multi type tokens
This commit is contained in:
@@ -5,32 +5,68 @@ import std/strutils
|
||||
import parser/parser
|
||||
import results
|
||||
|
||||
type StringBuilderT = string
|
||||
type StringBuilder = Builder[StringBuilderT]
|
||||
type StringBuilderResult = BuilderResult[StringBuilderT]
|
||||
proc stringConcat(typeInfo: StringBuilderT):
|
||||
(seq[ParserToken], seq[StringBuilderT]) -> seq[StringBuilderT] =
|
||||
|
||||
return proc(xs: seq[ParserToken], ys: seq[StringBuilderT]): seq[StringBuilderT] =
|
||||
return ys & xs.foldl(a & b.tokenStringValue(), typeInfo)
|
||||
|
||||
when isMainModule:
|
||||
let parseHeadingStars = @[
|
||||
manyUntil(ch('*'), ch(' ')),
|
||||
ignore(ch(' '))
|
||||
]
|
||||
|
||||
let parseHeadingText = @[
|
||||
anyUntil(endOfStream),
|
||||
proc createTodoKeywordParser(xs: seq[string]): (Parser -> ParserResult) =
|
||||
choice(xs.map((x: string) => str(x) + ignore(ch(' '))))
|
||||
|
||||
let todoKeywords = @["TODO"]
|
||||
let doneKeywords = @["DONE"]
|
||||
|
||||
let parseTodoKeyword = todoKeywords.createTodoKeywordParser()
|
||||
let parseDoneKeyword = doneKeywords.createTodoKeywordParser()
|
||||
|
||||
let propertiesKeyValueParser = anyUntil(ch(Newlines)) + ignore(ch(NewLines))
|
||||
let propertiesEndParser = str(":PROPERTIES_END:") + ch(Newlines)
|
||||
|
||||
let parseProperties = @[
|
||||
ignore(str(":PROPERTIES:") + ch(Newlines)),
|
||||
manyUntil(
|
||||
propertiesKeyValueParser,
|
||||
propertiesEndParser,
|
||||
),
|
||||
ignore(propertiesEndParser),
|
||||
]
|
||||
|
||||
type StringBuilderT = string
|
||||
type StringBuilder = Builder[StringBuilderT]
|
||||
type StringBuilderResult = BuilderResult[StringBuilderT]
|
||||
proc stringConcat(typeInfo: StringBuilderT):
|
||||
(seq[Token], seq[StringBuilderT]) -> seq[StringBuilderT] =
|
||||
return proc(xs: seq[Token], ys: seq[StringBuilderT]): seq[StringBuilderT] =
|
||||
return ys & xs.foldl(a & b.value, typeInfo)
|
||||
let parseHeadingText = @[
|
||||
anyUntil(newline),
|
||||
ignore(newline),
|
||||
]
|
||||
|
||||
let sampleBuilder = StringBuilderResult
|
||||
.ok(StringBuilder((
|
||||
parser: initParser("**** Some stars"),
|
||||
parser: initParser("""**** TODO Some stars
|
||||
:PROPERTIES:
|
||||
:PROP_NAME: Value
|
||||
:PROP_NAME: Value
|
||||
:PROP_NAME: Value
|
||||
:PROP_NAME: Value
|
||||
:PROPERTIES_END:
|
||||
"""),
|
||||
tree: newSeq[StringBuilderT](),
|
||||
)))
|
||||
.applyParsersSeq(@[
|
||||
(parseHeadingStars, stringConcat("Stars: ")),
|
||||
(parseHeadingText, stringConcat("Text: "))
|
||||
|
||||
(@[optional(parseTodoKeyword)], stringConcat("TODO: ")),
|
||||
(@[optional(parseDoneKeyword)], stringConcat("DONE: ")),
|
||||
|
||||
(parseHeadingText, stringConcat("Text: ")),
|
||||
(parseProperties, stringConcat("Properties: ")),
|
||||
# (@[optional(choice(parseProperties))], stringConcat("Properties: ")),
|
||||
])
|
||||
.foldBuilder(
|
||||
err => &"Error Parsing: {err}",
|
||||
|
||||
Reference in New Issue
Block a user