Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace std.variant with std.sumtype #2550

Merged
merged 4 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import std.process;
import std.stdio;
import std.string;
import std.typecons : Tuple, tuple;
import std.variant;
import std.path: setExtension;

/** Retrieves a list of all available commands.
Expand Down Expand Up @@ -598,8 +597,10 @@ struct CommonOptions {
*/
class CommandArgs {
struct Arg {
Variant defaultValue;
Variant value;
alias Value = SumType!(string[], string, bool, int, uint);

Value defaultValue;
Value value;
string names;
string[] helpText;
bool hidden;
Expand Down Expand Up @@ -658,23 +659,32 @@ class CommandArgs {

void getopt(T)(string names, T* var, void delegate(string, string) @safe parseValue, string[] help_text = null, bool hidden=false)
{
import std.traits : OriginalType;

foreach (ref arg; m_recognizedArgs)
if (names == arg.names) {
assert(help_text is null, format!("Duplicated argument '%s' must not change helptext, consider to remove the duplication")(names));
*var = arg.value.get!T;
*var = arg.value.match!(
(OriginalType!T v) => cast(T)v,
(_) {
if (false)
return T.init;
assert(false, "value from previous getopt has different type than the current getopt call");
}
);
return;
}
assert(help_text.length > 0);
Arg arg;
arg.defaultValue = *var;
arg.defaultValue = cast(OriginalType!T)*var;
arg.names = names;
arg.helpText = help_text;
arg.hidden = hidden;
if (parseValue is null)
m_args.getopt(config.passThrough, names, var);
else
m_args.getopt(config.passThrough, names, parseValue);
arg.value = *var;
arg.value = cast(OriginalType!T)*var;
m_recognizedArgs ~= arg;
}

Expand Down Expand Up @@ -2725,13 +2735,16 @@ private void writeOptions(CommandArgs args)
} else writeWS(longArgColumn);
size_t col = longArgColumn;
if (larg !is null) {
if (arg.defaultValue.peek!bool) {
writef("--%s", larg);
col += larg.length + 2;
} else {
writef("--%s=VALUE", larg);
col += larg.length + 8;
}
arg.defaultValue.match!(
(bool b) {
writef("--%s", larg);
col += larg.length + 2;
},
(_) {
writef("--%s=VALUE", larg);
col += larg.length + 8;
}
);
}
if (col < descColumn) {
writeWS(descColumn - col);
Expand Down
11 changes: 9 additions & 2 deletions source/dub/internal/sdlang/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import dub.internal.sdlang.exception;
import dub.internal.sdlang.token;
import dub.internal.sdlang.util;

import dub.internal.dyaml.stdsumtype;

class Attribute
{
Value value;
Expand Down Expand Up @@ -1102,7 +1104,10 @@ class Tag

// Values
foreach(val; values)
buf.put(" (%s): %s\n".format(.toString(val.type), val));
buf.put(" (%s): %s\n".format(
val.match!(v => typeof(v).stringof),
val
));

// Attributes
foreach(attrNamespace; _attributes.keys.sort())
Expand All @@ -1116,7 +1121,9 @@ class Tag

buf.put(
" %s%s(%s): %s\n".format(
namespaceStr, attr._name, .toString(attr.value.type), attr.value
namespaceStr, attr._name,
attr.value.match!(v => typeof(v).stringof),
attr.value
)
);
}
Expand Down
75 changes: 38 additions & 37 deletions source/dub/internal/sdlang/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ version (Have_sdlang_d) public import sdlang.parser;
else:

import std.file;
import std.variant : Algebraic;

import dub.internal.libInputVisitor;

Expand All @@ -18,6 +17,8 @@ import dub.internal.sdlang.symbol;
import dub.internal.sdlang.token;
import dub.internal.sdlang.util;

import dub.internal.dyaml.stdsumtype;

/// Returns root tag.
Tag parseFile(string filename)
{
Expand Down Expand Up @@ -119,7 +120,7 @@ auto pullParseSource(string source, string filename=null)
}

/// The element of the InputRange returned by pullParseFile and pullParseSource:
alias ParserEvent = Algebraic!(
alias ParserEvent = SumType!(
FileStartEvent,
FileEndEvent,
TagStartEvent,
Expand Down Expand Up @@ -483,41 +484,41 @@ private struct DOMParser
auto eventRange = inputVisitor!ParserEvent( parser );
foreach(event; eventRange)
{
if(auto e = event.peek!TagStartEvent())
{
auto newTag = new Tag(currTag, e.namespace, e.name);
newTag.location = e.location;

currTag = newTag;
}
else if(event.peek!TagEndEvent())
{
currTag = currTag.parent;

if(!currTag)
parser.error("Internal Error: Received an extra TagEndEvent");
}
else if(auto e = event.peek!ValueEvent())
{
currTag.add(e.value);
}
else if(auto e = event.peek!AttributeEvent())
{
auto attr = new Attribute(e.namespace, e.name, e.value, e.location);
currTag.add(attr);
}
else if(event.peek!FileStartEvent())
{
// Do nothing
}
else if(event.peek!FileEndEvent())
{
// There shouldn't be another parent.
if(currTag.parent)
parser.error("Internal Error: Unexpected end of file, not enough TagEndEvent");
}
else
parser.error("Internal Error: Received unknown parser event");
event.match!(
(TagStartEvent e)
{
auto newTag = new Tag(currTag, e.namespace, e.name);
newTag.location = e.location;

currTag = newTag;
},
(TagEndEvent _)
{
currTag = currTag.parent;

if(!currTag)
parser.error("Internal Error: Received an extra TagEndEvent");
},
(ValueEvent e)
{
currTag.add(e.value);
},
(AttributeEvent e)
{
auto attr = new Attribute(e.namespace, e.name, e.value, e.location);
currTag.add(attr);
},
(FileStartEvent _)
{
// Do nothing
},
(FileEndEvent _)
{
// There shouldn't be another parent.
if(currTag.parent)
parser.error("Internal Error: Unexpected end of file, not enough TagEndEvent");
}
);
}

return currTag;
Expand Down
19 changes: 5 additions & 14 deletions source/dub/internal/sdlang/token.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import std.datetime;
import std.range;
import std.string;
import std.typetuple;
import std.variant;

import dub.internal.dyaml.stdsumtype;

import dub.internal.sdlang.symbol;
import dub.internal.sdlang.util;
Expand Down Expand Up @@ -80,16 +81,16 @@ Date Time (with a known timezone): SysTime
Date Time (with an unknown timezone): DateTimeFracUnknownZone
+/
alias TypeTuple!(
typeof(null),
bool,
string, dchar,
int, long,
float, double, real,
Date, DateTimeFrac, SysTime, DateTimeFracUnknownZone, Duration,
ubyte[],
typeof(null),
) ValueTypes;

alias Algebraic!( ValueTypes ) Value; ///ditto
alias SumType!ValueTypes Value; /// ditto

template isSDLSink(T)
{
Expand Down Expand Up @@ -124,16 +125,7 @@ string toSDLString(T)(T value) if(

void toSDLString(Sink)(Value value, ref Sink sink) if(isOutputRange!(Sink,char))
{
foreach(T; ValueTypes)
{
if(value.type == typeid(T))
{
toSDLString( value.get!T(), sink );
return;
}
}

throw new Exception("Internal SDLang-D error: Unhandled type of Value. Contains: "~value.toString());
value.match!(v => toSDLString(v, sink));
}

void toSDLString(Sink)(typeof(null) value, ref Sink sink) if(isOutputRange!(Sink,char))
Expand Down Expand Up @@ -362,7 +354,6 @@ struct Token
{
if(
this.symbol != b.symbol ||
this.value.type != b.value.type ||
this.value != b.value
)
return false;
Expand Down
Loading