Skip to content

Commit 2c915b3

Browse files
committed
add token reader example
1 parent bca1e47 commit 2c915b3

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

.github/workflows/d.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
dub build dyaml:resolver
4545
dub build dyaml:testsuite
4646
dub build dyaml:tojson
47+
dub build dyaml:tokens
4748
dub build dyaml:yaml_gen
4849
dub build dyaml:yaml_stats
4950
ninja:

dub.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"examples/representer",
2929
"examples/resolver",
3030
"examples/tojson",
31+
"examples/tokens",
3132
"examples/yaml_bench",
3233
"examples/yaml_gen",
3334
"examples/yaml_stats",

examples/tokens/dub.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "tokens",
3+
"targetType": "executable",
4+
"dependencies":
5+
{
6+
"dyaml": "*"
7+
}
8+
}

examples/tokens/source/app.d

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
module dyaml.testsuite;
2+
3+
import dyaml;
4+
import dyaml.event;
5+
6+
import std.algorithm;
7+
import std.conv;
8+
import std.file;
9+
import std.format;
10+
import std.getopt;
11+
import std.json;
12+
import std.path;
13+
import std.range;
14+
import std.stdio;
15+
import std.string;
16+
import std.typecons;
17+
import std.utf;
18+
import std.uni;
19+
20+
void dumpEventString(string str) @safe
21+
{
22+
auto events = Loader.fromString(str).parse();
23+
foreach (event; events)
24+
{
25+
string line;
26+
final switch (event.id)
27+
{
28+
case EventID.scalar:
29+
line = "=VAL ";
30+
if (event.anchor != "")
31+
{
32+
line ~= text("&", event.anchor, " ");
33+
}
34+
if (event.tag != "")
35+
{
36+
line ~= text("<", event.tag, "> ");
37+
}
38+
switch(event.scalarStyle)
39+
{
40+
case ScalarStyle.singleQuoted:
41+
line ~= "'";
42+
break;
43+
case ScalarStyle.doubleQuoted:
44+
line ~= '"';
45+
break;
46+
case ScalarStyle.literal:
47+
line ~= "|";
48+
break;
49+
case ScalarStyle.folded:
50+
line ~= ">";
51+
break;
52+
default:
53+
line ~= ":";
54+
break;
55+
}
56+
if (event.value != "")
57+
{
58+
line ~= text(event.value.substitute("\n", "\\n", `\`, `\\`, "\r", "\\r", "\t", "\\t", "\b", "\\b"));
59+
}
60+
break;
61+
case EventID.streamStart:
62+
line = "+STR";
63+
break;
64+
case EventID.documentStart:
65+
line = "+DOC";
66+
if (event.explicitDocument)
67+
{
68+
line ~= text(" ---");
69+
}
70+
break;
71+
case EventID.mappingStart:
72+
line = "+MAP";
73+
if (event.collectionStyle == CollectionStyle.flow)
74+
{
75+
line ~= text(" {}");
76+
}
77+
if (event.anchor != "")
78+
{
79+
line ~= text(" &", event.anchor);
80+
}
81+
if (event.tag != "")
82+
{
83+
line ~= text(" <", event.tag, ">");
84+
}
85+
break;
86+
case EventID.sequenceStart:
87+
line = "+SEQ";
88+
if (event.collectionStyle == CollectionStyle.flow)
89+
{
90+
line ~= text(" []");
91+
}
92+
if (event.anchor != "")
93+
{
94+
line ~= text(" &", event.anchor);
95+
}
96+
if (event.tag != "")
97+
{
98+
line ~= text(" <", event.tag, ">");
99+
}
100+
break;
101+
case EventID.streamEnd:
102+
line = "-STR";
103+
break;
104+
case EventID.documentEnd:
105+
line = "-DOC";
106+
if (event.explicitDocument)
107+
{
108+
line ~= " ...";
109+
}
110+
break;
111+
case EventID.mappingEnd:
112+
line = "-MAP";
113+
break;
114+
case EventID.sequenceEnd:
115+
line = "-SEQ";
116+
break;
117+
case EventID.alias_:
118+
line = text("=ALI *", event.anchor);
119+
break;
120+
case EventID.invalid:
121+
assert(0, "Invalid EventID produced");
122+
}
123+
writeln(line);
124+
}
125+
}
126+
void dumpTokens(string str) @safe
127+
{
128+
writefln("%(%s\n%)", Loader.fromString(str).parse());
129+
}
130+
131+
132+
void main(string[] args) @system
133+
{
134+
bool tokens;
135+
getopt(args,
136+
"t|tokens", &tokens);
137+
string str;
138+
if (args[1] == "-") {
139+
str = cast(string)(stdin.byChunk(4096).joiner().array);
140+
} else {
141+
str = readText(args[1]);
142+
}
143+
if (tokens) {
144+
dumpTokens(str);
145+
} else {
146+
dumpEventString(str);
147+
}
148+
}

0 commit comments

Comments
 (0)