S-expressions: Difference between revisions
Content added Content deleted
(Updated D entry) |
(Better D entry) |
||
Line 850: | Line 850: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.conv, std.algorithm, std.variant, std.uni |
<lang d>import std.stdio, std.conv, std.algorithm, std.variant, std.uni, |
||
std.functional; |
|||
alias Sexp = Variant; |
alias Sexp = Variant; |
||
Line 856: | Line 857: | ||
struct Symbol { |
struct Symbol { |
||
private string name; |
private string name; |
||
string toString() const { return name; } |
string toString() @safe const pure nothrow { return name; } |
||
} |
} |
||
Sexp parseSexp(string txt) { |
Sexp parseSexp(string txt) @safe pure /*nothrow*/ { |
||
static bool isIdentChar(in char c) @safe pure /*nothrow*/ { |
static bool isIdentChar(in char c) @safe pure /*nothrow*/ { |
||
return c.isAlpha || "0123456789!@#-".canFind(c); |
return c.isAlpha || "0123456789!@#-".canFind(c); |
||
Line 866: | Line 867: | ||
size_t pos = 0; |
size_t pos = 0; |
||
Sexp _parse() { |
Sexp _parse() /*nothrow*/ { |
||
auto i = pos + 1; |
auto i = pos + 1; |
||
scope (exit) |
scope (exit) |
||
Line 882: | Line 883: | ||
while (txt[i].isNumber && i < txt.length) |
while (txt[i].isNumber && i < txt.length) |
||
i++; |
i++; |
||
auto aux = txt[pos .. i]; |
auto aux = txt[pos .. i]; // |
||
return aux.parse!double.Sexp; |
return aux.parse!double.Sexp; |
||
} |
} |
||
auto aux = txt[pos .. i]; |
auto aux = txt[pos .. i]; // |
||
return aux.parse!ulong.Sexp; |
return aux.parse!ulong.Sexp; |
||
} else if (isIdentChar(txt[pos])) { |
} else if (isIdentChar(txt[pos])) { |
||
Line 908: | Line 909: | ||
} |
} |
||
txt = txt.find!(not!isWhite); |
|||
pos++; |
|||
return _parse; |
return _parse; |
||
} |
} |
||
Line 936: | Line 936: | ||
"Printed: ".write; |
"Printed: ".write; |
||
pTest.writeSexp; |
pTest.writeSexp; |
||
writeln; |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |