Added testSuiteEvents tool

This commit is contained in:
Felix Krause 2017-01-09 19:09:07 +01:00
parent 55f423b00a
commit 356dc5c8bd
4 changed files with 63 additions and 0 deletions

View File

@ -13,6 +13,8 @@ Bugfixes:
* Fixed parsing floating point literals (#30)
* Fixed a bug with variant records (#31)
* Empty documents now always contain an empty scalar
* Block scalars with indentation indicator now have correct whitespace on first
line.
### 0.8.0

View File

@ -73,3 +73,7 @@ task clean, "Remove all generated files":
task server, "Compile server daemon":
--d:release
setCommand "c", "server/server"
task testSuiteEvents, "Compile the testSuiteEvents tool":
--d:release
setCommand "c", "tools/testSuiteEvents"

49
tools/testSuiteEvents.nim Normal file
View File

@ -0,0 +1,49 @@
import ../yaml/stream, ../yaml/parser, ../yaml/taglib, streams
var
tags = initExtendedTagLibrary()
p = newYamlParser(tags)
events = p.parse(newFileStream(stdin))
proc start(name: string, tag: TagId, anchor: AnchorId, finish: bool = true) =
stdout.write(name)
if tag != yTagQuestionMark: stdout.write(" <" & tags.uri(tag) & ">")
if anchor != yAnchorNone: stdout.write(" &" & p.anchorName(anchor))
if finish: stdout.write("\n")
proc writeEscaped(str: string) =
for c in str:
case c
of '\\': stdout.write("\\\\")
of '\l': stdout.write("\\n")
of '\r': stdout.write("\\r")
of '\0': stdout.write("\\0")
of '\b': stdout.write("\\b")
of '\t': stdout.write("\\t")
else: stdout.write(c)
stdout.write("+STR\n")
while not(events.finished()):
let cur = events.next()
case cur.kind
of yamlStartDoc: stdout.write("+DOC\n")
of yamlStartMap: start("+MAP", cur.mapTag, cur.mapAnchor)
of yamlStartSeq: start("+SEQ", cur.seqTag, cur.seqAnchor)
of yamlEndMap: stdout.write("-MAP\n")
of yamlEndSeq: stdout.write("-SEQ\n")
of yamlEndDoc: stdout.write("-DOC\n")
of yamlScalar:
var
isQuoted = false
tag = cur.scalartag
if cur.scalarTag == yTagExclamationMark:
isQuoted = true
tag = yTagQuestionMark
start("=VAL", tag, cur.scalarAnchor, false)
if isQuoted: stdout.write(" \"")
else: stdout.write(" :")
writeEscaped(cur.scalarContent)
stdout.write("\n")
of yamlAlias:
stdout.write("=ALI *" & p.anchorName(cur.aliasTarget) & "\n")
stdout.write("-STR\n")

View File

@ -1047,3 +1047,11 @@ proc parse*(p: YamlParser, str: string): YamlStream
c.lex = newYamlLexer(str)
c.init(p)
result = c
proc anchorName*(p: YamlParser, anchor: AnchorId): string {.raises: [].} =
## Retrieve the textual representation of the given anchor as it occurred in
## the input (without the leading `&`). Returns the empty string for unknown
## anchors.
for representation, value in p.anchors:
if value == anchor: return representation
return ""