Initial commit.
This commit is contained in:
commit
283c42bf8f
592 changed files with 26392 additions and 0 deletions
2
examples/constructor/Makefile
Normal file
2
examples/constructor/Makefile
Normal file
|
@ -0,0 +1,2 @@
|
|||
main:
|
||||
dmd -w -I../../ -L-L../../ -L-ldyaml main.d
|
8
examples/constructor/input.yaml
Normal file
8
examples/constructor/input.yaml
Normal file
|
@ -0,0 +1,8 @@
|
|||
scalar-red: !color FF0000
|
||||
scalar-orange: !color FFFF00
|
||||
mapping-red: !color-mapping {r: 255, g: 0, b: 0}
|
||||
mapping-orange:
|
||||
!color-mapping
|
||||
r: 255
|
||||
g: 255
|
||||
b: 0
|
110
examples/constructor/main.d
Normal file
110
examples/constructor/main.d
Normal file
|
@ -0,0 +1,110 @@
|
|||
import std.ascii;
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
import yaml;
|
||||
|
||||
struct Color
|
||||
{
|
||||
ubyte red;
|
||||
ubyte green;
|
||||
ubyte blue;
|
||||
}
|
||||
|
||||
Color constructColorScalar(Mark start, Mark end, string value)
|
||||
{
|
||||
if(value.length != 6)
|
||||
{
|
||||
throw new ConstructorException("Invalid color: " ~ value, start, end);
|
||||
}
|
||||
//We don't need to check for uppercase chars this way.
|
||||
value = value.toLower();
|
||||
|
||||
//Get value of a hex digit.
|
||||
uint hex(char c)
|
||||
{
|
||||
if(!std.ascii.isHexDigit(c))
|
||||
{
|
||||
throw new ConstructorException("Invalid color: " ~ value, start, end);
|
||||
}
|
||||
|
||||
if(std.ascii.isDigit(c))
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
return c - 'a' + 10;
|
||||
}
|
||||
|
||||
Color result;
|
||||
result.red = cast(ubyte)(16 * hex(value[0]) + hex(value[1]));
|
||||
result.green = cast(ubyte)(16 * hex(value[2]) + hex(value[3]));
|
||||
result.blue = cast(ubyte)(16 * hex(value[4]) + hex(value[5]));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Color constructColorMapping(Mark start, Mark end, Node.Pair[] pairs)
|
||||
{
|
||||
int r, g, b;
|
||||
r = g = b = -1;
|
||||
bool error = pairs.length != 3;
|
||||
|
||||
foreach(ref pair; pairs)
|
||||
{
|
||||
//Key might not be a string, and value might not be an int,
|
||||
//so we need to check for that
|
||||
try
|
||||
{
|
||||
switch(pair.key.get!string)
|
||||
{
|
||||
case "r": r = pair.value.get!int; break;
|
||||
case "g": g = pair.value.get!int; break;
|
||||
case "b": b = pair.value.get!int; break;
|
||||
default: error = true;
|
||||
}
|
||||
}
|
||||
catch(NodeException e)
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(error || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
|
||||
{
|
||||
throw new ConstructorException("Invalid color", start, end);
|
||||
}
|
||||
|
||||
return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
auto red = Color(255, 0, 0);
|
||||
auto orange = Color(255, 255, 0);
|
||||
|
||||
try
|
||||
{
|
||||
auto constructor = new Constructor;
|
||||
//both functions handle the same tag, but one handles scalar, one mapping.
|
||||
constructor.addConstructor("!color", &constructColorScalar);
|
||||
constructor.addConstructor("!color-mapping", &constructColorMapping);
|
||||
|
||||
auto loader = new Loader("input.yaml", constructor, new Resolver);
|
||||
|
||||
auto root = loader.loadSingleDocument();
|
||||
|
||||
if(root["scalar-red"].get!Color == red &&
|
||||
root["mapping-red"].get!Color == red &&
|
||||
root["scalar-orange"].get!Color == orange &&
|
||||
root["mapping-orange"].get!Color == orange)
|
||||
{
|
||||
writeln("SUCCESS");
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(YAMLException e)
|
||||
{
|
||||
writeln(e.msg);
|
||||
}
|
||||
|
||||
writeln("FAILURE");
|
||||
}
|
2
examples/getting_started/Makefile
Normal file
2
examples/getting_started/Makefile
Normal file
|
@ -0,0 +1,2 @@
|
|||
main:
|
||||
dmd -w -I../../ -L-L../../ -L-ldyaml main.d
|
4
examples/getting_started/input.yaml
Normal file
4
examples/getting_started/input.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
Hello World :
|
||||
- Hello
|
||||
- World
|
||||
Answer : 42
|
12
examples/getting_started/main.d
Normal file
12
examples/getting_started/main.d
Normal file
|
@ -0,0 +1,12 @@
|
|||
import std.stdio;
|
||||
import yaml;
|
||||
|
||||
void main()
|
||||
{
|
||||
yaml.Node root = yaml.load("input.yaml");
|
||||
foreach(string word; root["Hello World"])
|
||||
{
|
||||
writeln(word);
|
||||
}
|
||||
writeln("The answer is ", root["Answer"].get!int);
|
||||
}
|
2
examples/resolver/Makefile
Normal file
2
examples/resolver/Makefile
Normal file
|
@ -0,0 +1,2 @@
|
|||
main:
|
||||
dmd -w -I../../ -L-L../../ -L-ldyaml main.d
|
8
examples/resolver/input.yaml
Normal file
8
examples/resolver/input.yaml
Normal file
|
@ -0,0 +1,8 @@
|
|||
scalar-red: FF0000
|
||||
scalar-orange: FFFF00
|
||||
mapping-red: !color-mapping {r: 255, g: 0, b: 0}
|
||||
mapping-orange:
|
||||
!color-mapping
|
||||
r: 255
|
||||
g: 255
|
||||
b: 0
|
114
examples/resolver/main.d
Normal file
114
examples/resolver/main.d
Normal file
|
@ -0,0 +1,114 @@
|
|||
import std.ascii;
|
||||
import std.stdio;
|
||||
import std.string;
|
||||
import yaml;
|
||||
|
||||
struct Color
|
||||
{
|
||||
ubyte red;
|
||||
ubyte green;
|
||||
ubyte blue;
|
||||
}
|
||||
|
||||
Color constructColorScalar(Mark start, Mark end, string value)
|
||||
{
|
||||
if(value.length != 6)
|
||||
{
|
||||
throw new ConstructorException("Invalid color: " ~ value, start, end);
|
||||
}
|
||||
//We don't need to check for uppercase chars this way.
|
||||
value = value.toLower();
|
||||
|
||||
//Get value of a hex digit.
|
||||
uint hex(char c)
|
||||
{
|
||||
if(!std.ascii.isHexDigit(c))
|
||||
{
|
||||
throw new ConstructorException("Invalid color: " ~ value, start, end);
|
||||
}
|
||||
|
||||
if(std.ascii.isDigit(c))
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
return c - 'a' + 10;
|
||||
}
|
||||
|
||||
Color result;
|
||||
result.red = cast(ubyte)(16 * hex(value[0]) + hex(value[1]));
|
||||
result.green = cast(ubyte)(16 * hex(value[2]) + hex(value[3]));
|
||||
result.blue = cast(ubyte)(16 * hex(value[4]) + hex(value[5]));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Color constructColorMapping(Mark start, Mark end, Node.Pair[] pairs)
|
||||
{
|
||||
int r, g, b;
|
||||
r = g = b = -1;
|
||||
bool error = pairs.length != 3;
|
||||
|
||||
foreach(ref pair; pairs)
|
||||
{
|
||||
//Key might not be a string, and value might not be an int,
|
||||
//so we need to check for that
|
||||
try
|
||||
{
|
||||
switch(pair.key.get!string)
|
||||
{
|
||||
case "r": r = pair.value.get!int; break;
|
||||
case "g": g = pair.value.get!int; break;
|
||||
case "b": b = pair.value.get!int; break;
|
||||
default: error = true;
|
||||
}
|
||||
}
|
||||
catch(NodeException e)
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(error || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
|
||||
{
|
||||
throw new ConstructorException("Invalid color", start, end);
|
||||
}
|
||||
|
||||
return Color(cast(ubyte)r, cast(ubyte)g, cast(ubyte)b);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
auto red = Color(255, 0, 0);
|
||||
auto orange = Color(255, 255, 0);
|
||||
|
||||
try
|
||||
{
|
||||
auto constructor = new Constructor;
|
||||
//both functions handle the same tag, but one handles scalar, one mapping.
|
||||
constructor.addConstructor("!color", &constructColorScalar);
|
||||
constructor.addConstructor("!color-mapping", &constructColorMapping);
|
||||
|
||||
auto resolver = new Resolver;
|
||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||
"0123456789abcdefABCDEF");
|
||||
|
||||
auto loader = new Loader("input.yaml", constructor, resolver);
|
||||
|
||||
auto root = loader.loadSingleDocument();
|
||||
|
||||
if(root["scalar-red"].get!Color == red &&
|
||||
root["mapping-red"].get!Color == red &&
|
||||
root["scalar-orange"].get!Color == orange &&
|
||||
root["mapping-orange"].get!Color == orange)
|
||||
{
|
||||
writeln("SUCCESS");
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(YAMLException e)
|
||||
{
|
||||
writeln(e.msg);
|
||||
}
|
||||
|
||||
writeln("FAILURE");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue