forked from dlang-community/SDLang-D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.d
47 lines (38 loc) · 900 Bytes
/
example.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import std.stdio;
import sdlang;
int main()
{
Tag root;
try
{
// Or:
// root = parseFile("myFile.sdl");
root = parseSource(`
welcome "Hello world"
// Uncomment this for an error:
// badSuffix 12Q
myNamespace:person name="Joe Coder" {
age 36
}
`);
}
catch(SDLangParseException e)
{
// Messages will be of the form:
// myFile.sdl(5:28): Error: Invalid integer suffix.
stderr.writeln(e.msg);
return 1;
}
// Value is a std.variant.Algebraic
Value welcome = root.tags["welcome"][0].values[0];
assert(welcome.type == typeid(string));
writeln(welcome);
Tag person = root.namespaces["myNamespace"].tags["person"][0];
writeln("Name: ", person.attributes["name"][0].value);
int age = person.tags["age"][0].values[0].get!int();
writeln("Age: ", age);
// Output back to SDL
writeln("The full SDL:");
writeln(root.toSDLDocument());
return 0;
}