From 356dc5c8bdf79758847ee8c98a6108efc610e8da Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Mon, 9 Jan 2017 19:09:07 +0100 Subject: [PATCH] Added testSuiteEvents tool --- CHANGELOG.md | 2 ++ config.nims | 4 ++++ tools/testSuiteEvents.nim | 49 +++++++++++++++++++++++++++++++++++++++ yaml/parser.nim | 8 +++++++ 4 files changed, 63 insertions(+) create mode 100644 tools/testSuiteEvents.nim diff --git a/CHANGELOG.md b/CHANGELOG.md index 020a7f2..92369ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config.nims b/config.nims index da29fc0..bf83d95 100644 --- a/config.nims +++ b/config.nims @@ -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" \ No newline at end of file diff --git a/tools/testSuiteEvents.nim b/tools/testSuiteEvents.nim new file mode 100644 index 0000000..b4e5c84 --- /dev/null +++ b/tools/testSuiteEvents.nim @@ -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") \ No newline at end of file diff --git a/yaml/parser.nim b/yaml/parser.nim index 96c9f83..cfeab9c 100644 --- a/yaml/parser.nim +++ b/yaml/parser.nim @@ -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 "" \ No newline at end of file