Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 23 additions & 14 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import dub.packagesuppliers;
import dub.project;
import dub.internal.utils : getDUBVersion, getClosestMatch, getTempFile;

import dub.internal.dyaml.stdsumtype;
Comment thread
WebFreak001 marked this conversation as resolved.
import dub.internal.stdsumtype;

import std.algorithm;
import std.array;
Expand All @@ -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 @@ -575,8 +574,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 @@ -630,20 +631,25 @@ class CommandArgs {

void getopt(T)(string names, T* var, 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,
(_) => assert(false, "value from previous getopt has different type than the current getopt call"))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that return incorrect results if the previous getopt call set a value outside of T's bounds ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that could happen, but I think the previous code would have done the same. (just doing it inside the standard library and not in DUB) - It's just an issue for when the programmer makes a mistake anyway and can't usually be caused by the user doing something.

;
return;
}
assert(help_text.length > 0);
Arg arg;
arg.defaultValue = *var;
arg.defaultValue = Arg.Value(cast(OriginalType!T)*var);
Comment thread
WebFreak001 marked this conversation as resolved.
Outdated
arg.names = names;
arg.helpText = help_text;
arg.hidden = hidden;
m_args.getopt(config.passThrough, names, var);
arg.value = *var;
arg.value = Arg.Value(cast(OriginalType!T)*var);
m_recognizedArgs ~= arg;
}

Expand Down Expand Up @@ -2694,13 +2700,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
2 changes: 1 addition & 1 deletion source/dub/dependency.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dub.package_;
import dub.semver;
import dub.internal.logging;

import dub.internal.dyaml.stdsumtype;
import dub.internal.stdsumtype;

import std.algorithm;
import std.array;
Expand Down
2 changes: 1 addition & 1 deletion source/dub/internal/dyaml/node.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import std.typecons;

// FIXME: Switch back to upstream's when v2.101 is the oldest
// supported version (recommended: after v2.111 release).
import dub.internal.dyaml.stdsumtype;
import dub.internal.stdsumtype;

import dub.internal.dyaml.event;
import dub.internal.dyaml.exception;
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.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.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.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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ License: Boost License 1.0
Authors: Paul Backus
Source: $(PHOBOSSRC std/sumtype.d)
+/
module dub.internal.dyaml.stdsumtype;
module dub.internal.stdsumtype;

/// $(DIVID basic-usage,$(H3 Basic usage))
version (D_BetterC) {} else
Expand Down
2 changes: 1 addition & 1 deletion source/dub/packagemanager.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import dub.recipe.io;
import dub.internal.configy.Exceptions;
public import dub.internal.configy.Read : StrictMode;

import dub.internal.dyaml.stdsumtype;
import dub.internal.stdsumtype;

import std.algorithm : countUntil, filter, map, sort, canFind, remove;
import std.array;
Expand Down
2 changes: 1 addition & 1 deletion source/dub/recipe/packagerecipe.d
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ private static Dependency parseDMDDependency(string dep)

private T clone(T)(ref const(T) val)
{
import dub.internal.dyaml.stdsumtype;
import dub.internal.stdsumtype;
import std.traits : isSomeString, isDynamicArray, isAssociativeArray, isBasicType, ValueType;

static if (is(T == immutable)) return val;
Expand Down
Loading