NimYAML/src/yaml/private/lexer.nim

840 lines
29 KiB
Nim
Raw Normal View History

2015-11-27 22:42:11 +00:00
import lexbase, unicode, streams
type
Encoding* = enum
Unsupported, ## Unsupported encoding
UTF8, ## UTF-8
UTF16LE, ## UTF-16 Little Endian
UTF16BE, ## UTF-16 Big Endian
UTF32LE, ## UTF-32 Little Endian
UTF32BE ## UTF-32 Big Endian
2015-12-11 21:55:21 +00:00
YamlLexerToken* = enum
# separating tokens
yamlDirectivesEnd, yamlDocumentEnd, yamlStreamEnd,
# tokens only in directives
yamlTagDirective, yamlYamlDirective, yamlUnknownDirective,
yamlVersionPart, yamlTagURI,
yamlUnknownDirectiveParam,
# tokens in directives and content
yamlTagHandle, yamlComment,
# from here on tokens only in content
yamlLineStart,
# control characters
yamlColon, yamlDash, yamlQuestionmark, yamlComma, yamlOpeningBrace,
yamlOpeningBracket, yamlClosingBrace, yamlClosingBracket, yamlPipe,
yamlGreater,
# block scalar header
2015-12-17 20:44:41 +00:00
yamlBlockIndentationIndicator, yamlPlus,
# scalar content
yamlScalar, yamlScalarPart,
# tags
yamlVerbatimTag, yamlTagSuffix,
# anchoring
yamlAnchor, yamlAlias,
# error reporting
yamlError
2015-12-11 21:55:21 +00:00
2015-11-27 22:42:11 +00:00
YamlLexerState = enum
# initial states (not started reading any token)
ylInitial, ylInitialSpaces, ylInitialUnknown, ylInitialContent,
ylDefineTagHandleInitial, ylDefineTagURIInitial, ylInitialInLine,
ylLineEnd, ylDirectiveLineEnd,
# directive reading states
ylDirective, ylDefineTagHandle, ylDefineTagURI, ylMajorVersion,
ylMinorVersion, ylUnknownDirectiveParam, ylDirectiveComment,
# scalar reading states
ylPlainScalar, ylSingleQuotedScalar, ylDoublyQuotedScalar,
2015-11-29 15:50:27 +00:00
ylEscape, ylBlockScalar, ylBlockScalarHeader,
ylSpaceAfterPlainScalar, ylSpaceAfterQuotedScalar,
# indentation
ylIndentation,
# comments
ylComment,
# tags
ylTagHandle, ylTagSuffix, ylVerbatimTag,
# document separation
2015-11-29 21:43:10 +00:00
ylDashes, ylDots,
# anchoring
ylAnchor, ylAlias
2015-11-27 22:42:11 +00:00
YamlLexer* = object of BaseLexer
indentations: seq[int]
encoding: Encoding
charlen: int
charoffset: int
content*: string # my.content of the last returned token.
line*, column*: int
2015-11-27 22:42:11 +00:00
const
UTF8NextLine = toUTF8(Rune(0x85))
UTF8NonBreakingSpace = toUTF8(Rune(0xA0))
UTF8LineSeparator = toUTF8(Rune(0x2028))
2015-11-27 22:42:11 +00:00
UTF8ParagraphSeparator = toUTF8(Rune(0x2029))
proc detect_encoding(my: var YamlLexer) =
var numBomChars = 0
my.encoding = Unsupported
if my.bufpos == 3:
# BaseLexer already skipped UTF-8 BOM
my.encoding = UTF8
else:
case my.buf[0]
of '\0':
if my.buf[1] == '\0':
if my.buf[2] == '\0':
my.encoding = UTF32LE
elif my.buf[2] == '\xFE' and my.buf[3] == '\xFF':
my.encoding = UTF32BE
numBomChars = 4
else:
# this is probably not a unicode character stream,
# but we just use the next match in the table
my.encoding = UTF16BE
else:
# this is how a BOM-less UTF16BE input should actually look like
my.encoding = UTF16BE
of '\xFF':
case my.buf[1]
of '\xFE':
if my.buf[2] == '\0' and my.buf[3] == '\0':
my.encoding = UTF32LE
numBomChars = 4
else:
my.encoding = UTF16LE
numBomChars = 2
of '\0':
my.encoding = UTF16LE
else:
my.encoding = UTF8
of '\xFE':
case my.buf[1]
of '\xFF':
my.encoding = UTF16BE
numBomChars = 2
of '\0':
my.encoding = UTF16LE
else:
my.encoding = UTF8
else:
if my.buf[1] == '\0':
my.encoding = UTF16LE
else:
my.encoding = UTF8
inc(my.bufPos, numBomChars)
my.charlen = case my.encoding
of UTF8, Unsupported: 1
of UTF16LE, UTF16BE: 2
of UTF32LE, UTF32BE: 4
my.charoffset = case my.encoding
of UTF8, Unsupported, UTF16LE, UTF32LE: 0
of UTF16BE: 1
of UTF32BE: 3
proc open*(my: var YamlLexer, input: Stream) =
lexbase.open(my, input)
my.indentations = newSeq[int]()
my.detect_encoding()
my.content = ""
my.line = 0
my.column = 0
2015-11-27 22:42:11 +00:00
2015-12-11 21:55:21 +00:00
template yieldToken(kind: YamlLexerToken) {.dirty.} =
when defined(yamlDebug):
2015-12-11 21:55:21 +00:00
if kind == yamlScalar:
echo "Lexer token: yamlScalar(\"", my.content, "\")"
else:
2015-12-11 21:55:21 +00:00
echo "Lexer token: ", kind
2015-12-11 21:55:21 +00:00
yield kind
my.content = ""
2015-11-27 22:42:11 +00:00
template yieldError(message: string) {.dirty.} =
when defined(yamlDebug):
echo "Lexer error: " & message
my.content = message
2015-12-11 21:55:21 +00:00
yield yamlError
my.content = ""
2015-11-27 22:42:11 +00:00
template handleCR() {.dirty.} =
my.bufpos = lexbase.handleLF(my, my.bufpos + my.charoffset) + my.charlen -
my.charoffset - 1
my.line.inc()
curPos = 0
2015-11-27 22:42:11 +00:00
template handleLF() {.dirty.} =
my.bufpos = lexbase.handleLF(my, my.bufpos + my.charoffset) +
my.charlen - my.charoffset - 1
my.line.inc()
curPos = 0
2015-11-27 22:42:11 +00:00
template `or`(r: Rune, i: int): Rune =
cast[Rune](cast[int](r) or i)
iterator tokens*(my: var YamlLexer): YamlLexerToken {.closure.} =
2015-11-27 22:42:11 +00:00
var
# the following three values are used for parsing escaped unicode chars
2015-11-27 22:42:11 +00:00
unicodeChar: Rune = cast[Rune](0)
escapeLength = 0
expectedEscapeLength = 0
2015-11-29 15:50:27 +00:00
trailingSpace = ""
# used to temporarily store whitespace after a plain scalar
2015-11-27 22:42:11 +00:00
lastSpecialChar: char = '\0'
# stores chars that behave differently dependent on the following
# char. handling will be deferred to next loop iteration.
2015-11-27 22:42:11 +00:00
flowDepth = 0
# Lexer must know whether it parses block or flow style. Therefore,
# it counts the number of open flow arrays / maps here
state = ylInitial # lexer state
lastIndentationLength = 0
# after parsing the indentation of the line, this will hold the
# indentation length of the current line. Needed for checking where
# a block scalar ends.
blockScalarIndentation = -1
# when parsing a block scalar, this will be set to the indentation
# of the line that starts the flow scalar.
curPos = 0
2015-11-27 22:42:11 +00:00
while true:
let c = my.buf[my.bufpos + my.charoffset]
case state
of ylInitial:
case c
of '%':
state = ylDirective
continue
of ' ', '\t':
state = ylInitialSpaces
continue
of '#':
state = ylDirectiveComment
else:
state = ylInitialContent
continue
of ylInitialSpaces:
case c
of ' ', '\t':
my.content.add(c)
of '#':
2015-11-29 15:50:27 +00:00
my.content = ""
state = ylDirectiveComment
of EndOfFile, '\r', '\x0A':
state = ylDirectiveLineEnd
continue
else:
state = ylIndentation
continue
of ylInitialContent:
case c
of '-':
my.column = 0
state = ylDashes
continue
of '.':
yieldToken(yamlLineStart)
my.column = 0
state = ylDots
continue
else:
state = ylIndentation
continue
of ylDashes:
case c
of '-':
my.content.add(c)
of ' ', '\t', '\r', '\x0A', EndOfFile:
case my.content.len
of 3:
yieldToken(yamlDirectivesEnd)
state = ylInitialInLine
of 1:
my.content = ""
yieldToken(yamlLineStart)
lastSpecialChar = '-'
state = ylInitialInLine
else:
let tmp = my.content
my.content = ""
yieldToken(yamlLineStart)
my.content = tmp
my.column = curPos
state = ylPlainScalar
continue
else:
state = ylPlainScalar
continue
of ylDots:
case c
of '.':
my.content.add(c)
of ' ', '\t', '\r', '\x0A', EndOfFile:
case my.content.len
of 3:
yieldToken(yamlDocumentEnd)
state = ylDirectiveLineEnd
else:
state = ylPlainScalar
continue
else:
state = ylPlainScalar
continue
of ylDirectiveLineEnd:
case c
of '\r':
handleCR()
state = ylInitial
2015-11-29 15:50:27 +00:00
continue
of '\x0A':
handleLF()
state = ylInitial
2015-11-29 15:50:27 +00:00
continue
of EndOfFile:
yieldToken(yamlStreamEnd)
break
of ' ', '\t':
discard
of '#':
state = ylDirectiveComment
else:
yieldError("Unexpected content at end of directive: " & c)
2015-11-27 22:42:11 +00:00
of ylLineEnd:
case c
of '\r':
handleCR()
of '\x0A':
handleLF()
of EndOfFile:
yieldToken(yamlStreamEnd)
break
else:
2015-11-29 15:50:27 +00:00
yieldError("Internal error: Unexpected char at line end: " & c)
state = ylInitialContent
2015-11-29 15:50:27 +00:00
continue
2015-11-27 22:42:11 +00:00
of ylSingleQuotedScalar:
if lastSpecialChar != '\0':
# ' is the only special char
case c
of '\'':
my.content.add(c)
2015-11-27 22:42:11 +00:00
lastSpecialChar = '\0'
of EndOfFile, '\r', '\x0A':
yieldToken(yamlScalar)
lastSpecialChar = '\0'
state = ylLineEnd
continue
else:
yieldToken(yamlScalar)
lastSpecialChar = '\0'
2015-11-29 15:50:27 +00:00
state = ylSpaceAfterQuotedScalar
2015-11-27 22:42:11 +00:00
continue
else:
case c
of '\'':
lastSpecialChar = c
of EndOfFile:
yieldError("Unterminated single quoted string")
yieldToken(yamlStreamEnd)
break
else:
my.content.add(c)
2015-11-27 22:42:11 +00:00
of ylDoublyQuotedScalar:
case c
of '"':
yieldToken(yamlScalar)
2015-11-29 15:50:27 +00:00
state = ylSpaceAfterQuotedScalar
2015-11-27 22:42:11 +00:00
of EndOfFile:
yieldError("Unterminated doubly quoted string")
yieldToken(yamlStreamEnd)
break
of '\\':
state = ylEscape
escapeLength = 0
of '\r':
my.content.add("\x0A")
2015-11-27 22:42:11 +00:00
handleCR()
of '\x0A':
my.content.add(c)
2015-11-27 22:42:11 +00:00
handleLF()
else:
my.content.add(c)
2015-11-27 22:42:11 +00:00
of ylEscape:
if escapeLength == 0:
expectedEscapeLength = 0
case c
of EndOfFile:
yieldError("Unterminated doubly quoted string")
of '0': my.content.add('\0')
of 'a': my.content.add('\x07')
of 'b': my.content.add('\x08')
of '\t', 't': my.content.add('\t')
of 'n': my.content.add('\x0A')
of 'v': my.content.add('\v')
of 'f': my.content.add('\f')
of 'r': my.content.add('\r')
of 'e': my.content.add('\e')
of ' ': my.content.add(' ')
of '"': my.content.add('"')
of '/': my.content.add('/')
of '\\': my.content.add('\\')
of 'N': my.content.add(UTF8NextLine)
of '_': my.content.add(UTF8NonBreakingSpace)
of 'L': my.content.add(UTF8LineSeparator)
of 'P': my.content.add(UTF8ParagraphSeparator)
2015-11-27 22:42:11 +00:00
of 'x': unicodeChar = cast[Rune](0); expectedEscapeLength = 3
of 'u': unicodeChar = cast[Rune](0); expectedEscapeLength = 5
of 'U': unicodeChar = cast[Rune](0); expectedEscapeLength = 9
else:
yieldError("Unsupported escape sequence: \\" & c)
if expectedEscapeLength == 0: state = ylDoublyQuotedScalar
else:
2015-11-29 20:01:22 +00:00
let digitPosition = expectedEscapeLength - escapeLength - 1
2015-11-27 22:42:11 +00:00
case c
of EndOFFile:
yieldError("Unterminated escape sequence")
state = ylLineEnd
continue
of '0' .. '9':
unicodeChar = unicodechar or
2015-11-29 20:01:22 +00:00
(cast[int](c) - 0x30) shl (digitPosition * 4)
2015-11-27 22:42:11 +00:00
of 'A' .. 'F':
unicodeChar = unicodechar or
2015-11-29 20:01:22 +00:00
(cast[int](c) - 0x37) shl (digitPosition * 4)
2015-11-27 22:42:11 +00:00
of 'a' .. 'f':
unicodeChar = unicodechar or
2015-11-29 20:01:22 +00:00
(cast[int](c) - 0x57) shl (digitPosition * 4)
2015-11-27 22:42:11 +00:00
else:
2015-11-29 20:01:22 +00:00
yieldError("unsupported char in unicode escape sequence: " &
c)
2015-11-27 22:42:11 +00:00
escapeLength = 0
state = ylDoublyQuotedScalar
continue
inc(escapeLength)
if escapeLength == expectedEscapeLength and escapeLength > 0:
my.content.add(toUTF8(unicodeChar))
2015-11-27 22:42:11 +00:00
state = ylDoublyQuotedScalar
2015-11-29 15:50:27 +00:00
of ylSpaceAfterQuotedScalar:
case c
of ' ', '\t':
trailingSpace.add(c)
of '#':
if trailingSpace.len > 0:
yieldError("Missing space before comment start")
state = ylComment
trailingSpace = ""
else:
trailingSpace = ""
state = ylInitialInLine
2015-11-29 15:50:27 +00:00
continue
2015-11-27 22:42:11 +00:00
of ylPlainScalar:
2015-11-29 15:50:27 +00:00
case c
of EndOfFile, '\r', '\x0A':
yieldToken(yamlScalarPart)
2015-11-29 15:50:27 +00:00
state = ylLineEnd
continue
of ':':
lastSpecialChar = c
state = ylSpaceAfterPlainScalar
of ' ':
state = ylSpaceAfterPlainScalar
continue
of ',':
if flowDepth > 0:
lastSpecialChar = c
state = ylSpaceAfterPlainScalar
else:
my.content.add(c)
2015-11-29 15:50:27 +00:00
of '[', ']', '{', '}':
yieldToken(yamlScalar)
state = ylInitialInLine
2015-11-29 15:50:27 +00:00
continue
else:
my.content.add(c)
of ylSpaceAfterPlainScalar:
2015-11-27 22:42:11 +00:00
if lastSpecialChar != '\0':
case c
of ' ', '\t', EndOfFile, '\r', '\x0A':
yieldToken(yamlScalarPart)
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
else:
2015-11-29 15:50:27 +00:00
my.content.add(trailingSpace)
my.content.add(lastSpecialChar)
2015-11-27 22:42:11 +00:00
lastSpecialChar = '\0'
2015-11-29 15:50:27 +00:00
trailingSpace = ""
state = ylPlainScalar
continue
2015-11-27 22:42:11 +00:00
case c
of EndOfFile, '\r', '\x0A':
2015-11-29 15:50:27 +00:00
trailingSpace = ""
yieldToken(yamlScalarPart)
2015-11-27 22:42:11 +00:00
state = ylLineEnd
continue
2015-11-29 15:50:27 +00:00
of ' ', '\t':
trailingSpace.add(c)
of ',':
if flowDepth > 0:
lastSpecialChar = c
else:
my.content.add(trailingSpace)
my.content.add(c)
trailingSpace = ""
state = ylPlainScalar
2015-11-27 22:42:11 +00:00
of ':', '#':
lastSpecialChar = c
of '[', ']', '{', '}':
yieldToken(yamlScalar)
2015-11-29 15:50:27 +00:00
trailingSpace = ""
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
continue
else:
2015-11-29 15:50:27 +00:00
my.content.add(trailingSpace)
my.content.add(c)
2015-11-29 15:50:27 +00:00
trailingSpace = ""
state = ylPlainScalar
2015-11-27 22:42:11 +00:00
of ylInitialInLine:
2015-11-27 22:42:11 +00:00
if lastSpecialChar != '\0':
my.column = curPos - 1
2015-11-27 22:42:11 +00:00
case c
of ' ', '\t', '\r', '\x0A', EndOfFile:
case lastSpecialChar
of '#':
my.content = "#"
2015-11-27 22:42:11 +00:00
state = ylComment
of ':':
yieldToken(yamlColon)
of '?':
yieldToken(yamlQuestionmark)
of '-':
yieldToken(yamlDash)
of ',':
yieldToken(yamlComma)
2015-11-27 22:42:11 +00:00
else:
yieldError("Unexpected special char: \"" &
lastSpecialChar & "\"")
lastSpecialChar = '\0'
2015-11-27 22:42:11 +00:00
elif lastSpecialChar == '!':
case c
of '<':
state = ylVerbatimTag
lastSpecialChar = '\0'
my.bufpos += my.charlen
else:
state = ylTagHandle
my.content = "!"
2015-11-27 22:42:11 +00:00
lastSpecialChar = '\0'
else:
my.content.add(lastSpecialChar)
2015-11-27 22:42:11 +00:00
lastSpecialChar = '\0'
my.column = curPos - 1
2015-11-27 22:42:11 +00:00
state = ylPlainScalar
continue
case c
2015-11-29 15:50:27 +00:00
of '\r', '\x0A', EndOfFile:
2015-11-27 22:42:11 +00:00
state = ylLineEnd
continue
of ',':
if flowDepth > 0:
yieldToken(yamlComma)
2015-11-27 22:42:11 +00:00
else:
my.content = "" & c
my.column = curPos
2015-11-27 22:42:11 +00:00
state = ylPlainScalar
of '[':
2015-11-27 22:42:11 +00:00
inc(flowDepth)
yieldToken(yamlOpeningBracket)
of '{':
inc(flowDepth)
yieldToken(yamlOpeningBrace)
of ']':
yieldToken(yamlClosingBracket)
if flowDepth > 0:
inc(flowDepth, -1)
of '}':
yieldToken(yamlClosingBrace)
if flowDepth > 0:
2015-11-27 22:42:11 +00:00
inc(flowDepth, -1)
of '#':
lastSpecialChar = '#'
of '"':
my.column = curPos
2015-11-27 22:42:11 +00:00
state = ylDoublyQuotedScalar
of '\'':
my.column = curPos
2015-11-27 22:42:11 +00:00
state = ylSingleQuotedScalar
of '!':
2015-12-07 18:09:02 +00:00
lastSpecialChar = '!'
2015-11-27 22:42:11 +00:00
of '&':
2015-11-29 21:43:10 +00:00
state = ylAnchor
2015-11-27 22:42:11 +00:00
of '*':
2015-11-29 21:43:10 +00:00
state = ylAlias
2015-11-27 22:42:11 +00:00
of ' ':
discard
of '-':
if flowDepth == 0:
2015-11-27 22:42:11 +00:00
lastSpecialChar = '-'
else:
my.content = "" & c
my.column = curPos
2015-11-27 22:42:11 +00:00
state = ylPlainScalar
of '?', ':':
lastSpecialChar = c
of '|':
yieldToken(yamlPipe)
state = ylBlockScalarHeader
of '>':
yieldToken(yamlGreater)
state = ylBlockScalarHeader
2015-11-27 22:42:11 +00:00
of '\t':
discard
else:
my.content = "" & c
my.column = curPos
2015-11-27 22:42:11 +00:00
state = ylPlainScalar
of ylComment, ylDirectiveComment:
2015-11-27 22:42:11 +00:00
case c
of EndOfFile, '\r', '\x0A':
yieldToken(yamlComment)
case state
of ylComment:
state = ylLineEnd
of ylDirectiveComment:
state = ylDirectiveLineEnd
else:
yieldError("Should never happen")
2015-11-27 22:42:11 +00:00
continue
else:
my.content.add(c)
of ylIndentation:
2015-11-27 22:42:11 +00:00
case c
of EndOfFile, '\r', '\x0A':
lastIndentationLength = my.content.len
2015-11-27 22:42:11 +00:00
yieldToken(yamlLineStart)
state = ylLineEnd
continue
of ' ':
my.content.add(' ')
2015-11-27 22:42:11 +00:00
else:
lastIndentationLength = my.content.len
2015-11-27 22:42:11 +00:00
yieldToken(yamlLineStart)
if blockScalarIndentation != -1:
if lastIndentationLength <= blockScalarIndentation:
blockScalarIndentation = -1
else:
state = ylBlockScalar
continue
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
continue
of ylTagHandle:
case c
of '!':
my.content.add(c)
2015-11-27 22:42:11 +00:00
yieldToken(yamlTagHandle)
state = ylTagSuffix
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '-':
my.content.add(c)
2015-11-27 22:42:11 +00:00
of ' ', '\t', EndOfFile, '\r', '\x0A':
var suffix = my.content[1..^1]
my.content = "!"
2015-11-27 22:42:11 +00:00
yieldToken(yamlTagHandle)
my.content = suffix
2015-11-27 22:42:11 +00:00
yieldToken(yamlTagSuffix)
state = ylInitialInLine
2015-11-29 15:50:27 +00:00
continue
2015-11-27 22:42:11 +00:00
else:
yieldError("Invalid character in tag handle: " & c)
my.content = ""
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
of ylTagSuffix:
case c
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':', '@',
'&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')':
my.content.add(c)
2015-11-27 22:42:11 +00:00
of ' ', '\t', EndOfFile, '\r', '\x0A':
yieldToken(yamlTagSuffix)
state = ylInitialInLine
2015-11-29 15:50:27 +00:00
continue
2015-11-27 22:42:11 +00:00
else:
yieldError("Invalid character in tag suffix: " & c)
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
of ylVerbatimTag:
case c
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':', '@',
'&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')':
my.content.add(c)
2015-11-27 22:42:11 +00:00
of '>':
yieldToken(yamlVerbatimTag)
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
of EndOfFile, '\r', '\x0A':
yieldError("Unfinished verbatim tag")
state = ylLineEnd
continue
else:
yieldError("Invalid character in tag URI: " & c)
my.content = ""
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
of ylDirective:
case c
of ' ', '\t', '\r', '\x0A', EndOfFile:
if my.content == "%YAML":
2015-11-27 22:42:11 +00:00
yieldToken(yamlYamlDirective)
state = ylMajorVersion
elif my.content == "%TAG":
2015-11-27 22:42:11 +00:00
yieldToken(yamlTagDirective)
state = ylDefineTagHandleInitial
else:
yieldToken(yamlUnknownDirective)
state = ylInitialUnknown
2015-11-27 22:42:11 +00:00
if c == EndOfFile:
continue
else:
my.content.add(c)
of ylInitialUnknown:
case c
of ' ', '\t':
discard
of '\r', '\x0A', EndOfFile:
state = ylDirectiveLineEnd
continue
of '#':
state = ylDirectiveComment
continue
else:
state = ylUnknownDirectiveParam
2015-11-29 15:50:27 +00:00
continue
of ylUnknownDirectiveParam:
case c
of '\r', '\x0A', EndOfFile, ' ', '\t':
yieldToken(yamlUnknownDirectiveParam)
state = ylInitialUnknown
continue
else:
my.content.add(c)
of ylMajorVersion:
2015-11-27 22:42:11 +00:00
case c
of '0' .. '9':
my.content.add(c)
2015-11-27 22:42:11 +00:00
of '.':
yieldToken(yamlVersionPart)
state = ylMinorVersion
of EndOfFile, '\r', '\x0A', ' ', '\t':
yieldError("Missing YAML minor version.")
state = ylDirectiveLineEnd
2015-11-27 22:42:11 +00:00
continue
else:
yieldError("Invalid character in YAML version: " & c)
state = ylInitialUnknown
of ylMinorVersion:
case c
of '0' .. '9':
my.content.add(c)
of EndOfFile, '\r', '\x0A', ' ', '\t':
yieldToken(yamlVersionPart)
state = ylDirectiveLineEnd
continue
else:
yieldError("Invalid character in YAML version: " & c)
state = ylInitialUnknown
2015-11-27 22:42:11 +00:00
of ylDefineTagHandleInitial:
case c
of ' ', '\t':
discard
of EndOfFile, '\r', '\x0A':
yieldError("Unfinished %TAG directive")
state = ylDirectiveLineEnd
2015-11-27 22:42:11 +00:00
continue
of '!':
my.content.add(c)
2015-11-27 22:42:11 +00:00
state = ylDefineTagHandle
else:
yieldError("Unexpected character in %TAG directive: " & c)
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
of ylDefineTagHandle:
case c
of '!':
my.content.add(c)
2015-11-27 22:42:11 +00:00
yieldToken(yamlTagHandle)
state = ylDefineTagURIInitial
of 'a' .. 'z', 'A' .. 'Z', '-':
my.content.add(c)
2015-11-27 22:42:11 +00:00
of EndOfFile, '\r', '\x0A':
yieldError("Unfinished %TAG directive")
state = ylDirectiveLineEnd
2015-11-27 22:42:11 +00:00
continue
else:
yieldError("Unexpected char in %TAG directive: " & c)
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
of ylDefineTagURIInitial:
case c
of '\t', ' ':
my.content.add(c)
2015-11-27 22:42:11 +00:00
of '\x0A', '\r', EndOfFile:
yieldError("Unfinished %TAG directive")
state = ylDirectiveLineEnd
2015-11-27 22:42:11 +00:00
continue
else:
if my.content.len == 0:
2015-11-27 22:42:11 +00:00
yieldError("Missing whitespace in %TAG directive")
my.content = ""
2015-11-27 22:42:11 +00:00
state = ylDefineTagURI
continue
of ylDefineTagURI:
case c
of 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':', '@',
'&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')':
my.content.add(c)
of '\x0A', '\r', EndOfFile, ' ', '\t':
2015-11-27 22:42:11 +00:00
yieldToken(yamlTagURI)
state = ylDirectiveLineEnd
2015-11-27 22:42:11 +00:00
continue
else:
yieldError("Invalid URI character: " & c)
state = ylInitialInLine
2015-11-27 22:42:11 +00:00
continue
of ylBlockScalarHeader:
case c
of '0' .. '9':
my.content = "" & c
yieldToken(yamlBlockIndentationIndicator)
2015-12-17 20:44:41 +00:00
of '+':
yieldToken(yamlPlus)
of '-':
yieldToken(yamlDash)
of '\r', '\x0A', EndOfFile:
blockScalarIndentation = lastIndentationLength
state = ylLineEnd
continue
else:
yieldError("Unexpected character in block scalar header: " & c)
of ylBlockScalar:
case c
of EndOfFile, '\r', '\x0A':
yieldToken(yamlScalarPart)
state = ylLineEnd
continue
else:
my.content.add(c)
2015-11-29 21:43:10 +00:00
of ylAnchor:
case c
of EndOfFile, '\r', '\x0A', ' ', '\t', '{', '}', '[', ']':
yieldToken(yamlAnchor)
state = ylInitialInLine
continue
else:
my.content.add(c)
of ylAlias:
case c
of EndOfFile, '\r', '\x0A', ' ', '\t', '{', '}', '[', ']':
yieldToken(yamlAlias)
state = ylInitialInLine
continue
else:
my.content.add(c)
2015-11-27 22:42:11 +00:00
my.bufpos += my.charlen
curPos.inc