/ tools / testSuiteEvents.nim
testSuiteEvents.nim
 1  import ../yaml/stream, ../yaml/parser, ../yaml/taglib, streams
 2  
 3  var
 4    tags = initExtendedTagLibrary()
 5    p = newYamlParser(tags)
 6    events = p.parse(newFileStream(stdin))
 7  
 8  proc start(name: string, tag: TagId, anchor: AnchorId, finish: bool = true) =
 9    stdout.write(name)
10    if tag != yTagQuestionMark: stdout.write(" <" & tags.uri(tag) & ">")
11    if anchor != yAnchorNone: stdout.write(" &" & p.anchorName(anchor))
12    if finish: stdout.write("\n")
13  
14  proc writeEscaped(str: string) =
15    for c in str:
16      case c
17      of '\\': stdout.write("\\\\")
18      of '\l': stdout.write("\\n")
19      of '\r': stdout.write("\\r")
20      of '\0': stdout.write("\\0")
21      of '\b': stdout.write("\\b")
22      of '\t': stdout.write("\\t")
23      else: stdout.write(c)
24  
25  stdout.write("+STR\n")
26  while not(events.finished()):
27    let cur = events.next()
28    case cur.kind
29    of yamlStartDoc: stdout.write("+DOC\n")
30    of yamlStartMap: start("+MAP", cur.mapTag, cur.mapAnchor)
31    of yamlStartSeq: start("+SEQ", cur.seqTag, cur.seqAnchor)
32    of yamlEndMap: stdout.write("-MAP\n")
33    of yamlEndSeq: stdout.write("-SEQ\n")
34    of yamlEndDoc: stdout.write("-DOC\n")
35    of yamlScalar:
36      var
37        isQuoted = false
38        tag = cur.scalartag
39      if cur.scalarTag == yTagExclamationMark:
40        isQuoted = true
41        tag = yTagQuestionMark
42      start("=VAL", tag, cur.scalarAnchor, false)
43      if isQuoted: stdout.write(" \"")
44      else: stdout.write(" :")
45      writeEscaped(cur.scalarContent)
46      stdout.write("\n")
47    of yamlAlias:
48      stdout.write("=ALI *" & p.anchorName(cur.aliasTarget) & "\n")
49  stdout.write("-STR\n")