Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from wandernauta/master

Changes and fixes to clay-json
  • Loading branch information...
commit 4a76c92ba8dd7a7a60be2b0715fb1486f6fde301 2 parents d7438b6 + 657726b
@wtchappell authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+json
+testrunner
View
16 Makefile
@@ -0,0 +1,16 @@
+all: json
+
+json: json.clay
+ clay json.clay -o json
+
+testrunner: testrunner.clay
+ clay testrunner.clay -o testrunner
+
+tests: testrunner
+ find tests/ -name \*.json -print -exec ./testrunner {} \;
+
+clean:
+ rm -f json
+ rm -f testrunner
+
+.PHONY: clean all tests
View
16 json.clay
@@ -213,7 +213,7 @@ instance Exception = JSONParserException;
parse (input:Vector[Token]) {
- return parseObject(reverse(input));
+ return parseValue(reverse(input));
}
@@ -223,6 +223,10 @@ instance Exception = JSONParserException;
var left = pop(input);
while (true) {
var tag = pop(input);
+
+ if (variantIs?(tag, RiteCurl))
+ break;
+
if (not variantIs?(tag, String))
throw JSONParserException("JSON object tags must be strings.");
var mid = pop(input);
@@ -230,9 +234,12 @@ instance Exception = JSONParserException;
if (size(input) < 1)
throw JSONParserException("Missing right brace.");
var end = pop(input);
- if (not variantIs?(end, RiteCurl))
+
+ if ((not variantIs?(end, RiteCurl)) and (not variantIs?(end, Comma)))
throw JSONParserException("Missing right brace.");
+
put(map, variantAs(tag, String), val);
+
if (not variantIs?(end, Comma))
break;
}
@@ -269,6 +276,9 @@ instance Exception = JSONParserException;
var arr = Vector[JSONValue]();
var left = pop(input);
while (true) {
+ if (variantIs?(back(input), RiteSquare))
+ break;
+
push(arr, parseValue(input));
var end = pop(input);
if (not variantIs?(end, Comma))
@@ -312,7 +322,7 @@ dumpObject(x: JSONObject) {
for (k,v in items(x)) {
if (processed > 0) push(out, ", ");
printReprTo(out, k);
- push(out, ':');
+ push(out, ": ");
push(out, dump(v));
processed += 1;
}
View
20 testrunner.clay
@@ -0,0 +1,20 @@
+import json.*;
+import system.(commandLine);
+import io.files;
+
+main () {
+ print("\033[0m");
+
+ var f = File(commandLine()[1]);
+ var s = readAll(f);
+
+ var x = dump(parse(lex(s)));
+
+ if (x == s) {
+ println(" \033[0;32mOK!\033[0m");
+ } else {
+ println(" FAIL!");
+ println(" A: " + s);
+ println(" B: " + x);
+ }
+}
View
1  tests/001 Empty object.json
@@ -0,0 +1 @@
+{}
View
1  tests/002 Empty array.json
@@ -0,0 +1 @@
+[]
View
1  tests/003 Empty string.json
@@ -0,0 +1 @@
+[""]
View
1  tests/004 Basic object.json
@@ -0,0 +1 @@
+{"spam": "eggs", "foo": "bar"}
View
1  tests/005 Basic array.json
@@ -0,0 +1 @@
+{"Array": ["foo", "bar", 8, 9, 3]}
View
1  tests/006 Nested objects.json
@@ -0,0 +1 @@
+{"A": {"a": "b", "b": "c"}, "B": {"c": "d", "d": "e"}}
View
1  tests/007 Nested arrays.json
@@ -0,0 +1 @@
+[[true, null], [false, null]]
View
1  tests/008 Nested objects and arrays.json
@@ -0,0 +1 @@
+{"hello": ["foo", "bar"]}
View
1  tests/009 Nested arrays and objects.json
@@ -0,0 +1 @@
+{"Array": [{"foo": "bar"}]}
View
1  tests/010 Unicode object keys.json
@@ -0,0 +1 @@
+{"JSÔN": true}
View
1  tests/011 Object keys with quotes.json
@@ -0,0 +1 @@
+{"Fred \"Freddy\" Tungsten": 89}
View
1  tests/012 Object keys with backslashes.json
@@ -0,0 +1 @@
+{"Reverse solidus \\ backslash": 8}
View
1  tests/013 Deeply nested arrays.json
@@ -0,0 +1 @@
+[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["It doesn't get deeper than this"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
Please sign in to comment.
Something went wrong with that request. Please try again.