2016-08-15 19:30:49 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
|
|
|
# (c) Copyright 2015 Felix Krause
|
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-08-30 20:15:29 +00:00
|
|
|
type
|
2016-09-12 16:04:26 +00:00
|
|
|
FastParseLevelKind = enum
|
|
|
|
fplUnknown, fplSequence, fplMapKey, fplMapValue, fplSinglePairKey,
|
2016-09-12 19:38:56 +00:00
|
|
|
fplSinglePairValue, fplDocument
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-09-12 16:04:26 +00:00
|
|
|
FastParseLevel = object
|
|
|
|
kind: FastParseLevelKind
|
|
|
|
indentation: int
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
ParserContext = ref object of YamlStream
|
2016-08-15 19:30:49 +00:00
|
|
|
p: YamlParser
|
2016-09-12 16:04:26 +00:00
|
|
|
lex: YamlLexer
|
2016-08-15 19:30:49 +00:00
|
|
|
storedState: proc(s: YamlStream, e: var YamlStreamEvent): bool
|
|
|
|
atSequenceItem: bool
|
2016-08-17 18:04:59 +00:00
|
|
|
flowdepth: int
|
2016-08-17 20:21:34 +00:00
|
|
|
ancestry: seq[FastParseLevel]
|
|
|
|
level: FastParseLevel
|
|
|
|
tag: TagId
|
|
|
|
anchor: AnchorId
|
|
|
|
shorthands: Table[string, string]
|
|
|
|
nextAnchorId: AnchorId
|
|
|
|
newlines: int
|
2016-09-12 19:38:56 +00:00
|
|
|
explicitFlowKey: bool
|
2016-08-15 19:30:49 +00:00
|
|
|
|
|
|
|
LevelEndResult = enum
|
|
|
|
lerNothing, lerOne, lerAdditionalMapEnd
|
|
|
|
|
|
|
|
proc newYamlParser*(tagLib: TagLibrary = initExtendedTagLibrary(),
|
|
|
|
callback: WarningCallback = nil): YamlParser =
|
|
|
|
new(result)
|
|
|
|
result.tagLib = tagLib
|
|
|
|
result.callback = callback
|
|
|
|
|
|
|
|
template debug(message: string) {.dirty.} =
|
|
|
|
when defined(yamlDebug):
|
|
|
|
try: styledWriteLine(stdout, fgBlue, message)
|
|
|
|
except IOError: discard
|
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc generateError(c: ParserContext, message: string):
|
2016-08-15 19:30:49 +00:00
|
|
|
ref YamlParserError {.raises: [].} =
|
|
|
|
result = newException(YamlParserError, message)
|
2016-09-12 16:04:26 +00:00
|
|
|
(result.line, result.column) = c.lex.curStartPos
|
|
|
|
result.lineContent = c.lex.getTokenLine()
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc callCallback(c: ParserContext, msg: string) {.raises: [YamlParserError].} =
|
2016-08-15 19:30:49 +00:00
|
|
|
try:
|
2016-08-17 20:21:34 +00:00
|
|
|
if not isNil(c.p.callback):
|
2016-09-12 19:38:56 +00:00
|
|
|
c.p.callback(c.lex.curStartPos.line, c.lex.curStartPos.column,
|
|
|
|
c.lex.getTokenLine(), msg)
|
2016-08-15 19:30:49 +00:00
|
|
|
except:
|
|
|
|
var e = newException(YamlParserError,
|
|
|
|
"Warning callback raised exception: " & getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
|
|
|
|
|
|
|
proc initLevel(k: FastParseLevelKind): FastParseLevel {.raises: [], inline.} =
|
|
|
|
FastParseLevel(kind: k, indentation: UnknownIndentation)
|
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc emptyScalar(c: ParserContext): YamlStreamEvent {.raises: [], inline.} =
|
|
|
|
result = scalarEvent("", c.tag, c.anchor)
|
|
|
|
c.tag = yTagQuestionMark
|
|
|
|
c.anchor = yAnchorNone
|
|
|
|
|
|
|
|
proc currentScalar(c: ParserContext): YamlStreamEvent {.raises: [], inline.} =
|
|
|
|
result = YamlStreamEvent(kind: yamlScalar, scalarTag: c.tag,
|
2016-09-12 16:04:26 +00:00
|
|
|
scalarAnchor: c.anchor)
|
|
|
|
shallowCopy(result.scalarContent, c.lex.buf)
|
2016-09-12 19:38:56 +00:00
|
|
|
c.lex.buf = cast[string not nil](newStringOfCap(256))
|
2016-08-17 20:21:34 +00:00
|
|
|
c.tag = yTagQuestionMark
|
|
|
|
c.anchor = yAnchorNone
|
|
|
|
|
|
|
|
proc objectStart(c: ParserContext, k: static[YamlStreamEventKind],
|
2016-08-15 19:30:49 +00:00
|
|
|
single: bool = false): YamlStreamEvent {.raises: [].} =
|
2016-08-17 20:21:34 +00:00
|
|
|
yAssert(c.level.kind == fplUnknown)
|
2016-08-15 19:30:49 +00:00
|
|
|
when k == yamlStartMap:
|
2016-08-17 20:21:34 +00:00
|
|
|
result = startMapEvent(c.tag, c.anchor)
|
2016-08-15 19:30:49 +00:00
|
|
|
if single:
|
|
|
|
debug("started single-pair map at " &
|
2016-09-12 19:38:56 +00:00
|
|
|
(if c.level.indentation == UnknownIndentation:
|
|
|
|
$c.lex.indentation else: $c.level.indentation))
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplSinglePairKey
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
|
|
|
debug("started map at " &
|
2016-09-12 19:38:56 +00:00
|
|
|
(if c.level.indentation == UnknownIndentation:
|
|
|
|
$c.lex.indentation else: $c.level.indentation))
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplMapKey
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
2016-08-17 20:21:34 +00:00
|
|
|
result = startSeqEvent(c.tag, c.anchor)
|
2016-08-15 19:30:49 +00:00
|
|
|
debug("started sequence at " &
|
2016-09-12 19:38:56 +00:00
|
|
|
(if c.level.indentation == UnknownIndentation: $c.lex.indentation else:
|
2016-08-17 20:21:34 +00:00
|
|
|
$c.level.indentation))
|
|
|
|
c.level.kind = fplSequence
|
|
|
|
c.tag = yTagQuestionMark
|
|
|
|
c.anchor = yAnchorNone
|
|
|
|
if c.level.indentation == UnknownIndentation:
|
2016-09-12 19:38:56 +00:00
|
|
|
c.level.indentation = c.lex.indentation
|
2016-08-17 20:21:34 +00:00
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
|
|
|
|
|
|
|
proc initDocValues(c: ParserContext) {.raises: [].} =
|
|
|
|
c.shorthands = initTable[string, string]()
|
|
|
|
c.p.anchors = initTable[string, AnchorId]()
|
|
|
|
c.shorthands["!"] = "!"
|
|
|
|
c.shorthands["!!"] = "tag:yaml.org,2002:"
|
|
|
|
c.nextAnchorId = 0.AnchorId
|
|
|
|
c.level = initLevel(fplUnknown)
|
|
|
|
c.tag = yTagQuestionMark
|
|
|
|
c.anchor = yAnchorNone
|
|
|
|
c.ancestry.add(FastParseLevel(kind: fplDocument, indentation: -1))
|
|
|
|
|
2016-09-13 10:01:21 +00:00
|
|
|
proc advance(c: ParserContext) {.inline.} =
|
|
|
|
try: c.lex.next()
|
|
|
|
except YamlLexerError:
|
|
|
|
let e = (ref YamlLexerError)(getCurrentException())
|
|
|
|
let pe = newException(YamlParserError, e.msg)
|
|
|
|
pe.line = e.line
|
|
|
|
pe.column = e.column
|
|
|
|
pe.lineContent = e.lineContent
|
|
|
|
raise pe
|
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc handleAnchor(c: ParserContext) {.raises: [YamlParserError].} =
|
|
|
|
if c.level.kind != fplUnknown: raise c.generateError("Unexpected token")
|
|
|
|
if c.anchor != yAnchorNone:
|
|
|
|
raise c.generateError("Only one anchor is allowed per node")
|
|
|
|
c.anchor = c.nextAnchorId
|
2016-09-12 16:04:26 +00:00
|
|
|
c.p.anchors[c.lex.buf] = c.anchor
|
2016-08-17 20:21:34 +00:00
|
|
|
c.nextAnchorId = AnchorId(int(c.nextAnchorId) + 1)
|
2016-09-12 16:04:26 +00:00
|
|
|
c.lex.buf.setLen(0)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc continueMultilineScalar(c: ParserContext) {.raises: [].} =
|
2016-09-12 16:04:26 +00:00
|
|
|
c.lex.buf.add(if c.newlines == 1: " " else: repeat('\l', c.newlines - 1))
|
|
|
|
c.newlines = 0
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc handleTagHandle(c: ParserContext) {.raises: [YamlParserError].} =
|
|
|
|
if c.level.kind != fplUnknown: raise c.generateError("Unexpected tag handle")
|
|
|
|
if c.tag != yTagQuestionMark:
|
|
|
|
raise c.generateError("Only one tag handle is allowed per node")
|
2016-09-12 16:04:26 +00:00
|
|
|
if c.lex.cur == ltTagHandle:
|
|
|
|
var tagUri = ""
|
2016-08-15 19:30:49 +00:00
|
|
|
try:
|
2016-09-12 16:04:26 +00:00
|
|
|
tagUri.add(c.shorthands[c.lex.buf[0..c.lex.shorthandEnd]])
|
|
|
|
tagUri.add(c.lex.buf[c.lex.shorthandEnd + 1 .. ^1])
|
2016-08-15 19:30:49 +00:00
|
|
|
except KeyError:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError(
|
2016-09-12 16:04:26 +00:00
|
|
|
"Undefined tag shorthand: " & c.lex.buf[0..c.lex.shorthandEnd])
|
|
|
|
try: c.tag = c.p.tagLib.tags[tagUri]
|
|
|
|
except KeyError: c.tag = c.p.tagLib.registerUri(tagUri)
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
2016-09-12 16:04:26 +00:00
|
|
|
try: c.tag = c.p.tagLib.tags[c.lex.buf]
|
|
|
|
except KeyError: c.tag = c.p.tagLib.registerUri(c.lex.buf)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc handlePossibleMapStart(c: ParserContext, e: var YamlStreamEvent,
|
|
|
|
flow: bool = false, single: bool = false): bool =
|
2016-08-15 19:30:49 +00:00
|
|
|
result = false
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.level.indentation == UnknownIndentation:
|
2016-09-12 16:04:26 +00:00
|
|
|
if c.lex.isImplicitKeyStart():
|
|
|
|
e = c.objectStart(yamlStartMap, single)
|
|
|
|
result = true
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc handleMapKeyIndicator(c: ParserContext, e: var YamlStreamEvent): bool =
|
2016-08-15 19:30:49 +00:00
|
|
|
result = false
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplUnknown:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.objectStart(yamlStartMap)
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
|
|
|
of fplMapValue:
|
2016-09-12 16:04:26 +00:00
|
|
|
if c.level.indentation != c.lex.indentation:
|
2016-09-13 08:39:54 +00:00
|
|
|
raise c.generateError("Invalid p.indentation of map key indicator " &
|
|
|
|
"(expected" & $c.level.indentation & ", got " & $c.lex.indentation &
|
|
|
|
")")
|
2016-08-15 19:30:49 +00:00
|
|
|
e = scalarEvent("", yTagQuestionMark, yAnchorNone)
|
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplMapKey
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplMapKey:
|
2016-09-12 16:04:26 +00:00
|
|
|
if c.level.indentation != c.lex.indentation:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Invalid p.indentation of map key indicator")
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSequence:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected map key indicator (expected '- ')")
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSinglePairKey, fplSinglePairValue, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-13 08:39:54 +00:00
|
|
|
if c.lex.cur != ltIndentation:
|
2016-09-13 10:01:21 +00:00
|
|
|
# this enables the parser to properly parse compact structures, like
|
|
|
|
# a: - a
|
|
|
|
# - b
|
|
|
|
# and such. At the first `-`, the indentation must equal its level to be
|
|
|
|
# parsed properly.
|
2016-09-13 08:39:54 +00:00
|
|
|
c.lex.indentation = c.lex.curStartPos.column - 1
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc handleBlockSequenceIndicator(c: ParserContext, e: var YamlStreamEvent):
|
|
|
|
bool =
|
2016-08-15 19:30:49 +00:00
|
|
|
result = false
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-30 20:15:29 +00:00
|
|
|
of fplUnknown:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.objectStart(yamlStartSeq)
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
|
|
|
of fplSequence:
|
2016-09-12 16:04:26 +00:00
|
|
|
if c.level.indentation != c.lex.indentation:
|
2016-09-13 08:39:54 +00:00
|
|
|
raise c.generateError(
|
|
|
|
"Invalid p.indentation of block sequence indicator (expected " &
|
|
|
|
$c.level.indentation & ", got " & $c.lex.indentation & ")")
|
2016-08-17 20:21:34 +00:00
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
|
|
|
else: raise c.generateError("Illegal sequence item in map")
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-13 08:39:54 +00:00
|
|
|
if c.lex.cur != ltIndentation:
|
2016-09-13 10:01:21 +00:00
|
|
|
# see comment in previous proc, this time with structures like
|
|
|
|
# - - a
|
|
|
|
# - b
|
2016-09-13 08:39:54 +00:00
|
|
|
c.lex.indentation = c.lex.curStartPos.column - 1
|
2016-08-17 20:21:34 +00:00
|
|
|
|
|
|
|
proc handleBlockItemStart(c: ParserContext, e: var YamlStreamEvent): bool =
|
2016-08-15 19:30:49 +00:00
|
|
|
result = false
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplUnknown:
|
2016-08-17 20:21:34 +00:00
|
|
|
result = c.handlePossibleMapStart(e)
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSequence:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError(
|
2016-08-15 19:30:49 +00:00
|
|
|
"Unexpected token (expected block sequence indicator)")
|
|
|
|
of fplMapKey:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.ancestry.add(c.level)
|
2016-09-12 19:38:56 +00:00
|
|
|
c.level = FastParseLevel(kind: fplUnknown, indentation: c.lex.indentation)
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplMapValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = emptyScalar(c)
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplMapKey
|
|
|
|
c.ancestry.add(c.level)
|
2016-09-12 19:38:56 +00:00
|
|
|
c.level = FastParseLevel(kind: fplUnknown, indentation: c.lex.indentation)
|
|
|
|
of fplSinglePairKey, fplSinglePairValue, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
|
|
|
|
|
|
|
proc handleFlowItemStart(c: ParserContext, e: var YamlStreamEvent): bool =
|
|
|
|
if c.level.kind == fplUnknown and
|
|
|
|
c.ancestry[c.ancestry.high].kind == fplSequence:
|
|
|
|
result = c.handlePossibleMapStart(e, true, true)
|
|
|
|
|
2016-09-12 19:38:56 +00:00
|
|
|
proc handleFlowPlainScalar(c: ParserContext) =
|
2016-09-12 16:04:26 +00:00
|
|
|
while c.lex.cur in {ltScalarPart, ltEmptyLine}:
|
|
|
|
c.lex.newlines.inc()
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 16:04:26 +00:00
|
|
|
c.lex.newlines = 0
|
2016-09-12 19:38:56 +00:00
|
|
|
|
|
|
|
proc handleBlockScalar(c: ParserContext) =
|
|
|
|
while true:
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
|
|
|
of ltScalarPart: discard
|
|
|
|
of ltEmptyLine: c.lex.newlines.inc()
|
|
|
|
of ltIndentation:
|
|
|
|
if c.lex.indentation <= c.ancestry[^1].indentation:
|
|
|
|
# TODO: handle clipping
|
|
|
|
break
|
|
|
|
of ltStreamEnd, ltDirectivesEnd, ltDocumentEnd:
|
|
|
|
# TODO: handle clipping
|
|
|
|
break
|
|
|
|
else: internalError("Unexpected token: " & $c.lex.cur)
|
2016-08-17 18:04:59 +00:00
|
|
|
|
2016-08-15 19:30:49 +00:00
|
|
|
# --- macros for defining parser states ---
|
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
template capitalize(s: string): string =
|
|
|
|
when declared(strutils.capitalizeAscii): strutils.capitalizeAscii(s)
|
|
|
|
else: strutils.capitalize(s)
|
|
|
|
|
|
|
|
macro parserStates(names: varargs[untyped]): typed =
|
2016-08-15 19:30:49 +00:00
|
|
|
## generates proc declaration for each state in list like this:
|
2016-08-30 20:15:29 +00:00
|
|
|
##
|
2016-08-15 19:30:49 +00:00
|
|
|
## proc name(s: YamlStream, e: var YamlStreamEvent):
|
|
|
|
## bool {.raises: [YamlParserError].}
|
|
|
|
result = newStmtList()
|
|
|
|
for name in names:
|
2016-08-17 20:50:37 +00:00
|
|
|
let nameId = newIdentNode("state" & capitalize($name.ident))
|
2016-08-15 19:30:49 +00:00
|
|
|
result.add(newProc(nameId, [ident("bool"), newIdentDefs(ident("s"),
|
|
|
|
ident("YamlStream")), newIdentDefs(ident("e"), newNimNode(nnkVarTy).add(
|
|
|
|
ident("YamlStreamEvent")))], newEmptyNode()))
|
|
|
|
result[0][4] = newNimNode(nnkPragma).add(newNimNode(nnkExprColonExpr).add(
|
2016-09-12 19:38:56 +00:00
|
|
|
ident("raises"), newNimNode(nnkBracket).add(ident("YamlParserError"),
|
|
|
|
ident("YamlLexerError"))))
|
2016-08-15 19:30:49 +00:00
|
|
|
|
|
|
|
proc processStateAsgns(source, target: NimNode) {.compileTime.} =
|
|
|
|
## copies children of source to target and replaces all assignments
|
|
|
|
## `state = [name]` with the appropriate code for changing states.
|
|
|
|
for child in source.children:
|
|
|
|
if child.kind == nnkAsgn and child[0].kind == nnkIdent:
|
|
|
|
if $child[0].ident == "state":
|
|
|
|
assert child[1].kind == nnkIdent
|
|
|
|
var newNameId: NimNode
|
|
|
|
if child[1].kind == nnkIdent and $child[1].ident == "stored":
|
2016-08-17 20:21:34 +00:00
|
|
|
newNameId = newDotExpr(ident("c"), ident("storedState"))
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
|
|
|
newNameId =
|
2016-08-17 20:50:37 +00:00
|
|
|
newIdentNode("state" & capitalize($child[1].ident))
|
2016-08-15 19:30:49 +00:00
|
|
|
target.add(newAssignment(newDotExpr(
|
|
|
|
newIdentNode("s"), newIdentNode("nextImpl")), newNameId))
|
|
|
|
continue
|
|
|
|
elif $child[0].ident == "stored":
|
|
|
|
assert child[1].kind == nnkIdent
|
|
|
|
let newNameId =
|
2016-08-17 20:50:37 +00:00
|
|
|
newIdentNode("state" & capitalize($child[1].ident))
|
2016-08-17 20:21:34 +00:00
|
|
|
target.add(newAssignment(newDotExpr(newIdentNode("c"),
|
|
|
|
newIdentNode("storedState")), newNameId))
|
2016-08-15 19:30:49 +00:00
|
|
|
continue
|
|
|
|
var processed = copyNimNode(child)
|
|
|
|
processStateAsgns(child, processed)
|
|
|
|
target.add(processed)
|
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
macro parserState(name: untyped, impl: untyped): typed =
|
2016-08-15 19:30:49 +00:00
|
|
|
## Creates a parser state. Every parser state is a proc with the signature
|
|
|
|
##
|
|
|
|
## proc(s: YamlStream, e: var YamlStreamEvent):
|
|
|
|
## bool {.raises: [YamlParserError].}
|
|
|
|
##
|
|
|
|
## The proc name will be prefixed with "state" and the original name will be
|
|
|
|
## capitalized, so a state "foo" will yield a proc named "stateFoo".
|
|
|
|
##
|
2016-08-17 20:21:34 +00:00
|
|
|
## Inside the proc, you have access to the ParserContext with the let variable
|
|
|
|
## `c`. You can change the parser state by a assignment `state = [newState]`.
|
2016-08-15 19:30:49 +00:00
|
|
|
## The [newState] must have been declared with states(...) previously.
|
|
|
|
let
|
|
|
|
nameStr = $name.ident
|
2016-08-17 20:50:37 +00:00
|
|
|
nameId = newIdentNode("state" & capitalize(nameStr))
|
2016-08-15 19:30:49 +00:00
|
|
|
var procImpl = quote do:
|
|
|
|
debug("state: " & `nameStr`)
|
2016-08-17 20:21:34 +00:00
|
|
|
procImpl.add(newLetStmt(ident("c"), newCall("ParserContext", ident("s"))))
|
2016-08-15 19:30:49 +00:00
|
|
|
procImpl.add(newAssignment(newIdentNode("result"), newLit(false)))
|
|
|
|
assert impl.kind == nnkStmtList
|
|
|
|
processStateAsgns(impl, procImpl)
|
|
|
|
result = newProc(nameId, [ident("bool"),
|
|
|
|
newIdentDefs(ident("s"), ident("YamlStream")), newIdentDefs(ident("e"),
|
|
|
|
newNimNode(nnkVarTy).add(ident("YamlStreamEvent")))], procImpl)
|
|
|
|
|
|
|
|
# --- parser states ---
|
|
|
|
|
2016-09-13 08:39:54 +00:00
|
|
|
parserStates(initial, blockLineStart, blockObjectStart, blockAfterObject,
|
|
|
|
scalarEnd, plainScalarEnd, objectEnd, expectDocEnd, startDoc,
|
|
|
|
afterDocument, closeStream, closeMoreIndentedLevels,
|
|
|
|
emitEmptyScalar, tagHandle, anchor, alias, flow, leaveFlowMap,
|
|
|
|
leaveFlowSeq, flowAfterObject, leaveFlowSinglePairMap)
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc closeEverything(c: ParserContext) =
|
2016-09-12 16:04:26 +00:00
|
|
|
c.lex.indentation = -1
|
2016-08-17 20:21:34 +00:00
|
|
|
c.nextImpl = stateCloseMoreIndentedLevels
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc endLevel(c: ParserContext, e: var YamlStreamEvent):
|
2016-08-15 19:30:49 +00:00
|
|
|
LevelEndResult =
|
|
|
|
result = lerOne
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSequence:
|
|
|
|
e = endSeqEvent()
|
|
|
|
of fplMapKey:
|
|
|
|
e = endMapEvent()
|
|
|
|
of fplMapValue, fplSinglePairValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = emptyScalar(c)
|
|
|
|
c.level.kind = fplMapKey
|
2016-08-15 19:30:49 +00:00
|
|
|
result = lerAdditionalMapEnd
|
|
|
|
of fplUnknown:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.ancestry.len > 1:
|
|
|
|
e = emptyScalar(c) # don't yield scalar for empty doc
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
|
|
|
result = lerNothing
|
|
|
|
of fplDocument:
|
|
|
|
e = endDocEvent()
|
|
|
|
of fplSinglePairKey:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc handleMapValueIndicator(c: ParserContext, e: var YamlStreamEvent): bool =
|
2016-09-12 19:38:56 +00:00
|
|
|
result = false
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplUnknown:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.level.indentation == UnknownIndentation:
|
|
|
|
e = c.objectStart(yamlStartMap)
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.storedState = c.nextImpl
|
|
|
|
c.nextImpl = stateEmitEmptyScalar
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = emptyScalar(c)
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.ancestry[c.ancestry.high].kind = fplMapValue
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplMapKey:
|
2016-09-12 19:38:56 +00:00
|
|
|
if c.level.indentation != c.lex.indentation:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Invalid p.indentation of map key indicator")
|
2016-08-15 19:30:49 +00:00
|
|
|
e = scalarEvent("", yTagQuestionMark, yAnchorNone)
|
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplMapValue
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplMapValue:
|
2016-09-12 19:38:56 +00:00
|
|
|
if c.level.indentation != c.lex.indentation:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Invalid p.indentation of map key indicator")
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSequence:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected map value indicator (expected '- ')")
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSinglePairKey, fplSinglePairValue, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
template handleObjectEnd(c: ParserContext, mayHaveEmptyValue: bool = false):
|
|
|
|
bool =
|
2016-08-15 19:30:49 +00:00
|
|
|
var result = false
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level = c.ancestry.pop()
|
2016-08-15 19:30:49 +00:00
|
|
|
when mayHaveEmptyValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.level.kind == fplSinglePairValue:
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level = c.ancestry.pop()
|
|
|
|
case c.level.kind
|
|
|
|
of fplMapKey: c.level.kind = fplMapValue
|
|
|
|
of fplSinglePairKey: c.level.kind = fplSinglePairValue
|
|
|
|
of fplMapValue: c.level.kind = fplMapKey
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSequence, fplDocument: discard
|
2016-09-12 19:38:56 +00:00
|
|
|
of fplUnknown, fplSinglePairValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
2016-08-15 19:30:49 +00:00
|
|
|
result
|
|
|
|
|
2016-08-17 20:21:34 +00:00
|
|
|
proc leaveFlowLevel(c: ParserContext, e: var YamlStreamEvent): bool =
|
|
|
|
c.flowdepth.dec()
|
|
|
|
result = (c.endLevel(e) == lerOne) # lerAdditionalMapEnd cannot happen
|
|
|
|
if c.flowdepth == 0:
|
2016-09-12 19:38:56 +00:00
|
|
|
c.lex.setFlow(false)
|
2016-08-17 20:21:34 +00:00
|
|
|
c.storedState = stateBlockAfterObject
|
2016-08-17 18:04:59 +00:00
|
|
|
else:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.storedState = stateFlowAfterObject
|
|
|
|
c.nextImpl = stateObjectEnd
|
2016-08-17 18:04:59 +00:00
|
|
|
|
2016-08-15 19:30:49 +00:00
|
|
|
parserState initial:
|
2016-09-12 16:04:26 +00:00
|
|
|
case c.lex.cur
|
|
|
|
of ltYamlDirective:
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
assert c.lex.cur == ltYamlVersion, $c.lex.cur
|
2016-09-12 16:04:26 +00:00
|
|
|
if c.lex.buf != "1.2":
|
|
|
|
c.callCallback("Version is not 1.2, but " & c.lex.buf)
|
2016-09-13 08:39:54 +00:00
|
|
|
c.lex.buf.setLen(0)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 16:04:26 +00:00
|
|
|
of ltTagDirective:
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 16:04:26 +00:00
|
|
|
assert c.lex.cur == ltTagShorthand
|
|
|
|
var tagShorthand: string
|
|
|
|
shallowCopy(tagShorthand, c.lex.buf)
|
|
|
|
c.lex.buf = ""
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 16:04:26 +00:00
|
|
|
assert c.lex.cur == ltTagUri
|
|
|
|
c.shorthands[tagShorthand] = c.lex.buf
|
|
|
|
c.lex.buf.setLen(0)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 16:04:26 +00:00
|
|
|
of ltUnknownDirective:
|
|
|
|
c.callCallback("Unknown directive: " & c.lex.buf)
|
|
|
|
c.lex.buf.setLen(0)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 16:04:26 +00:00
|
|
|
assert c.lex.cur == ltUnknownDirectiveParams
|
|
|
|
of ltIndentation:
|
2016-08-15 19:30:49 +00:00
|
|
|
e = startDocEvent()
|
|
|
|
result = true
|
|
|
|
state = blockObjectStart
|
2016-09-12 16:04:26 +00:00
|
|
|
of ltStreamEnd: c.isFinished = true
|
|
|
|
of ltDirectivesEnd:
|
2016-08-15 19:30:49 +00:00
|
|
|
e = startDocEvent()
|
|
|
|
result = true
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
state = blockObjectStart
|
2016-09-12 16:04:26 +00:00
|
|
|
else: internalError("Unexpected lexer token: " & $c.lex.cur)
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-09-13 08:39:54 +00:00
|
|
|
parserState blockLineStart:
|
|
|
|
case c.lex.cur
|
|
|
|
of ltIndentation: discard
|
2016-09-13 10:01:21 +00:00
|
|
|
of ltEmptyLine: c.advance()
|
2016-09-13 08:39:54 +00:00
|
|
|
of ltStreamEnd:
|
|
|
|
c.closeEverything()
|
|
|
|
stored = afterDocument
|
|
|
|
else:
|
|
|
|
if c.lex.indentation <= c.ancestry[^1].indentation:
|
|
|
|
state = closeMoreIndentedLevels
|
|
|
|
stored = blockObjectStart
|
|
|
|
else:
|
|
|
|
state = blockObjectStart
|
2016-09-12 19:38:56 +00:00
|
|
|
|
2016-09-13 08:39:54 +00:00
|
|
|
parserState blockObjectStart:
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
2016-09-13 10:01:21 +00:00
|
|
|
of ltEmptyLine: c.advance()
|
2016-09-13 08:39:54 +00:00
|
|
|
of ltIndentation:
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-13 08:39:54 +00:00
|
|
|
state = blockLineStart
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltDirectivesEnd:
|
|
|
|
c.closeEverything()
|
|
|
|
stored = startDoc
|
|
|
|
of ltDocumentEnd:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.closeEverything()
|
2016-08-15 19:30:49 +00:00
|
|
|
stored = afterDocument
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltMapKeyInd:
|
|
|
|
result = c.handleMapKeyIndicator(e)
|
|
|
|
of ltMapValInd:
|
|
|
|
result = c.handleMapValueIndicator(e)
|
|
|
|
of ltQuotedScalar:
|
|
|
|
result = c.handleBlockItemStart(e)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
state = scalarEnd
|
|
|
|
of ltBlockScalarHeader:
|
|
|
|
result = c.handleBlockItemStart(e)
|
|
|
|
c.handleBlockScalar()
|
|
|
|
state = scalarEnd
|
|
|
|
of ltScalarPart:
|
|
|
|
result = c.handleBlockItemStart(e)
|
2016-09-13 08:39:54 +00:00
|
|
|
while true:
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-13 08:39:54 +00:00
|
|
|
c.lex.newlines.inc()
|
|
|
|
case c.lex.cur
|
|
|
|
of ltEmptyLine: c.lex.newlines.inc()
|
|
|
|
of ltIndentation:
|
|
|
|
if c.lex.indentation <= c.ancestry[^1].indentation: break
|
|
|
|
of ltScalarPart: discard
|
|
|
|
else: break
|
|
|
|
c.lex.newlines = 0
|
2016-09-12 19:38:56 +00:00
|
|
|
state = plainScalarEnd
|
|
|
|
stored = blockAfterObject
|
|
|
|
of ltSeqItemInd:
|
|
|
|
result = c.handleBlockSequenceIndicator(e)
|
|
|
|
of ltTagHandle:
|
|
|
|
result = c.handleBlockItemStart(e)
|
|
|
|
state = tagHandle
|
|
|
|
stored = blockObjectStart
|
|
|
|
of ltAnchor:
|
|
|
|
result = c.handleBlockItemStart(e)
|
|
|
|
state = anchor
|
|
|
|
stored = blockObjectStart
|
|
|
|
of ltAlias:
|
|
|
|
result = c.handleBlockItemStart(e)
|
|
|
|
state = alias
|
|
|
|
stored = blockAfterObject
|
|
|
|
of ltBraceOpen, ltBracketOpen:
|
|
|
|
result = c.handleBlockItemStart(e)
|
|
|
|
c.lex.setFlow(true)
|
|
|
|
state = flow
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
2016-09-12 19:38:56 +00:00
|
|
|
raise c.generateError("Unexpected token: " & $c.lex.cur)
|
2016-08-15 19:30:49 +00:00
|
|
|
|
|
|
|
parserState scalarEnd:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.tag == yTagQuestionMark: c.tag = yTagExclamationMark
|
|
|
|
e = c.currentScalar()
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
|
|
|
state = objectEnd
|
|
|
|
stored = blockAfterObject
|
|
|
|
|
2016-09-12 19:38:56 +00:00
|
|
|
parserState plainScalarEnd:
|
|
|
|
e = c.currentScalar()
|
|
|
|
result = true
|
|
|
|
state = objectEnd
|
|
|
|
stored = blockAfterObject
|
2016-08-15 19:30:49 +00:00
|
|
|
|
|
|
|
parserState blockAfterObject:
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
|
|
|
of ltIndentation:
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-13 08:39:54 +00:00
|
|
|
state = blockLineStart
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltMapValInd:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplUnknown:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.objectStart(yamlStartMap)
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
|
|
|
of fplMapKey:
|
|
|
|
e = scalarEvent("", yTagQuestionMark, yAnchorNone)
|
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplMapValue
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplMapValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplMapValue
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSequence:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Illegal token (expected sequence item)")
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplSinglePairKey, fplSinglePairValue, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
state = blockObjectStart
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltStreamEnd:
|
|
|
|
c.closeEverything()
|
|
|
|
stored = afterDocument
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError(
|
2016-08-15 19:30:49 +00:00
|
|
|
"Illegal token (expected ':', comment or line end)")
|
|
|
|
|
|
|
|
parserState objectEnd:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.handleObjectEnd(true):
|
2016-08-15 19:30:49 +00:00
|
|
|
e = endMapEvent()
|
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.level.kind == fplDocument: state = expectDocEnd
|
2016-08-15 19:30:49 +00:00
|
|
|
else: state = stored
|
|
|
|
|
|
|
|
parserState expectDocEnd:
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
2016-09-13 10:01:21 +00:00
|
|
|
of ltIndentation, ltEmptyLine: c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltDirectivesEnd:
|
|
|
|
e = endDocEvent()
|
|
|
|
result = true
|
|
|
|
state = startDoc
|
|
|
|
c.ancestry.setLen(0)
|
|
|
|
of ltDocumentEnd:
|
|
|
|
e = endDocEvent()
|
|
|
|
result = true
|
|
|
|
state = afterDocument
|
|
|
|
of ltStreamEnd:
|
2016-08-15 19:30:49 +00:00
|
|
|
e = endDocEvent()
|
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.isFinished = true
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
2016-09-12 19:38:56 +00:00
|
|
|
raise c.generateError("Unexpected token (expected document end): " &
|
|
|
|
$c.lex.cur)
|
2016-08-15 19:30:49 +00:00
|
|
|
|
|
|
|
parserState startDoc:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.initDocValues()
|
2016-08-15 19:30:49 +00:00
|
|
|
e = startDocEvent()
|
|
|
|
result = true
|
|
|
|
state = blockObjectStart
|
|
|
|
|
|
|
|
parserState afterDocument:
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
|
|
|
of ltStreamEnd: c.isFinished = true
|
2016-09-13 10:01:21 +00:00
|
|
|
of ltIndentation, ltEmptyLine, ltDocumentEnd: c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
else:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.initDocValues()
|
2016-08-15 19:30:49 +00:00
|
|
|
state = initial
|
|
|
|
|
|
|
|
parserState closeStream:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
|
|
|
of fplUnknown: discard c.ancestry.pop()
|
2016-08-15 19:30:49 +00:00
|
|
|
of fplDocument: discard
|
|
|
|
else:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.endLevel(e)
|
2016-08-15 19:30:49 +00:00
|
|
|
of lerNothing: discard
|
|
|
|
of lerOne: result = true
|
|
|
|
of lerAdditionalMapEnd: return true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level = c.ancestry.pop()
|
2016-08-15 19:30:49 +00:00
|
|
|
if result: return
|
|
|
|
e = endDocEvent()
|
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.isFinished = true
|
2016-08-15 19:30:49 +00:00
|
|
|
|
|
|
|
parserState closeMoreIndentedLevels:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.ancestry.len > 0:
|
|
|
|
let parent = c.ancestry[c.ancestry.high]
|
2016-09-12 19:38:56 +00:00
|
|
|
if parent.indentation >= c.lex.indentation:
|
|
|
|
if c.lex.cur == ltSeqItemInd:
|
|
|
|
if (c.lex.indentation == c.level.indentation and
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind == fplSequence) or
|
2016-09-12 19:38:56 +00:00
|
|
|
(c.lex.indentation == parent.indentation and
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind == fplUnknown and parent.kind != fplSequence):
|
2016-08-15 19:30:49 +00:00
|
|
|
state = stored
|
|
|
|
return false
|
|
|
|
debug("Closing because parent.indentation (" & $parent.indentation &
|
2016-09-12 19:38:56 +00:00
|
|
|
") >= indentation(" & $c.lex.indentation & ")")
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.endLevel(e)
|
2016-08-15 19:30:49 +00:00
|
|
|
of lerNothing: discard
|
|
|
|
of lerOne: result = true
|
|
|
|
of lerAdditionalMapEnd: return true
|
2016-08-17 20:21:34 +00:00
|
|
|
discard c.handleObjectEnd(false)
|
2016-08-15 19:30:49 +00:00
|
|
|
return result
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.level.kind == fplDocument: state = expectDocEnd
|
2016-08-15 19:30:49 +00:00
|
|
|
else: state = stored
|
2016-09-12 19:38:56 +00:00
|
|
|
elif c.lex.indentation == c.level.indentation:
|
2016-08-17 20:21:34 +00:00
|
|
|
let res = c.endLevel(e)
|
2016-08-15 19:30:49 +00:00
|
|
|
yAssert(res == lerOne)
|
|
|
|
result = true
|
|
|
|
state = stored
|
|
|
|
else:
|
|
|
|
state = stored
|
|
|
|
|
|
|
|
parserState emitEmptyScalar:
|
|
|
|
e = scalarEvent("", yTagQuestionMark, yAnchorNone)
|
|
|
|
result = true
|
|
|
|
state = stored
|
|
|
|
|
|
|
|
parserState tagHandle:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.handleTagHandle()
|
2016-08-15 19:30:49 +00:00
|
|
|
state = stored
|
|
|
|
|
|
|
|
parserState anchor:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.handleAnchor()
|
2016-08-15 19:30:49 +00:00
|
|
|
state = stored
|
|
|
|
|
|
|
|
parserState alias:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.level.kind != fplUnknown: raise c.generateError("Unexpected token")
|
|
|
|
if c.anchor != yAnchorNone or c.tag != yTagQuestionMark:
|
|
|
|
raise c.generateError("Alias may not have anchor or tag")
|
2016-08-15 19:30:49 +00:00
|
|
|
var id: AnchorId
|
2016-09-12 19:38:56 +00:00
|
|
|
try: id = c.p.anchors[c.lex.buf]
|
2016-08-17 20:21:34 +00:00
|
|
|
except KeyError: raise c.generateError("Unknown anchor")
|
2016-08-15 19:30:49 +00:00
|
|
|
e = aliasEvent(id)
|
|
|
|
result = true
|
|
|
|
state = objectEnd
|
|
|
|
|
|
|
|
parserState flow:
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
|
|
|
of ltBraceOpen:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.handleFlowItemStart(e): return true
|
|
|
|
e = c.objectStart(yamlStartMap)
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.flowdepth.inc()
|
|
|
|
c.explicitFlowKey = false
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltBracketOpen:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.handleFlowItemStart(e): return true
|
|
|
|
e = c.objectStart(yamlStartSeq)
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.flowdepth.inc()
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltBraceClose:
|
2016-08-17 20:21:34 +00:00
|
|
|
yAssert(c.level.kind == fplUnknown)
|
|
|
|
c.level = c.ancestry.pop()
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-08-17 18:04:59 +00:00
|
|
|
state = leaveFlowMap
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltBracketClose:
|
2016-08-17 20:21:34 +00:00
|
|
|
yAssert(c.level.kind == fplUnknown)
|
|
|
|
c.level = c.ancestry.pop()
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-08-17 18:04:59 +00:00
|
|
|
state = leaveFlowSeq
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltComma:
|
2016-08-17 20:21:34 +00:00
|
|
|
yAssert(c.level.kind == fplUnknown)
|
|
|
|
c.level = c.ancestry.pop()
|
|
|
|
case c.level.kind
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplSequence:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.emptyScalar()
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
|
|
|
of fplMapValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.emptyScalar()
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplMapKey
|
|
|
|
c.explicitFlowKey = false
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplMapKey:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.emptyScalar()
|
|
|
|
c.level.kind = fplMapValue
|
2016-08-17 18:04:59 +00:00
|
|
|
return true
|
|
|
|
of fplSinglePairValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.emptyScalar()
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level = c.ancestry.pop()
|
2016-08-17 18:04:59 +00:00
|
|
|
state = leaveFlowSinglePairMap
|
|
|
|
stored = flow
|
2016-09-12 19:38:56 +00:00
|
|
|
of fplUnknown, fplSinglePairKey, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltMapValInd:
|
|
|
|
c.level = c.ancestry.pop()
|
|
|
|
case c.level.kind
|
|
|
|
of fplSequence:
|
|
|
|
e = startMapEvent(c.tag, c.anchor)
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
2016-09-12 19:38:56 +00:00
|
|
|
debug("started single-pair map at " &
|
|
|
|
(if c.level.indentation == UnknownIndentation:
|
|
|
|
$c.lex.indentation else: $c.level.indentation))
|
|
|
|
c.tag = yTagQuestionMark
|
|
|
|
c.anchor = yAnchorNone
|
|
|
|
if c.level.indentation == UnknownIndentation:
|
|
|
|
c.level.indentation = c.lex.indentation
|
2016-08-17 20:21:34 +00:00
|
|
|
c.ancestry.add(c.level)
|
2016-09-12 19:38:56 +00:00
|
|
|
c.level = initLevel(fplSinglePairKey)
|
|
|
|
of fplMapValue, fplSinglePairValue:
|
|
|
|
raise c.generateError("Unexpected token (expected ',')")
|
|
|
|
of fplMapKey:
|
|
|
|
e = c.emptyScalar()
|
|
|
|
result = true
|
|
|
|
c.level.kind = fplMapValue
|
|
|
|
of fplSinglePairKey:
|
|
|
|
e = c.emptyScalar()
|
|
|
|
result = true
|
|
|
|
c.level.kind = fplSinglePairValue
|
|
|
|
of fplUnknown, fplDocument:
|
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
2016-09-13 10:01:21 +00:00
|
|
|
if c.level.kind != fplSinglePairKey: c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
|
|
|
of ltQuotedScalar:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.handleFlowItemStart(e): return true
|
|
|
|
if c.tag == yTagQuestionMark: c.tag = yTagExclamationMark
|
|
|
|
e = c.currentScalar()
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
|
|
|
state = objectEnd
|
|
|
|
stored = flowAfterObject
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltTagHandle:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.handleFlowItemStart(e): return true
|
|
|
|
c.handleTagHandle()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltAnchor:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.handleFlowItemStart(e): return true
|
|
|
|
c.handleAnchor()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltAlias:
|
2016-08-17 18:04:59 +00:00
|
|
|
state = alias
|
|
|
|
stored = flowAfterObject
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltMapKeyInd:
|
|
|
|
if c.explicitFlowKey:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Duplicate '?' in flow mapping")
|
|
|
|
elif c.level.kind == fplUnknown:
|
|
|
|
case c.ancestry[c.ancestry.high].kind
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplMapKey, fplMapValue, fplDocument: discard
|
|
|
|
of fplSequence:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.objectStart(yamlStartMap, true)
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
|
|
|
else:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected token")
|
2016-09-12 19:38:56 +00:00
|
|
|
c.explicitFlowKey = true
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltScalarPart:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.handleFlowItemStart(e): return true
|
2016-09-12 19:38:56 +00:00
|
|
|
c.handleFlowPlainScalar()
|
|
|
|
if c.tag == yTagQuestionMark: c.tag = yTagExclamationMark
|
|
|
|
e = c.currentScalar()
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
|
|
|
state = objectEnd
|
|
|
|
stored = flowAfterObject
|
2016-09-12 19:38:56 +00:00
|
|
|
else:
|
|
|
|
raise c.generateError("Unexpected toked: " & $c.lex.cur)
|
2016-08-17 18:04:59 +00:00
|
|
|
|
|
|
|
parserState leaveFlowMap:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplMapValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.emptyScalar()
|
|
|
|
c.level.kind = fplMapKey
|
2016-08-17 18:04:59 +00:00
|
|
|
return true
|
|
|
|
of fplMapKey:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.tag != yTagQuestionMark or c.anchor != yAnchorNone or
|
|
|
|
c.explicitFlowKey:
|
|
|
|
e = c.emptyScalar()
|
|
|
|
c.level.kind = fplMapValue
|
|
|
|
c.explicitFlowKey = false
|
2016-08-17 18:04:59 +00:00
|
|
|
return true
|
|
|
|
of fplSequence:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected token (expected ']')")
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplSinglePairValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected token (expected ']')")
|
2016-09-12 19:38:56 +00:00
|
|
|
of fplUnknown, fplSinglePairKey, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
|
|
|
result = c.leaveFlowLevel(e)
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-08-17 18:04:59 +00:00
|
|
|
parserState leaveFlowSeq:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplSequence:
|
2016-08-17 20:21:34 +00:00
|
|
|
if c.tag != yTagQuestionMark or c.anchor != yAnchorNone:
|
|
|
|
e = c.emptyScalar()
|
2016-08-17 18:04:59 +00:00
|
|
|
return true
|
|
|
|
of fplSinglePairValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
e = c.emptyScalar()
|
|
|
|
c.level = c.ancestry.pop()
|
2016-08-17 18:04:59 +00:00
|
|
|
state = leaveFlowSinglePairMap
|
|
|
|
stored = leaveFlowSeq
|
|
|
|
return true
|
|
|
|
of fplMapKey, fplMapValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected token (expected '}')")
|
2016-09-12 19:38:56 +00:00
|
|
|
of fplUnknown, fplSinglePairKey, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
|
|
|
result = c.leaveFlowLevel(e)
|
2016-08-17 18:04:59 +00:00
|
|
|
|
|
|
|
parserState leaveFlowSinglePairMap:
|
|
|
|
e = endMapEvent()
|
|
|
|
result = true
|
|
|
|
state = stored
|
|
|
|
|
|
|
|
parserState flowAfterObject:
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
|
|
|
of ltBracketClose:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplSequence: discard
|
|
|
|
of fplMapKey, fplMapValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected token (expected '}')")
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplSinglePairValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level = c.ancestry.pop()
|
|
|
|
yAssert(c.level.kind == fplSequence)
|
2016-08-17 18:04:59 +00:00
|
|
|
e = endMapEvent()
|
|
|
|
return true
|
2016-09-12 19:38:56 +00:00
|
|
|
of fplUnknown, fplSinglePairKey, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
|
|
|
result = c.leaveFlowLevel(e)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltBraceClose:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplMapKey, fplMapValue: discard
|
|
|
|
of fplSequence, fplSinglePairValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected token (expected ']')")
|
2016-09-12 19:38:56 +00:00
|
|
|
of fplUnknown, fplSinglePairKey, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
|
|
|
result = c.leaveFlowLevel(e)
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltComma:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplSequence: discard
|
|
|
|
of fplMapValue:
|
|
|
|
e = scalarEvent("", yTagQuestionMark, yAnchorNone)
|
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level.kind = fplMapKey
|
|
|
|
c.explicitFlowKey = false
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplSinglePairValue:
|
2016-08-17 20:21:34 +00:00
|
|
|
c.level = c.ancestry.pop()
|
|
|
|
yAssert(c.level.kind == fplSequence)
|
2016-08-17 18:04:59 +00:00
|
|
|
e = endMapEvent()
|
|
|
|
result = true
|
2016-08-17 20:21:34 +00:00
|
|
|
of fplMapKey: c.explicitFlowKey = false
|
2016-09-12 19:38:56 +00:00
|
|
|
of fplUnknown, fplSinglePairKey, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-08-17 18:04:59 +00:00
|
|
|
state = flow
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltMapValInd:
|
2016-08-17 20:21:34 +00:00
|
|
|
case c.level.kind
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplSequence, fplMapKey:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected token (expected ',')")
|
2016-08-17 18:04:59 +00:00
|
|
|
of fplMapValue, fplSinglePairValue: discard
|
2016-09-12 19:38:56 +00:00
|
|
|
of fplUnknown, fplSinglePairKey, fplDocument:
|
2016-08-17 20:21:34 +00:00
|
|
|
internalError("Unexpected level kind: " & $c.level.kind)
|
|
|
|
c.ancestry.add(c.level)
|
|
|
|
c.level = initLevel(fplUnknown)
|
2016-08-17 18:04:59 +00:00
|
|
|
state = flow
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltStreamEnd:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unclosed flow content")
|
2016-08-17 18:04:59 +00:00
|
|
|
else:
|
2016-08-17 20:21:34 +00:00
|
|
|
raise c.generateError("Unexpected content (expected flow indicator)")
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-09-12 19:38:56 +00:00
|
|
|
proc lastTokenContext(s: YamlStream, line, column: var int,
|
|
|
|
lineContent: var string): bool =
|
|
|
|
let c = ParserContext(s)
|
|
|
|
line = c.lex.curStartPos.line
|
|
|
|
column = c.lex.curStartPos.column
|
|
|
|
lineContent = c.lex.getTokenLine(true)
|
|
|
|
result = true
|
|
|
|
|
2016-08-15 19:30:49 +00:00
|
|
|
# --- parser initialization ---
|
|
|
|
|
2016-09-12 16:04:26 +00:00
|
|
|
proc init(c: ParserContext, p: YamlParser) =
|
2016-08-17 20:21:34 +00:00
|
|
|
c.p = p
|
2016-09-12 16:04:26 +00:00
|
|
|
c.ancestry = newSeq[FastParseLevel]()
|
|
|
|
c.initDocValues()
|
|
|
|
c.flowdepth = 0
|
|
|
|
c.isFinished = false
|
|
|
|
c.peeked = false
|
|
|
|
c.nextImpl = stateInitial
|
2016-09-12 19:38:56 +00:00
|
|
|
c.explicitFlowKey = false
|
2016-09-13 10:01:21 +00:00
|
|
|
c.lastTokenContextImpl = lastTokenContext
|
|
|
|
c.advance()
|
|
|
|
|
2016-09-12 16:04:26 +00:00
|
|
|
proc parse*(p: YamlParser, s: Stream): YamlStream =
|
|
|
|
let c = new(ParserContext)
|
|
|
|
try: c.lex = newYamlLexer(s)
|
2016-08-15 19:30:49 +00:00
|
|
|
except:
|
|
|
|
let e = newException(YamlParserError,
|
|
|
|
"Error while opening stream: " & getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
e.line = 1
|
|
|
|
e.column = 1
|
|
|
|
e.lineContent = ""
|
|
|
|
raise e
|
2016-09-12 19:38:56 +00:00
|
|
|
c.init(p)
|
2016-09-12 16:04:26 +00:00
|
|
|
result = c
|
|
|
|
|
|
|
|
proc parse*(p: YamlParser, str: string): YamlStream =
|
|
|
|
let c = new(ParserContext)
|
|
|
|
c.lex = newYamlLexer(str)
|
2016-09-12 19:38:56 +00:00
|
|
|
c.init(p)
|
|
|
|
result = c
|