From 2c915b3f08020b2ede7f97b9a42f1a1e245b0d03 Mon Sep 17 00:00:00 2001 From: Cameron Ross Date: Tue, 22 Nov 2022 19:57:57 -0400 Subject: [PATCH] add token reader example --- .github/workflows/d.yml | 1 + dub.json | 1 + examples/tokens/dub.json | 8 ++ examples/tokens/source/app.d | 148 +++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 examples/tokens/dub.json create mode 100644 examples/tokens/source/app.d diff --git a/.github/workflows/d.yml b/.github/workflows/d.yml index c5dee8fb..b14a0691 100644 --- a/.github/workflows/d.yml +++ b/.github/workflows/d.yml @@ -44,6 +44,7 @@ jobs: dub build dyaml:resolver dub build dyaml:testsuite dub build dyaml:tojson + dub build dyaml:tokens dub build dyaml:yaml_gen dub build dyaml:yaml_stats ninja: diff --git a/dub.json b/dub.json index 91cbb68b..601f9027 100644 --- a/dub.json +++ b/dub.json @@ -28,6 +28,7 @@ "examples/representer", "examples/resolver", "examples/tojson", + "examples/tokens", "examples/yaml_bench", "examples/yaml_gen", "examples/yaml_stats", diff --git a/examples/tokens/dub.json b/examples/tokens/dub.json new file mode 100644 index 00000000..4ffb2182 --- /dev/null +++ b/examples/tokens/dub.json @@ -0,0 +1,8 @@ +{ + "name": "tokens", + "targetType": "executable", + "dependencies": + { + "dyaml": "*" + } +} diff --git a/examples/tokens/source/app.d b/examples/tokens/source/app.d new file mode 100644 index 00000000..d7f087b1 --- /dev/null +++ b/examples/tokens/source/app.d @@ -0,0 +1,148 @@ +module dyaml.testsuite; + +import dyaml; +import dyaml.event; + +import std.algorithm; +import std.conv; +import std.file; +import std.format; +import std.getopt; +import std.json; +import std.path; +import std.range; +import std.stdio; +import std.string; +import std.typecons; +import std.utf; +import std.uni; + +void dumpEventString(string str) @safe +{ + auto events = Loader.fromString(str).parse(); + foreach (event; events) + { + string line; + final switch (event.id) + { + case EventID.scalar: + line = "=VAL "; + if (event.anchor != "") + { + line ~= text("&", event.anchor, " "); + } + if (event.tag != "") + { + line ~= text("<", event.tag, "> "); + } + switch(event.scalarStyle) + { + case ScalarStyle.singleQuoted: + line ~= "'"; + break; + case ScalarStyle.doubleQuoted: + line ~= '"'; + break; + case ScalarStyle.literal: + line ~= "|"; + break; + case ScalarStyle.folded: + line ~= ">"; + break; + default: + line ~= ":"; + break; + } + if (event.value != "") + { + line ~= text(event.value.substitute("\n", "\\n", `\`, `\\`, "\r", "\\r", "\t", "\\t", "\b", "\\b")); + } + break; + case EventID.streamStart: + line = "+STR"; + break; + case EventID.documentStart: + line = "+DOC"; + if (event.explicitDocument) + { + line ~= text(" ---"); + } + break; + case EventID.mappingStart: + line = "+MAP"; + if (event.collectionStyle == CollectionStyle.flow) + { + line ~= text(" {}"); + } + if (event.anchor != "") + { + line ~= text(" &", event.anchor); + } + if (event.tag != "") + { + line ~= text(" <", event.tag, ">"); + } + break; + case EventID.sequenceStart: + line = "+SEQ"; + if (event.collectionStyle == CollectionStyle.flow) + { + line ~= text(" []"); + } + if (event.anchor != "") + { + line ~= text(" &", event.anchor); + } + if (event.tag != "") + { + line ~= text(" <", event.tag, ">"); + } + break; + case EventID.streamEnd: + line = "-STR"; + break; + case EventID.documentEnd: + line = "-DOC"; + if (event.explicitDocument) + { + line ~= " ..."; + } + break; + case EventID.mappingEnd: + line = "-MAP"; + break; + case EventID.sequenceEnd: + line = "-SEQ"; + break; + case EventID.alias_: + line = text("=ALI *", event.anchor); + break; + case EventID.invalid: + assert(0, "Invalid EventID produced"); + } + writeln(line); + } +} +void dumpTokens(string str) @safe +{ + writefln("%(%s\n%)", Loader.fromString(str).parse()); +} + + +void main(string[] args) @system +{ + bool tokens; + getopt(args, + "t|tokens", &tokens); + string str; + if (args[1] == "-") { + str = cast(string)(stdin.byChunk(4096).joiner().array); + } else { + str = readText(args[1]); + } + if (tokens) { + dumpTokens(str); + } else { + dumpEventString(str); + } +}