JSON parser checkpoint
This commit is contained in:
parent
c6ea4f83c2
commit
2bc28d47ea
40
spec/json_types.d
Normal file
40
spec/json_types.d
Normal file
@ -0,0 +1,40 @@
|
||||
class JSONValue
|
||||
{
|
||||
}
|
||||
|
||||
class JSONObject < JSONValue
|
||||
{
|
||||
JSONValue[string] value;
|
||||
}
|
||||
|
||||
class JSONArray < JSONValue
|
||||
{
|
||||
JSONValue[] value;
|
||||
}
|
||||
|
||||
class JSONNumber < JSONValue
|
||||
{
|
||||
double value;
|
||||
}
|
||||
|
||||
class JSONString < JSONValue
|
||||
{
|
||||
string value;
|
||||
|
||||
this(string value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
class JSONTrue < JSONValue
|
||||
{
|
||||
}
|
||||
|
||||
class JSONFalse < JSONValue
|
||||
{
|
||||
}
|
||||
|
||||
class JSONNull < JSONValue
|
||||
{
|
||||
}
|
@ -334,4 +334,131 @@ EOF
|
||||
expect(results.stderr).to eq ""
|
||||
expect(results.status).to eq 0
|
||||
end
|
||||
|
||||
it "allows creating a JSON parser" do
|
||||
write_grammar <<EOF
|
||||
<<
|
||||
import json_types;
|
||||
string string_value;
|
||||
>>
|
||||
drop /\\s+/;
|
||||
token lbrace /\\{/;
|
||||
token rbrace /\\}/RuRfaw2CHgpvCWu;
|
||||
token lbracket /\\[/;
|
||||
token rbracket /\\]/;
|
||||
token comma /,/;
|
||||
token colon /:/;
|
||||
token number /-?(0|[1-9][0-9]*)(\\.[0-9]+)?([eE][-+]?[0-9]+)?/ <<
|
||||
bool negative;
|
||||
bool in_fraction;
|
||||
bool in_exponent;
|
||||
double exponent;
|
||||
foreach (c; match)
|
||||
{
|
||||
if (c == '-')
|
||||
{
|
||||
negative = true;
|
||||
}
|
||||
}
|
||||
$$ = new JSONNumber();
|
||||
>>
|
||||
token true <<
|
||||
$$ = new JSONTrue();
|
||||
>>
|
||||
token false <<
|
||||
$$ = new JSONFalse();
|
||||
>>
|
||||
token null <<
|
||||
$$ = new JSONNull();
|
||||
>>
|
||||
/"/ <<
|
||||
$mode(string);
|
||||
string_value = "";
|
||||
>>
|
||||
string: token string /"/ <<
|
||||
$$ = new JSONString(string_value);
|
||||
$mode(default);
|
||||
>>
|
||||
string: /\\\\"/ <<
|
||||
string_value ~= "\\"";
|
||||
>>
|
||||
string: /\\\\\\\\/ <<
|
||||
string_value ~= "\\\\";
|
||||
>>
|
||||
string: /\\\\\\// <<
|
||||
string_value ~= "/";
|
||||
>>
|
||||
string: /\\\\b/ <<
|
||||
string_value ~= "\\b";
|
||||
>>
|
||||
string: /\\\\f/ <<
|
||||
string_value ~= "\\f";
|
||||
>>
|
||||
string: /\\\\n/ <<
|
||||
string_value ~= "\\n";
|
||||
>>
|
||||
string: /\\\\r/ <<
|
||||
string_value ~= "\\r";
|
||||
>>
|
||||
string: /\\\\t/ <<
|
||||
string_value ~= "\\t";
|
||||
>>
|
||||
string: /\\\\u[0-9a-fA-F]{4}/ <<
|
||||
/* Not actually going to encode the code point for this example... */
|
||||
string_value ~= "{" ~ match[2..6] ~ "}";
|
||||
>>
|
||||
string: /[^\\\\]/ <<
|
||||
string_value ~= match;
|
||||
>>
|
||||
Start -> Value <<
|
||||
$$ = $1;
|
||||
>>
|
||||
Value -> string <<
|
||||
$$ = $1;
|
||||
>>
|
||||
Value -> number <<
|
||||
$$ = $1;
|
||||
>>
|
||||
Value -> Object <<
|
||||
$$ = $1;
|
||||
>>
|
||||
Value -> Array <<
|
||||
$$ = $1;
|
||||
>>
|
||||
Value -> true <<
|
||||
$$ = $1;
|
||||
>>
|
||||
Value -> false <<
|
||||
$$ = $1;
|
||||
>>
|
||||
Value -> null <<
|
||||
$$ = $1;
|
||||
>>
|
||||
Object -> lbrace rbrace <<
|
||||
$$ = new JSONObject();
|
||||
>>
|
||||
Object -> lbrace KeyValues rbrace <<
|
||||
$$ = new JSONObject($2);
|
||||
>>
|
||||
KeyValues -> KeyValue <<
|
||||
$$ = $1;
|
||||
>>
|
||||
KeyValues -> KeyValues comma KeyValue <<
|
||||
foreach (key, value; $3)
|
||||
{
|
||||
$1[key] = value;
|
||||
}
|
||||
$$ = $1;
|
||||
>>
|
||||
KeyValue -> string colon Value <<
|
||||
$$ =
|
||||
>>
|
||||
Array -> lbracket rbracket;
|
||||
Array -> lbracket Values rbracket;
|
||||
Values -> Value;
|
||||
Values -> Values comma Value;
|
||||
EOF
|
||||
build_parser
|
||||
compile("spec/test_parsing_json.d")
|
||||
end
|
||||
end
|
||||
|
14
spec/test_parsing_json.d
Normal file
14
spec/test_parsing_json.d
Normal file
@ -0,0 +1,14 @@
|
||||
import testparser;
|
||||
import std.stdio;
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
string input = ``;
|
||||
auto parser = new Testparser.Parser(input);
|
||||
assert(parser.parse() == true);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user