Skip to content

Commit

Permalink
implement semi-official string representation for tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Herringway committed Sep 22, 2023
1 parent a0d7705 commit dfc4049
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 198 deletions.
100 changes: 1 addition & 99 deletions examples/tokens/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,105 +25,7 @@ void dumpEventString(string str) @safe
auto events = new Parser(Scanner(new Reader(cast(ubyte[])str.dup)));
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);
writeln(event);
}
}
void dumpTokens(string str) @safe
Expand Down
111 changes: 111 additions & 0 deletions source/dyaml/event.d
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,117 @@ struct Event
assert(id == EventID.documentStart, "Only DocumentStart events have tag directives.");
return _tagDirectives;
}
void toString(W)(ref W writer) const
{
import std.algorithm.iteration : substitute;
import std.format : formattedWrite;
import std.range : put;
final switch (id)

Check warning on line 110 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L110

Added line #L110 was not covered by tests
{
case EventID.scalar:
put(writer, "=VAL ");
if (anchor != "")

Check warning on line 114 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L112-L114

Added lines #L112 - L114 were not covered by tests
{
writer.formattedWrite!"&%s " (anchor);

Check warning on line 116 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L116

Added line #L116 was not covered by tests
}
if (tag != "")

Check warning on line 118 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L118

Added line #L118 was not covered by tests
{
writer.formattedWrite!"<%s> " (tag);

Check warning on line 120 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L120

Added line #L120 was not covered by tests
}
final switch(scalarStyle)

Check warning on line 122 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L122

Added line #L122 was not covered by tests
{
case ScalarStyle.singleQuoted:
put(writer, "'");
break;
case ScalarStyle.doubleQuoted:
put(writer, "\"");
break;
case ScalarStyle.literal:
put(writer, "|");
break;
case ScalarStyle.folded:
put(writer, ">");
break;
case ScalarStyle.invalid: //default to plain
case ScalarStyle.plain:
put(writer, ":");
break;

Check warning on line 139 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L124-L139

Added lines #L124 - L139 were not covered by tests
}
if (value != "")

Check warning on line 141 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L141

Added line #L141 was not covered by tests
{
writer.formattedWrite!"%s"(value.substitute("\n", "\\n", `\`, `\\`, "\r", "\\r", "\t", "\\t", "\b", "\\b"));

Check warning on line 143 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L143

Added line #L143 was not covered by tests
}
break;
case EventID.streamStart:
put(writer, "+STR");
break;
case EventID.documentStart:
put(writer, "+DOC");
if (explicitDocument)

Check warning on line 151 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L145-L151

Added lines #L145 - L151 were not covered by tests
{
put(writer, " ---");

Check warning on line 153 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L153

Added line #L153 was not covered by tests
}
break;
case EventID.mappingStart:
put(writer, "+MAP");
if (collectionStyle == CollectionStyle.flow)

Check warning on line 158 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L155-L158

Added lines #L155 - L158 were not covered by tests
{
put(writer, " {}");

Check warning on line 160 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L160

Added line #L160 was not covered by tests
}
if (anchor != "")

Check warning on line 162 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L162

Added line #L162 was not covered by tests
{
put(writer, " &");
put(writer, anchor);

Check warning on line 165 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L164-L165

Added lines #L164 - L165 were not covered by tests
}
if (tag != "")

Check warning on line 167 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L167

Added line #L167 was not covered by tests
{
put(writer, " <");
put(writer, tag);
put(writer, ">");

Check warning on line 171 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L169-L171

Added lines #L169 - L171 were not covered by tests
}
break;
case EventID.sequenceStart:
put(writer, "+SEQ");
if (collectionStyle == CollectionStyle.flow)

Check warning on line 176 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L173-L176

Added lines #L173 - L176 were not covered by tests
{
put(writer, " []");

Check warning on line 178 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L178

Added line #L178 was not covered by tests
}
if (anchor != "")

Check warning on line 180 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L180

Added line #L180 was not covered by tests
{
put(writer, " &");
put(writer, anchor);

Check warning on line 183 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L182-L183

Added lines #L182 - L183 were not covered by tests
}
if (tag != "")

Check warning on line 185 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L185

Added line #L185 was not covered by tests
{
put(writer, " <");
put(writer, tag);
put(writer, ">");

Check warning on line 189 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L187-L189

Added lines #L187 - L189 were not covered by tests
}
break;
case EventID.streamEnd:
put(writer, "-STR");
break;
case EventID.documentEnd:
put(writer, "-DOC");
if (explicitDocument)

Check warning on line 197 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L191-L197

Added lines #L191 - L197 were not covered by tests
{
put(writer, " ...");

Check warning on line 199 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L199

Added line #L199 was not covered by tests
}
break;
case EventID.mappingEnd:
put(writer, "-MAP");
break;
case EventID.sequenceEnd:
put(writer, "-SEQ");
break;
case EventID.alias_:
put(writer, "=ALI *");
put(writer, anchor);
break;

Check warning on line 211 in source/dyaml/event.d

View check run for this annotation

Codecov / codecov/patch

source/dyaml/event.d#L201-L211

Added lines #L201 - L211 were not covered by tests
case EventID.invalid:
assert(0, "Invalid EventID produced");
}
}
}

/**
Expand Down
100 changes: 1 addition & 99 deletions testsuite/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,105 +27,7 @@ auto dumpEventString(string str) @safe
auto events = new Parser(Scanner(new Reader(cast(ubyte[])str.dup)));
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");
}
output ~= line;
output ~= event.text;
}
}
catch (Exception) {} //Exceptions should just stop adding output
Expand Down

0 comments on commit dfc4049

Please sign in to comment.