Simplify and make the code of resolver more const-correct

Before it would trigger every time a thread was started.
This commit is contained in:
Geod24 2019-08-05 17:38:12 +09:00 committed by Petar Kirov
parent 974a6edd1a
commit 6e31faf9a3

View file

@ -24,14 +24,19 @@ import dyaml.node;
import dyaml.exception; import dyaml.exception;
static Tuple!(string, "tag", Regex!char, "regexp", string, "chars")[] regexes; /// Type of `regexes`
private alias RegexType = Tuple!(string, "tag", const Regex!char, "regexp", string, "chars");
static this() @safe { private immutable RegexType[] regexes;
regexes ~= tuple!("tag", "regexp", "chars")("tag:yaml.org,2002:bool",
shared static this() @safe
{
RegexType[] tmp;
tmp ~= RegexType("tag:yaml.org,2002:bool",
regex(r"^(?:yes|Yes|YES|no|No|NO|true|True|TRUE" ~ regex(r"^(?:yes|Yes|YES|no|No|NO|true|True|TRUE" ~
"|false|False|FALSE|on|On|ON|off|Off|OFF)$"), "|false|False|FALSE|on|On|ON|off|Off|OFF)$"),
"yYnNtTfFoO"); "yYnNtTfFoO");
regexes ~= tuple!("tag", "regexp", "chars")("tag:yaml.org,2002:float", tmp ~= RegexType("tag:yaml.org,2002:float",
regex(r"^(?:[-+]?([0-9][0-9_]*)\\.[0-9_]*" ~ regex(r"^(?:[-+]?([0-9][0-9_]*)\\.[0-9_]*" ~
"(?:[eE][-+][0-9]+)?|[-+]?(?:[0-9][0-9_]" ~ "(?:[eE][-+][0-9]+)?|[-+]?(?:[0-9][0-9_]" ~
"*)?\\.[0-9_]+(?:[eE][-+][0-9]+)?|[-+]?" ~ "*)?\\.[0-9_]+(?:[eE][-+][0-9]+)?|[-+]?" ~
@ -39,17 +44,17 @@ static this() @safe {
"*|[-+]?\\.(?:inf|Inf|INF)|\\." ~ "*|[-+]?\\.(?:inf|Inf|INF)|\\." ~
"(?:nan|NaN|NAN))$"), "(?:nan|NaN|NAN))$"),
"-+0123456789."); "-+0123456789.");
regexes ~= tuple!("tag", "regexp", "chars")("tag:yaml.org,2002:int", tmp ~= RegexType("tag:yaml.org,2002:int",
regex(r"^(?:[-+]?0b[0-1_]+" ~ regex(r"^(?:[-+]?0b[0-1_]+" ~
"|[-+]?0[0-7_]+" ~ "|[-+]?0[0-7_]+" ~
"|[-+]?(?:0|[1-9][0-9_]*)" ~ "|[-+]?(?:0|[1-9][0-9_]*)" ~
"|[-+]?0x[0-9a-fA-F_]+" ~ "|[-+]?0x[0-9a-fA-F_]+" ~
"|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$"), "|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$"),
"-+0123456789"); "-+0123456789");
regexes ~= tuple!("tag", "regexp", "chars")("tag:yaml.org,2002:merge", regex(r"^<<$"), "<"); tmp ~= RegexType("tag:yaml.org,2002:merge", regex(r"^<<$"), "<");
regexes ~= tuple!("tag", "regexp", "chars")("tag:yaml.org,2002:null", tmp ~= RegexType("tag:yaml.org,2002:null",
regex(r"^$|^(?:~|null|Null|NULL)$"), "~nN\0"); regex(r"^$|^(?:~|null|Null|NULL)$"), "~nN\0");
regexes ~= tuple!("tag", "regexp", "chars")("tag:yaml.org,2002:timestamp", tmp ~= RegexType("tag:yaml.org,2002:timestamp",
regex(r"^[0-9][0-9][0-9][0-9]-[0-9][0-9]-" ~ regex(r"^[0-9][0-9][0-9][0-9]-[0-9][0-9]-" ~
"[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9]" ~ "[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9]" ~
"[0-9]?-[0-9][0-9]?[Tt]|[ \t]+[0-9]" ~ "[0-9]?-[0-9][0-9]?[Tt]|[ \t]+[0-9]" ~
@ -57,12 +62,14 @@ static this() @safe {
"(?:\\.[0-9]*)?(?:[ \t]*Z|[-+][0-9]" ~ "(?:\\.[0-9]*)?(?:[ \t]*Z|[-+][0-9]" ~
"[0-9]?(?::[0-9][0-9])?)?$"), "[0-9]?(?::[0-9][0-9])?)?$"),
"0123456789"); "0123456789");
regexes ~= tuple!("tag", "regexp", "chars")("tag:yaml.org,2002:value", regex(r"^=$"), "="); tmp ~= RegexType("tag:yaml.org,2002:value", regex(r"^=$"), "=");
//The following resolver is only for documentation purposes. It cannot work //The following resolver is only for documentation purposes. It cannot work
//because plain scalars cannot start with '!', '&', or '*'. //because plain scalars cannot start with '!', '&', or '*'.
regexes ~= tuple!("tag", "regexp", "chars")("tag:yaml.org,2002:yaml", regex(r"^(?:!|&|\*)$"), "!&*"); tmp ~= RegexType("tag:yaml.org,2002:yaml", regex(r"^(?:!|&|\*)$"), "!&*");
regexes = () @trusted { return cast(immutable)tmp; }();
} }
/** /**
@ -86,7 +93,7 @@ struct Resolver
* Each tuple stores regular expression the scalar must match, * Each tuple stores regular expression the scalar must match,
* and tag to assign to it if it matches. * and tag to assign to it if it matches.
*/ */
Tuple!(string, Regex!char)[][dchar] yamlImplicitResolvers_; Tuple!(string, const Regex!char)[][dchar] yamlImplicitResolvers_;
package: package:
static auto withDefaultResolvers() @safe static auto withDefaultResolvers() @safe
@ -120,7 +127,7 @@ struct Resolver
* first = String of possible starting characters of the scalar. * first = String of possible starting characters of the scalar.
* *
*/ */
void addImplicitResolver(string tag, Regex!char regexp, string first) void addImplicitResolver(string tag, const Regex!char regexp, string first)
pure @safe pure @safe
{ {
foreach(const dchar c; first) foreach(const dchar c; first)