2016-09-10 08:30:40 +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-09-10 11:38:42 +00:00
|
|
|
import lexbase, streams, strutils, unicode
|
2016-09-10 15:19:37 +00:00
|
|
|
when defined(yamlDebug):
|
|
|
|
import terminal
|
|
|
|
export terminal
|
2016-09-10 08:30:40 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
StringSource* = object
|
|
|
|
src: string
|
|
|
|
pos: int
|
|
|
|
line, lineStart: int
|
|
|
|
|
|
|
|
SourceProvider* = concept c
|
|
|
|
advance(c) is char
|
|
|
|
lexCR(c)
|
|
|
|
lexLF(c)
|
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
YamlLexerObj* = object
|
2016-09-11 10:13:51 +00:00
|
|
|
cur*: LexerToken
|
2016-09-11 12:55:29 +00:00
|
|
|
curStartPos*: tuple[line, column: int]
|
2016-09-11 10:13:51 +00:00
|
|
|
# ltScalarPart, ltQuotedScalar, ltYamlVersion, ltTagShorthand, ltTagUri,
|
|
|
|
# ltLiteralTag, ltTagHandle, ltAnchor, ltAlias
|
2016-09-10 08:30:40 +00:00
|
|
|
buf*: string not nil
|
2016-09-11 10:13:51 +00:00
|
|
|
# ltIndentation
|
2016-09-10 08:30:40 +00:00
|
|
|
indentation*: int
|
2016-09-11 10:13:51 +00:00
|
|
|
# ltTagHandle
|
|
|
|
shorthandEnd*: int
|
|
|
|
|
2016-09-11 17:20:02 +00:00
|
|
|
# may be modified from outside; will be consumed at plain scalar starts
|
|
|
|
newlines*: int
|
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
# internals
|
|
|
|
source: pointer
|
|
|
|
inFlow: bool
|
|
|
|
literalEndIndent: int
|
2016-09-11 17:20:02 +00:00
|
|
|
nextState, lineStartState, inlineState, insideLineImpl, insideDocImpl,
|
2016-09-12 19:38:56 +00:00
|
|
|
insideFlowImpl, outsideDocImpl: LexerState
|
2016-09-11 10:13:51 +00:00
|
|
|
blockScalarIndent: int
|
2016-09-13 18:19:02 +00:00
|
|
|
folded: bool
|
|
|
|
chomp: ChompType
|
2016-09-10 10:38:03 +00:00
|
|
|
c: char
|
2016-09-23 13:42:24 +00:00
|
|
|
tokenLineGetter: proc(lex: YamlLexer, pos: tuple[line, column: int],
|
|
|
|
marker: bool): string {.raises: [].}
|
2016-09-11 16:23:47 +00:00
|
|
|
searchColonImpl: proc(lex: YamlLexer): bool
|
2016-09-11 09:28:05 +00:00
|
|
|
|
|
|
|
YamlLexer* = ref YamlLexerObj
|
|
|
|
|
2016-09-12 19:38:56 +00:00
|
|
|
YamlLexerError* = object of Exception
|
|
|
|
line*, column*: int
|
|
|
|
lineContent*: string
|
|
|
|
|
|
|
|
LexerState = proc(lex: YamlLexer): bool {.raises: YamlLexerError, locks: 0,
|
|
|
|
gcSafe.}
|
2016-09-10 08:30:40 +00:00
|
|
|
|
|
|
|
LexerToken* = enum
|
|
|
|
ltYamlDirective, ltYamlVersion, ltTagDirective, ltTagShorthand,
|
2016-09-10 15:19:37 +00:00
|
|
|
ltTagUri, ltUnknownDirective, ltUnknownDirectiveParams, ltEmptyLine,
|
2016-09-10 08:30:40 +00:00
|
|
|
ltDirectivesEnd, ltDocumentEnd, ltStreamEnd, ltIndentation, ltQuotedScalar,
|
2016-09-14 10:18:00 +00:00
|
|
|
ltScalarPart, ltBlockScalarHeader, ltBlockScalar, ltSeqItemInd, ltMapKeyInd,
|
|
|
|
ltMapValInd, ltBraceOpen, ltBraceClose, ltBracketOpen, ltBracketClose,
|
|
|
|
ltComma, ltLiteralTag, ltTagHandle, ltAnchor, ltAlias
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-10 15:19:37 +00:00
|
|
|
ChompType* = enum
|
|
|
|
ctKeep, ctClip, ctStrip
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
# consts
|
2016-09-10 08:30:40 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
space = {' ', '\t'}
|
|
|
|
lineEnd = {'\l', '\c', EndOfFile}
|
|
|
|
spaceOrLineEnd = {' ', '\t', '\l', '\c', EndOfFile}
|
|
|
|
digits = {'0'..'9'}
|
|
|
|
flowIndicators = {'[', ']', '{', '}', ','}
|
2016-09-11 10:52:24 +00:00
|
|
|
uriChars = {'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':',
|
|
|
|
'@', '&', '-', '=', '+', '$', '_', '.', '~', '*', '\'', '(', ')'}
|
2016-09-11 09:28:05 +00:00
|
|
|
|
2016-09-10 11:38:42 +00:00
|
|
|
UTF8NextLine = toUTF8(0x85.Rune)
|
|
|
|
UTF8NonBreakingSpace = toUTF8(0xA0.Rune)
|
|
|
|
UTF8LineSeparator = toUTF8(0x2028.Rune)
|
|
|
|
UTF8ParagraphSeparator = toUTF8(0x2029.Rune)
|
2016-09-11 09:28:05 +00:00
|
|
|
|
2016-09-12 19:38:56 +00:00
|
|
|
UnknownIndentation* = int.low
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
# lexer backend implementations
|
|
|
|
|
|
|
|
template blSource(lex: YamlLexer): var BaseLexer =
|
|
|
|
(cast[ptr BaseLexer](lex.source))[]
|
|
|
|
template sSource(lex: YamlLexer): var StringSource =
|
|
|
|
(cast[ptr StringSource](lex.source))[]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
proc advance(lex: YamlLexer, t: typedesc[BaseLexer], step: int = 1) {.inline.} =
|
|
|
|
lex.blSource.bufpos.inc(step)
|
|
|
|
lex.c = lex.blSource.buf[lex.blSource.bufpos]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
proc advance(lex: YamlLexer, t: typedesc[StringSource], step: int = 1)
|
|
|
|
{.inline.} =
|
|
|
|
lex.sSource.pos.inc(step)
|
|
|
|
if lex.sSource.pos >= lex.sSource.src.len: lex.c = EndOfFile
|
|
|
|
else: lex.c = lex.sSource.src[lex.sSource.pos]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
template lexCR(lex: YamlLexer, t: typedesc[BaseLexer]) =
|
2016-09-12 19:38:56 +00:00
|
|
|
try: lex.blSource.bufpos = lex.blSource.handleCR(lex.blSource.bufpos)
|
|
|
|
except:
|
|
|
|
var e = generateError[T](lex, "Encountered stream error: " &
|
|
|
|
getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.c = lex.blSource.buf[lex.blSource.bufpos]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
template lexCR(lex: YamlLexer, t: typedesc[StringSource]) =
|
|
|
|
lex.sSource.pos.inc()
|
|
|
|
if lex.sSource.src[lex.sSource.pos] == '\l': lex.sSource.pos.inc()
|
|
|
|
lex.sSource.lineStart = lex.sSource.pos
|
|
|
|
lex.sSource.line.inc()
|
|
|
|
lex.c = lex.sSource.src[lex.sSource.pos]
|
2016-09-10 10:38:03 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
template lexLF(lex: YamlLexer, t: typedesc[BaseLexer]) =
|
2016-09-12 19:38:56 +00:00
|
|
|
try: lex.blSource.bufpos = lex.blSource.handleLF(lex.blSource.bufpos)
|
|
|
|
except:
|
|
|
|
var e = generateError[T](lex, "Encountered stream error: " &
|
|
|
|
getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.c = lex.blSource.buf[lex.blSource.bufpos]
|
2016-09-10 10:38:03 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
template lexLF(lex: YamlLexer, t: typedesc[StringSource]) =
|
|
|
|
lex.sSource.pos.inc()
|
|
|
|
lex.sSource.lineStart = lex.sSource.pos
|
|
|
|
lex.sSource.line.inc()
|
|
|
|
lex.c = lex.sSource.src[lex.sSource.pos]
|
2016-09-10 10:38:03 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
template lineNumber(lex: YamlLexer, t: typedesc[BaseLexer]): int =
|
|
|
|
lex.blSource.lineNumber
|
2016-09-10 10:38:03 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
template lineNumber(lex: YamlLexer, t: typedesc[StringSource]): int =
|
|
|
|
lex.sSource.line
|
2016-09-10 10:38:03 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
template columnNumber(lex: YamlLexer, t: typedesc[BaseLexer]): int =
|
|
|
|
lex.blSource.getColNumber(lex.blSource.bufpos) + 1
|
2016-09-10 10:38:03 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
template columnNumber(lex: YamlLexer, t: typedesc[StringSource]): int =
|
|
|
|
lex.sSource.pos - lex.sSource.lineStart + 1
|
|
|
|
|
|
|
|
template currentLine(lex: YamlLexer, t: typedesc[BaseLexer]): string =
|
|
|
|
lex.blSource.getCurrentLine(true)
|
|
|
|
|
|
|
|
template currentLine(lex: YamlLexer, t: typedesc[StringSource]): string =
|
2016-09-10 10:38:03 +00:00
|
|
|
var result = ""
|
2016-09-11 09:28:05 +00:00
|
|
|
var i = lex.sSource.lineStart
|
|
|
|
while lex.sSource.src[i] notin lineEnd:
|
|
|
|
result.add(lex.sSource.src[i])
|
2016-09-10 10:38:03 +00:00
|
|
|
inc(i)
|
2016-09-11 09:28:05 +00:00
|
|
|
result.add("\n" & spaces(lex.columnNumber(t) - 1) & "^\n")
|
2016-09-10 10:38:03 +00:00
|
|
|
result
|
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
proc nextIsPlainSafe(lex: YamlLexer, t: typedesc[BaseLexer], inFlow: bool):
|
|
|
|
bool {.inline.} =
|
|
|
|
case lex.blSource.buf[lex.blSource.bufpos + 1]
|
|
|
|
of spaceOrLineEnd: result = false
|
|
|
|
of flowIndicators: result = not inFlow
|
|
|
|
else: result = true
|
|
|
|
|
|
|
|
proc nextIsPlainSafe(lex: YamlLexer, t: typedesc[StringSource],
|
|
|
|
inFlow: bool): bool {.inline.} =
|
|
|
|
case lex.sSource.src[lex.sSource.pos + 1]
|
|
|
|
of spaceOrLineEnd: result = false
|
|
|
|
of flowIndicators: result = not inFlow
|
|
|
|
else: result = true
|
|
|
|
|
2016-09-11 16:23:47 +00:00
|
|
|
proc getPos(lex: YamlLexer, t: typedesc[BaseLexer]): int = lex.blSource.bufpos
|
|
|
|
proc getPos(lex: YamlLexer, t: typedesc[StringSource]): int = lex.sSource.pos
|
|
|
|
|
|
|
|
proc at(lex: YamlLexer, t: typedesc[BaseLexer], pos: int): char {.inline.} =
|
|
|
|
lex.blSource.buf[pos]
|
|
|
|
|
|
|
|
proc at(lex: YamlLexer, t: typedesc[StringSource], pos: int): char {.inline.} =
|
|
|
|
lex.sSource.src[pos]
|
|
|
|
|
2016-09-11 10:52:24 +00:00
|
|
|
proc mark(lex: YamlLexer, t: typedesc[BaseLexer]): int = lex.blSource.bufpos
|
|
|
|
proc mark(lex: YamlLexer, t: typedesc[StringSource]): int = lex.sSource.pos
|
|
|
|
|
2016-09-11 12:55:29 +00:00
|
|
|
proc afterMark(lex: YamlLexer, t: typedesc[BaseLexer], m: int): int {.inline.} =
|
2016-09-11 10:52:24 +00:00
|
|
|
lex.blSource.bufpos - m
|
|
|
|
|
2016-09-11 12:55:29 +00:00
|
|
|
proc afterMark(lex: YamlLexer, t: typedesc[StringSource], m: int):
|
|
|
|
int {.inline.} =
|
2016-09-11 10:52:24 +00:00
|
|
|
lex.sSource.pos - m
|
|
|
|
|
2016-09-23 13:42:24 +00:00
|
|
|
proc lineWithMarker(lex: YamlLexer, pos: tuple[line, column: int],
|
|
|
|
t: typedesc[BaseLexer], marker: bool): string =
|
|
|
|
if pos.line == lex.blSource.lineNumber:
|
2016-09-12 19:38:56 +00:00
|
|
|
result = lex.blSource.getCurrentLine(false)
|
2016-09-23 13:42:24 +00:00
|
|
|
if marker: result.add(spaces(pos.column - 1) & "^\n")
|
2016-09-14 12:53:13 +00:00
|
|
|
else: result = ""
|
2016-09-11 12:55:29 +00:00
|
|
|
|
2016-09-23 13:42:24 +00:00
|
|
|
proc lineWithMarker(lex: YamlLexer, pos: tuple[line, column: int],
|
|
|
|
t: typedesc[StringSource], marker: bool): string =
|
2016-09-11 12:55:29 +00:00
|
|
|
var
|
|
|
|
lineStartIndex = lex.sSource.pos
|
|
|
|
lineEndIndex: int
|
|
|
|
curLine = lex.sSource.line
|
2016-09-23 13:42:24 +00:00
|
|
|
if pos.line == curLine:
|
2016-09-11 12:55:29 +00:00
|
|
|
lineEndIndex = lex.sSource.pos
|
|
|
|
while lex.sSource.src[lineEndIndex] notin lineEnd: inc(lineEndIndex)
|
|
|
|
while true:
|
|
|
|
while lineStartIndex >= 0 and lex.sSource.src[lineStartIndex] notin lineEnd:
|
|
|
|
dec(lineStartIndex)
|
2016-09-23 13:42:24 +00:00
|
|
|
if curLine == pos.line:
|
2016-09-11 12:55:29 +00:00
|
|
|
inc(lineStartIndex)
|
|
|
|
break
|
|
|
|
let wasLF = lex.sSource.src[lineStartIndex] == '\l'
|
|
|
|
lineEndIndex = lineStartIndex
|
|
|
|
dec(lineStartIndex)
|
|
|
|
if lex.sSource.src[lineStartIndex] == '\c' and wasLF:
|
|
|
|
dec(lineStartIndex)
|
|
|
|
dec(lineEndIndex)
|
|
|
|
dec(curLine)
|
2016-09-14 12:53:13 +00:00
|
|
|
result = lex.sSource.src.substr(lineStartIndex, lineEndIndex - 1) & "\n"
|
2016-09-23 13:42:24 +00:00
|
|
|
if marker: result.add(spaces(pos.column - 1) & "^\n")
|
2016-09-11 12:55:29 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
# lexer states
|
|
|
|
|
2017-02-13 16:10:56 +00:00
|
|
|
{.push gcSafe, locks: 0.}
|
|
|
|
# `raises` cannot be pushed.
|
|
|
|
proc outsideDoc[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc yamlVersion[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc tagShorthand[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc tagUri[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc unknownDirParams[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc expectLineEnd[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc possibleDirectivesEnd[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc possibleDocumentEnd[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc afterSeqInd[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc insideDoc[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc insideFlow[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc insideLine[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc plainScalarPart[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc blockScalarHeader[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc blockScalar[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc indentationAfterBlockScalar[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc dirEndAfterBlockScalar[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc docEndAfterBlockScalar[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc tagHandle[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc anchor[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc alias[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
|
|
|
proc streamEnd[T](lex: YamlLexer): bool {.raises: YamlLexerError.}
|
2016-09-12 19:38:56 +00:00
|
|
|
{.pop.}
|
2016-09-11 09:28:05 +00:00
|
|
|
|
|
|
|
# implementation
|
|
|
|
|
|
|
|
template debug(message: string) {.dirty.} =
|
|
|
|
when defined(yamlDebug):
|
|
|
|
try: styledWriteLine(stdout, fgBlue, message)
|
|
|
|
except IOError: discard
|
|
|
|
|
|
|
|
proc generateError[T](lex: YamlLexer, message: string):
|
2016-09-10 10:38:03 +00:00
|
|
|
ref YamlLexerError {.raises: [].} =
|
|
|
|
result = newException(YamlLexerError, message)
|
2016-09-11 09:28:05 +00:00
|
|
|
result.line = lex.lineNumber(T)
|
|
|
|
result.column = lex.columnNumber(T)
|
|
|
|
result.lineContent = lex.currentLine(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 12:55:29 +00:00
|
|
|
proc startToken[T](lex: YamlLexer) {.inline.} =
|
|
|
|
lex.curStartPos = (lex.lineNumber(T), lex.columnNumber(T))
|
|
|
|
|
|
|
|
proc directiveName[T](lex: YamlLexer) =
|
2016-09-10 08:30:40 +00:00
|
|
|
while lex.c notin spaceOrLineEnd:
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 12:55:29 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-13 18:19:02 +00:00
|
|
|
proc consumeNewlines(lex: YamlLexer) {.inline, raises: [].} =
|
2016-09-11 17:20:02 +00:00
|
|
|
case lex.newlines
|
|
|
|
of 0: return
|
|
|
|
of 1: lex.buf.add(' ')
|
|
|
|
else: lex.buf.add(repeat('\l', lex.newlines - 1))
|
|
|
|
lex.newlines = 0
|
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc yamlVersion[T](lex: YamlLexer): bool =
|
2016-09-10 08:30:40 +00:00
|
|
|
debug("lex: yamlVersion")
|
2016-09-11 09:28:05 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
|
|
|
if lex.c notin digits:
|
|
|
|
raise generateError[T](lex, "Invalid YAML version number")
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
while lex.c in digits:
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
|
|
|
if lex.c != '.': raise generateError[T](lex, "Invalid YAML version number")
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add('.')
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
|
|
|
if lex.c notin digits:
|
|
|
|
raise generateError[T](lex, "Invalid YAML version number")
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
while lex.c in digits:
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c notin spaceOrLineEnd:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex, "Invalid YAML version number")
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltYamlVersion
|
2016-09-10 08:30:40 +00:00
|
|
|
result = true
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc tagShorthand[T](lex: YamlLexer): bool =
|
2016-09-10 08:30:40 +00:00
|
|
|
debug("lex: tagShorthand")
|
2016-09-11 09:28:05 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
|
|
|
if lex.c != '!':
|
|
|
|
raise generateError[T](lex, "Tag shorthand must start with a '!'")
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
|
|
|
|
if lex.c in spaceOrLineEnd: discard
|
|
|
|
else:
|
|
|
|
while lex.c != '!':
|
|
|
|
case lex.c
|
|
|
|
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '-':
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
|
|
|
else: raise generateError[T](lex, "Illegal character in tag shorthand")
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c notin spaceOrLineEnd:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex, "Missing space after tag shorthand")
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltTagShorthand
|
2016-09-10 15:19:37 +00:00
|
|
|
result = true
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = tagUri[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc tagUri[T](lex: YamlLexer): bool =
|
2016-09-10 08:30:40 +00:00
|
|
|
debug("lex: tagUri")
|
2016-09-11 09:28:05 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c == '!':
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of spaceOrLineEnd: break
|
|
|
|
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':', '@', '&',
|
|
|
|
'-', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')':
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
|
|
|
else: raise generateError[T](lex, "Invalid character in tag uri: " &
|
2016-09-10 08:30:40 +00:00
|
|
|
escape("" & lex.c))
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltTagUri
|
2016-09-10 15:19:37 +00:00
|
|
|
result = true
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc unknownDirParams[T](lex: YamlLexer): bool =
|
2016-09-10 15:19:37 +00:00
|
|
|
debug("lex: unknownDirParams")
|
2016-09-11 09:28:05 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-10 15:19:37 +00:00
|
|
|
while lex.c notin lineEnd + {'#'}:
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltUnknownDirectiveParams
|
2016-09-10 08:30:40 +00:00
|
|
|
result = true
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc expectLineEnd[T](lex: YamlLexer): bool =
|
2016-09-10 08:30:40 +00:00
|
|
|
debug("lex: expectLineEnd")
|
|
|
|
result = false
|
2016-09-11 09:28:05 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of '#':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
|
|
|
while lex.c notin lineEnd: lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
of EndOfFile:
|
2016-09-23 13:42:24 +00:00
|
|
|
lex.nextState = streamEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
break
|
|
|
|
of '\l':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.lexLF(T)
|
|
|
|
lex.nextState = lex.lineStartState
|
2016-09-10 08:30:40 +00:00
|
|
|
break
|
|
|
|
of '\c':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.lexCR(T)
|
|
|
|
lex.nextState = lex.lineStartState
|
2016-09-10 08:30:40 +00:00
|
|
|
break
|
|
|
|
else:
|
2016-09-12 19:38:56 +00:00
|
|
|
raise generateError[T](lex,
|
|
|
|
"Unexpected character (expected line end): " & escape("" & lex.c))
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc possibleDirectivesEnd[T](lex: YamlLexer): bool =
|
2016-09-10 15:19:37 +00:00
|
|
|
debug("lex: possibleDirectivesEnd")
|
2016-09-13 10:01:21 +00:00
|
|
|
lex.indentation = 0
|
2016-09-12 19:38:56 +00:00
|
|
|
lex.lineStartState = lex.insideDocImpl # could be insideDoc[T]
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c == '-':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c == '-':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c in spaceOrLineEnd:
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltDirectivesEnd
|
2016-09-13 11:38:10 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
2016-09-13 18:19:02 +00:00
|
|
|
lex.nextState = lex.insideLineImpl
|
2016-09-10 15:19:37 +00:00
|
|
|
return true
|
2016-09-11 17:20:02 +00:00
|
|
|
lex.consumeNewlines()
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add('-')
|
2016-09-11 17:20:02 +00:00
|
|
|
else: lex.consumeNewlines()
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add('-')
|
|
|
|
elif lex.c in spaceOrLineEnd:
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltIndentation
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = afterSeqInd[T]
|
2016-09-10 15:19:37 +00:00
|
|
|
return true
|
2016-09-11 17:20:02 +00:00
|
|
|
else: lex.consumeNewlines()
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add('-')
|
2016-09-13 10:01:21 +00:00
|
|
|
lex.cur = ltIndentation
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = plainScalarPart[T]
|
2016-09-13 10:01:21 +00:00
|
|
|
result = true
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc afterSeqInd[T](lex: YamlLexer): bool =
|
2016-09-10 15:19:37 +00:00
|
|
|
result = true
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltSeqItemInd
|
2016-09-13 11:38:10 +00:00
|
|
|
if lex.c notin lineEnd:
|
|
|
|
lex.advance(T)
|
|
|
|
while lex.c in space: lex.advance(T)
|
2016-09-13 18:19:02 +00:00
|
|
|
lex.nextState = lex.insideLineImpl
|
2016-09-10 15:19:37 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc possibleDocumentEnd[T](lex: YamlLexer): bool =
|
2016-09-10 15:19:37 +00:00
|
|
|
debug("lex: possibleDocumentEnd")
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c == '.':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c == '.':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c in spaceOrLineEnd:
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltDocumentEnd
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-12 19:38:56 +00:00
|
|
|
lex.lineStartState = lex.outsideDocImpl
|
2016-09-10 15:19:37 +00:00
|
|
|
return true
|
2016-09-11 17:20:02 +00:00
|
|
|
lex.consumeNewlines()
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add('.')
|
2016-09-11 17:20:02 +00:00
|
|
|
else: lex.consumeNewlines()
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add('.')
|
2016-09-11 17:20:02 +00:00
|
|
|
else: lex.consumeNewlines()
|
2016-09-10 08:30:40 +00:00
|
|
|
lex.buf.add('.')
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = plainScalarPart[T]
|
2016-09-10 15:19:37 +00:00
|
|
|
result = false
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc outsideDoc[T](lex: YamlLexer): bool =
|
2016-09-10 15:19:37 +00:00
|
|
|
debug("lex: outsideDoc")
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-10 08:30:40 +00:00
|
|
|
case lex.c
|
|
|
|
of '%':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-11 12:55:29 +00:00
|
|
|
directiveName[T](lex)
|
2016-09-10 08:30:40 +00:00
|
|
|
case lex.buf
|
2016-09-10 10:38:03 +00:00
|
|
|
of "YAML":
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltYamlDirective
|
2016-09-10 15:19:37 +00:00
|
|
|
lex.buf.setLen(0)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = yamlVersion[T]
|
2016-09-10 10:38:03 +00:00
|
|
|
of "TAG":
|
2016-09-10 15:19:37 +00:00
|
|
|
lex.buf.setLen(0)
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltTagDirective
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = tagShorthand[T]
|
2016-09-10 10:38:03 +00:00
|
|
|
else:
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltUnknownDirective
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = unknownDirParams[T]
|
2016-09-10 10:38:03 +00:00
|
|
|
return true
|
2016-09-10 08:30:40 +00:00
|
|
|
of '-':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = possibleDirectivesEnd[T]
|
2016-09-10 15:19:37 +00:00
|
|
|
return false
|
2016-09-10 08:30:40 +00:00
|
|
|
of '.':
|
2016-09-10 15:19:37 +00:00
|
|
|
lex.indentation = 0
|
2016-09-14 10:18:00 +00:00
|
|
|
if possibleDocumentEnd[T](lex): return true
|
2016-09-10 08:30:40 +00:00
|
|
|
of spaceOrLineEnd + {'#'}:
|
|
|
|
lex.indentation = 0
|
|
|
|
while lex.c == ' ':
|
|
|
|
lex.indentation.inc()
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c in spaceOrLineEnd + {'#'}:
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
return false
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = insideLine[T]
|
2016-09-10 15:19:37 +00:00
|
|
|
else:
|
|
|
|
lex.indentation = 0
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = insideLine[T]
|
|
|
|
lex.lineStartState = insideDoc[T]
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltIndentation
|
2016-09-10 08:30:40 +00:00
|
|
|
result = true
|
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc insideDoc[T](lex: YamlLexer): bool =
|
2016-09-11 08:02:10 +00:00
|
|
|
debug("lex: insideDoc")
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-10 15:19:37 +00:00
|
|
|
lex.indentation = 0
|
2016-09-10 08:30:40 +00:00
|
|
|
case lex.c
|
|
|
|
of '-':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = possibleDirectivesEnd[T]
|
2016-09-10 15:19:37 +00:00
|
|
|
return false
|
2016-09-11 09:28:05 +00:00
|
|
|
of '.': lex.nextState = possibleDocumentEnd[T]
|
2016-09-10 15:19:37 +00:00
|
|
|
of spaceOrLineEnd:
|
2016-09-10 08:30:40 +00:00
|
|
|
while lex.c == ' ':
|
|
|
|
lex.indentation.inc()
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-14 10:18:00 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
2016-09-13 11:38:10 +00:00
|
|
|
case lex.c
|
|
|
|
of lineEnd:
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltEmptyLine
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
return true
|
2016-09-10 15:19:37 +00:00
|
|
|
else:
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = lex.inlineState
|
|
|
|
else: lex.nextState = lex.inlineState
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltIndentation
|
2016-09-10 08:30:40 +00:00
|
|
|
result = true
|
|
|
|
|
2016-09-11 17:20:02 +00:00
|
|
|
proc insideFlow[T](lex: YamlLexer): bool =
|
|
|
|
debug("lex: insideFlow")
|
|
|
|
startToken[T](lex)
|
|
|
|
while lex.c in space: lex.advance(T)
|
|
|
|
if lex.c in lineEnd + {'#'}:
|
|
|
|
lex.cur = ltEmptyLine
|
|
|
|
lex.nextState = expectLineEnd[T]
|
|
|
|
return true
|
|
|
|
lex.nextState = insideLine[T]
|
|
|
|
result = false
|
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
proc possibleIndicatorChar[T](lex: YamlLexer, indicator: LexerToken,
|
2016-09-11 10:13:51 +00:00
|
|
|
jsonContext: bool = false): bool =
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-13 11:38:10 +00:00
|
|
|
if not(jsonContext) and lex.nextIsPlainSafe(T, lex.inFlow):
|
2016-09-11 17:20:02 +00:00
|
|
|
lex.consumeNewlines()
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = plainScalarPart[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
result = false
|
|
|
|
else:
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = indicator
|
2016-09-10 08:30:40 +00:00
|
|
|
result = true
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
|
|
|
while lex.c in space: lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
if lex.c in lineEnd:
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc flowIndicator[T](lex: YamlLexer, indicator: LexerToken): bool {.inline.} =
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = indicator
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
|
|
|
while lex.c in space: lex.advance(T)
|
2016-09-14 10:18:00 +00:00
|
|
|
if lex.c in lineEnd + {'#'}:
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 15:33:58 +00:00
|
|
|
result = true
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-10 11:38:42 +00:00
|
|
|
proc addMultiple(s: var string, c: char, num: int) {.raises: [], inline.} =
|
2016-09-10 15:19:37 +00:00
|
|
|
for i in 1..num: s.add(c)
|
2016-09-10 11:38:42 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
proc processQuotedWhitespace[T](lex: YamlLexer, newlines: var int) =
|
2016-09-10 11:38:42 +00:00
|
|
|
block outer:
|
|
|
|
let beforeSpace = lex.buf.len
|
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of ' ', '\t': lex.buf.add(lex.c)
|
|
|
|
of '\l':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.lexLF(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
break
|
|
|
|
of '\c':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.lexCR(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
break
|
|
|
|
else: break outer
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
lex.buf.setLen(beforeSpace)
|
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of ' ', '\t': discard
|
|
|
|
of '\l':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.lexLF(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
newlines.inc()
|
|
|
|
continue
|
|
|
|
of '\c':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.lexCR(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
newlines.inc()
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if newlines == 0: discard
|
|
|
|
elif newlines == 1: lex.buf.add(' ')
|
|
|
|
else: lex.buf.addMultiple('\l', newlines - 1)
|
|
|
|
break
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
proc singleQuotedScalar[T](lex: YamlLexer) =
|
2016-09-10 11:38:42 +00:00
|
|
|
debug("lex: singleQuotedScalar")
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of '\'':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
if lex.c == '\'': lex.buf.add('\'')
|
|
|
|
else: break
|
2016-09-11 09:28:05 +00:00
|
|
|
of EndOfFile: raise generateError[T](lex, "Unfinished single quoted string")
|
2016-09-10 11:38:42 +00:00
|
|
|
of '\l', '\c', '\t', ' ':
|
|
|
|
var newlines = 1
|
2016-09-11 09:28:05 +00:00
|
|
|
processQuotedWhitespace[T](lex, newlines)
|
2016-09-10 11:38:42 +00:00
|
|
|
continue
|
|
|
|
else: lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-14 10:18:00 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
|
|
|
if lex.c in lineEnd + {'#'}:
|
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 11:38:42 +00:00
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
proc unicodeSequence[T](lex: YamlLexer, length: int) =
|
2016-09-10 11:38:42 +00:00
|
|
|
debug("lex: unicodeSequence")
|
|
|
|
var unicodeChar = 0.int
|
|
|
|
for i in countup(0, length - 1):
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
let digitPosition = length - i - 1
|
|
|
|
case lex.c
|
|
|
|
of EndOFFile, '\l', '\c':
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex, "Unfinished unicode escape sequence")
|
2016-09-10 11:38:42 +00:00
|
|
|
of '0' .. '9':
|
|
|
|
unicodeChar = unicodechar or (int(lex.c) - 0x30) shl (digitPosition * 4)
|
|
|
|
of 'A' .. 'F':
|
|
|
|
unicodeChar = unicodechar or (int(lex.c) - 0x37) shl (digitPosition * 4)
|
|
|
|
of 'a' .. 'f':
|
|
|
|
unicodeChar = unicodechar or (int(lex.c) - 0x57) shl (digitPosition * 4)
|
|
|
|
else:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex,
|
2016-09-10 11:38:42 +00:00
|
|
|
"Invalid character in unicode escape sequence: " &
|
|
|
|
escape("" & lex.c))
|
|
|
|
lex.buf.add(toUTF8(Rune(unicodeChar)))
|
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
proc doubleQuotedScalar[T](lex: YamlLexer) =
|
2016-09-10 11:38:42 +00:00
|
|
|
debug("lex: doubleQuotedScalar")
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of EndOfFile:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex, "Unfinished double quoted string")
|
2016-09-10 11:38:42 +00:00
|
|
|
of '\\':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
case lex.c
|
|
|
|
of EndOfFile:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex, "Unfinished escape sequence")
|
2016-09-10 11:38:42 +00:00
|
|
|
of '0': lex.buf.add('\0')
|
|
|
|
of 'a': lex.buf.add('\x07')
|
|
|
|
of 'b': lex.buf.add('\x08')
|
|
|
|
of '\t', 't': lex.buf.add('\t')
|
|
|
|
of 'n': lex.buf.add('\l')
|
|
|
|
of 'v': lex.buf.add('\v')
|
|
|
|
of 'f': lex.buf.add('\f')
|
|
|
|
of 'r': lex.buf.add('\c')
|
|
|
|
of 'e': lex.buf.add('\e')
|
|
|
|
of ' ': lex.buf.add(' ')
|
|
|
|
of '"': lex.buf.add('"')
|
|
|
|
of '/': lex.buf.add('/')
|
|
|
|
of '\\': lex.buf.add('\\')
|
|
|
|
of 'N': lex.buf.add(UTF8NextLine)
|
|
|
|
of '_': lex.buf.add(UTF8NonBreakingSpace)
|
|
|
|
of 'L': lex.buf.add(UTF8LineSeparator)
|
|
|
|
of 'P': lex.buf.add(UTF8ParagraphSeparator)
|
2016-09-11 09:28:05 +00:00
|
|
|
of 'x': unicodeSequence[T](lex, 2)
|
|
|
|
of 'u': unicodeSequence[T](lex, 4)
|
|
|
|
of 'U': unicodeSequence[T](lex, 8)
|
2016-09-10 11:38:42 +00:00
|
|
|
of '\l', '\c':
|
|
|
|
var newlines = 0
|
2016-09-11 09:28:05 +00:00
|
|
|
processQuotedWhitespace[T](lex, newlines)
|
2016-09-10 11:38:42 +00:00
|
|
|
continue
|
2016-09-11 09:28:05 +00:00
|
|
|
else: raise generateError[T](lex, "Illegal character in escape sequence")
|
2016-09-10 11:38:42 +00:00
|
|
|
of '"':
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 11:38:42 +00:00
|
|
|
break
|
|
|
|
of '\l', '\c', '\t', ' ':
|
|
|
|
var newlines = 1
|
2016-09-11 09:28:05 +00:00
|
|
|
processQuotedWhitespace[T](lex, newlines)
|
2016-09-10 11:38:42 +00:00
|
|
|
continue
|
|
|
|
else: lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-14 10:18:00 +00:00
|
|
|
while lex.c in space: lex.advance(T)
|
|
|
|
if lex.c in lineEnd + {'#'}:
|
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 10:38:03 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc insideLine[T](lex: YamlLexer): bool =
|
2016-09-11 08:02:10 +00:00
|
|
|
debug("lex: insideLine")
|
2016-09-10 08:30:40 +00:00
|
|
|
case lex.c
|
2016-09-11 10:13:51 +00:00
|
|
|
of ':':
|
|
|
|
result = possibleIndicatorChar[T](lex, ltMapValInd,
|
|
|
|
lex.inFlow and
|
|
|
|
lex.cur in [ltBraceClose, ltBracketClose, ltQuotedScalar])
|
|
|
|
of '?': result = possibleIndicatorChar[T](lex, ltMapKeyInd)
|
|
|
|
of '-': result = possibleIndicatorChar[T](lex, ltSeqItemInd)
|
2016-09-10 08:30:40 +00:00
|
|
|
of lineEnd + {'#'}:
|
|
|
|
result = false
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
of '\"':
|
2016-09-11 09:28:05 +00:00
|
|
|
doubleQuotedScalar[T](lex)
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltQuotedScalar
|
2016-09-10 08:30:40 +00:00
|
|
|
result = true
|
|
|
|
of '\'':
|
2016-09-11 09:28:05 +00:00
|
|
|
singleQuotedScalar[T](lex)
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltQuotedScalar
|
2016-09-10 08:30:40 +00:00
|
|
|
result = true
|
|
|
|
of '>', '|':
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-11 17:20:02 +00:00
|
|
|
lex.consumeNewlines()
|
2016-09-11 09:28:05 +00:00
|
|
|
if lex.inFlow: lex.nextState = plainScalarPart[T]
|
|
|
|
else: lex.nextState = blockScalarHeader[T]
|
2016-09-10 15:19:37 +00:00
|
|
|
result = false
|
2016-09-11 10:13:51 +00:00
|
|
|
of '{': result = flowIndicator[T](lex, ltBraceOpen)
|
|
|
|
of '}': result = flowIndicator[T](lex, ltBraceClose)
|
|
|
|
of '[': result = flowIndicator[T](lex, ltBracketOpen)
|
|
|
|
of ']': result = flowIndicator[T](lex, ltBracketClose)
|
|
|
|
of ',': result = flowIndicator[T](lex, ltComma)
|
2016-09-11 10:52:24 +00:00
|
|
|
of '!':
|
|
|
|
lex.nextState = tagHandle[T]
|
|
|
|
result = false
|
2016-09-11 11:04:10 +00:00
|
|
|
of '&':
|
|
|
|
lex.nextState = anchor[T]
|
|
|
|
result = false
|
|
|
|
of '*':
|
|
|
|
lex.nextState = alias[T]
|
|
|
|
result = false
|
|
|
|
of '@', '`':
|
|
|
|
raise generateError[T](lex,
|
|
|
|
"Reserved characters cannot start a plain scalar")
|
2016-09-10 08:30:40 +00:00
|
|
|
else:
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-11 17:20:02 +00:00
|
|
|
lex.consumeNewlines()
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = plainScalarPart[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
result = false
|
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc plainScalarPart[T](lex: YamlLexer): bool =
|
2016-09-10 15:19:37 +00:00
|
|
|
debug("lex: plainScalarPart")
|
2016-09-10 08:30:40 +00:00
|
|
|
block outer:
|
|
|
|
while true:
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
case lex.c
|
|
|
|
of space:
|
|
|
|
let lenBeforeSpace = lex.buf.len()
|
|
|
|
while true:
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 10:38:03 +00:00
|
|
|
case lex.c
|
2016-09-10 08:30:40 +00:00
|
|
|
of lineEnd + {'#'}:
|
|
|
|
lex.buf.setLen(lenBeforeSpace)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
break outer
|
|
|
|
of ':':
|
2016-09-11 09:28:05 +00:00
|
|
|
if lex.nextIsPlainSafe(T, lex.inFlow): break
|
2016-09-10 08:30:40 +00:00
|
|
|
else:
|
|
|
|
lex.buf.setLen(lenBeforeSpace)
|
2016-09-12 19:38:56 +00:00
|
|
|
lex.nextState = lex.insideLineImpl # could be insideLine[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
break outer
|
|
|
|
of flowIndicators:
|
|
|
|
if lex.inFlow:
|
|
|
|
lex.buf.setLen(lenBeforeSpace)
|
2016-09-12 19:38:56 +00:00
|
|
|
lex.nextState = lex.insideLineImpl # could be insideLine[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
break outer
|
|
|
|
else:
|
|
|
|
lex.buf.add(lex.c)
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 08:30:40 +00:00
|
|
|
break
|
|
|
|
of space: discard
|
|
|
|
else: break
|
2016-09-10 11:38:42 +00:00
|
|
|
of lineEnd:
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-10 11:38:42 +00:00
|
|
|
break
|
2016-09-10 08:30:40 +00:00
|
|
|
of flowIndicators:
|
|
|
|
if lex.inFlow:
|
2016-09-12 19:38:56 +00:00
|
|
|
lex.nextState = lex.insideLineImpl # could be insideLine[T]
|
2016-09-10 08:30:40 +00:00
|
|
|
break
|
|
|
|
of ':':
|
2016-09-11 09:28:05 +00:00
|
|
|
if not lex.nextIsPlainSafe(T, lex.inFlow):
|
2016-09-12 19:38:56 +00:00
|
|
|
lex.nextState = lex.insideLineImpl # could be insideLine[T]
|
2016-09-10 11:38:42 +00:00
|
|
|
break outer
|
2016-09-10 08:30:40 +00:00
|
|
|
else: discard
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltScalarPart
|
2016-09-10 08:30:40 +00:00
|
|
|
result = true
|
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc blockScalarHeader[T](lex: YamlLexer): bool =
|
2016-09-10 15:19:37 +00:00
|
|
|
debug("lex: blockScalarHeader")
|
|
|
|
lex.chomp = ctClip
|
|
|
|
lex.blockScalarIndent = UnknownIndentation
|
|
|
|
lex.folded = lex.c == '>'
|
2016-09-13 18:19:02 +00:00
|
|
|
startToken[T](lex)
|
2016-09-10 15:19:37 +00:00
|
|
|
while true:
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.advance(T)
|
2016-09-10 15:19:37 +00:00
|
|
|
case lex.c
|
|
|
|
of '+':
|
|
|
|
if lex.chomp != ctClip:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex, "Only one chomping indicator is allowed")
|
2016-09-10 15:19:37 +00:00
|
|
|
lex.chomp = ctKeep
|
|
|
|
of '-':
|
|
|
|
if lex.chomp != ctClip:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex, "Only one chomping indicator is allowed")
|
2016-09-10 15:19:37 +00:00
|
|
|
lex.chomp = ctStrip
|
|
|
|
of '1'..'9':
|
|
|
|
if lex.blockScalarIndent != UnknownIndentation:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex, "Only one indentation indicator is allowed")
|
2016-12-13 20:40:46 +00:00
|
|
|
lex.blockScalarIndent = ord(lex.c) - ord('\x30')
|
2016-09-10 15:19:37 +00:00
|
|
|
of spaceOrLineEnd: break
|
|
|
|
else:
|
2016-09-11 09:28:05 +00:00
|
|
|
raise generateError[T](lex,
|
2016-09-10 15:19:37 +00:00
|
|
|
"Illegal character in block scalar header: '" & escape("" & lex.c) &
|
|
|
|
'\'')
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-13 18:19:02 +00:00
|
|
|
lex.lineStartState = blockScalar[T]
|
2016-09-14 10:18:00 +00:00
|
|
|
lex.cur = ltBlockScalarHeader
|
|
|
|
result = true
|
|
|
|
|
2016-09-14 12:35:41 +00:00
|
|
|
proc blockScalarAfterLineStart[T](lex: YamlLexer,
|
|
|
|
recentWasMoreIndented: var bool): bool =
|
|
|
|
if lex.indentation < lex.blockScalarIndent:
|
|
|
|
lex.nextState = indentationAfterBlockScalar[T]
|
|
|
|
return false
|
|
|
|
|
|
|
|
if lex.folded and not recentWasMoreIndented: lex.consumeNewlines()
|
|
|
|
else:
|
|
|
|
recentWasMoreIndented = false
|
|
|
|
lex.buf.add(repeat('\l', lex.newlines))
|
|
|
|
lex.newlines = 0
|
|
|
|
result = true
|
|
|
|
|
2016-09-14 10:18:00 +00:00
|
|
|
proc blockScalarLineStart[T](lex: YamlLexer, recentWasMoreIndented: var bool):
|
|
|
|
bool =
|
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of '-':
|
|
|
|
if lex.indentation < lex.blockScalarIndent:
|
|
|
|
lex.nextState = indentationAfterBlockScalar[T]
|
|
|
|
return false
|
|
|
|
discard possibleDirectivesEnd[T](lex)
|
|
|
|
case lex.cur
|
|
|
|
of ltDirectivesEnd:
|
|
|
|
lex.nextState = dirEndAfterBlockScalar[T]
|
|
|
|
return false
|
|
|
|
of ltIndentation:
|
|
|
|
if lex.nextState == afterSeqInd[T]:
|
|
|
|
lex.consumeNewlines()
|
|
|
|
lex.buf.add("- ")
|
|
|
|
else: discard
|
|
|
|
break
|
|
|
|
of '.':
|
|
|
|
if lex.indentation < lex.blockScalarIndent:
|
|
|
|
lex.nextState = indentationAfterBlockScalar[T]
|
|
|
|
return false
|
|
|
|
if possibleDocumentEnd[T](lex):
|
|
|
|
lex.nextState = docEndAfterBlockScalar[T]
|
|
|
|
return false
|
|
|
|
break
|
|
|
|
of spaceOrLineEnd:
|
|
|
|
while lex.c == ' ' and lex.indentation < lex.blockScalarIndent:
|
|
|
|
lex.indentation.inc()
|
|
|
|
lex.advance(T)
|
|
|
|
case lex.c
|
|
|
|
of '\l':
|
|
|
|
lex.newlines.inc()
|
|
|
|
lex.lexLF(T)
|
|
|
|
lex.indentation = 0
|
|
|
|
of '\c':
|
|
|
|
lex.newlines.inc()
|
|
|
|
lex.lexCR(T)
|
|
|
|
lex.indentation = 0
|
|
|
|
of EndOfFile:
|
2016-09-23 13:42:24 +00:00
|
|
|
lex.nextState = streamEnd[T]
|
2016-09-14 10:18:00 +00:00
|
|
|
return false
|
|
|
|
of ' ', '\t':
|
|
|
|
recentWasMoreIndented = true
|
|
|
|
lex.buf.add(repeat('\l', lex.newlines))
|
|
|
|
lex.newlines = 0
|
|
|
|
return true
|
|
|
|
else: break
|
|
|
|
else: break
|
2016-09-14 12:35:41 +00:00
|
|
|
result = blockScalarAfterLineStart[T](lex, recentWasMoreIndented)
|
2016-09-11 09:28:05 +00:00
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc blockScalar[T](lex: YamlLexer): bool =
|
2016-09-13 18:19:02 +00:00
|
|
|
debug("lex: blockScalar")
|
|
|
|
block outer:
|
2016-09-14 10:18:00 +00:00
|
|
|
var recentWasMoreIndented = true
|
2016-09-13 18:19:02 +00:00
|
|
|
if lex.blockScalarIndent == UnknownIndentation:
|
|
|
|
while true:
|
|
|
|
lex.blockScalarIndent = 0
|
|
|
|
while lex.c == ' ':
|
|
|
|
lex.blockScalarIndent.inc()
|
|
|
|
lex.advance(T)
|
|
|
|
case lex.c
|
2016-09-14 10:18:00 +00:00
|
|
|
of '\l':
|
|
|
|
lex.lexLF(T)
|
|
|
|
lex.newlines.inc()
|
|
|
|
of '\c':
|
|
|
|
lex.lexCR(T)
|
|
|
|
lex.newlines.inc()
|
2016-09-13 18:19:02 +00:00
|
|
|
of EndOfFile:
|
2016-09-23 13:42:24 +00:00
|
|
|
lex.nextState = streamEnd[T]
|
2016-09-13 18:19:02 +00:00
|
|
|
break outer
|
|
|
|
else:
|
|
|
|
if lex.blockScalarIndent <= lex.indentation:
|
|
|
|
lex.indentation = lex.blockScalarIndent
|
|
|
|
lex.nextState = indentationAfterBlockScalar[T]
|
|
|
|
break outer
|
|
|
|
lex.indentation = lex.blockScalarIndent
|
|
|
|
break
|
2016-12-13 20:40:46 +00:00
|
|
|
else:
|
|
|
|
lex.blockScalarIndent += lex.indentation
|
|
|
|
lex.indentation = 0
|
2016-09-14 12:35:41 +00:00
|
|
|
if lex.c notin {'.', '-'} or lex.indentation == 0:
|
|
|
|
if not blockScalarLineStart[T](lex, recentWasMoreIndented): break outer
|
|
|
|
else:
|
|
|
|
if not blockScalarAfterLineStart[T](lex, recentWasMoreIndented):
|
|
|
|
break outer
|
2016-09-13 18:19:02 +00:00
|
|
|
while true:
|
|
|
|
while lex.c notin lineEnd:
|
|
|
|
lex.buf.add(lex.c)
|
|
|
|
lex.advance(T)
|
2016-09-14 10:18:00 +00:00
|
|
|
if not blockScalarLineStart[T](lex, recentWasMoreIndented): break outer
|
2016-09-19 17:33:29 +00:00
|
|
|
|
2016-09-14 12:35:41 +00:00
|
|
|
debug("lex: leaving block scalar at indentation " & $lex.indentation)
|
2016-09-13 18:19:02 +00:00
|
|
|
case lex.chomp
|
|
|
|
of ctStrip: discard
|
2016-09-14 10:18:00 +00:00
|
|
|
of ctClip:
|
|
|
|
if lex.buf.len > 0: lex.buf.add('\l')
|
2016-09-13 18:19:02 +00:00
|
|
|
of ctKeep: lex.buf.add(repeat('\l', lex.newlines))
|
|
|
|
lex.newlines = 0
|
|
|
|
lex.lineStartState = insideDoc[T]
|
2016-09-14 10:18:00 +00:00
|
|
|
lex.cur = ltBlockScalar
|
2016-09-10 15:19:37 +00:00
|
|
|
result = true
|
2016-09-13 18:19:02 +00:00
|
|
|
|
|
|
|
proc indentationAfterBlockScalar[T](lex: YamlLexer): bool =
|
2016-09-14 11:37:56 +00:00
|
|
|
if lex.indentation == 0:
|
|
|
|
lex.nextState = lex.insideDocImpl
|
|
|
|
elif lex.c == '#':
|
2016-09-13 18:19:02 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
|
|
|
result = false
|
|
|
|
else:
|
|
|
|
lex.cur = ltIndentation
|
|
|
|
result = true
|
2016-09-14 11:37:56 +00:00
|
|
|
lex.nextState = lex.insideLineImpl
|
2016-09-13 18:19:02 +00:00
|
|
|
|
|
|
|
proc dirEndAfterBlockScalar[T](lex: YamlLexer): bool =
|
|
|
|
lex.cur = ltDirectivesEnd
|
|
|
|
while lex.c in space: lex.advance(T)
|
|
|
|
lex.nextState = lex.insideLineImpl
|
2016-09-14 10:18:00 +00:00
|
|
|
result = true
|
2016-09-19 17:33:29 +00:00
|
|
|
|
2016-09-13 18:19:02 +00:00
|
|
|
proc docEndAfterBlockScalar[T](lex: YamlLexer): bool =
|
|
|
|
lex.cur = ltDocumentEnd
|
2016-09-11 09:28:05 +00:00
|
|
|
lex.nextState = expectLineEnd[T]
|
2016-09-13 18:19:02 +00:00
|
|
|
lex.lineStartState = lex.outsideDocImpl
|
2016-09-14 10:18:00 +00:00
|
|
|
result = true
|
2016-09-10 08:30:40 +00:00
|
|
|
|
2016-09-11 10:52:24 +00:00
|
|
|
proc byteSequence[T](lex: YamlLexer) =
|
|
|
|
debug("lex: byteSequence")
|
|
|
|
var charCode = 0.int8
|
|
|
|
for i in 0 .. 1:
|
|
|
|
lex.advance(T)
|
|
|
|
let digitPosition = int8(1 - i)
|
|
|
|
case lex.c
|
|
|
|
of EndOfFile, '\l', 'r':
|
|
|
|
raise generateError[T](lex, "Unfinished octet escape sequence")
|
|
|
|
of '0' .. '9':
|
|
|
|
charCode = charCode or (int8(lex.c) - 0x30.int8) shl (digitPosition * 4)
|
|
|
|
of 'A' .. 'F':
|
|
|
|
charCode = charCode or (int8(lex.c) - 0x37.int8) shl (digitPosition * 4)
|
|
|
|
of 'a' .. 'f':
|
|
|
|
charCode = charCode or (int8(lex.c) - 0x57.int8) shl (digitPosition * 4)
|
|
|
|
else:
|
|
|
|
raise generateError[T](lex, "Invalid character in octet escape sequence")
|
|
|
|
lex.buf.add(char(charCode))
|
|
|
|
|
|
|
|
proc tagHandle[T](lex: YamlLexer): bool =
|
|
|
|
debug("lex: tagHandle")
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-11 10:52:24 +00:00
|
|
|
lex.advance(T)
|
|
|
|
if lex.c == '<':
|
|
|
|
lex.advance(T)
|
|
|
|
if lex.c == '!':
|
|
|
|
lex.buf.add('!')
|
|
|
|
lex.advance(T)
|
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of spaceOrLineEnd: raise generateError[T](lex, "Unclosed verbatim tag")
|
|
|
|
of '%': byteSequence[T](lex)
|
|
|
|
of uriChars + {','}: lex.buf.add(lex.c)
|
|
|
|
of '>': break
|
|
|
|
else: raise generateError[T](lex, "Illegal character in verbatim tag")
|
|
|
|
lex.advance(T)
|
|
|
|
lex.advance(T)
|
|
|
|
lex.cur = ltLiteralTag
|
|
|
|
else:
|
|
|
|
lex.shorthandEnd = 0
|
|
|
|
let m = lex.mark(T)
|
|
|
|
lex.buf.add('!')
|
|
|
|
while true:
|
|
|
|
case lex.c
|
|
|
|
of spaceOrLineEnd: break
|
|
|
|
of '!':
|
|
|
|
if lex.shorthandEnd != 0:
|
|
|
|
raise generateError[T](lex, "Illegal character in tag suffix")
|
|
|
|
lex.shorthandEnd = lex.afterMark(T, m) + 1
|
|
|
|
lex.buf.add('!')
|
|
|
|
of ',':
|
|
|
|
if lex.shorthandEnd > 0: break # ',' after shorthand is flow indicator
|
|
|
|
lex.buf.add(',')
|
|
|
|
of '%':
|
|
|
|
if lex.shorthandEnd == 0:
|
|
|
|
raise generateError[T](lex, "Illegal character in tag handle")
|
|
|
|
byteSequence[T](lex)
|
|
|
|
of uriChars: lex.buf.add(lex.c)
|
|
|
|
else: raise generateError[T](lex, "Illegal character in tag handle")
|
|
|
|
lex.advance(T)
|
|
|
|
lex.cur = ltTagHandle
|
|
|
|
while lex.c in space: lex.advance(T)
|
|
|
|
if lex.c in lineEnd: lex.nextState = expectLineEnd[T]
|
2016-09-12 19:38:56 +00:00
|
|
|
else: lex.nextState = lex.insideLineImpl # could be insideLine[T]
|
2016-09-11 10:52:24 +00:00
|
|
|
result = true
|
|
|
|
|
2016-09-11 11:04:10 +00:00
|
|
|
proc anchorName[T](lex: YamlLexer) =
|
|
|
|
debug("lex: anchorName")
|
2016-09-11 12:55:29 +00:00
|
|
|
startToken[T](lex)
|
2016-09-11 11:04:10 +00:00
|
|
|
while true:
|
|
|
|
lex.advance(T)
|
|
|
|
case lex.c
|
|
|
|
of spaceOrLineEnd, '[', ']', '{', '}', ',': break
|
|
|
|
else: lex.buf.add(lex.c)
|
|
|
|
while lex.c in space: lex.advance(T)
|
|
|
|
if lex.c in lineEnd: lex.nextState = expectLineEnd[T]
|
2016-09-12 19:38:56 +00:00
|
|
|
else: lex.nextState = lex.insideLineImpl # could be insideLine[T]
|
2016-09-11 11:04:10 +00:00
|
|
|
|
|
|
|
proc anchor[T](lex: YamlLexer): bool =
|
|
|
|
debug("lex: anchor")
|
|
|
|
anchorName[T](lex)
|
|
|
|
lex.cur = ltAnchor
|
|
|
|
result = true
|
|
|
|
|
|
|
|
proc alias[T](lex: YamlLexer): bool =
|
|
|
|
debug("lex: alias")
|
|
|
|
anchorName[T](lex)
|
|
|
|
lex.cur = ltAlias
|
|
|
|
result = true
|
|
|
|
|
2016-09-23 13:42:24 +00:00
|
|
|
proc streamEnd[T](lex: YamlLexer): bool =
|
2016-09-10 15:19:37 +00:00
|
|
|
debug("lex: streamEnd")
|
2016-09-23 13:42:24 +00:00
|
|
|
startToken[T](lex)
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.cur = ltStreamEnd
|
2016-09-11 09:28:05 +00:00
|
|
|
result = true
|
|
|
|
|
2016-09-23 13:42:24 +00:00
|
|
|
proc tokenLine[T](lex: YamlLexer, pos: tuple[line, column: int], marker: bool):
|
|
|
|
string =
|
|
|
|
result = lex.lineWithMarker(pos, T, marker)
|
2016-09-11 12:55:29 +00:00
|
|
|
|
2016-09-11 16:23:47 +00:00
|
|
|
proc searchColon[T](lex: YamlLexer): bool =
|
|
|
|
var flowDepth = if lex.cur in [ltBraceOpen, ltBracketOpen]: 1 else: 0
|
|
|
|
let start = lex.getPos(T)
|
|
|
|
var
|
|
|
|
peek = start
|
|
|
|
recentAllowsAdjacent = lex.cur == ltQuotedScalar
|
|
|
|
result = false
|
|
|
|
|
|
|
|
proc skipPlainScalarContent(lex: YamlLexer) {.inline.} =
|
|
|
|
while true:
|
|
|
|
inc(peek)
|
|
|
|
case lex.at(T, peek)
|
|
|
|
of ']', '}', ',':
|
|
|
|
if flowDepth > 0 or lex.inFlow: break
|
|
|
|
of '#':
|
|
|
|
if lex.at(T, peek - 1) in space: break
|
|
|
|
of ':':
|
|
|
|
if lex.at(T, peek + 1) in spaceOrLineEnd: break
|
|
|
|
of lineEnd: break
|
|
|
|
else: discard
|
|
|
|
|
|
|
|
while peek < start + 1024:
|
|
|
|
case lex.at(T, peek)
|
|
|
|
of ':':
|
|
|
|
if flowDepth == 0:
|
|
|
|
if recentAllowsAdjacent or lex.at(T, peek + 1) in spaceOrLineEnd:
|
|
|
|
result = true
|
|
|
|
break
|
|
|
|
lex.skipPlainScalarContent()
|
|
|
|
continue
|
|
|
|
of '{', '[': inc(flowDepth)
|
|
|
|
of '}', ']':
|
|
|
|
dec(flowDepth)
|
|
|
|
if flowDepth < 0:
|
|
|
|
if lex.inFlow: break
|
|
|
|
else:
|
|
|
|
flowDepth = 0
|
|
|
|
lex.skipPlainScalarContent()
|
|
|
|
continue
|
|
|
|
recentAllowsAdjacent = true
|
|
|
|
of lineEnd: break
|
|
|
|
of '"':
|
|
|
|
while true:
|
|
|
|
inc(peek)
|
|
|
|
case lex.at(T, peek)
|
|
|
|
of lineEnd, '"': break
|
|
|
|
of '\\': inc(peek)
|
|
|
|
else: discard
|
|
|
|
if lex.at(T, peek) != '"': break
|
|
|
|
recentAllowsAdjacent = true
|
|
|
|
of '\'':
|
|
|
|
inc(peek)
|
|
|
|
while lex.at(T, peek) notin {'\''} + lineEnd: inc(peek)
|
|
|
|
if lex.at(T, peek) != '\'': break
|
|
|
|
recentAllowsAdjacent = true
|
|
|
|
of '?', ',':
|
|
|
|
if flowDepth == 0: break
|
|
|
|
of '#':
|
|
|
|
if lex.at(T, peek - 1) in space: break
|
|
|
|
lex.skipPlainScalarContent()
|
|
|
|
continue
|
|
|
|
of '&', '*', '!':
|
|
|
|
inc(peek)
|
|
|
|
while lex.at(T, peek) notin spaceOrLineEnd: inc(peek)
|
|
|
|
recentAllowsAdjacent = false
|
|
|
|
continue
|
|
|
|
of space: discard
|
|
|
|
else:
|
|
|
|
lex.skipPlainScalarContent()
|
|
|
|
continue
|
|
|
|
inc(peek)
|
|
|
|
|
2016-09-11 09:28:05 +00:00
|
|
|
# interface
|
|
|
|
|
|
|
|
proc init*[T](lex: YamlLexer) =
|
|
|
|
lex.nextState = outsideDoc[T]
|
|
|
|
lex.lineStartState = outsideDoc[T]
|
|
|
|
lex.inlineState = insideLine[T]
|
|
|
|
lex.insideLineImpl = insideLine[T]
|
2016-09-11 10:13:51 +00:00
|
|
|
lex.insideDocImpl = insideDoc[T]
|
2016-09-11 17:20:02 +00:00
|
|
|
lex.insideFlowImpl = insideFlow[T]
|
2016-09-12 19:38:56 +00:00
|
|
|
lex.outsideDocImpl = outsideDoc[T] # only needed because of compiler checks
|
2016-09-11 12:55:29 +00:00
|
|
|
lex.tokenLineGetter = tokenLine[T]
|
2016-09-11 16:23:47 +00:00
|
|
|
lex.searchColonImpl = searchColon[T]
|
2016-09-11 09:28:05 +00:00
|
|
|
|
2016-09-30 18:12:17 +00:00
|
|
|
proc newYamlLexer*(source: Stream): YamlLexer {.raises: [YamlLexerError].} =
|
|
|
|
let blSource = new(BaseLexer)
|
|
|
|
try: blSource[].open(source)
|
2016-09-14 12:35:41 +00:00
|
|
|
except:
|
2016-09-30 18:12:17 +00:00
|
|
|
var e = newException(YamlLexerError,
|
|
|
|
"Could not open stream for reading:\n" & getCurrentExceptionMsg())
|
|
|
|
e.parent = getCurrentException()
|
|
|
|
raise e
|
|
|
|
GC_ref(blSource)
|
|
|
|
new(result, proc(x: ref YamlLexerObj) {.nimcall.} =
|
|
|
|
GC_unref(cast[ref BaseLexer](x.source))
|
|
|
|
)
|
|
|
|
result[] = YamlLexerObj(source: cast[pointer](blSource), inFlow: false,
|
|
|
|
buf: "", c: blSource[].buf[blSource[].bufpos], newlines: 0,
|
|
|
|
folded: true)
|
2016-09-11 09:28:05 +00:00
|
|
|
init[BaseLexer](result)
|
|
|
|
|
2016-09-14 12:35:41 +00:00
|
|
|
proc newYamlLexer*(source: string, startAt: int = 0): YamlLexer
|
2016-09-30 18:12:17 +00:00
|
|
|
{.raises: [].} =
|
|
|
|
let sSource = new(StringSource)
|
|
|
|
sSource[] = StringSource(pos: startAt, lineStart: startAt, line: 1,
|
|
|
|
src: source)
|
|
|
|
GC_ref(sSource)
|
|
|
|
new(result, proc(x: ref YamlLexerObj) {.nimcall.} =
|
|
|
|
GC_unref(cast[ref StringSource](x.source))
|
|
|
|
)
|
|
|
|
result[] = YamlLexerObj(buf: "", source: cast[pointer](sSource),
|
|
|
|
inFlow: false, c: sSource.src[startAt], newlines: 0, folded: true)
|
2016-09-11 09:28:05 +00:00
|
|
|
init[StringSource](result)
|
|
|
|
|
2016-09-11 10:13:51 +00:00
|
|
|
proc next*(lex: YamlLexer) =
|
|
|
|
while not lex.nextState(lex): discard
|
2016-09-14 11:37:56 +00:00
|
|
|
debug("lexer -> " & $lex.cur)
|
2016-09-11 09:28:05 +00:00
|
|
|
|
|
|
|
proc setFlow*(lex: YamlLexer, value: bool) =
|
|
|
|
lex.inFlow = value
|
2016-09-11 10:52:24 +00:00
|
|
|
# in flow mode, no indentation tokens are generated because they are not
|
|
|
|
# necessary. actually, the lexer will behave wrongly if we do that, because
|
|
|
|
# adjacent values need to check if the preceding token was a JSON value, and
|
|
|
|
# if indentation tokens are generated, that information is not available.
|
2016-09-11 17:20:02 +00:00
|
|
|
# therefore, we use insideFlow instead of insideDoc in flow mode. another
|
|
|
|
# reason is that this would erratically check for document markers (---, ...)
|
|
|
|
# which are simply scalars in flow mode.
|
|
|
|
if value: lex.lineStartState = lex.insideFlowImpl
|
2016-09-11 10:13:51 +00:00
|
|
|
else: lex.lineStartState = lex.insideDocImpl
|
2016-09-11 09:28:05 +00:00
|
|
|
|
|
|
|
proc endBlockScalar*(lex: YamlLexer) =
|
|
|
|
lex.inlineState = lex.insideLineImpl
|
2016-09-11 12:55:29 +00:00
|
|
|
lex.nextState = lex.insideLineImpl
|
2016-09-13 18:19:02 +00:00
|
|
|
lex.folded = true
|
2016-09-11 12:55:29 +00:00
|
|
|
|
2016-09-12 19:38:56 +00:00
|
|
|
proc getTokenLine*(lex: YamlLexer, marker: bool = true): string =
|
2016-09-23 13:42:24 +00:00
|
|
|
result = lex.tokenLineGetter(lex, lex.curStartPos, marker)
|
|
|
|
|
|
|
|
proc getTokenLine*(lex: YamlLexer, pos: tuple[line, column: int],
|
|
|
|
marker: bool = true): string =
|
|
|
|
result = lex.tokenLineGetter(lex, pos, marker)
|
2016-09-11 16:23:47 +00:00
|
|
|
|
|
|
|
proc isImplicitKeyStart*(lex: YamlLexer): bool =
|
2016-09-19 17:33:29 +00:00
|
|
|
result = lex.searchColonImpl(lex)
|