Skip to content

Commit

Permalink
JSON access in Rascal (even better than in SLPS)
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Nov 30, 2012
1 parent d7262e7 commit 3430e55
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
11 changes: 11 additions & 0 deletions rascal/src/TwiSKO.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module TwiSKO
// TWItter to subatomic Scientific Knowledge Objects

import io::json::Parser;
import io::json::Access;

public void go()
{
list[JSO] tweets = gimmeArray(loc2jso(|home:///workspace/open/data/last1000.json|));
}
11 changes: 11 additions & 0 deletions rascal/src/io/json/ADT.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::json::ADT

data JSO
= jsnumber(real n)
| jsstring(str s)
| jsboolean(bool b)
| jsarray(list[JSO] xs)
| jsobject(map[JSO,JSO] kvs)
| jsnull()
;
25 changes: 25 additions & 0 deletions rascal/src/io/json/Access.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::json::Access

import io::json::ADT;
import util::Math;

public real gimmeReal(jsnumber(n)) = n;
public default real gimmeReal(JSO x) = 0.0;

public int gimmeInt(jsnumber(n)) = toInt(n);
public default int gimmeInt(JSO x) = 0;

public str gimmeString(jsstring(s)) = s;
public default str gimmeString(JSO x) = "";

public bool gimmeBool(jsboolean(b)) = b;
public default bool gimmeBool(JSO x) = false;

public list[JSO] gimmeArray(jsarray(xs)) = xs;
public default list[JSO] gimmeArray(JSO x) = [];

public map[JSO,JSO] gimmeMap(jsobject(kvs)) = kvs;
public default map[JSO,JSO] gimmeMap(JSO x) = ();

public JSO gimmeNull(JSO x) = jsnull(); // well, if this is what you are asking...
35 changes: 35 additions & 0 deletions rascal/src/io/json/Parser.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::json::Parser

import io::json::ADT;
import io::json::Syntax;
import IO;
import String;
import ParseTree;
import util::Math;

public JSO loc2jso(loc l) = str2jso(readFile(l));

JSO str2jso(str s) = basic2jso(parse(#JSONBasicType,trim(s)));

JSO basic2jso((JSONBasicType)`<JSONNumber v>`) = str2num("<v>");
JSO basic2jso((JSONBasicType)`<JSONString v>`) = str2str("<v>");
JSO basic2jso((JSONBasicType)`<JSONBoolean v>`) = str2bool("<v>");
JSO basic2jso((JSONBasicType)`<JSONArray v>`) = jsarray(arr2array(v));
JSO basic2jso((JSONBasicType)`<JSONObject v>`) = jsobject(obj2object(v));
JSO basic2jso((JSONBasicType)`<JSONNull v>`) = jsnull();
default JSO basic2jso(JSONBasicType t)
{
println("wtf?!");
return jsnull();
}

JSO str2num(str s) = jsnumber(toReal(s)); // TODO so that it recognizes ints? (not crucial)
JSO str2str(str s) = jsstring(s);

JSO str2bool("true") = jsboolean(true);
default JSO str2bool(str s) = jsboolean(false);

list[JSO] arr2array((JSONArray)`[<{JSONBasicType ","}* vs>]`) = [basic2jso(v) | JSONBasicType v <- vs];
map[JSO,JSO] obj2object((JSONObject)`{<{JSONKeyValue ","}* kvs>}`) = (basic2jso(kv.key):basic2jso(kv.val) | JSONKeyValue kv <- kvs);
32 changes: 32 additions & 0 deletions rascal/src/io/json/Syntax.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::json::Syntax

layout L = WS;
lexical WS = [\ \n\r\t]* !>> [\ \n\r\t];

start syntax JSONData = JSONBasicType;

syntax JSONBasicType
= JSONNumber
| JSONString
| JSONBoolean
| JSONArray
| JSONObject
| JSONNull
;

syntax JSONNumber = "-"? Digits ("." Digits)?;
lexical Digits = [0-9]+ !>> [0-9];

syntax JSONString = DoubleQuotedString;
lexical DoubleQuotedString = [\"] DQSElement* [\"]; //"
lexical DQSElement = ![\"] | [\\][\"] ; //"

syntax JSONBoolean = "false" | "true" ;

syntax JSONArray = "[" {JSONBasicType ","}* "]";

syntax JSONObject = "{" {JSONKeyValue ","}* "}";
syntax JSONKeyValue = JSONBasicType key ":" JSONBasicType val;

syntax JSONNull = "null" ;

0 comments on commit 3430e55

Please sign in to comment.