-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON access in Rascal (even better than in SLPS)
- Loading branch information
1 parent
d7262e7
commit 3430e55
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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|)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ; |