2016-03-04 18:10:48 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
|
|
|
# (c) Copyright 2016 Felix Krause
|
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2021-03-23 17:51:05 +00:00
|
|
|
import ../yaml, ../yaml/data
|
2016-12-05 18:22:51 +00:00
|
|
|
import lexbase, streams, tables, strutils
|
2016-03-04 18:10:48 +00:00
|
|
|
|
|
|
|
type
|
2016-04-02 15:48:22 +00:00
|
|
|
LexerToken = enum
|
|
|
|
plusStr, minusStr, plusDoc, minusDoc, plusMap, minusMap, plusSeq, minusSeq,
|
2021-10-22 15:42:48 +00:00
|
|
|
mapBraces, seqBrackets,
|
2020-11-05 19:23:42 +00:00
|
|
|
eqVal, eqAli, chevTag, andAnchor, starAnchor, colonContent, sqContent,
|
|
|
|
dqContent, litContent, foContent,
|
2016-12-05 18:22:51 +00:00
|
|
|
explDirEnd, explDocEnd, noToken
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
StreamPos = enum
|
|
|
|
beforeStream, inStream, afterStream
|
2016-03-04 18:10:48 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
EventLexer = object of BaseLexer
|
|
|
|
content: string
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2020-11-04 15:40:37 +00:00
|
|
|
EventStreamError = object of ValueError
|
2016-03-04 18:10:48 +00:00
|
|
|
|
2016-03-09 19:15:43 +00:00
|
|
|
proc nextToken(lex: var EventLexer): LexerToken =
|
2016-04-02 15:48:22 +00:00
|
|
|
while true:
|
2016-03-04 18:10:48 +00:00
|
|
|
case lex.buf[lex.bufpos]
|
2016-04-02 15:48:22 +00:00
|
|
|
of ' ', '\t': lex.bufpos.inc()
|
|
|
|
of '\r': lex.bufpos = lex.handleCR(lex.bufpos)
|
|
|
|
of '\l': lex.bufpos = lex.handleLF(lex.bufpos)
|
|
|
|
else: break
|
|
|
|
if lex.buf[lex.bufpos] == EndOfFile: return noToken
|
|
|
|
case lex.buf[lex.bufpos]
|
2016-12-05 18:22:51 +00:00
|
|
|
of ':', '"', '\'', '|', '>':
|
2020-11-05 19:23:42 +00:00
|
|
|
let t = case lex.buf[lex.bufpos]
|
|
|
|
of ':': colonContent
|
|
|
|
of '"': dqContent
|
|
|
|
of '\'': sqContent
|
|
|
|
of '|': litContent
|
|
|
|
of '>': foContent
|
|
|
|
else: colonContent
|
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
lex.content = ""
|
|
|
|
lex.bufpos.inc()
|
|
|
|
while true:
|
|
|
|
case lex.buf[lex.bufpos]
|
|
|
|
of EndOfFile: break
|
|
|
|
of '\c':
|
|
|
|
lex.bufpos = lex.handleCR(lex.bufpos)
|
|
|
|
break
|
|
|
|
of '\l':
|
|
|
|
lex.bufpos = lex.handleLF(lex.bufpos)
|
|
|
|
break
|
|
|
|
of '\\':
|
2016-03-09 19:15:43 +00:00
|
|
|
lex.bufpos.inc()
|
2016-04-02 15:48:22 +00:00
|
|
|
case lex.buf[lex.bufpos]
|
|
|
|
of 'n': lex.content.add('\l')
|
|
|
|
of 'r': lex.content.add('\r')
|
|
|
|
of '0': lex.content.add('\0')
|
|
|
|
of 'b': lex.content.add('\b')
|
|
|
|
of 't': lex.content.add('\t')
|
|
|
|
of '\\': lex.content.add('\\')
|
2016-03-09 19:15:43 +00:00
|
|
|
else: raise newException(EventStreamError,
|
2016-04-02 15:48:22 +00:00
|
|
|
"Unknown escape character: " & lex.buf[lex.bufpos])
|
|
|
|
else: lex.content.add(lex.buf[lex.bufpos])
|
|
|
|
lex.bufpos.inc()
|
|
|
|
result = t
|
|
|
|
of '<':
|
|
|
|
lex.content = ""
|
|
|
|
lex.bufpos.inc()
|
|
|
|
while lex.buf[lex.bufpos] != '>':
|
|
|
|
lex.content.add(lex.buf[lex.bufpos])
|
|
|
|
lex.bufpos.inc()
|
|
|
|
if lex.buf[lex.bufpos] == EndOfFile:
|
|
|
|
raise newException(EventStreamError, "Unclosed tag URI!")
|
|
|
|
result = chevTag
|
|
|
|
lex.bufpos.inc()
|
|
|
|
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 = andAnchor
|
2016-12-05 18:22:51 +00:00
|
|
|
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
|
2021-10-22 15:42:48 +00:00
|
|
|
of '{':
|
|
|
|
lex.bufpos.inc()
|
|
|
|
if lex.buf[lex.bufpos] == '}':
|
|
|
|
result = mapBraces
|
|
|
|
else: raise newException(EventStreamError, "Invalid token: {" & lex.buf[lex.bufpos])
|
|
|
|
lex.bufpos.inc()
|
|
|
|
of '[':
|
|
|
|
lex.bufpos.inc()
|
|
|
|
if lex.buf[lex.bufpos] == ']':
|
|
|
|
result = seqBrackets
|
|
|
|
else: raise newException(EventStreamError, "Invalid token: [" & lex.buf[lex.bufpos])
|
|
|
|
lex.bufpos.inc()
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
|
|
|
lex.content = ""
|
|
|
|
while lex.buf[lex.bufpos] notin {' ', '\t', '\r', '\l', EndOfFile}:
|
|
|
|
lex.content.add(lex.buf[lex.bufpos])
|
|
|
|
lex.bufpos.inc()
|
|
|
|
case lex.content
|
|
|
|
of "+STR": result = plusStr
|
|
|
|
of "-STR": result = minusStr
|
|
|
|
of "+DOC": result = plusDoc
|
|
|
|
of "-DOC": result = minusDoc
|
|
|
|
of "+MAP": result = plusMap
|
|
|
|
of "-MAP": result = minusMap
|
|
|
|
of "+SEQ": result = plusSeq
|
|
|
|
of "-SEQ": result = minusSeq
|
|
|
|
of "=VAL": result = eqVal
|
|
|
|
of "=ALI": result = eqAli
|
2016-12-05 18:22:51 +00:00
|
|
|
of "---": result = explDirEnd
|
|
|
|
of "...": result = explDocEnd
|
2016-04-02 15:48:22 +00:00
|
|
|
else: raise newException(EventStreamError, "Invalid token: " & lex.content)
|
2016-03-04 18:10:48 +00:00
|
|
|
|
|
|
|
template assertInEvent(name: string) {.dirty.} =
|
2016-04-02 15:48:22 +00:00
|
|
|
if not inEvent:
|
|
|
|
raise newException(EventStreamError, "Illegal token: " & name)
|
2016-03-04 18:10:48 +00:00
|
|
|
|
|
|
|
template yieldEvent() {.dirty.} =
|
2016-04-02 15:48:22 +00:00
|
|
|
if inEvent:
|
|
|
|
yield curEvent
|
|
|
|
inEvent = false
|
2016-03-04 18:10:48 +00:00
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
template setTag(t: Tag) {.dirty.} =
|
2016-04-02 15:48:22 +00:00
|
|
|
case curEvent.kind
|
2020-11-04 15:40:37 +00:00
|
|
|
of yamlStartSeq: curEvent.seqProperties.tag = t
|
|
|
|
of yamlStartMap: curEvent.mapProperties.tag = t
|
|
|
|
of yamlScalar: curEvent.scalarProperties.tag = t
|
2016-04-02 15:48:22 +00:00
|
|
|
else: discard
|
2016-03-04 18:10:48 +00:00
|
|
|
|
2020-11-04 15:40:37 +00:00
|
|
|
template setAnchor(a: Anchor) {.dirty.} =
|
2016-04-02 15:48:22 +00:00
|
|
|
case curEvent.kind
|
2020-11-04 15:40:37 +00:00
|
|
|
of yamlStartSeq: curEvent.seqProperties.anchor = a
|
|
|
|
of yamlStartMap: curEvent.mapProperties.anchor = a
|
|
|
|
of yamlScalar: curEvent.scalarProperties.anchor = a
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlAlias: curEvent.aliasTarget = a
|
|
|
|
else: discard
|
2016-03-04 18:10:48 +00:00
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
template curTag(): Tag =
|
|
|
|
var foo: Tag
|
2016-04-02 15:48:22 +00:00
|
|
|
case curEvent.kind
|
2020-11-04 15:40:37 +00:00
|
|
|
of yamlStartSeq: foo = curEvent.seqProperties.tag
|
|
|
|
of yamlStartMap: foo = curEvent.mapProperties.tag
|
|
|
|
of yamlScalar: foo = curEvent.scalarProperties.tag
|
2016-04-02 15:48:22 +00:00
|
|
|
else: raise newException(EventStreamError,
|
|
|
|
$curEvent.kind & " may not have a tag")
|
|
|
|
foo
|
2016-03-04 18:10:48 +00:00
|
|
|
|
2020-11-10 12:55:22 +00:00
|
|
|
template setCurTag(val: Tag) =
|
2016-04-02 15:48:22 +00:00
|
|
|
case curEvent.kind
|
2020-11-04 15:40:37 +00:00
|
|
|
of yamlStartSeq: curEvent.seqProperties.tag = val
|
|
|
|
of yamlStartMap: curEvent.mapProperties.tag = val
|
|
|
|
of yamlScalar: curEvent.scalarProperties.tag = val
|
2016-04-02 15:48:22 +00:00
|
|
|
else: raise newException(EventStreamError,
|
|
|
|
$curEvent.kind & " may not have a tag")
|
2016-03-09 19:15:43 +00:00
|
|
|
|
2020-11-04 15:40:37 +00:00
|
|
|
template curAnchor(): Anchor =
|
|
|
|
var foo: Anchor
|
2016-04-02 15:48:22 +00:00
|
|
|
case curEvent.kind
|
2020-11-04 15:40:37 +00:00
|
|
|
of yamlStartSeq: foo = curEvent.seqProperties.anchor
|
|
|
|
of yamlStartMap: foo = curEvent.mapProperties.anchor
|
|
|
|
of yamlScalar: foo = curEvent.scalarProperties.anchor
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlAlias: foo = curEvent.aliasTarget
|
|
|
|
else: raise newException(EventStreamError,
|
|
|
|
$curEvent.kind & "may not have an anchor")
|
|
|
|
foo
|
2016-03-09 19:15:43 +00:00
|
|
|
|
2020-11-04 15:40:37 +00:00
|
|
|
template setCurAnchor(val: Anchor) =
|
2016-04-02 15:48:22 +00:00
|
|
|
case curEvent.kind
|
2020-11-04 15:40:37 +00:00
|
|
|
of yamlStartSeq: curEvent.seqProperties.anchor = val
|
|
|
|
of yamlStartMap: curEvent.mapProperties.anchor = val
|
|
|
|
of yamlScalar: curEvent.scalarProperties.anchor = val
|
2016-04-02 15:48:22 +00:00
|
|
|
of yamlAlias: curEvent.aliasTarget = val
|
|
|
|
else: raise newException(EventStreamError,
|
|
|
|
$curEvent.kind & " may not have an anchor")
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2020-11-04 15:40:37 +00:00
|
|
|
template eventStart(k: EventKind) {.dirty.} =
|
2020-11-04 21:47:52 +00:00
|
|
|
if streamPos == beforeStream:
|
|
|
|
yield Event(kind: yamlStartStream)
|
|
|
|
streamPos = inStream
|
|
|
|
else: yieldEvent()
|
2020-11-04 15:40:37 +00:00
|
|
|
curEvent = Event(kind: k)
|
2016-04-02 15:48:22 +00:00
|
|
|
setTag(yTagQuestionMark)
|
|
|
|
setAnchor(yAnchorNone)
|
|
|
|
inEvent = true
|
2016-03-04 18:10:48 +00:00
|
|
|
|
2021-03-23 17:51:05 +00:00
|
|
|
proc parseEventStream*(input: Stream): YamlStream =
|
2020-11-04 15:40:37 +00:00
|
|
|
var backend = iterator(): Event =
|
2016-04-02 15:48:22 +00:00
|
|
|
var lex: EventLexer
|
|
|
|
lex.open(input)
|
|
|
|
var
|
|
|
|
inEvent = false
|
2020-11-04 15:40:37 +00:00
|
|
|
curEvent: Event
|
2016-04-03 09:45:48 +00:00
|
|
|
streamPos: StreamPos = beforeStream
|
2016-04-02 15:48:22 +00:00
|
|
|
while lex.buf[lex.bufpos] != EndOfFile:
|
|
|
|
let token = lex.nextToken()
|
|
|
|
case token
|
|
|
|
of plusStr:
|
|
|
|
if streamPos != beforeStream:
|
|
|
|
raise newException(EventStreamError, "Illegal +STR")
|
|
|
|
streamPos = inStream
|
2020-11-04 15:40:37 +00:00
|
|
|
eventStart(yamlStartStream)
|
2016-04-02 15:48:22 +00:00
|
|
|
of minusStr:
|
|
|
|
if streamPos != inStream:
|
|
|
|
raise newException(EventStreamError, "Illegal -STR")
|
|
|
|
streamPos = afterStream
|
2020-11-04 15:40:37 +00:00
|
|
|
eventStart(yamlEndStream)
|
2016-04-02 15:48:22 +00:00
|
|
|
of plusDoc: eventStart(yamlStartDoc)
|
|
|
|
of minusDoc: eventStart(yamlEndDoc)
|
|
|
|
of plusMap: eventStart(yamlStartMap)
|
|
|
|
of minusMap: eventStart(yamlEndMap)
|
|
|
|
of plusSeq: eventStart(yamlStartSeq)
|
|
|
|
of minusSeq: eventStart(yamlEndSeq)
|
|
|
|
of eqVal: eventStart(yamlScalar)
|
|
|
|
of eqAli: eventStart(yamlAlias)
|
2021-10-22 15:42:48 +00:00
|
|
|
of mapBraces:
|
|
|
|
assertInEvent("braces")
|
|
|
|
curEvent.mapStyle = csFlow
|
|
|
|
of seqBrackets:
|
|
|
|
assertInEvent("brackets")
|
|
|
|
curEvent.seqStyle = csFlow
|
2016-04-02 15:48:22 +00:00
|
|
|
of chevTag:
|
|
|
|
assertInEvent("tag")
|
|
|
|
if curTag() != yTagQuestionMark:
|
|
|
|
raise newException(EventStreamError,
|
|
|
|
"Duplicate tag in " & $curEvent.kind)
|
2021-03-23 17:51:05 +00:00
|
|
|
setCurTag(Tag(lex.content))
|
2016-04-02 15:48:22 +00:00
|
|
|
of andAnchor:
|
|
|
|
assertInEvent("anchor")
|
|
|
|
if curAnchor() != yAnchorNone:
|
|
|
|
raise newException(EventStreamError,
|
|
|
|
"Duplicate anchor in " & $curEvent.kind)
|
2020-11-04 21:47:52 +00:00
|
|
|
setCurAnchor(lex.content.Anchor)
|
2016-12-05 18:22:51 +00:00
|
|
|
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))
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2020-11-04 15:40:37 +00:00
|
|
|
curEvent.aliasTarget = lex.content.Anchor
|
2020-11-05 19:23:42 +00:00
|
|
|
of colonContent:
|
2016-04-02 15:48:22 +00:00
|
|
|
assertInEvent("scalar content")
|
2020-11-05 19:23:42 +00:00
|
|
|
curEvent.scalarContent = lex.content
|
2016-04-02 15:48:22 +00:00
|
|
|
if curEvent.kind != yamlScalar:
|
|
|
|
raise newException(EventStreamError,
|
|
|
|
"scalar content in non-scalar tag")
|
2020-11-05 19:23:42 +00:00
|
|
|
of sqContent:
|
|
|
|
assertInEvent("scalar content")
|
2016-04-02 15:48:22 +00:00
|
|
|
curEvent.scalarContent = lex.content
|
2020-11-05 19:23:42 +00:00
|
|
|
if curTag() == yTagQuestionMark: setCurTag(yTagExclamationMark)
|
|
|
|
curEvent.scalarStyle = ssSingleQuoted
|
|
|
|
of dqContent:
|
2016-04-02 15:48:22 +00:00
|
|
|
assertInEvent("scalar content")
|
|
|
|
curEvent.scalarContent = lex.content
|
2020-11-05 19:23:42 +00:00
|
|
|
if curTag() == yTagQuestionMark: setCurTag(yTagExclamationMark)
|
|
|
|
curEvent.scalarStyle = ssDoubleQuoted
|
|
|
|
of litContent:
|
|
|
|
assertInEvent("scalar content")
|
|
|
|
curEvent.scalarContent = lex.content
|
|
|
|
curEvent.scalarStyle = ssLiteral
|
|
|
|
of foContent:
|
|
|
|
assertInEvent("scalar content")
|
|
|
|
curEvent.scalarContent = lex.content
|
|
|
|
curEvent.scalarStyle = ssFolded
|
2016-12-05 18:22:51 +00:00
|
|
|
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")
|
2016-04-02 15:48:22 +00:00
|
|
|
of noToken: discard
|
2020-11-04 21:47:52 +00:00
|
|
|
yieldEvent()
|
|
|
|
if streamPos == inStream:
|
|
|
|
yield Event(kind: yamlEndStream)
|
2016-04-02 15:48:22 +00:00
|
|
|
result = initYamlStream(backend)
|