2016-08-15 19:30:49 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2016-09-21 19:20:57 +00:00
|
|
|
# (c) Copyright 2016 Felix Krause
|
2016-08-15 19:30:49 +00:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-09-21 19:20:57 +00:00
|
|
|
## ==================
|
|
|
|
## Module yaml.parser
|
|
|
|
## ==================
|
|
|
|
##
|
|
|
|
## This is the low-level parser API. A ``YamlParser`` enables you to parse any
|
|
|
|
## non-nil string or Stream object as YAML character stream.
|
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
import tables, strutils, macros, streams
|
2017-02-06 19:39:04 +00:00
|
|
|
import taglib, stream, private/lex, private/internal
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-08-30 20:15:29 +00:00
|
|
|
type
|
2016-09-20 19:53:38 +00:00
|
|
|
WarningCallback* = proc(line, column: int, lineContent: string,
|
|
|
|
message: string)
|
|
|
|
## Callback for parser warnings. Currently, this callback may be called
|
|
|
|
## on two occasions while parsing a YAML document stream:
|
|
|
|
##
|
|
|
|
## - If the version number in the ``%YAML`` directive does not match
|
|
|
|
## ``1.2``.
|
|
|
|
## - If there is an unknown directive encountered.
|
|
|
|
|
|
|
|
YamlParser* = ref object
|
|
|
|
## A parser object. Retains its ``TagLibrary`` across calls to
|
|
|
|
## `parse <#parse,YamlParser,Stream>`_. Can be used
|
|
|
|
## to access anchor names while parsing a YAML character stream, but
|
|
|
|
## only until the document goes out of scope (i.e. until
|
|
|
|
## ``yamlEndDocument`` is yielded).
|
|
|
|
tagLib: TagLibrary
|
|
|
|
callback: WarningCallback
|
|
|
|
anchors: Table[string, AnchorId]
|
|
|
|
|
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-09-23 13:42:24 +00:00
|
|
|
plainScalarStart: tuple[line, column: int]
|
2016-08-15 19:30:49 +00:00
|
|
|
|
|
|
|
LevelEndResult = enum
|
|
|
|
lerNothing, lerOne, lerAdditionalMapEnd
|
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
YamlLoadingError* = object of Exception
|
|
|
|
## Base class for all exceptions that may be raised during the process
|
|
|
|
## of loading a YAML character stream.
|
|
|
|
line*: int ## line number (1-based) where the error was encountered
|
|
|
|
column*: int ## column number (1-based) where the error was encountered
|
|
|
|
lineContent*: string ## \
|
|
|
|
## content of the line where the error was encountered. Includes a
|
|
|
|
## second line with a marker ``^`` at the position where the error
|
|
|
|
## was encountered.
|
|
|
|
|
|
|
|
YamlParserError* = object of YamlLoadingError
|
|
|
|
## A parser error is raised if the character stream that is parsed is
|
|
|
|
## not a valid YAML character stream. This stream cannot and will not be
|
|
|
|
## parsed wholly nor partially and all events that have been emitted by
|
|
|
|
## the YamlStream the parser provides should be discarded.
|
|
|
|
##
|
|
|
|
## A character stream is invalid YAML if and only if at least one of the
|
|
|
|
## following conditions apply:
|
|
|
|
##
|
|
|
|
## - There are invalid characters in an element whose contents is
|
|
|
|
## restricted to a limited set of characters. For example, there are
|
|
|
|
## characters in a tag URI which are not valid URI characters.
|
|
|
|
## - An element has invalid indentation. This can happen for example if
|
|
|
|
## a block list element indicated by ``"- "`` is less indented than
|
|
|
|
## the element in the previous line, but there is no block sequence
|
|
|
|
## list open at the same indentation level.
|
|
|
|
## - The YAML structure is invalid. For example, an explicit block map
|
|
|
|
## indicated by ``"? "`` and ``": "`` may not suddenly have a block
|
|
|
|
## sequence item (``"- "``) at the same indentation level. Another
|
|
|
|
## possible violation is closing a flow style object with the wrong
|
|
|
|
## closing character (``}``, ``]``) or not closing it at all.
|
|
|
|
## - A custom tag shorthand is used that has not previously been
|
|
|
|
## declared with a ``%TAG`` directive.
|
|
|
|
## - Multiple tags or anchors are defined for the same node.
|
|
|
|
## - An alias is used which does not map to any anchor that has
|
|
|
|
## previously been declared in the same document.
|
|
|
|
## - An alias has a tag or anchor associated with it.
|
|
|
|
##
|
|
|
|
## Some elements in this list are vague. For a detailed description of a
|
|
|
|
## valid YAML character stream, see the YAML specification.
|
|
|
|
|
2016-08-15 19:30:49 +00:00
|
|
|
proc newYamlParser*(tagLib: TagLibrary = initExtendedTagLibrary(),
|
|
|
|
callback: WarningCallback = nil): YamlParser =
|
2016-09-20 19:53:38 +00:00
|
|
|
## Creates a YAML parser. if ``callback`` is not ``nil``, it will be called
|
|
|
|
## whenever the parser yields a warning.
|
2016-08-15 19:30:49 +00:00
|
|
|
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-09-13 11:38:10 +00:00
|
|
|
proc illegalToken(c: ParserContext, expected: string = ""):
|
|
|
|
ref YamlParserError {.raises: [].} =
|
|
|
|
var msg = "Illegal token"
|
|
|
|
if expected.len > 0: msg.add(" (expected " & expected & ")")
|
|
|
|
msg.add(": " & $c.lex.cur)
|
|
|
|
result = c.generateError(msg)
|
|
|
|
|
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.} =
|
2017-02-14 18:06:41 +00:00
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
result = scalarEvent("", c.tag, c.anchor, srPlain)
|
|
|
|
else:
|
|
|
|
result = scalarEvent("", c.tag, c.anchor)
|
2016-08-17 20:21:34 +00:00
|
|
|
c.tag = yTagQuestionMark
|
|
|
|
c.anchor = yAnchorNone
|
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
proc currentScalar(c: ParserContext, e: var YamlStreamEvent)
|
|
|
|
{.raises: [], inline.} =
|
|
|
|
e = YamlStreamEvent(kind: yamlScalar, scalarTag: c.tag,
|
|
|
|
scalarAnchor: c.anchor)
|
|
|
|
shallowCopy(e.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))
|
|
|
|
|
2017-02-13 16:10:56 +00:00
|
|
|
proc advance(c: ParserContext) {.inline, raises: [YamlParserError].} =
|
2016-09-13 10:01:21 +00:00
|
|
|
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 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 11:38:10 +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 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-09-13 11:38:10 +00:00
|
|
|
c.level.indentation = c.lex.indentation
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2017-02-14 18:06:41 +00:00
|
|
|
template implicitScalar(): YamlStreamEvent =
|
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
scalarEvent("", yTagQuestionMark, yAnchorNone, srPlain)
|
|
|
|
else:
|
|
|
|
scalarEvent("", yTagQuestionMark, yAnchorNone)
|
|
|
|
|
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 &
|
|
|
|
")")
|
2017-02-14 18:06:41 +00:00
|
|
|
e = implicitScalar()
|
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)
|
|
|
|
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
|
2016-09-14 11:37:56 +00:00
|
|
|
# ? - a
|
|
|
|
# - b
|
2016-09-13 10:01:21 +00:00
|
|
|
# and such. At the first `-`, the indentation must equal its level to be
|
2016-09-19 17:33:29 +00:00
|
|
|
# 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-13 11:38:10 +00:00
|
|
|
else: result = false
|
2016-08-17 20:21:34 +00:00
|
|
|
|
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
|
|
|
|
2016-09-23 13:42:24 +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
|
|
|
# --- 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,
|
2016-09-23 13:42:24 +00:00
|
|
|
afterDocument, closeMoreIndentedLevels, afterPlainScalarYield,
|
2016-09-13 08:39:54 +00:00
|
|
|
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-11-30 20:58:46 +00:00
|
|
|
of fplSequence: e = endSeqEvent()
|
|
|
|
of fplMapKey: e = endMapEvent()
|
2016-08-15 19:30:49 +00:00
|
|
|
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
|
2016-11-30 20:58:46 +00:00
|
|
|
of fplUnknown: e = emptyScalar(c)
|
2017-02-14 21:22:56 +00:00
|
|
|
of fplDocument:
|
2017-02-18 09:40:39 +00:00
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
e = endDocEvent(c.lex.cur == ltDocumentEnd)
|
2017-02-14 21:22:56 +00:00
|
|
|
else: e = endDocEvent()
|
2017-02-18 09:40:39 +00:00
|
|
|
if c.lex.cur == ltDocumentEnd: c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
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")
|
2017-02-14 18:06:41 +00:00
|
|
|
e = implicitScalar()
|
2016-08-15 19:30:49 +00:00
|
|
|
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-09-14 11:37:56 +00:00
|
|
|
if c.lex.cur != ltIndentation:
|
|
|
|
# see comment in handleMapKeyIndicator, this time with structures like
|
|
|
|
# a: - a
|
|
|
|
# - b
|
|
|
|
c.lex.indentation = c.lex.curStartPos.column - 1
|
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-09-14 11:37:56 +00:00
|
|
|
c.advance()
|
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-13 18:19:02 +00:00
|
|
|
if c.lex.cur == ltUnknownDirectiveParams:
|
|
|
|
c.lex.buf.setLen(0)
|
|
|
|
c.advance()
|
2016-09-12 16:04:26 +00:00
|
|
|
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:
|
2017-02-14 21:06:48 +00:00
|
|
|
when defined(yamlScalarRepInd): e = startDocEvent(true)
|
|
|
|
else: e = startDocEvent()
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
state = blockObjectStart
|
2016-09-14 10:18:00 +00:00
|
|
|
of ltDocumentEnd:
|
2016-09-14 11:37:56 +00:00
|
|
|
c.advance()
|
|
|
|
state = afterDocument
|
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
|
2016-09-14 10:18:00 +00:00
|
|
|
of ltIndentation: c.advance()
|
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 18:19:02 +00:00
|
|
|
c.level.indentation = UnknownIndentation
|
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
|
2016-09-14 10:18:00 +00:00
|
|
|
of ltBlockScalarHeader:
|
|
|
|
c.lex.indentation = c.ancestry[^1].indentation
|
|
|
|
c.advance()
|
2017-01-13 15:25:46 +00:00
|
|
|
assert c.lex.cur in {ltBlockScalar, ltStreamEnd}
|
2016-09-14 10:18:00 +00:00
|
|
|
if c.level.indentation == UnknownIndentation:
|
|
|
|
c.level.indentation = c.lex.indentation
|
|
|
|
c.advance()
|
|
|
|
state = scalarEnd
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltScalarPart:
|
2017-01-13 15:19:04 +00:00
|
|
|
let needsValueIndicator = c.level.kind == fplMapKey
|
2016-09-12 19:38:56 +00:00
|
|
|
result = c.handleBlockItemStart(e)
|
2016-09-23 13:42:24 +00:00
|
|
|
c.plainScalarStart = c.lex.curStartPos
|
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
|
|
|
case c.lex.cur
|
|
|
|
of ltIndentation:
|
2017-01-13 15:19:04 +00:00
|
|
|
if c.lex.indentation <= c.ancestry[^1].indentation:
|
|
|
|
if needsValueIndicator and
|
|
|
|
c.lex.indentation == c.ancestry[^1].indentation:
|
|
|
|
raise c.generateError("Illegal multiline implicit key")
|
|
|
|
break
|
2016-09-14 10:18:00 +00:00
|
|
|
c.lex.newlines.inc()
|
2016-09-13 08:39:54 +00:00
|
|
|
of ltScalarPart: discard
|
2016-09-14 10:18:00 +00:00
|
|
|
of ltEmptyLine: c.lex.newlines.inc()
|
2016-09-13 08:39:54 +00:00
|
|
|
else: break
|
2017-01-13 15:19:04 +00:00
|
|
|
if needsValueIndicator and c.lex.cur != ltMapValInd:
|
|
|
|
raise c.generateError("Missing mapping value indicator (`:`)")
|
2016-09-13 08:39:54 +00:00
|
|
|
c.lex.newlines = 0
|
2016-09-12 19:38:56 +00:00
|
|
|
state = plainScalarEnd
|
|
|
|
stored = blockAfterObject
|
|
|
|
of ltSeqItemInd:
|
|
|
|
result = c.handleBlockSequenceIndicator(e)
|
2016-09-14 11:37:56 +00:00
|
|
|
of ltTagHandle, ltLiteralTag:
|
2016-09-12 19:38:56 +00:00
|
|
|
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-09-14 10:18:00 +00:00
|
|
|
of ltStreamEnd:
|
|
|
|
c.closeEverything()
|
|
|
|
stored = afterDocument
|
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
|
2016-09-14 16:31:09 +00:00
|
|
|
c.currentScalar(e)
|
2017-02-14 18:06:41 +00:00
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
case c.lex.scalarKind
|
|
|
|
of skSingleQuoted: e.scalarRep = srSingleQuoted
|
|
|
|
of skDoubleQuoted: e.scalarRep = srDoubleQuoted
|
|
|
|
of skLiteral: e.scalarRep = srLiteral
|
|
|
|
of skFolded: e.scalarRep = srFolded
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
|
|
|
state = objectEnd
|
|
|
|
stored = blockAfterObject
|
|
|
|
|
2016-09-12 19:38:56 +00:00
|
|
|
parserState plainScalarEnd:
|
2016-09-14 16:31:09 +00:00
|
|
|
c.currentScalar(e)
|
2016-09-12 19:38:56 +00:00
|
|
|
result = true
|
2016-09-23 13:42:24 +00:00
|
|
|
c.lastTokenContextImpl = proc(s: YamlStream, line, column: var int,
|
|
|
|
lineContent: var string): bool {.raises: [].} =
|
|
|
|
let c = ParserContext(s)
|
|
|
|
(line, column) = c.plainScalarStart
|
|
|
|
lineContent = c.lex.getTokenLine(c.plainScalarStart, true)
|
|
|
|
result = true
|
|
|
|
state = afterPlainScalarYield
|
2016-09-12 19:38:56 +00:00
|
|
|
stored = blockAfterObject
|
2016-08-15 19:30:49 +00:00
|
|
|
|
2016-09-23 13:42:24 +00:00
|
|
|
parserState afterPlainScalarYield:
|
|
|
|
c.lastTokenContextImpl = lastTokenContext
|
|
|
|
state = objectEnd
|
|
|
|
|
2016-08-15 19:30:49 +00:00
|
|
|
parserState blockAfterObject:
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
2016-09-13 11:38:10 +00:00
|
|
|
of ltIndentation, ltEmptyLine:
|
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:
|
2017-02-14 18:06:41 +00:00
|
|
|
e = implicitScalar()
|
2016-08-15 19:30:49 +00:00
|
|
|
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-09-13 11:38:10 +00:00
|
|
|
of fplSequence: raise c.illegalToken("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-13 11:38:10 +00:00
|
|
|
of ltDirectivesEnd:
|
|
|
|
c.closeEverything()
|
|
|
|
stored = startDoc
|
2016-09-12 19:38:56 +00:00
|
|
|
of ltStreamEnd:
|
|
|
|
c.closeEverything()
|
|
|
|
stored = afterDocument
|
2016-09-13 11:38:10 +00:00
|
|
|
else: raise c.illegalToken("':', comment or line end")
|
2016-08-15 19:30:49 +00:00
|
|
|
|
|
|
|
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:
|
2017-02-14 21:22:56 +00:00
|
|
|
when defined(yamlScalarRepInd): e = endDocEvent(true)
|
|
|
|
else: e = endDocEvent()
|
2016-09-12 19:38:56 +00:00
|
|
|
result = true
|
|
|
|
state = afterDocument
|
2016-09-14 11:37:56 +00:00
|
|
|
c.advance()
|
2016-09-12 19:38:56 +00:00
|
|
|
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()
|
2017-02-14 21:06:48 +00:00
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
e = startDocEvent(c.lex.cur == ltDirectivesEnd)
|
|
|
|
else: e = startDocEvent()
|
2016-08-15 19:30:49 +00:00
|
|
|
result = true
|
2016-09-14 11:37:56 +00:00
|
|
|
c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
state = blockObjectStart
|
|
|
|
|
|
|
|
parserState afterDocument:
|
2016-09-12 19:38:56 +00:00
|
|
|
case c.lex.cur
|
|
|
|
of ltStreamEnd: c.isFinished = true
|
2016-09-14 10:18:00 +00:00
|
|
|
of ltEmptyLine: 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 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
|
2016-09-14 11:37:56 +00:00
|
|
|
debug("Not closing because sequence indicator")
|
2016-08-15 19:30:49 +00:00
|
|
|
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-09-14 11:37:56 +00:00
|
|
|
debug("Not closing level because parent.indentation (" &
|
|
|
|
$parent.indentation & ") < indentation(" & $c.lex.indentation &
|
|
|
|
")")
|
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-09-14 11:37:56 +00:00
|
|
|
debug("Closing document")
|
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:
|
2017-02-14 18:06:41 +00:00
|
|
|
e = implicitScalar()
|
2016-08-15 19:30:49 +00:00
|
|
|
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-09-13 11:38:10 +00:00
|
|
|
c.lex.buf.setLen(0)
|
2016-08-15 19:30:49 +00:00
|
|
|
e = aliasEvent(id)
|
2016-09-13 11:38:10 +00:00
|
|
|
c.advance()
|
2016-08-15 19:30:49 +00:00
|
|
|
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-13 11:38:10 +00:00
|
|
|
c.advance()
|
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-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-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
|
2016-09-14 16:31:09 +00:00
|
|
|
c.currentScalar(e)
|
2017-02-18 09:40:39 +00:00
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
case c.lex.scalarKind
|
|
|
|
of skSingleQuoted: e.scalarRep = srSingleQuoted
|
|
|
|
of skDoubleQuoted: e.scalarRep = srDoubleQuoted
|
|
|
|
of skLiteral: e.scalarRep = srLiteral
|
|
|
|
of skFolded: e.scalarRep = srFolded
|
2016-08-17 18:04:59 +00:00
|
|
|
result = true
|
|
|
|
state = objectEnd
|
|
|
|
stored = flowAfterObject
|
2016-09-13 11:38:10 +00:00
|
|
|
c.advance()
|
2016-09-14 11:37:56 +00:00
|
|
|
of ltTagHandle, ltLiteralTag:
|
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()
|
2016-09-14 16:31:09 +00:00
|
|
|
c.currentScalar(e)
|
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-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)
|
2017-01-10 11:00:15 +00:00
|
|
|
# we need the extra state for possibly emitting an additional empty value.
|
|
|
|
state = leaveFlowMap
|
|
|
|
return false
|
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:
|
2017-02-14 18:06:41 +00:00
|
|
|
e = implicitScalar()
|
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 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:
|
2017-02-06 19:35:17 +00:00
|
|
|
c.explicitFlowKey = false
|
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
|
|
|
|
|
|
|
# --- parser initialization ---
|
|
|
|
|
2017-02-13 16:10:56 +00:00
|
|
|
proc init(c: ParserContext, p: YamlParser) {.raises: [YamlParserError].} =
|
|
|
|
# this try/except should not be necessary because basicInit cannot raise
|
|
|
|
# anything. however, compiling to JS does not work without it.
|
|
|
|
try: c.basicInit(lastTokenContext)
|
|
|
|
except: discard
|
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.nextImpl = stateInitial
|
2016-09-12 19:38:56 +00:00
|
|
|
c.explicitFlowKey = false
|
2016-09-13 10:01:21 +00:00
|
|
|
c.advance()
|
2016-09-19 17:33:29 +00:00
|
|
|
|
2017-03-29 15:14:50 +00:00
|
|
|
when not defined(JS):
|
|
|
|
proc parse*(p: YamlParser, s: Stream): YamlStream
|
|
|
|
{.raises: [YamlParserError].} =
|
|
|
|
## Parse the given stream as YAML character stream.
|
|
|
|
let c = new(ParserContext)
|
|
|
|
try: c.lex = newYamlLexer(s)
|
|
|
|
except:
|
|
|
|
let e = newException(YamlParserError,
|
|
|
|
"Error while opening stream: " & getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
e.line = 1
|
|
|
|
e.column = 1
|
|
|
|
e.lineContent = ""
|
|
|
|
raise e
|
|
|
|
c.init(p)
|
|
|
|
result = c
|
2016-09-12 16:04:26 +00:00
|
|
|
|
2016-09-20 19:53:38 +00:00
|
|
|
proc parse*(p: YamlParser, str: string): YamlStream
|
|
|
|
{.raises: [YamlParserError].} =
|
|
|
|
## Parse the given string as YAML character stream.
|
2016-09-12 16:04:26 +00:00
|
|
|
let c = new(ParserContext)
|
|
|
|
c.lex = newYamlLexer(str)
|
2016-09-12 19:38:56 +00:00
|
|
|
c.init(p)
|
|
|
|
result = c
|
2017-01-09 18:09:07 +00:00
|
|
|
|
|
|
|
proc anchorName*(p: YamlParser, anchor: AnchorId): string {.raises: [].} =
|
|
|
|
## Retrieve the textual representation of the given anchor as it occurred in
|
|
|
|
## the input (without the leading `&`). Returns the empty string for unknown
|
|
|
|
## anchors.
|
|
|
|
for representation, value in p.anchors:
|
|
|
|
if value == anchor: return representation
|
2017-02-14 18:40:40 +00:00
|
|
|
return ""
|
|
|
|
|
2017-02-14 20:53:15 +00:00
|
|
|
proc renderAttrs(p: YamlParser, tag: TagId, anchor: AnchorId,
|
|
|
|
isPlain: bool): string =
|
2017-02-14 18:40:40 +00:00
|
|
|
result = ""
|
2017-02-14 21:08:38 +00:00
|
|
|
if anchor != yAnchorNone: result &= " &" & p.anchorName(anchor)
|
2017-02-14 18:40:40 +00:00
|
|
|
case tag
|
|
|
|
of yTagQuestionmark: discard
|
2017-02-14 20:53:15 +00:00
|
|
|
of yTagExclamationmark:
|
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
if isPlain: result &= " <!>"
|
2017-02-14 18:40:40 +00:00
|
|
|
else:
|
2017-02-14 21:22:56 +00:00
|
|
|
result &= " <" & p.taglib.uri(tag) & ">"
|
2017-02-14 18:40:40 +00:00
|
|
|
|
|
|
|
proc display*(p: YamlParser, event: YamlStreamEvent): string
|
|
|
|
{.raises: [KeyError].} =
|
|
|
|
## Generate a representation of the given event with proper visualization of
|
|
|
|
## anchor and tag (if any). The generated representation is conformant to the
|
|
|
|
## format used in the yaml test suite.
|
|
|
|
##
|
|
|
|
## This proc is an informed version of ``$`` on ``YamlStreamEvent`` which can
|
|
|
|
## properly display the anchor and tag name as it occurs in the input.
|
|
|
|
## However, it shall only be used while using the streaming API because after
|
|
|
|
## finishing the parsing of a document, the parser drops all information about
|
|
|
|
## anchor and tag names.
|
|
|
|
case event.kind
|
|
|
|
of yamlEndMap: result = "-MAP"
|
|
|
|
of yamlEndSeq: result = "-SEQ"
|
2017-02-14 21:06:48 +00:00
|
|
|
of yamlStartDoc:
|
|
|
|
result = "+DOC"
|
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
if event.explicitDirectivesEnd: result &= " ---"
|
2017-02-14 21:30:48 +00:00
|
|
|
of yamlEndDoc:
|
|
|
|
result = "-DOC"
|
|
|
|
when defined(yamlScalarRepInd):
|
|
|
|
if event.explicitDocumentEnd: result &= " ..."
|
2017-02-14 20:53:15 +00:00
|
|
|
of yamlStartMap:
|
|
|
|
result = "+MAP" & p.renderAttrs(event.mapTag, event.mapAnchor, true)
|
|
|
|
of yamlStartSeq:
|
|
|
|
result = "+SEQ" & p.renderAttrs(event.seqTag, event.seqAnchor, true)
|
2017-02-14 18:40:40 +00:00
|
|
|
of yamlScalar:
|
|
|
|
when defined(yamlScalarRepInd):
|
2017-02-14 20:53:15 +00:00
|
|
|
result = "=VAL" & p.renderAttrs(event.scalarTag, event.scalarAnchor,
|
|
|
|
event.scalarRep == srPlain)
|
2017-02-14 18:40:40 +00:00
|
|
|
case event.scalarRep
|
|
|
|
of srPlain: result &= " :"
|
|
|
|
of srSingleQuoted: result &= " \'"
|
|
|
|
of srDoubleQuoted: result &= " \""
|
|
|
|
of srLiteral: result &= " |"
|
|
|
|
of srFolded: result &= " >"
|
2017-02-14 20:53:15 +00:00
|
|
|
else:
|
|
|
|
let isPlain = event.scalarTag == yTagExclamationmark
|
|
|
|
result = "=VAL" & p.renderAttrs(event.scalarTag, event.scalarAnchor,
|
|
|
|
isPlain)
|
|
|
|
if isPlain: result &= " :"
|
|
|
|
else: result &= " \""
|
2017-02-14 18:40:40 +00:00
|
|
|
result &= yamlTestSuiteEscape(event.scalarContent)
|
|
|
|
of yamlAlias: result = "=ALI *" & p.anchorName(event.aliasTarget)
|