2016-02-27 12:09:50 +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-12 19:38:56 +00:00
|
|
|
proc noLastContext(s: YamlStream, line, column: var int,
|
|
|
|
lineContent: var string): bool =
|
|
|
|
(line, column, lineContent) = (-1, -1, "")
|
|
|
|
result = false
|
|
|
|
|
2016-09-01 18:56:34 +00:00
|
|
|
when not defined(JS):
|
|
|
|
type IteratorYamlStream = ref object of YamlStream
|
|
|
|
backend: iterator(): YamlStreamEvent
|
2016-08-10 18:42:44 +00:00
|
|
|
|
2016-09-01 18:56:34 +00:00
|
|
|
proc initYamlStream*(backend: iterator(): YamlStreamEvent): YamlStream =
|
|
|
|
result = new(IteratorYamlStream)
|
|
|
|
result.peeked = false
|
|
|
|
result.isFinished = false
|
|
|
|
IteratorYamlStream(result).backend = backend
|
|
|
|
result.nextImpl = proc(s: YamlStream, e: var YamlStreamEvent): bool =
|
|
|
|
e = IteratorYamlStream(s).backend()
|
|
|
|
if finished(IteratorYamlStream(s).backend):
|
|
|
|
s.isFinished = true
|
|
|
|
result = false
|
|
|
|
else: result = true
|
2016-09-12 19:38:56 +00:00
|
|
|
result.lastTokenContextImpl = noLastContext
|
2016-09-01 18:56:34 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
BufferYamlStream = ref object of YamlStream
|
|
|
|
pos: int
|
|
|
|
buf: seq[YamlStreamEvent] not nil
|
|
|
|
|
|
|
|
proc newBufferYamlStream(): BufferYamlStream not nil =
|
|
|
|
BufferYamlStream(peeked: false, isFinished: false, buf: @[], pos: 0,
|
2016-09-12 19:38:56 +00:00
|
|
|
lastTokenContextImpl: noLastContext,
|
2016-09-01 18:56:34 +00:00
|
|
|
nextImpl: proc(s: YamlStream, e: var YamlStreamEvent): bool =
|
|
|
|
let bys = BufferYamlStream(s)
|
|
|
|
if bys.pos == bys.buf.len:
|
|
|
|
result = false
|
|
|
|
s.isFinished = true
|
|
|
|
else:
|
|
|
|
e = bys.buf[bys.pos]
|
|
|
|
inc(bys.pos)
|
|
|
|
result = true
|
|
|
|
)
|
2016-02-12 18:53:25 +00:00
|
|
|
|
2016-08-10 18:42:44 +00:00
|
|
|
proc next*(s: YamlStream): YamlStreamEvent =
|
2016-04-02 15:48:22 +00:00
|
|
|
if s.peeked:
|
|
|
|
s.peeked = false
|
|
|
|
shallowCopy(result, s.cached)
|
2016-09-14 11:37:56 +00:00
|
|
|
return
|
2016-04-02 15:48:22 +00:00
|
|
|
else:
|
2016-08-15 19:30:49 +00:00
|
|
|
yAssert(not s.isFinished)
|
2016-04-02 15:48:22 +00:00
|
|
|
try:
|
2016-08-10 18:42:44 +00:00
|
|
|
while true:
|
|
|
|
if s.nextImpl(s, result): break
|
|
|
|
yAssert(not s.isFinished)
|
2016-04-02 15:48:22 +00:00
|
|
|
except YamlStreamError:
|
|
|
|
let cur = getCurrentException()
|
|
|
|
var e = newException(YamlStreamError, cur.msg)
|
|
|
|
e.parent = cur.parent
|
|
|
|
raise e
|
|
|
|
except Exception:
|
|
|
|
let cur = getCurrentException()
|
|
|
|
var e = newException(YamlStreamError, cur.msg)
|
|
|
|
e.parent = cur
|
|
|
|
raise e
|
2016-02-12 18:53:25 +00:00
|
|
|
|
2016-08-10 18:42:44 +00:00
|
|
|
proc peek*(s: YamlStream): YamlStreamEvent =
|
2016-04-02 15:48:22 +00:00
|
|
|
if not s.peeked:
|
|
|
|
s.cached = s.next()
|
|
|
|
s.peeked = true
|
|
|
|
shallowCopy(result, s.cached)
|
2016-02-12 18:53:25 +00:00
|
|
|
|
2016-08-10 18:42:44 +00:00
|
|
|
proc `peek=`*(s: YamlStream, value: YamlStreamEvent) =
|
2016-04-02 15:48:22 +00:00
|
|
|
s.cached = value
|
|
|
|
s.peeked = true
|
2016-08-30 20:15:29 +00:00
|
|
|
|
2016-08-10 18:42:44 +00:00
|
|
|
proc finished*(s: YamlStream): bool =
|
2016-04-02 15:48:22 +00:00
|
|
|
if s.peeked: result = false
|
|
|
|
else:
|
|
|
|
try:
|
2016-08-10 18:42:44 +00:00
|
|
|
while true:
|
|
|
|
if s.isFinished: return true
|
|
|
|
if s.nextImpl(s, s.cached):
|
|
|
|
s.peeked = true
|
|
|
|
return false
|
2016-04-02 15:48:22 +00:00
|
|
|
except YamlStreamError:
|
|
|
|
let cur = getCurrentException()
|
|
|
|
var e = newException(YamlStreamError, cur.msg)
|
|
|
|
e.parent = cur.parent
|
|
|
|
raise e
|
|
|
|
except Exception:
|
|
|
|
let cur = getCurrentException()
|
|
|
|
var e = newException(YamlStreamError, cur.msg)
|
|
|
|
e.parent = cur
|
2016-08-30 20:15:29 +00:00
|
|
|
raise e
|
2016-09-12 19:38:56 +00:00
|
|
|
|
|
|
|
proc getLastTokenContext*(s: YamlStream, line, column: var int,
|
|
|
|
lineContent: var string): bool =
|
|
|
|
result = s.lastTokenContextImpl(s, line, column, lineContent)
|