Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
960ca5a
commit 75823c1
Showing
4 changed files
with
165 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,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."); | ||
} |
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,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."); | ||
} |
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,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."); | ||
// } |
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 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" ; |