Updated yaml-dev-kit test data parser

This commit is contained in:
Felix Krause 2016-12-05 19:22:51 +01:00
parent 4b529c1fd6
commit 9b5d0e60c6
1 changed files with 34 additions and 8 deletions

View File

@ -5,12 +5,13 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
import "../yaml" import "../yaml"
import lexbase, streams, tables import lexbase, streams, tables, strutils
type type
LexerToken = enum LexerToken = enum
plusStr, minusStr, plusDoc, minusDoc, plusMap, minusMap, plusSeq, minusSeq, plusStr, minusStr, plusDoc, minusDoc, plusMap, minusMap, plusSeq, minusSeq,
eqVal, eqAli, chevTag, andAnchor, quotContent, colonContent, noToken eqVal, eqAli, chevTag, andAnchor, starAnchor, quotContent, colonContent,
explDirEnd, explDocEnd, noToken
StreamPos = enum StreamPos = enum
beforeStream, inStream, afterStream beforeStream, inStream, afterStream
@ -29,7 +30,7 @@ proc nextToken(lex: var EventLexer): LexerToken =
else: break else: break
if lex.buf[lex.bufpos] == EndOfFile: return noToken if lex.buf[lex.bufpos] == EndOfFile: return noToken
case lex.buf[lex.bufpos] case lex.buf[lex.bufpos]
of ':', '"': of ':', '"', '\'', '|', '>':
let t = if lex.buf[lex.bufpos] == ':': colonContent else: quotContent let t = if lex.buf[lex.bufpos] == ':': colonContent else: quotContent
lex.content = "" lex.content = ""
lex.bufpos.inc() lex.bufpos.inc()
@ -73,6 +74,13 @@ proc nextToken(lex: var EventLexer): LexerToken =
lex.content.add(lex.buf[lex.bufpos]) lex.content.add(lex.buf[lex.bufpos])
lex.bufpos.inc() lex.bufpos.inc()
result = andAnchor result = andAnchor
of '*':
lex.content = ""
lex.bufpos.inc()
while lex.buf[lex.bufpos] notin {' ', '\t', '\r', '\l', EndOfFile}:
lex.content.add(lex.buf[lex.bufpos])
lex.bufpos.inc()
result = starAnchor
else: else:
lex.content = "" lex.content = ""
while lex.buf[lex.bufpos] notin {' ', '\t', '\r', '\l', EndOfFile}: while lex.buf[lex.bufpos] notin {' ', '\t', '\r', '\l', EndOfFile}:
@ -89,6 +97,8 @@ proc nextToken(lex: var EventLexer): LexerToken =
of "-SEQ": result = minusSeq of "-SEQ": result = minusSeq
of "=VAL": result = eqVal of "=VAL": result = eqVal
of "=ALI": result = eqAli of "=ALI": result = eqAli
of "---": result = explDirEnd
of "...": result = explDocEnd
else: raise newException(EventStreamError, "Invalid token: " & lex.content) else: raise newException(EventStreamError, "Invalid token: " & lex.content)
template assertInStream() {.dirty.} = template assertInStream() {.dirty.} =
@ -210,12 +220,19 @@ proc parseEventStream*(input: Stream, tagLib: TagLibrary): YamlStream =
if curAnchor() != yAnchorNone: if curAnchor() != yAnchorNone:
raise newException(EventStreamError, raise newException(EventStreamError,
"Duplicate anchor in " & $curEvent.kind) "Duplicate anchor in " & $curEvent.kind)
if curEvent.kind == yamlAlias: anchors[lex.content] = nextAnchorId
curEvent.aliasTarget = anchors[lex.content] setCurAnchor(nextAnchorId)
nextAnchorId = (AnchorId)(((int)nextAnchorId) + 1)
of starAnchor:
assertInEvent("alias")
if curEvent.kind != yamlAlias:
raise newException(EventStreamError, "Unexpected alias: " &
escape(lex.content))
elif curEvent.aliasTarget != yAnchorNone:
raise newException(EventStreamError, "Duplicate alias target: " &
escape(lex.content))
else: else:
anchors[lex.content] = nextAnchorId curEvent.aliasTarget = anchors[lex.content]
setCurAnchor(nextAnchorId)
nextAnchorId = (AnchorId)(((int)nextAnchorId) + 1)
of quotContent: of quotContent:
assertInEvent("scalar content") assertInEvent("scalar content")
if curTag() == yTagQuestionMark: setCurTag(yTagExclamationMark) if curTag() == yTagQuestionMark: setCurTag(yTagExclamationMark)
@ -229,5 +246,14 @@ proc parseEventStream*(input: Stream, tagLib: TagLibrary): YamlStream =
if curEvent.kind != yamlScalar: if curEvent.kind != yamlScalar:
raise newException(EventStreamError, raise newException(EventStreamError,
"scalar content in non-scalar tag") "scalar content in non-scalar tag")
of explDirEnd:
assertInEvent("explicit directives end")
if curEvent.kind != yamlStartDoc:
raise newException(EventStreamError,
"Unexpected explicit directives end")
of explDocEnd:
if curEvent.kind != yamlEndDoc:
raise newException(EventStreamError,
"Unexpected explicit document end")
of noToken: discard of noToken: discard
result = initYamlStream(backend) result = initYamlStream(backend)