Skip to content

Commit

Permalink
JSON import: not quite done yet
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Nov 27, 2012
1 parent 960ca5a commit 75823c1
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
50 changes: 50 additions & 0 deletions shared/rascal/src/io/json/JSON.rsc
@@ -0,0 +1,50 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module \syntax::JSON

import IO;

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

start syntax Data = BasicType;

syntax BasicType
= Number
| String
| Boolean
| Array
| Object
| Null
;

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

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

syntax Boolean = "false" | "true" ;

syntax Array = "[" {BasicType ","}* "]";

syntax Object = "{" {KeyValue ","}* "}";
syntax KeyValue = BasicType ":" BasicType;

syntax Null = "null" ;

data JSO
= jsnumber(real n)
| jsstring(str s)
| jsboolean(bool b)
| jsarray(list[JSO] xs)
| jsobject(map[JSO,JSO] kvs)
| jsnull()
;

public void main(list[str] args)
{
loc src = |cwd:///|+args[0];
PT = parse(#BasicType,src);
println("Extraction completed.");
}
22 changes: 22 additions & 0 deletions shared/rascal/src/io/json/Main.rsc
@@ -0,0 +1,22 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::json::Main

import io::json::Syntax;
import io::json::Parse;
import IO;
import ParseTree;

public void main(list[str] args)
{
loc src = |cwd:///|+args[0];
println(io::json::Parse::str2jso(readFile(src)));
// PT = parse(#BasicType,src);
println("Parsing completed.");
}

public void go()
{
loc src = |project://slps/src/last1000.json|;
println(io::json::Parse::str2jso(readFile(src)));
println("Parsing completed.");
}
61 changes: 61 additions & 0 deletions shared/rascal/src/io/json/Parse.rsc
@@ -0,0 +1,61 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::json::Parse

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

data JSO
= jsnumber(real n)
| jsstring(str s)
| jsboolean(bool b)
| jsarray(list[JSO] xs)
| jsobject(map[JSO,JSO] kvs)
| jsnull()
;

JSO str2jso(str s)
{
PT = parse(#BasicType,trim(s));
return basic2jso(PT);
}

JSO basic2jso(BasicType t)
{
//if ((BasicType)`Number v` := PT)
//println("numberr!");
switch(t)
{
case (BasicType)`<Number v>`: return str2num("<v>");
case (BasicType)`<String v>`: return str2str("<v>");
case (BasicType)`<Boolean v>`: return str2bool("<v>");
case (BasicType)`<Array v>`: return arr2array(v);
case (BasicType)`<Object v>`: return obj2object(v);
case (BasicType)`<Null v>`: return jsnull();
default:
println("dunno");
}
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((Array)`[<{BasicType ","}* vs>]`) = [basic2jso(v) | BasicType v <- vs];
map[JSO,JSO] obj2object((Object)`{<{KeyValue ","}* kvs>}`) = (basic2jso(kv.key):basic2jso(kv.val) | KeyValue kv <- kvs);

//| jsarray(list[JSO] xs)
// | jsobject(map[JSO,JSO] kvs)
// | jsnull()

//
// public void main(list[str] args)
// {
// loc src = |cwd:///|+args[0];
// PT = parse(#BasicType,src);
// println("Extraction completed.");
// }
32 changes: 32 additions & 0 deletions shared/rascal/src/io/json/Syntax.rsc
@@ -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 Data = BasicType;

syntax BasicType
= Number
| String
| Boolean
| Array
| Object
| Null
;

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

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

syntax Boolean = "false" | "true" ;

syntax Array = "[" {BasicType ","}* "]";

syntax Object = "{" {KeyValue ","}* "}";
syntax KeyValue = BasicType key ":" BasicType val;

syntax Null = "null" ;

0 comments on commit 75823c1

Please sign in to comment.