2016-01-24 10:44:10 +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-01-20 20:15:33 +00:00
|
|
|
type
|
|
|
|
FastParseState = enum
|
2016-01-22 20:36:11 +00:00
|
|
|
fpInitial, fpBlockLineStart, fpBlockAfterObject, fpBlockAfterPlainScalar,
|
2016-01-20 22:38:39 +00:00
|
|
|
fpBlockObjectStart, fpBlockContinueScalar, fpExpectDocEnd, fpFlow,
|
|
|
|
fpFlowAfterObject
|
2016-01-20 20:15:33 +00:00
|
|
|
|
|
|
|
FastParseLevelKind = enum
|
|
|
|
fplUnknown, fplSequence, fplMapKey, fplMapValue, fplScalar
|
|
|
|
|
|
|
|
FastParseLevel = object
|
|
|
|
kind: FastParseLevelKind
|
|
|
|
indentation: int
|
|
|
|
|
|
|
|
LexedDirective = enum
|
|
|
|
ldYaml, ldTag, ldUnknown
|
|
|
|
|
|
|
|
LexedPossibleDirectivesEnd = enum
|
|
|
|
lpdeDirectivesEnd, lpdeSequenceItem, lpdeScalarContent
|
|
|
|
|
|
|
|
YamlContext = enum
|
|
|
|
cFlowIn, cFlowOut, cFlowKey, cBlockKey, cBlockIn, cBlockOut
|
|
|
|
|
|
|
|
const
|
|
|
|
space = [' ', '\t']
|
|
|
|
lineEnd = ['\x0A', '\c', EndOfFile]
|
|
|
|
spaceOrLineEnd = [' ', '\t', '\x0A', '\c', EndOfFile]
|
|
|
|
digits = '0'..'9'
|
|
|
|
flowIndicators = ['[', ']', '{', '}', ',']
|
2016-01-24 10:44:10 +00:00
|
|
|
|
|
|
|
UTF8NextLine = toUTF8(0x85.Rune)
|
|
|
|
UTF8NonBreakingSpace = toUTF8(0xA0.Rune)
|
|
|
|
UTF8LineSeparator = toUTF8(0x2028.Rune)
|
|
|
|
UTF8ParagraphSeparator = toUTF8(0x2029.Rune)
|
|
|
|
|
|
|
|
proc newYamlParser*(tagLib: TagLibrary = initExtendedTagLibrary(),
|
|
|
|
callback: WarningCallback = nil): YamlParser =
|
|
|
|
new(result)
|
|
|
|
result.tagLib = tagLib
|
|
|
|
result.callback = callback
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-24 17:24:09 +00:00
|
|
|
proc getLineNumber*(p: YamlParser): int = p.lexer.lineNumber
|
|
|
|
|
|
|
|
proc getColNumber*(p: YamlParser): int = p.tokenstart + 1 # column is 1-based
|
|
|
|
|
|
|
|
proc getLineContent*(p: YamlParser, marker: bool = true): string =
|
|
|
|
result = p.lexer.getCurrentLine(false)
|
|
|
|
if marker:
|
|
|
|
result.add(repeat(' ', p.tokenstart) & "^\n")
|
|
|
|
|
2016-01-20 20:15:33 +00:00
|
|
|
template debug(message: string) {.dirty.} =
|
|
|
|
when defined(yamlDebug):
|
|
|
|
try: styledWriteLine(stdout, fgBlue, message)
|
|
|
|
except IOError: discard
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template parserError(message: string) {.dirty.} =
|
2016-01-20 20:15:33 +00:00
|
|
|
var e = newException(YamlParserError, message)
|
2016-01-24 10:44:10 +00:00
|
|
|
e.line = p.lexer.lineNumber
|
2016-01-24 17:24:09 +00:00
|
|
|
e.column = p.tokenstart + 1
|
|
|
|
e.lineContent = p.getLineContent(true)
|
2016-01-20 20:15:33 +00:00
|
|
|
raise e
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template lexerError(lx: BaseLexer, message: string) {.dirty.} =
|
2016-01-20 20:15:33 +00:00
|
|
|
var e = newException(YamlParserError, message)
|
2016-01-24 10:44:10 +00:00
|
|
|
e.line = lx.lineNumber
|
2016-01-24 17:24:09 +00:00
|
|
|
e.column = lx.bufpos + 1
|
2016-01-24 10:44:10 +00:00
|
|
|
e.lineContent = lx.getCurrentLine(false) &
|
|
|
|
repeat(' ', lx.getColNumber(lx.bufpos)) & "^\n"
|
2016-01-20 20:15:33 +00:00
|
|
|
raise e
|
|
|
|
|
2016-01-22 20:36:11 +00:00
|
|
|
template yieldLevelEnd() {.dirty.} =
|
2016-01-20 20:15:33 +00:00
|
|
|
case level.kind
|
|
|
|
of fplSequence:
|
|
|
|
yield endSeqEvent()
|
|
|
|
of fplMapKey:
|
|
|
|
yield endMapEvent()
|
|
|
|
of fplMapValue:
|
|
|
|
yield scalarEvent("", tag, anchor)
|
|
|
|
tag = yTagQuestionMark
|
|
|
|
anchor = yAnchorNone
|
|
|
|
yield endMapEvent()
|
|
|
|
of fplScalar:
|
2016-01-22 20:36:11 +00:00
|
|
|
yield scalarEvent(content, tag, anchor)
|
2016-01-23 16:44:50 +00:00
|
|
|
tag = yTagQuestionMark
|
|
|
|
anchor = yAnchorNone
|
2016-01-20 20:15:33 +00:00
|
|
|
of fplUnknown:
|
2016-01-23 16:44:50 +00:00
|
|
|
yield scalarEvent("", tag, anchor)
|
|
|
|
tag = yTagQuestionMark
|
|
|
|
anchor = yAnchorNone
|
2016-01-20 20:15:33 +00:00
|
|
|
|
|
|
|
template handleLineEnd(insideDocument: bool) {.dirty.} =
|
2016-01-24 10:44:10 +00:00
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\x0A':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\c':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
of EndOfFile:
|
|
|
|
when insideDocument:
|
|
|
|
closeEverything()
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2016-01-22 20:36:11 +00:00
|
|
|
template handleObjectEnd(nextState: FastParseState) {.dirty.} =
|
|
|
|
if ancestry.len == 0:
|
|
|
|
state = fpExpectDocEnd
|
|
|
|
else:
|
|
|
|
level = ancestry.pop()
|
|
|
|
state = nextState
|
|
|
|
tag = yTagQuestionMark
|
|
|
|
anchor = yAnchorNone
|
|
|
|
case level.kind
|
|
|
|
of fplMapKey:
|
|
|
|
level.kind = fplMapValue
|
|
|
|
of fplMapValue:
|
|
|
|
level.kind = fplMapKey
|
|
|
|
of fplSequence:
|
|
|
|
discard
|
|
|
|
of fplUnknown, fplScalar:
|
|
|
|
assert(false)
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-23 16:44:50 +00:00
|
|
|
template handleObjectStart(k: YamlStreamEventKind) {.dirty.} =
|
2016-01-22 20:36:11 +00:00
|
|
|
assert(level.kind == fplUnknown)
|
2016-01-20 20:15:33 +00:00
|
|
|
when k == yamlStartMap:
|
2016-01-23 16:44:50 +00:00
|
|
|
yield startMapEvent(tag, anchor)
|
|
|
|
debug("started map at " & (if level.indentation == -1: $indentation else:
|
|
|
|
$level.indentation))
|
2016-01-22 20:36:11 +00:00
|
|
|
level.kind = fplMapKey
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
2016-01-23 16:44:50 +00:00
|
|
|
yield startSeqEvent(tag, anchor)
|
|
|
|
debug("started sequence at " & (if level.indentation == -1: $indentation else:
|
|
|
|
$level.indentation))
|
2016-01-22 20:36:11 +00:00
|
|
|
level.kind = fplSequence
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-23 16:44:50 +00:00
|
|
|
anchor = yAnchorNone
|
|
|
|
if level.indentation == -1:
|
|
|
|
level.indentation = indentation
|
2016-01-22 20:36:11 +00:00
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-20 20:15:33 +00:00
|
|
|
|
|
|
|
template closeMoreIndentedLevels() {.dirty.} =
|
|
|
|
while ancestry.len > 0:
|
|
|
|
let parent = ancestry[ancestry.high]
|
|
|
|
if parent.indentation >= indentation:
|
2016-01-22 20:36:11 +00:00
|
|
|
debug("Closing because parent.indentation (" & $parent.indentation &
|
|
|
|
") >= indentation(" & $indentation & ")")
|
|
|
|
yieldLevelEnd()
|
|
|
|
handleObjectEnd(fpBlockAfterObject)
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
template closeEverything() {.dirty.} =
|
|
|
|
indentation = 0
|
|
|
|
closeMoreIndentedLevels()
|
2016-01-22 20:36:11 +00:00
|
|
|
yieldLevelEnd()
|
2016-01-20 20:15:33 +00:00
|
|
|
yield endDocEvent()
|
|
|
|
|
2016-01-22 20:36:11 +00:00
|
|
|
template handleBlockSequenceIndicator() {.dirty.} =
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
2016-01-20 20:15:33 +00:00
|
|
|
case level.kind
|
|
|
|
of fplUnknown:
|
2016-01-23 16:44:50 +00:00
|
|
|
handleObjectStart(yamlStartSequence)
|
2016-01-20 20:15:33 +00:00
|
|
|
of fplSequence:
|
|
|
|
if level.indentation != indentation:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Invalid indentation of block sequence indicator")
|
2016-01-22 20:36:11 +00:00
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Illegal sequence item in map")
|
|
|
|
p.lexer.skipWhitespace()
|
|
|
|
indentation = p.lexer.getColNumber(p.lexer.bufpos)
|
2016-01-22 22:44:26 +00:00
|
|
|
|
|
|
|
template handleMapKeyIndicator() {.dirty.} =
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
2016-01-22 22:44:26 +00:00
|
|
|
case level.kind
|
|
|
|
of fplUnknown:
|
2016-01-23 16:44:50 +00:00
|
|
|
handleObjectStart(yamlStartMap)
|
2016-01-22 22:44:26 +00:00
|
|
|
of fplMapValue:
|
|
|
|
if level.indentation != indentation:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Invalid indentation of map key indicator")
|
2016-01-24 18:15:50 +00:00
|
|
|
yield scalarEvent("", yTagQuestionMark, yAnchorNone)
|
2016-01-22 22:44:26 +00:00
|
|
|
level.kind = fplMapKey
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-22 22:44:26 +00:00
|
|
|
of fplMapKey:
|
|
|
|
if level.indentation != indentation:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Invalid indentation of map key indicator")
|
2016-01-22 22:44:26 +00:00
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-22 22:44:26 +00:00
|
|
|
of fplSequence:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected map key indicator (expected '- ')")
|
2016-01-22 22:44:26 +00:00
|
|
|
of fplScalar:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected map key indicator (expected multiline scalar end)")
|
|
|
|
p.lexer.skipWhitespace()
|
|
|
|
indentation = p.lexer.getColNumber(p.lexer.bufpos)
|
2016-01-22 22:44:26 +00:00
|
|
|
|
|
|
|
template handleMapValueIndicator() {.dirty.} =
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
2016-01-22 22:44:26 +00:00
|
|
|
case level.kind
|
|
|
|
of fplUnknown:
|
2016-01-23 16:44:50 +00:00
|
|
|
if level.indentation == -1:
|
|
|
|
handleObjectStart(yamlStartMap)
|
2016-01-24 18:15:50 +00:00
|
|
|
yield scalarEvent("", yTagQuestionMark, yAnchorNone)
|
2016-01-23 16:44:50 +00:00
|
|
|
else:
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-23 16:44:50 +00:00
|
|
|
anchor = yAnchorNone
|
|
|
|
ancestry[ancestry.high].kind = fplMapValue
|
2016-01-22 22:44:26 +00:00
|
|
|
of fplMapKey:
|
|
|
|
if level.indentation != indentation:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Invalid indentation of map key indicator")
|
2016-01-24 18:15:50 +00:00
|
|
|
yield scalarEvent("", yTagQuestionMark, yAnchorNone)
|
2016-01-22 22:44:26 +00:00
|
|
|
level.kind = fplMapValue
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-22 22:44:26 +00:00
|
|
|
of fplMapValue:
|
|
|
|
if level.indentation != indentation:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Invalid indentation of map key indicator")
|
2016-01-22 22:44:26 +00:00
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-22 22:44:26 +00:00
|
|
|
of fplSequence:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected map value indicator (expected '- ')")
|
2016-01-22 22:44:26 +00:00
|
|
|
of fplScalar:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError(
|
|
|
|
"Unexpected map value indicator (expected multiline scalar end)")
|
|
|
|
p.lexer.skipWhitespace()
|
|
|
|
indentation = p.lexer.getColNumber(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
|
|
|
|
template initDocValues() {.dirty.} =
|
|
|
|
shorthands = initTable[string, string]()
|
|
|
|
anchors = initTable[string, AnchorId]()
|
|
|
|
shorthands["!"] = "!"
|
|
|
|
shorthands["!!"] = "tag:yaml.org,2002:"
|
|
|
|
nextAnchorId = 0.AnchorId
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-20 22:38:39 +00:00
|
|
|
anchor = yAnchorNone
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-20 22:38:39 +00:00
|
|
|
template handleTagHandle() {.dirty.} =
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
2016-01-22 20:36:11 +00:00
|
|
|
if level.kind != fplUnknown:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected tag handle")
|
2016-01-24 18:15:50 +00:00
|
|
|
if tag != yTagQuestionMark:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Only one tag handle is allowed per node")
|
2016-01-20 22:38:39 +00:00
|
|
|
content = ""
|
|
|
|
var
|
|
|
|
shorthandEnd: int
|
|
|
|
tagUri: string
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.tagHandle(content, shorthandEnd)
|
2016-01-20 22:38:39 +00:00
|
|
|
if shorthandEnd != -1:
|
|
|
|
try:
|
|
|
|
let prefix = shorthands[content[0..shorthandEnd]]
|
|
|
|
tagUri = prefix & content[shorthandEnd + 1 .. ^1]
|
|
|
|
except KeyError:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Undefined tag shorthand: " & content[0..shorthandEnd])
|
2016-01-20 22:38:39 +00:00
|
|
|
else:
|
|
|
|
shallowCopy(tagUri, content)
|
|
|
|
try:
|
2016-01-24 10:44:10 +00:00
|
|
|
tag = p.tagLib.tags[tagUri]
|
2016-01-20 22:38:39 +00:00
|
|
|
except KeyError:
|
2016-01-24 10:44:10 +00:00
|
|
|
tag = p.tagLib.registerUri(tagUri)
|
2016-01-20 22:38:39 +00:00
|
|
|
|
|
|
|
template handleAnchor() {.dirty.} =
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
2016-01-22 20:36:11 +00:00
|
|
|
if level.kind != fplUnknown:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected token")
|
2016-01-20 22:38:39 +00:00
|
|
|
if anchor != yAnchorNone:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Only one anchor is allowed per node")
|
2016-01-20 22:38:39 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.anchorName(content)
|
2016-01-20 22:38:39 +00:00
|
|
|
anchor = nextAnchorId
|
|
|
|
anchors[content] = anchor
|
|
|
|
nextAnchorId = cast[AnchorId](cast[int](nextAnchorId) + 1)
|
|
|
|
|
|
|
|
template handleAlias() {.dirty.} =
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
2016-01-22 20:36:11 +00:00
|
|
|
if level.kind != fplUnknown:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected token")
|
2016-01-24 18:15:50 +00:00
|
|
|
if anchor != yAnchorNone or tag != yTagQuestionMark:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Alias may not have anchor or tag")
|
2016-01-20 22:38:39 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.anchorName(content)
|
2016-01-22 20:36:11 +00:00
|
|
|
var id: AnchorId
|
2016-01-20 22:38:39 +00:00
|
|
|
try:
|
2016-01-22 20:36:11 +00:00
|
|
|
id = anchors[content]
|
2016-01-20 22:38:39 +00:00
|
|
|
except KeyError:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unknown anchor")
|
2016-01-22 20:36:11 +00:00
|
|
|
yield aliasEvent(id)
|
|
|
|
handleObjectEnd(fpBlockAfterObject)
|
2016-01-20 22:38:39 +00:00
|
|
|
|
|
|
|
template leaveFlowLevel() {.dirty.} =
|
|
|
|
flowdepth.inc(-1)
|
2016-01-22 20:36:11 +00:00
|
|
|
if flowdepth == 0:
|
|
|
|
yieldLevelEnd()
|
|
|
|
handleObjectEnd(fpBlockAfterObject)
|
2016-01-20 22:38:39 +00:00
|
|
|
else:
|
2016-01-22 20:36:11 +00:00
|
|
|
yieldLevelEnd()
|
|
|
|
handleObjectEnd(fpFlowAfterObject)
|
|
|
|
|
|
|
|
template handlePossibleMapStart() {.dirty.} =
|
2016-01-23 16:44:50 +00:00
|
|
|
if level.indentation == -1:
|
|
|
|
var flowDepth = 0
|
2016-01-24 10:44:10 +00:00
|
|
|
for pos in countup(p.lexer.bufpos, p.lexer.bufpos + 1024):
|
|
|
|
case p.lexer.buf[pos]
|
2016-01-23 16:44:50 +00:00
|
|
|
of ':':
|
2016-01-24 10:44:10 +00:00
|
|
|
if flowDepth == 0 and p.lexer.buf[pos + 1] in spaceOrLineEnd:
|
2016-01-23 16:44:50 +00:00
|
|
|
handleObjectStart(yamlStartMap)
|
|
|
|
break
|
|
|
|
of lineEnd:
|
2016-01-22 20:36:11 +00:00
|
|
|
break
|
2016-01-23 16:44:50 +00:00
|
|
|
of '[', '{':
|
|
|
|
flowDepth.inc()
|
|
|
|
of '}', ']':
|
|
|
|
flowDepth.inc(-1)
|
|
|
|
of '?':
|
|
|
|
if flowDepth == 0: break
|
|
|
|
of '#':
|
2016-01-24 10:44:10 +00:00
|
|
|
if p.lexer.buf[pos - 1] in space:
|
2016-01-23 16:44:50 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
if level.indentation == -1:
|
|
|
|
level.indentation = indentation
|
2016-01-22 20:36:11 +00:00
|
|
|
|
|
|
|
template handleBlockItemStart() {.dirty.} =
|
|
|
|
case level.kind
|
|
|
|
of fplUnknown:
|
2016-01-23 16:44:50 +00:00
|
|
|
handlePossibleMapStart()
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplSequence:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected token (expected block sequence indicator)")
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplMapKey:
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: indentation)
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplMapValue:
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-22 20:36:11 +00:00
|
|
|
anchor = yAnchorNone
|
|
|
|
level.kind = fplMapKey
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: indentation)
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplScalar:
|
|
|
|
assert(false)
|
2016-01-20 22:38:39 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template startToken() {.dirty.} =
|
|
|
|
p.tokenstart = p.lexer.getColNumber(p.lexer.bufpos)
|
|
|
|
|
|
|
|
template finishLine(lexer: BaseLexer) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: finishLine")
|
|
|
|
while lexer.buf[lexer.bufpos] notin lineEnd:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template skipWhitespace(lexer: BaseLexer) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: skipWhitespace")
|
|
|
|
while lexer.buf[lexer.bufpos] in space: lexer.bufpos.inc()
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template skipWhitespaceAndNewlines(lexer: BaseLexer) =
|
2016-01-20 22:38:39 +00:00
|
|
|
debug("lex: skipWhitespaceAndNewLines")
|
|
|
|
while true:
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of space:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
of '\x0A':
|
|
|
|
lexer.bufpos = lexer.handleLF(lexer.bufpos)
|
|
|
|
of '\c':
|
|
|
|
lexer.bufpos = lexer.handleCR(lexer.bufpos)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template skipIndentation(lexer: BaseLexer) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: skipIndentation")
|
|
|
|
while lexer.buf[lexer.bufpos] == ' ': lexer.bufpos.inc()
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template directiveName(lexer: BaseLexer, directive: var LexedDirective) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: directiveName")
|
|
|
|
directive = ldUnknown
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] == 'Y':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] == 'A':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] == 'M':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] == 'L':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] in spaceOrLineEnd:
|
|
|
|
directive = ldYaml
|
|
|
|
elif lexer.buf[lexer.bufpos] == 'T':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] == 'A':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] == 'G':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] in [' ', '\t', '\x0A', '\c', EndOfFile]:
|
|
|
|
directive = ldTag
|
|
|
|
while lexer.buf[lexer.bufpos] notin spaceOrLineEnd:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template yamlVersion(lexer: BaseLexer, o: var string) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: yamlVersion")
|
|
|
|
while lexer.buf[lexer.bufpos] in space:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
var c = lexer.buf[lexer.bufpos]
|
|
|
|
if c notin digits:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Invalid YAML version number")
|
2016-01-20 20:15:33 +00:00
|
|
|
o.add(c)
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
c = lexer.buf[lexer.bufpos]
|
|
|
|
while c in digits:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
o.add(c)
|
|
|
|
c = lexer.buf[lexer.bufpos]
|
|
|
|
if lexer.buf[lexer.bufpos] != '.':
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Invalid YAML version number")
|
2016-01-24 17:24:09 +00:00
|
|
|
o.add('.')
|
2016-01-20 20:15:33 +00:00
|
|
|
lexer.bufpos.inc()
|
2016-01-24 17:24:09 +00:00
|
|
|
c = lexer.buf[lexer.bufpos]
|
|
|
|
if c notin digits:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Invalid YAML version number")
|
2016-01-24 17:24:09 +00:00
|
|
|
o.add(c)
|
2016-01-20 20:15:33 +00:00
|
|
|
lexer.bufpos.inc()
|
2016-01-24 17:24:09 +00:00
|
|
|
c = lexer.buf[lexer.bufpos]
|
|
|
|
while c in digits:
|
|
|
|
o.add(c)
|
2016-01-20 20:15:33 +00:00
|
|
|
lexer.bufpos.inc()
|
2016-01-24 17:24:09 +00:00
|
|
|
c = lexer.buf[lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
if lexer.buf[lexer.bufpos] notin spaceOrLineEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Invalid YAML version number")
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template lineEnding(lexer: BaseLexer) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: lineEnding")
|
|
|
|
if lexer.buf[lexer.bufpos] notin lineEnd:
|
|
|
|
while lexer.buf[lexer.bufpos] in space:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] in lineEnd:
|
|
|
|
discard
|
|
|
|
elif lexer.buf[lexer.bufpos] == '#':
|
|
|
|
while lexer.buf[lexer.bufpos] notin lineEnd:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected token (expected comment or line end)")
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template tagShorthand(lexer: BaseLexer, shorthand: var string) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: tagShorthand")
|
|
|
|
while lexer.buf[lexer.bufpos] in space:
|
|
|
|
lexer.bufpos.inc()
|
2016-01-24 10:44:10 +00:00
|
|
|
assert lexer.buf[lexer.bufpos] == '!'
|
2016-01-20 20:15:33 +00:00
|
|
|
shorthand.add('!')
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
var c = lexer.buf[lexer.bufpos]
|
|
|
|
if c in spaceOrLineEnd:
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
while c != '!':
|
|
|
|
case c
|
|
|
|
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '-':
|
|
|
|
shorthand.add(c)
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
c = lexer.buf[lexer.bufpos]
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Illegal character in tag shorthand")
|
2016-01-20 20:15:33 +00:00
|
|
|
shorthand.add(c)
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] notin spaceOrLineEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Missing space after tag shorthand")
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template tagUri(lexer: BaseLexer, uri: var string) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: tagUri")
|
|
|
|
while lexer.buf[lexer.bufpos] in space:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
var c = lexer.buf[lexer.bufpos]
|
|
|
|
while c notin spaceOrLineEnd:
|
|
|
|
case c
|
|
|
|
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':', '@', '&',
|
|
|
|
'-', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')':
|
|
|
|
uri.add(c)
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
c = lexer.buf[lexer.bufpos]
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Invalid tag uri")
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template directivesEnd(lexer: BaseLexer,
|
2016-01-20 20:15:33 +00:00
|
|
|
token: var LexedPossibleDirectivesEnd) =
|
|
|
|
debug("lex: directivesEnd")
|
2016-01-22 20:36:11 +00:00
|
|
|
var p = lexer.bufpos + 1
|
|
|
|
case lexer.buf[p]
|
2016-01-20 20:15:33 +00:00
|
|
|
of '-':
|
2016-01-22 20:36:11 +00:00
|
|
|
p.inc()
|
|
|
|
if lexer.buf[p] == '-':
|
|
|
|
p.inc()
|
|
|
|
if lexer.buf[p] in spaceOrLineEnd:
|
2016-01-20 20:15:33 +00:00
|
|
|
token = lpdeDirectivesEnd
|
|
|
|
else:
|
|
|
|
token = lpdeScalarContent
|
|
|
|
else:
|
|
|
|
token = lpdeScalarContent
|
|
|
|
of spaceOrLineEnd:
|
|
|
|
token = lpdeSequenceItem
|
|
|
|
else:
|
|
|
|
token = lpdeScalarContent
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template documentEnd(lexer: var BaseLexer, isDocumentEnd: var bool) =
|
2016-01-22 20:36:11 +00:00
|
|
|
var p = lexer.bufpos + 1
|
|
|
|
if lexer.buf[p] == '.':
|
|
|
|
p.inc()
|
|
|
|
if lexer.buf[p] == '.':
|
|
|
|
p.inc()
|
|
|
|
if lexer.buf[p] in spaceOrLineEnd:
|
2016-01-20 20:15:33 +00:00
|
|
|
isDocumentEnd = true
|
|
|
|
else:
|
|
|
|
isDocumentEnd = false
|
|
|
|
else:
|
|
|
|
isDocumentEnd = false
|
|
|
|
else:
|
|
|
|
isDocumentEnd = false
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template singleQuotedScalar(lexer: BaseLexer, content: var string) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: singleQuotedScalar")
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
while true:
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of '\'':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] == '\'':
|
|
|
|
content.add('\'')
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
of EndOfFile:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Unfinished single quoted string")
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
|
|
|
content.add(lexer.buf[lexer.bufpos])
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
proc unicodeSequence(lexer: var BaseLexer, length: int):
|
2016-01-20 20:15:33 +00:00
|
|
|
string {.raises: [YamlParserError].} =
|
|
|
|
debug("lex: unicodeSequence")
|
2016-01-24 10:44:10 +00:00
|
|
|
var unicodeChar = 0.int
|
2016-01-20 20:15:33 +00:00
|
|
|
for i in countup(0, length - 1):
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
let
|
|
|
|
digitPosition = length - i - 1
|
|
|
|
c = lexer.buf[lexer.bufpos]
|
|
|
|
case c
|
|
|
|
of EndOFFile:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Unfinished unicode escape sequence")
|
2016-01-20 20:15:33 +00:00
|
|
|
of '0' .. '9':
|
|
|
|
unicodeChar = unicodechar or
|
|
|
|
(cast[int](c) - 0x30) shl (digitPosition * 4)
|
|
|
|
of 'A' .. 'F':
|
|
|
|
unicodeChar = unicodechar or
|
|
|
|
(cast[int](c) - 0x37) shl (digitPosition * 4)
|
|
|
|
of 'a' .. 'f':
|
|
|
|
unicodeChar = unicodechar or
|
|
|
|
(cast[int](c) - 0x57) shl (digitPosition * 4)
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Invalid character in unicode escape sequence")
|
|
|
|
return toUTF8(cast[Rune](unicodeChar))
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-23 14:29:45 +00:00
|
|
|
template processDoubleQuotedWhitespace(newlines: var int) {.dirty.} =
|
|
|
|
var
|
|
|
|
after = ""
|
|
|
|
block outer:
|
|
|
|
while true:
|
2016-01-24 10:44:10 +00:00
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-23 14:29:45 +00:00
|
|
|
of ' ', '\t':
|
2016-01-24 10:44:10 +00:00
|
|
|
after.add(p.lexer.buf[p.lexer.bufpos])
|
2016-01-23 14:29:45 +00:00
|
|
|
of '\x0A':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-23 14:29:45 +00:00
|
|
|
break
|
|
|
|
of '\c':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-23 14:29:45 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
content.add(after)
|
|
|
|
break outer
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-23 14:29:45 +00:00
|
|
|
while true:
|
2016-01-24 10:44:10 +00:00
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-23 14:29:45 +00:00
|
|
|
of ' ', '\t':
|
|
|
|
discard
|
|
|
|
of '\x0A':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-23 14:29:45 +00:00
|
|
|
newlines.inc()
|
|
|
|
of '\c':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-23 14:29:45 +00:00
|
|
|
newlines.inc()
|
|
|
|
else:
|
|
|
|
if newlines == 0:
|
|
|
|
discard
|
|
|
|
elif newlines == 1:
|
|
|
|
content.add(' ')
|
|
|
|
else:
|
|
|
|
content.add(repeat('\x0A', newlines - 1))
|
|
|
|
break
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-23 14:29:45 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template doubleQuotedScalar(lexer: BaseLexer, content: var string) =
|
2016-01-23 14:29:45 +00:00
|
|
|
debug("lex: doubleQuotedScalar")
|
|
|
|
lexer.bufpos.inc()
|
2016-01-20 20:15:33 +00:00
|
|
|
while true:
|
2016-01-23 14:29:45 +00:00
|
|
|
var c = lexer.buf[lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
case c
|
|
|
|
of EndOfFile:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Unfinished double quoted string")
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\\':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of EndOfFile:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Unfinished escape sequence")
|
2016-01-20 20:15:33 +00:00
|
|
|
of '0': content.add('\0')
|
|
|
|
of 'a': content.add('\x07')
|
|
|
|
of 'b': content.add('\x08')
|
|
|
|
of '\t', 't': content.add('\t')
|
|
|
|
of 'n': content.add('\x0A')
|
|
|
|
of 'v': content.add('\v')
|
|
|
|
of 'f': content.add('\f')
|
|
|
|
of 'r': content.add('\r')
|
|
|
|
of 'e': content.add('\e')
|
|
|
|
of ' ': content.add(' ')
|
|
|
|
of '"': content.add('"')
|
|
|
|
of '/': content.add('/')
|
|
|
|
of '\\': content.add('\\')
|
|
|
|
of 'N': content.add(UTF8NextLine)
|
|
|
|
of '_': content.add(UTF8NonBreakingSpace)
|
|
|
|
of 'L': content.add(UTF8LineSeparator)
|
|
|
|
of 'P': content.add(UTF8ParagraphSeparator)
|
|
|
|
of 'x': content.add(lexer.unicodeSequence(2))
|
|
|
|
of 'u': content.add(lexer.unicodeSequence(4))
|
|
|
|
of 'U': content.add(lexer.unicodeSequence(8))
|
2016-01-23 14:29:45 +00:00
|
|
|
of '\x0A', '\c':
|
|
|
|
var newlines = 0
|
|
|
|
processDoubleQuotedWhitespace(newlines)
|
|
|
|
continue
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Illegal character in escape sequence")
|
2016-01-20 20:15:33 +00:00
|
|
|
of '"':
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
break
|
2016-01-23 14:29:45 +00:00
|
|
|
of '\x0A', '\c', '\t', ' ':
|
|
|
|
var newlines = 1
|
|
|
|
processdoubleQuotedWhitespace(newlines)
|
|
|
|
continue
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
|
|
|
content.add(c)
|
2016-01-23 14:29:45 +00:00
|
|
|
lexer.bufpos.inc()
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
proc isPlainSafe(lexer: BaseLexer, index: int, context: YamlContext): bool =
|
2016-01-20 20:15:33 +00:00
|
|
|
case lexer.buf[lexer.bufpos + 1]
|
|
|
|
of spaceOrLineEnd:
|
|
|
|
result = false
|
|
|
|
of flowIndicators:
|
2016-01-22 22:44:26 +00:00
|
|
|
result = context in [cBlockIn, cBlockOut, cBlockKey]
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
|
|
|
result = true
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template plainScalar(lexer: BaseLexer, content: var string,
|
2016-01-20 20:15:33 +00:00
|
|
|
context: YamlContext) =
|
|
|
|
debug("lex: plainScalar")
|
|
|
|
content.add(lexer.buf[lexer.bufpos])
|
|
|
|
block outer:
|
|
|
|
while true:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
let c = lexer.buf[lexer.bufpos]
|
|
|
|
case c
|
|
|
|
of lineEnd:
|
|
|
|
break
|
|
|
|
of ' ', '\t':
|
|
|
|
var after = "" & c
|
|
|
|
while true:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
let c2 = lexer.buf[lexer.bufpos]
|
|
|
|
case c2
|
|
|
|
of ' ', '\t':
|
|
|
|
after.add(c2)
|
|
|
|
of lineEnd:
|
|
|
|
break outer
|
|
|
|
of ':':
|
|
|
|
if lexer.isPlainSafe(lexer.bufpos + 1, context):
|
|
|
|
content.add(after & ':')
|
|
|
|
else:
|
|
|
|
break outer
|
|
|
|
of '#':
|
|
|
|
break outer
|
|
|
|
else:
|
|
|
|
content.add(after)
|
|
|
|
content.add(c2)
|
|
|
|
break
|
|
|
|
of flowIndicators:
|
2016-01-20 22:38:39 +00:00
|
|
|
if context in [cBlockOut, cBlockIn, cBlockKey]:
|
2016-01-20 20:15:33 +00:00
|
|
|
content.add(c)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
of ':':
|
|
|
|
if lexer.isPlainSafe(lexer.bufpos + 1, context):
|
|
|
|
content.add(':')
|
|
|
|
else:
|
|
|
|
break outer
|
|
|
|
of '#':
|
|
|
|
break outer
|
|
|
|
else:
|
|
|
|
content.add(c)
|
|
|
|
|
|
|
|
template continueMultilineScalar() {.dirty.} =
|
2016-01-22 20:36:11 +00:00
|
|
|
content.add(if newlines == 1: " " else: repeat('\x0A', newlines - 1))
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.plainScalar(content, cBlockOut)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockAfterPlainScalar
|
|
|
|
|
2016-01-20 22:38:39 +00:00
|
|
|
template handleFlowPlainScalar() {.dirty.} =
|
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.plainScalar(content, cFlowOut)
|
|
|
|
if p.lexer.buf[p.lexer.bufpos] in ['{', '}', '[', ']', ',', ':', '#']:
|
2016-01-20 22:38:39 +00:00
|
|
|
discard
|
|
|
|
else:
|
|
|
|
var newlines = 0
|
|
|
|
while true:
|
2016-01-24 10:44:10 +00:00
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 22:38:39 +00:00
|
|
|
of ':':
|
2016-01-24 10:44:10 +00:00
|
|
|
if p.lexer.isPlainSafe(p.lexer.bufpos + 1, cFlowOut):
|
2016-01-22 22:44:26 +00:00
|
|
|
if newlines == 1:
|
|
|
|
content.add(' ')
|
|
|
|
newlines = 0
|
|
|
|
elif newlines > 1:
|
|
|
|
content.add(repeat(' ', newlines - 1))
|
|
|
|
newlines = 0
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.plainScalar(content, cFlowOut)
|
2016-01-22 22:44:26 +00:00
|
|
|
elif explicitFlowKey:
|
|
|
|
break
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Multiline scalar is not allowed as implicit key")
|
2016-01-20 22:38:39 +00:00
|
|
|
of '#', EndOfFile:
|
|
|
|
break
|
|
|
|
of '\x0A':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-20 22:38:39 +00:00
|
|
|
newlines.inc()
|
|
|
|
of '\c':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-20 22:38:39 +00:00
|
|
|
newlines.inc()
|
|
|
|
of flowIndicators:
|
|
|
|
break
|
|
|
|
of ' ', '\t':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.skipWhitespace()
|
2016-01-20 22:38:39 +00:00
|
|
|
else:
|
|
|
|
if newlines == 1:
|
|
|
|
content.add(' ')
|
|
|
|
newlines = 0
|
|
|
|
elif newlines > 1:
|
|
|
|
content.add(repeat(' ', newlines - 1))
|
|
|
|
newlines = 0
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.plainScalar(content, cFlowOut)
|
2016-01-20 22:38:39 +00:00
|
|
|
yield scalarEvent(content, tag, anchor)
|
2016-01-22 20:36:11 +00:00
|
|
|
handleObjectEnd(fpFlowAfterObject)
|
|
|
|
|
|
|
|
template ensureCorrectIndentation() {.dirty.} =
|
|
|
|
if level.indentation != indentation:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Invalid indentation (expected indentation of " &
|
|
|
|
$level.indentation & ")")
|
2016-01-20 22:38:39 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template tagHandle(lexer: var BaseLexer, content: var string,
|
2016-01-20 20:15:33 +00:00
|
|
|
shorthandEnd: var int) =
|
|
|
|
debug("lex: tagHandle")
|
|
|
|
shorthandEnd = 0
|
|
|
|
content.add(lexer.buf[lexer.bufpos])
|
|
|
|
var i = 0
|
|
|
|
while true:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
i.inc()
|
|
|
|
let c = lexer.buf[lexer.bufpos]
|
|
|
|
case c
|
|
|
|
of spaceOrLineEnd:
|
|
|
|
if shorthandEnd == -1:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Unclosed verbatim tag")
|
2016-01-20 20:15:33 +00:00
|
|
|
break
|
|
|
|
of '!':
|
|
|
|
if shorthandEnd == -1 and i == 2:
|
|
|
|
content.add(c)
|
|
|
|
elif shorthandEnd != 0:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Illegal character in tag suffix")
|
2016-01-20 20:15:33 +00:00
|
|
|
shorthandEnd = i
|
|
|
|
content.add(c)
|
|
|
|
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':', '@', '&',
|
|
|
|
'-', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')':
|
|
|
|
content.add(c)
|
|
|
|
of '<':
|
|
|
|
if i == 1:
|
|
|
|
shorthandEnd = -1
|
|
|
|
content = ""
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Illegal character in tag handle")
|
2016-01-20 20:15:33 +00:00
|
|
|
of '>':
|
|
|
|
if shorthandEnd == -1:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if lexer.buf[lexer.bufpos] notin spaceOrLineEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Missing space after verbatim tag handle")
|
2016-01-20 20:15:33 +00:00
|
|
|
break
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Illegal character in tag handle")
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Illegal character in tag handle")
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template anchorName(lexer: BaseLexer, content: var string) =
|
2016-01-20 20:15:33 +00:00
|
|
|
debug("lex: anchorName")
|
|
|
|
while true:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
let c = lexer.buf[lexer.bufpos]
|
|
|
|
case c
|
2016-01-23 16:44:50 +00:00
|
|
|
of spaceOrLineEnd, '[', ']', '{', '}', ',':
|
2016-01-20 20:15:33 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
content.add(c)
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
template blockScalar(lexer: BaseLexer, content: var string,
|
2016-01-23 13:09:52 +00:00
|
|
|
stateAfter: var FastParseState) =
|
|
|
|
type ChompType = enum
|
|
|
|
ctKeep, ctClip, ctStrip
|
|
|
|
var
|
|
|
|
literal: bool
|
|
|
|
blockIndent = 0
|
|
|
|
chomp: ChompType = ctClip
|
|
|
|
detectedIndent = false
|
2016-01-24 10:44:10 +00:00
|
|
|
|
2016-01-23 13:09:52 +00:00
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of '|':
|
|
|
|
literal = true
|
|
|
|
of '>':
|
|
|
|
literal = false
|
|
|
|
else:
|
|
|
|
assert(false)
|
|
|
|
|
|
|
|
while true:
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of '+':
|
|
|
|
if chomp != ctClip:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Only one chomping indicator is allowed")
|
2016-01-23 13:09:52 +00:00
|
|
|
chomp = ctKeep
|
|
|
|
of '-':
|
|
|
|
if chomp != ctClip:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Only one chomping indicator is allowed")
|
2016-01-23 13:09:52 +00:00
|
|
|
chomp = ctStrip
|
|
|
|
of '1'..'9':
|
|
|
|
if detectedIndent:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Only one indentation indicator is allowed")
|
2016-01-23 13:09:52 +00:00
|
|
|
blockIndent = int(lexer.buf[lexer.bufpos]) - int('\x30')
|
|
|
|
detectedIndent = true
|
|
|
|
of spaceOrLineEnd:
|
|
|
|
break
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Illegal character in block scalar header")
|
2016-01-23 13:09:52 +00:00
|
|
|
lexer.lineEnding()
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of '\x0A':
|
|
|
|
lexer.bufpos = lexer.handleLF(lexer.bufpos)
|
|
|
|
of '\c':
|
|
|
|
lexer.bufpos = lexer.handleCR(lexer.bufpos)
|
|
|
|
of EndOfFile:
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(lexer, "Missing content of block scalar")
|
|
|
|
# TODO: is this correct?
|
2016-01-23 13:09:52 +00:00
|
|
|
else:
|
|
|
|
assert(false)
|
|
|
|
var newlines = 0
|
|
|
|
let parentIndent = ancestry[ancestry.high].indentation
|
|
|
|
content = ""
|
|
|
|
block outer:
|
|
|
|
while true:
|
|
|
|
block inner:
|
|
|
|
for i in countup(1, parentIndent):
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of ' ':
|
|
|
|
discard
|
|
|
|
of '\x0A':
|
|
|
|
lexer.bufpos = lexer.handleLF(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
of '\c':
|
|
|
|
lexer.bufpos = lexer.handleCR(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
else:
|
|
|
|
stateAfter = if i == 1: fpBlockLineStart else: fpBlockObjectStart
|
|
|
|
break outer
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
if detectedIndent:
|
|
|
|
for i in countup(1, blockIndent):
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of ' ':
|
|
|
|
discard
|
|
|
|
of '\x0A':
|
|
|
|
lexer.bufpos = lexer.handleLF(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
of '\c':
|
|
|
|
lexer.bufpos = lexer.handleCR(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
of EndOfFile:
|
|
|
|
stateAfter = fpBlockLineStart
|
|
|
|
break outer
|
|
|
|
of '#':
|
|
|
|
lexer.lineEnding()
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of '\x0A':
|
|
|
|
lexer.bufpos = lexer.handleLF(lexer.bufpos)
|
|
|
|
of '\c':
|
|
|
|
lexer.bufpos = lexer.handleCR(lexer.bufpos)
|
|
|
|
else: discard
|
|
|
|
stateAfter = fpBlockLineStart
|
|
|
|
break outer
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("The text is less indented than expected")
|
2016-01-23 13:09:52 +00:00
|
|
|
lexer.bufpos.inc()
|
|
|
|
else:
|
|
|
|
while true:
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of ' ':
|
|
|
|
discard
|
|
|
|
of '\x0A':
|
|
|
|
lexer.bufpos = lexer.handleLF(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
of '\c':
|
|
|
|
lexer.bufpos = lexer.handleCR(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
of EndOfFile:
|
|
|
|
stateAfter = fpBlockLineStart
|
|
|
|
break outer
|
|
|
|
else:
|
|
|
|
blockIndent = lexer.getColNumber(lexer.bufpos) - parentIndent
|
|
|
|
detectedIndent = true
|
|
|
|
break
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
case lexer.buf[lexer.bufpos]
|
|
|
|
of '\x0A':
|
|
|
|
lexer.bufpos = lexer.handleLF(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
of '\c':
|
|
|
|
lexer.bufpos = lexer.handleCR(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
of EndOfFile:
|
|
|
|
stateAfter = fpBlockLineStart
|
|
|
|
break outer
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
if newlines > 0:
|
|
|
|
if literal:
|
|
|
|
content.add(repeat('\x0A', newlines))
|
|
|
|
elif newlines == 1:
|
|
|
|
content.add(' ')
|
|
|
|
else:
|
|
|
|
content.add(repeat('\x0A', newlines - 1))
|
|
|
|
newlines = 0
|
|
|
|
while true:
|
|
|
|
let c = lexer.buf[lexer.bufpos]
|
|
|
|
case c
|
|
|
|
of '\x0A':
|
|
|
|
lexer.bufpos = lexer.handleLF(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break
|
|
|
|
of '\c':
|
|
|
|
lexer.bufpos = lexer.handleCR(lexer.bufpos)
|
|
|
|
newlines.inc()
|
|
|
|
break inner
|
|
|
|
of EndOfFile:
|
|
|
|
stateAfter = fpBlockLineStart
|
|
|
|
break outer
|
|
|
|
else:
|
|
|
|
content.add(c)
|
|
|
|
lexer.bufpos.inc()
|
|
|
|
case chomp
|
|
|
|
of ctClip:
|
|
|
|
content.add('\x0A')
|
|
|
|
of ctKeep:
|
|
|
|
content.add(repeat('\x0A', newlines))
|
|
|
|
of ctStrip:
|
|
|
|
discard
|
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
proc parse*(p: YamlParser, s: Stream): YamlStream =
|
2016-01-20 20:15:33 +00:00
|
|
|
result = iterator(): YamlStreamEvent =
|
|
|
|
var
|
|
|
|
state = fpInitial
|
|
|
|
shorthands: Table[string, string]
|
|
|
|
anchors: Table[string, AnchorId]
|
|
|
|
nextAnchorId: AnchorId
|
|
|
|
content: string
|
2016-01-23 16:44:50 +00:00
|
|
|
tag: TagId
|
|
|
|
anchor: AnchorId
|
2016-01-20 20:15:33 +00:00
|
|
|
ancestry = newSeq[FastParseLevel]()
|
|
|
|
level: FastParseLevel
|
|
|
|
indentation: int
|
|
|
|
newlines: int
|
2016-01-20 22:38:39 +00:00
|
|
|
flowdepth: int = 0
|
2016-01-22 22:44:26 +00:00
|
|
|
explicitFlowKey: bool
|
2016-01-20 20:15:33 +00:00
|
|
|
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.open(s)
|
2016-01-20 20:15:33 +00:00
|
|
|
initDocValues()
|
|
|
|
|
|
|
|
while true:
|
|
|
|
case state
|
|
|
|
of fpInitial:
|
|
|
|
debug("state: initial")
|
2016-01-24 10:44:10 +00:00
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
of '%':
|
|
|
|
var ld: LexedDirective
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.directiveName(ld)
|
2016-01-20 20:15:33 +00:00
|
|
|
case ld
|
|
|
|
of ldYaml:
|
|
|
|
var version = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.yamlVersion(version)
|
2016-01-20 20:15:33 +00:00
|
|
|
if version != "1.2":
|
2016-01-24 17:24:09 +00:00
|
|
|
if p.callback != nil:
|
|
|
|
p.callback(p.lexer.lineNumber, p.getColNumber(),
|
|
|
|
p.getLineContent(),
|
|
|
|
"Version is not 1.2, but " & version)
|
2016-01-20 20:15:33 +00:00
|
|
|
discard
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-20 20:15:33 +00:00
|
|
|
handleLineEnd(false)
|
|
|
|
of ldTag:
|
|
|
|
var shorthand, uri = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.tagShorthand(shorthand)
|
|
|
|
p.lexer.tagUri(uri)
|
2016-01-20 20:15:33 +00:00
|
|
|
shorthands.add(shorthand, uri)
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-20 20:15:33 +00:00
|
|
|
handleLineEnd(false)
|
|
|
|
of ldUnknown:
|
2016-01-24 17:24:09 +00:00
|
|
|
if p.callback != nil:
|
|
|
|
p.callback(p.lexer.lineNumber, p.getColNumber(),
|
|
|
|
p.getLineContent(), "Unknown directive")
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.finishLine()
|
2016-01-20 20:15:33 +00:00
|
|
|
handleLineEnd(false)
|
|
|
|
of ' ', '\t':
|
2016-01-23 16:44:50 +00:00
|
|
|
while true:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-23 16:44:50 +00:00
|
|
|
of ' ', '\t':
|
|
|
|
discard
|
|
|
|
of '\x0A':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-23 16:44:50 +00:00
|
|
|
break
|
|
|
|
of '\c':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-23 16:44:50 +00:00
|
|
|
break
|
|
|
|
of '#', EndOfFile:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-23 16:44:50 +00:00
|
|
|
handleLineEnd(false)
|
|
|
|
break
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
indentation = p.lexer.getColNumber(p.lexer.bufpos)
|
2016-01-23 16:44:50 +00:00
|
|
|
yield startDocEvent()
|
|
|
|
state = fpBlockObjectStart
|
|
|
|
break
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\x0A':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\c':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
of EndOfFile:
|
|
|
|
return
|
|
|
|
of '#':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-20 20:15:33 +00:00
|
|
|
handleLineEnd(false)
|
|
|
|
of '-':
|
|
|
|
var token: LexedPossibleDirectivesEnd
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.directivesEnd(token)
|
2016-01-20 20:15:33 +00:00
|
|
|
yield startDocEvent()
|
|
|
|
case token
|
|
|
|
of lpdeDirectivesEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc(3)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockObjectStart
|
|
|
|
of lpdeSequenceItem:
|
|
|
|
indentation = 0
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockSequenceIndicator()
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockObjectStart
|
|
|
|
of lpdeScalarContent:
|
2016-01-22 20:36:11 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.plainScalar(content, cBlockOut)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockAfterPlainScalar
|
|
|
|
else:
|
|
|
|
yield startDocEvent()
|
|
|
|
state = fpBlockLineStart
|
|
|
|
of fpBlockLineStart:
|
|
|
|
debug("state: blockLineStart")
|
2016-01-24 10:44:10 +00:00
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
of '-':
|
|
|
|
var token: LexedPossibleDirectivesEnd
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.directivesEnd(token)
|
2016-01-20 20:15:33 +00:00
|
|
|
case token
|
|
|
|
of lpdeDirectivesEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc(3)
|
2016-01-20 20:15:33 +00:00
|
|
|
closeEverything()
|
|
|
|
initDocValues()
|
|
|
|
yield startDocEvent()
|
|
|
|
state = fpBlockObjectStart
|
|
|
|
of lpdeSequenceItem:
|
|
|
|
indentation = 0
|
|
|
|
closeMoreIndentedLevels()
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockSequenceIndicator()
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockObjectStart
|
|
|
|
of lpdeScalarContent:
|
2016-01-22 20:36:11 +00:00
|
|
|
case level.kind
|
|
|
|
of fplScalar:
|
2016-01-20 20:15:33 +00:00
|
|
|
continueMultilineScalar()
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplUnknown:
|
|
|
|
handlePossibleMapStart()
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
2016-01-22 20:36:11 +00:00
|
|
|
ensureCorrectIndentation()
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-22 20:36:11 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.plainScalar(content, cBlockOut)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockAfterPlainScalar
|
|
|
|
of '.':
|
|
|
|
var isDocumentEnd: bool
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.documentEnd(isDocumentEnd)
|
2016-01-20 20:15:33 +00:00
|
|
|
if isDocumentEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc(3)
|
|
|
|
p.lexer.lineEnding()
|
2016-01-22 20:36:11 +00:00
|
|
|
handleLineEnd(true)
|
2016-01-20 20:15:33 +00:00
|
|
|
closeEverything()
|
|
|
|
initDocValues()
|
|
|
|
state = fpInitial
|
|
|
|
else:
|
2016-01-22 20:36:11 +00:00
|
|
|
indentation = 0
|
|
|
|
closeMoreIndentedLevels()
|
|
|
|
case level.kind
|
|
|
|
of fplUnknown:
|
|
|
|
handlePossibleMapStart()
|
|
|
|
of fplScalar:
|
|
|
|
continueMultilineScalar()
|
|
|
|
else:
|
|
|
|
ensureCorrectIndentation()
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-22 20:36:11 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.plainScalar(content, cBlockOut)
|
2016-01-22 20:36:11 +00:00
|
|
|
state = fpBlockAfterPlainScalar
|
2016-01-20 20:15:33 +00:00
|
|
|
of ' ':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.skipIndentation()
|
|
|
|
if p.lexer.buf[p.lexer.bufpos] in ['\t', '\x0A', '\c', '#']:
|
|
|
|
p.lexer.lineEnding()
|
2016-01-23 13:09:52 +00:00
|
|
|
handleLineEnd(true)
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
indentation = p.lexer.getColNumber(p.lexer.bufpos)
|
2016-01-23 13:09:52 +00:00
|
|
|
closeMoreIndentedLevels()
|
|
|
|
case level.kind
|
|
|
|
of fplScalar:
|
|
|
|
state = fpBlockContinueScalar
|
|
|
|
of fplUnknown:
|
|
|
|
state = fpBlockObjectStart
|
|
|
|
else:
|
|
|
|
ensureCorrectIndentation()
|
|
|
|
state = fpBlockObjectStart
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
|
|
|
indentation = 0
|
|
|
|
closeMoreIndentedLevels()
|
|
|
|
case level.kind
|
|
|
|
of fplScalar:
|
|
|
|
state = fpBlockContinueScalar
|
|
|
|
of fplUnknown:
|
|
|
|
state = fpBlockObjectStart
|
|
|
|
else:
|
2016-01-22 20:36:11 +00:00
|
|
|
ensureCorrectIndentation()
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockObjectStart
|
|
|
|
of fpBlockContinueScalar:
|
|
|
|
debug("state: blockAfterPlainScalar")
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.skipWhitespace()
|
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\x0A':
|
|
|
|
newlines.inc()
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockLineStart
|
|
|
|
of '\c':
|
|
|
|
newlines.inc()
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
of ':':
|
2016-01-24 10:44:10 +00:00
|
|
|
if p.lexer.isPlainSafe(p.lexer.bufpos + 1, cBlockOut):
|
2016-01-20 20:15:33 +00:00
|
|
|
continueMultilineScalar()
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected token")
|
2016-01-20 20:15:33 +00:00
|
|
|
of '#':
|
2016-01-22 20:36:11 +00:00
|
|
|
yield scalarEvent(content, tag, anchor)
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-20 20:15:33 +00:00
|
|
|
handleLineEnd(true)
|
2016-01-22 20:36:11 +00:00
|
|
|
handleObjectEnd(fpBlockLineStart)
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
|
|
|
continueMultilineScalar()
|
|
|
|
of fpBlockAfterPlainScalar:
|
|
|
|
debug("state: blockAfterPlainScalar")
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.skipWhitespace()
|
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\x0A':
|
|
|
|
if level.kind notin [fplUnknown, fplScalar]:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected scalar")
|
2016-01-20 20:15:33 +00:00
|
|
|
newlines = 1
|
|
|
|
level.kind = fplScalar
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockLineStart
|
|
|
|
of '\c':
|
|
|
|
if level.kind notin [fplUnknown, fplScalar]:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected scalar")
|
2016-01-20 20:15:33 +00:00
|
|
|
newlines = 1
|
|
|
|
level.kind = fplScalar
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockLineStart
|
|
|
|
else:
|
2016-01-22 20:36:11 +00:00
|
|
|
yield scalarEvent(content, tag, anchor)
|
|
|
|
handleObjectEnd(fpBlockAfterObject)
|
|
|
|
of fpBlockAfterObject:
|
|
|
|
debug("state: blockAfterObject")
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.skipWhitespace()
|
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
of EndOfFile:
|
|
|
|
closeEverything()
|
|
|
|
break
|
|
|
|
of '\x0A':
|
2016-01-22 20:36:11 +00:00
|
|
|
state = fpBlockLineStart
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\c':
|
2016-01-22 20:36:11 +00:00
|
|
|
state = fpBlockLineStart
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
of ':':
|
|
|
|
case level.kind
|
|
|
|
of fplUnknown:
|
2016-01-23 16:44:50 +00:00
|
|
|
handleObjectStart(yamlStartMap)
|
2016-01-20 20:15:33 +00:00
|
|
|
of fplMapKey:
|
2016-01-22 20:36:11 +00:00
|
|
|
yield scalarEvent("", yTagQuestionMark, yAnchorNone)
|
|
|
|
level.kind = fplMapValue
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplMapValue:
|
|
|
|
level.kind = fplMapValue
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-20 20:15:33 +00:00
|
|
|
of fplSequence:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Illegal token (expected sequence item)")
|
2016-01-20 20:15:33 +00:00
|
|
|
of fplScalar:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Multiline scalars may not be implicit map keys")
|
|
|
|
p.lexer.bufpos.inc()
|
|
|
|
p.lexer.skipWhitespace()
|
|
|
|
indentation = p.lexer.getColNumber(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockObjectStart
|
|
|
|
of '#':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-20 20:15:33 +00:00
|
|
|
handleLineEnd(true)
|
2016-01-22 20:36:11 +00:00
|
|
|
handleObjectEnd(fpBlockLineStart)
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Illegal token (expected ':', comment or line end)")
|
2016-01-20 20:15:33 +00:00
|
|
|
of fpBlockObjectStart:
|
|
|
|
debug("state: blockObjectStart")
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.skipWhitespace()
|
|
|
|
indentation = p.lexer.getColNumber(p.lexer.bufpos)
|
|
|
|
let objectStart = p.lexer.getColNumber(p.lexer.bufpos)
|
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\x0A':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockLineStart
|
2016-01-23 16:44:50 +00:00
|
|
|
level.indentation = -1
|
2016-01-20 20:15:33 +00:00
|
|
|
of '\c':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockLineStart
|
2016-01-23 16:44:50 +00:00
|
|
|
level.indentation = -1
|
2016-01-20 20:15:33 +00:00
|
|
|
of EndOfFile:
|
|
|
|
closeEverything()
|
|
|
|
return
|
|
|
|
of '#':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-20 20:15:33 +00:00
|
|
|
handleLineEnd(true)
|
|
|
|
of '\'':
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-20 20:15:33 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.singleQuotedScalar(content)
|
2016-01-20 20:15:33 +00:00
|
|
|
if tag == yTagQuestionMark:
|
|
|
|
tag = yTagExclamationMark
|
2016-01-22 20:36:11 +00:00
|
|
|
yield scalarEvent(content, tag, anchor)
|
|
|
|
handleObjectEnd(fpBlockAfterObject)
|
2016-01-20 20:15:33 +00:00
|
|
|
of '"':
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-20 20:15:33 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.doubleQuotedScalar(content)
|
2016-01-20 20:15:33 +00:00
|
|
|
if tag == yTagQuestionMark:
|
|
|
|
tag = yTagExclamationMark
|
2016-01-22 20:36:11 +00:00
|
|
|
yield scalarEvent(content, tag, anchor)
|
|
|
|
handleObjectEnd(fpBlockAfterObject)
|
2016-01-23 13:09:52 +00:00
|
|
|
of '|', '>':
|
2016-01-23 16:44:50 +00:00
|
|
|
# TODO: this will scan for possible map start, which is not
|
|
|
|
# neccessary in this case
|
2016-01-23 13:09:52 +00:00
|
|
|
handleBlockItemStart()
|
|
|
|
var stateAfter: FastParseState
|
2016-01-23 16:44:50 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.blockScalar(content, stateAfter)
|
2016-01-24 18:15:50 +00:00
|
|
|
if tag == yTagQuestionMark:
|
|
|
|
tag = yTagExclamationMark
|
2016-01-23 13:09:52 +00:00
|
|
|
yield scalarEvent(content, tag, anchor)
|
|
|
|
handleObjectEnd(stateAfter)
|
2016-01-20 20:15:33 +00:00
|
|
|
of '-':
|
2016-01-24 10:44:10 +00:00
|
|
|
if p.lexer.isPlainSafe(p.lexer.bufpos + 1, cBlockOut):
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-23 16:44:50 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.plainScalar(content, cBlockOut)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockAfterPlainScalar
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockSequenceIndicator()
|
2016-01-20 20:15:33 +00:00
|
|
|
of '!':
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-20 22:38:39 +00:00
|
|
|
handleTagHandle()
|
2016-01-20 20:15:33 +00:00
|
|
|
of '&':
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-20 22:38:39 +00:00
|
|
|
handleAnchor()
|
2016-01-20 20:15:33 +00:00
|
|
|
of '*':
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-20 22:38:39 +00:00
|
|
|
handleAlias()
|
|
|
|
of '[', '{':
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-20 22:38:39 +00:00
|
|
|
state = fpFlow
|
2016-01-22 22:44:26 +00:00
|
|
|
of '?':
|
2016-01-24 10:44:10 +00:00
|
|
|
if p.lexer.isPlainSafe(p.lexer.bufpos + 1, cBlockOut):
|
2016-01-22 22:44:26 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-23 16:44:50 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.plainScalar(content, cBlockOut)
|
2016-01-22 22:44:26 +00:00
|
|
|
state = fpBlockAfterPlainScalar
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-22 22:44:26 +00:00
|
|
|
handleMapKeyIndicator()
|
|
|
|
of ':':
|
2016-01-24 10:44:10 +00:00
|
|
|
if p.lexer.isPlainSafe(p.lexer.bufpos + 1, cBlockOut):
|
2016-01-22 22:44:26 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-23 16:44:50 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.plainScalar(content, cBlockOut)
|
2016-01-22 22:44:26 +00:00
|
|
|
state = fpBlockAfterPlainScalar
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-22 22:44:26 +00:00
|
|
|
handleMapValueIndicator()
|
2016-01-23 16:44:50 +00:00
|
|
|
of '@', '`':
|
2016-01-24 10:44:10 +00:00
|
|
|
lexerError(p.lexer, "Reserved characters cannot start a plain scalar")
|
2016-01-20 20:15:33 +00:00
|
|
|
else:
|
2016-01-22 20:36:11 +00:00
|
|
|
handleBlockItemStart()
|
2016-01-20 20:15:33 +00:00
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.plainScalar(content, cBlockOut)
|
2016-01-20 20:15:33 +00:00
|
|
|
state = fpBlockAfterPlainScalar
|
|
|
|
of fpExpectDocEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 22:38:39 +00:00
|
|
|
of '-':
|
|
|
|
var token: LexedPossibleDirectivesEnd
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.directivesEnd(token)
|
2016-01-20 22:38:39 +00:00
|
|
|
case token
|
|
|
|
of lpdeDirectivesEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc(3)
|
2016-01-20 22:38:39 +00:00
|
|
|
yield endDocEvent()
|
|
|
|
initDocValues()
|
|
|
|
yield startDocEvent()
|
|
|
|
state = fpBlockObjectStart
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected content (expected document end)")
|
2016-01-20 22:38:39 +00:00
|
|
|
of '.':
|
|
|
|
var isDocumentEnd: bool
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.documentEnd(isDocumentEnd)
|
2016-01-20 22:38:39 +00:00
|
|
|
if isDocumentEnd:
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc(3)
|
2016-01-20 22:38:39 +00:00
|
|
|
yield endDocEvent()
|
|
|
|
initDocValues()
|
|
|
|
state = fpInitial
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
parserError("Unexpected content (expected document end)")
|
2016-01-20 22:38:39 +00:00
|
|
|
of ' ', '\t', '#':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-20 22:38:39 +00:00
|
|
|
handleLineEnd(true)
|
|
|
|
of '\x0A':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleLF(p.lexer.bufpos)
|
2016-01-20 22:38:39 +00:00
|
|
|
of '\c':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos = p.lexer.handleCR(p.lexer.bufpos)
|
2016-01-20 22:38:39 +00:00
|
|
|
of EndOfFile:
|
|
|
|
yield endDocEvent()
|
|
|
|
break
|
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected content (expected document end)")
|
2016-01-20 22:38:39 +00:00
|
|
|
of fpFlow:
|
2016-01-22 20:36:11 +00:00
|
|
|
debug("state: flow")
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.skipWhitespaceAndNewlines()
|
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 22:38:39 +00:00
|
|
|
of '{':
|
2016-01-23 16:44:50 +00:00
|
|
|
handleObjectStart(yamlStartMap)
|
2016-01-20 22:38:39 +00:00
|
|
|
flowdepth.inc()
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-22 22:44:26 +00:00
|
|
|
explicitFlowKey = false
|
2016-01-20 22:38:39 +00:00
|
|
|
of '[':
|
2016-01-23 16:44:50 +00:00
|
|
|
handleObjectStart(yamlStartSequence)
|
2016-01-20 22:38:39 +00:00
|
|
|
flowdepth.inc()
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-20 22:38:39 +00:00
|
|
|
of '}':
|
|
|
|
assert(level.kind == fplUnknown)
|
|
|
|
level = ancestry.pop()
|
|
|
|
case level.kind
|
|
|
|
of fplMapValue:
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-20 22:38:39 +00:00
|
|
|
anchor = yAnchorNone
|
2016-01-22 20:36:11 +00:00
|
|
|
level.kind = fplMapKey
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplMapKey:
|
2016-01-24 18:15:50 +00:00
|
|
|
if tag != yTagQuestionMark or anchor != yAnchorNone or
|
2016-01-22 22:44:26 +00:00
|
|
|
explicitFlowKey:
|
2016-01-22 21:40:22 +00:00
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-22 21:40:22 +00:00
|
|
|
anchor = yAnchorNone
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplSequence:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected token (expected ']')")
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplUnknown, fplScalar:
|
|
|
|
assert(false)
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-22 20:36:11 +00:00
|
|
|
leaveFlowLevel()
|
2016-01-20 22:38:39 +00:00
|
|
|
of ']':
|
|
|
|
assert(level.kind == fplUnknown)
|
|
|
|
level = ancestry.pop()
|
|
|
|
case level.kind
|
|
|
|
of fplSequence:
|
2016-01-24 18:15:50 +00:00
|
|
|
if tag != yTagQuestionMark or anchor != yAnchorNone:
|
2016-01-22 21:40:22 +00:00
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-22 21:40:22 +00:00
|
|
|
anchor = yAnchorNone
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplMapKey, fplMapValue:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected token (expected '}')")
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplUnknown, fplScalar:
|
|
|
|
assert(false)
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-22 20:36:11 +00:00
|
|
|
leaveFlowLevel()
|
2016-01-20 22:38:39 +00:00
|
|
|
of ',':
|
|
|
|
assert(level.kind == fplUnknown)
|
|
|
|
level = ancestry.pop()
|
|
|
|
case level.kind
|
|
|
|
of fplSequence:
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-20 22:38:39 +00:00
|
|
|
anchor = yAnchorNone
|
|
|
|
of fplMapValue:
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-20 22:38:39 +00:00
|
|
|
anchor = yAnchorNone
|
|
|
|
level.kind = fplMapKey
|
2016-01-22 22:44:26 +00:00
|
|
|
explicitFlowKey = false
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplMapKey:
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-20 22:38:39 +00:00
|
|
|
anchor = yAnchorNone
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-22 22:44:26 +00:00
|
|
|
explicitFlowKey = false
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplUnknown, fplScalar:
|
|
|
|
assert(false)
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-20 22:38:39 +00:00
|
|
|
of ':':
|
|
|
|
assert(level.kind == fplUnknown)
|
2016-01-24 10:44:10 +00:00
|
|
|
if p.lexer.isPlainSafe(p.lexer.bufpos + 1, cFlowIn):
|
2016-01-20 22:38:39 +00:00
|
|
|
level = ancestry.pop()
|
|
|
|
case level.kind
|
|
|
|
of fplSequence, fplMapValue:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected token (expected ',')")
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplMapKey:
|
|
|
|
yield scalarEvent("", tag, anchor)
|
2016-01-24 18:15:50 +00:00
|
|
|
tag = yTagQuestionMark
|
2016-01-20 22:38:39 +00:00
|
|
|
anchor = yAnchorNone
|
|
|
|
level.kind = fplMapValue
|
|
|
|
of fplUnknown, fplScalar:
|
|
|
|
assert(false)
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-20 22:38:39 +00:00
|
|
|
else:
|
|
|
|
handleFlowPlainScalar()
|
2016-01-21 18:23:45 +00:00
|
|
|
of '\'':
|
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.singleQuotedScalar(content)
|
2016-01-21 18:23:45 +00:00
|
|
|
if tag == yTagQuestionMark:
|
|
|
|
tag = yTagExclamationMark
|
|
|
|
yield scalarEvent(content, tag, anchor)
|
2016-01-22 20:36:11 +00:00
|
|
|
handleObjectEnd(fpFlowAfterObject)
|
2016-01-21 18:23:45 +00:00
|
|
|
of '"':
|
|
|
|
content = ""
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
p.lexer.doubleQuotedScalar(content)
|
2016-01-24 18:15:50 +00:00
|
|
|
if tag == yTagQuestionMark:
|
|
|
|
tag = yTagExclamationMark
|
2016-01-21 18:23:45 +00:00
|
|
|
yield scalarEvent(content, tag, anchor)
|
2016-01-22 20:36:11 +00:00
|
|
|
handleObjectEnd(fpFlowAfterObject)
|
2016-01-20 22:38:39 +00:00
|
|
|
of '!':
|
|
|
|
handleTagHandle()
|
|
|
|
of '&':
|
|
|
|
handleAnchor()
|
|
|
|
of '*':
|
|
|
|
handleAlias()
|
2016-01-21 18:23:45 +00:00
|
|
|
state = fpFlowAfterObject
|
2016-01-22 22:44:26 +00:00
|
|
|
of '?':
|
2016-01-24 10:44:10 +00:00
|
|
|
if p.lexer.isPlainSafe(p.lexer.bufpos + 1, cFlowOut):
|
2016-01-22 22:44:26 +00:00
|
|
|
handleFlowPlainScalar()
|
|
|
|
elif explicitFlowKey:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Duplicate '?' in flow mapping")
|
2016-01-22 22:44:26 +00:00
|
|
|
else:
|
|
|
|
explicitFlowKey = true
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-20 22:38:39 +00:00
|
|
|
else:
|
|
|
|
handleFlowPlainScalar()
|
|
|
|
of fpFlowAfterObject:
|
2016-01-22 20:36:11 +00:00
|
|
|
debug("state: flowAfterObject")
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.skipWhitespaceAndNewlines()
|
|
|
|
case p.lexer.buf[p.lexer.bufpos]
|
2016-01-20 22:38:39 +00:00
|
|
|
of ']':
|
|
|
|
case level.kind
|
|
|
|
of fplSequence:
|
2016-01-22 20:36:11 +00:00
|
|
|
discard
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplMapKey, fplMapValue:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected token (expected '}')")
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplScalar, fplUnknown:
|
|
|
|
assert(false)
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-20 22:38:39 +00:00
|
|
|
leaveFlowLevel()
|
|
|
|
of '}':
|
|
|
|
case level.kind
|
2016-01-22 20:36:11 +00:00
|
|
|
of [fplMapKey, fplMapValue]:
|
|
|
|
discard
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplSequence:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected token (expected ']')")
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplUnknown, fplScalar:
|
|
|
|
assert(false)
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-20 22:38:39 +00:00
|
|
|
leaveFlowLevel()
|
|
|
|
of ',':
|
|
|
|
case level.kind
|
|
|
|
of fplSequence:
|
|
|
|
discard
|
|
|
|
of fplMapValue:
|
2016-01-24 18:15:50 +00:00
|
|
|
yield scalarEvent("", yTagQuestionMark, yAnchorNone)
|
2016-01-20 22:38:39 +00:00
|
|
|
level.kind = fplMapKey
|
2016-01-22 22:44:26 +00:00
|
|
|
explicitFlowKey = false
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplMapKey:
|
2016-01-22 22:44:26 +00:00
|
|
|
explicitFlowKey = false
|
2016-01-20 22:38:39 +00:00
|
|
|
of fplUnknown, fplScalar:
|
|
|
|
assert(false)
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-20 22:38:39 +00:00
|
|
|
state = fpFlow
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-20 22:38:39 +00:00
|
|
|
of ':':
|
|
|
|
case level.kind
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplSequence, fplMapKey:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected token (expected ',')")
|
2016-01-22 20:36:11 +00:00
|
|
|
of fplMapValue:
|
2016-01-20 22:38:39 +00:00
|
|
|
level.kind = fplMapValue
|
|
|
|
of fplUnknown, fplScalar:
|
|
|
|
assert(false)
|
|
|
|
ancestry.add(level)
|
2016-01-23 16:44:50 +00:00
|
|
|
level = FastParseLevel(kind: fplUnknown, indentation: -1)
|
2016-01-20 22:38:39 +00:00
|
|
|
state = fpFlow
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.bufpos.inc()
|
2016-01-20 22:38:39 +00:00
|
|
|
of '#':
|
2016-01-24 10:44:10 +00:00
|
|
|
p.lexer.lineEnding()
|
2016-01-22 20:36:11 +00:00
|
|
|
handleLineEnd(true)
|
2016-01-20 22:38:39 +00:00
|
|
|
of EndOfFile:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unclosed flow content")
|
2016-01-20 22:38:39 +00:00
|
|
|
else:
|
2016-01-24 10:44:10 +00:00
|
|
|
startToken()
|
|
|
|
parserError("Unexpected content (expected flow indicator)")
|