Skip to content

Commit 55f8a11

Browse files
committed
json-to-js converter
1 parent c2b93a1 commit 55f8a11

28 files changed

Lines changed: 1282 additions & 67 deletions

‎js-syntax.k‎

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//JavaScript syntax module
2+
3+
require "json-to-js/js-on-common.k"
4+
25
module JS-SYNTAX
6+
imports JS-ON-COMMON
37

48
//Program - Ecma262-14---------------
59
syntax Program ::= SourceElements //using List causes the listing of .SourceElements
@@ -32,7 +36,7 @@ module JS-SYNTAX
3236

3337
//check if works!!
3438
syntax VarDecl ::= Id
35-
| Id "=" Expression //!!!!
39+
| Id "=" Expression [strict(2)] //!!!!
3640

3741
syntax VarDecls ::= VarDecl
3842
| VarDecls "," VarDecl //List can't be empty
@@ -106,11 +110,9 @@ module JS-SYNTAX
106110
//FUNCTIONAPPLICATION!
107111

108112
syntax Literal ::= "this"
109-
| "null"
110-
| "true"
111-
| "false"
112-
| Int //check
113-
| Float //--precision is off!
113+
| Null
114+
| Bool
115+
| JNumber
114116
| String //check
115117
// | RegExp //check
116118

@@ -123,44 +125,6 @@ module JS-SYNTAX
123125

124126
syntax Asgn ::= "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | ">>>=" | "&=" | "ˆ=" | "|="
125127

126-
127-
//ARRAYNOTATION---------------------
128-
//Array Initializer - Ecma262-11.1.4
129-
//--introduces amb with Exp,Exp --find a better way!
130-
syntax ArrayLiteral ::= "[" "]"
131-
| "[" Elision "]"
132-
| "[" ElementList "]"
133-
| "[" ElementList "," Elision "]"
134-
| "[" ElementList "," "]"
135-
136-
syntax ElementList ::= Elision Expression
137-
| Expression
138-
| ElementList "," Elision Expression
139-
| ElementList "," Expression
140-
141-
syntax Elision ::= ","
142-
| Elision ","
143-
//ARRAYNOTATION---------------------
144-
145-
//OBJECTNOTATION--------------------
146-
//Object Initializer - Ecma262-11.1.5
147-
//check if works!
148-
syntax ObjectLiteral ::= "{" "}"
149-
| "{" PropertyNameAndValueList "}"
150-
| "{" PropertyNameAndValueList "," "}"
151-
152-
syntax PropertyNameAndValueList ::= PropertyAssignment
153-
| PropertyNameAndValueList "," PropertyAssignment
154-
155-
syntax PropertyAssignment ::= PropertyName ":" Expression //Field
156-
| "get" PropertyName "(" ")" "{" FuncBody "}" //GetProp
157-
| "set" PropertyName "(" Id ")" "{" FuncBody "}" //SetProp
158-
159-
syntax PropertyName ::= Id
160-
| String
161-
| Int | Float
162-
//OBJECTNOTATION--------------------
163-
164128
//CASEBLOCK-------------------------
165129
//The switch Statement - Ecma262-12.11
166130
syntax CaseBlock ::= "{" "}"

‎json-to-js/Makefile‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
all:
2+
node parsejs.js ../programs/break.js > jsonprograms/break.json
3+
node parsejs.js ../programs/continue.js > jsonprograms/continue.json
4+
node parsejs.js ../programs/dowhile.js > jsonprograms/dowhile.json
5+
node parsejs.js ../programs/for.js > jsonprograms/for.json
6+
node parsejs.js ../programs/forin.js > jsonprograms/forin.json
7+
node parsejs.js ../programs/func2.js > jsonprograms/func2.json
8+
node parsejs.js ../programs/func3.js > jsonprograms/func3.json
9+
node parsejs.js ../programs/switch.js > jsonprograms/switch.json
10+
node parsejs.js ../programs/trycatch.js > jsonprograms/trycatch.json
11+
12+
run:
13+
krun jsonprograms/bool.json
14+
krun jsonprograms/multipvar.json
15+
krun jsonprograms/var.json
16+
krun jsonprograms/if.json
17+
krun jsonprograms/null.json
18+
krun jsonprograms/post.json
19+
krun jsonprograms/pre.json
20+
krun jsonprograms/while.json
21+
krun jsonprograms/asgn.json
22+
krun jsonprograms/arr.json
23+
krun jsonprograms/float.json
24+
krun jsonprograms/str.json
25+
# krun jsonprograms/break.json
26+
# krun jsonprograms/continue.json
27+
# krun jsonprograms/dowhile.json
28+
# krun jsonprograms/for.json
29+
# krun jsonprograms/forin.json
30+
# krun jsonprograms/func2.json
31+
# krun jsonprograms/func3.json
32+
# krun jsonprograms/switch.json
33+
# krun jsonprograms/trycatch.json

‎json-to-js/js-on-common.k‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//Common parts of JSON and JS syntaxes
2+
3+
module JS-ON-COMMON
4+
5+
syntax JValue ::= ArrayLiteral
6+
| ObjectLiteral
7+
| JNumber
8+
9+
syntax JNumber ::= Int | Float
10+
// JInt
11+
// | JFloat
12+
// | JZero
13+
14+
// syntax JInt ::= Token{"^[-+]?[1-9]+[0-9]*$"}
15+
// syntax JFloat ::= Token{"^[-+]?[1-9]+[0-9]*.[0-9]*$"}
16+
// syntax JZero ::= Token{"^[-+]?0(.0)?$"}
17+
18+
syntax #Null ::= "null"
19+
syntax Null ::= #Null
20+
21+
//ARRAYNOTATION---------------------
22+
//Array Initializer - Ecma262-11.1.4
23+
//--introduces amb with Exp,Exp --find a better way!
24+
syntax ArrayLiteral ::= "[" "]"
25+
| "[" Elision "]"
26+
| "[" ElementList "]"
27+
| "[" ElementList "," Elision "]"
28+
| "[" ElementList "," "]"
29+
30+
syntax ElementList ::= Elision Expression
31+
| Expression
32+
| ElementList "," Elision Expression
33+
| ElementList "," Expression
34+
35+
syntax Elision ::= ","
36+
| Elision ","
37+
//ARRAYNOTATION---------------------
38+
39+
//OBJECTNOTATION--------------------
40+
//Object Initializer - Ecma262-11.1.5
41+
//check if works!
42+
syntax ObjectLiteral ::= "{" "}"
43+
| "{" PropertyNameAndValueList "}"
44+
| "{" PropertyNameAndValueList "," "}"
45+
46+
syntax PropertyNameAndValueList ::= PropertyAssignment
47+
| PropertyNameAndValueList "," PropertyAssignment
48+
49+
syntax PropertyAssignment ::= PropertyName ":" Expression //Field
50+
| "get" PropertyName "(" ")" "{" FuncBody "}" //GetProp
51+
| "set" PropertyName "(" Id ")" "{" FuncBody "}" //SetProp
52+
53+
syntax PropertyName ::= Id
54+
| String
55+
| JNumber
56+
//OBJECTNOTATION--------------------
57+
58+
59+
endmodule

‎json-to-js/json-to-js.k‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//JSON-TO-JAVASCRIPT
2+
3+
require "json.k"
4+
require "../js-syntax.k"
5+
6+
module JSON-TO-JS
7+
imports JSON
8+
imports JS-SYNTAX
9+
10+
//HOW TO GET RID OF QUOTES IN STRINGS?
11+
12+
13+
rule { "type" : "Program" , "body" : ([Es]):ArrayLiteral } => Es [macro] //array notation?
14+
15+
//var / multipvar
16+
//HOW TO PARSE THE ARRAY ELEMENTS? --WORKS BUT AMBIGUITIES!
17+
rule { "type": "VariableDeclaration" , "declarations": [Es] , "kind": "var" } => var (Es)::VarDecls; [macro]
18+
rule { "type": "VariableDeclarator" , "id": O , "init": N:Null } => O [macro]
19+
rule { "type": "Identifier" , "name": V } => String2Id(V) [macro]
20+
21+
//bool
22+
rule { "type": "VariableDeclarator", "id": O1, "init": O2 } => (O1 = O2):VarDecl [macro]
23+
// rule { "type": "Literal", "value": B:Bool } => B [macro]
24+
25+
//null / asgn
26+
rule { "type": "ExpressionStatement", "expression": O } => (O;):Statement [macro]
27+
//HOW TO TYPE IT??
28+
rule { "type": "AssignmentExpression", "operator": V, "left": O1, "right": O2 } => O1 (V)::Asgn O2 [macro]
29+
// rule { "type": "Literal", "value": N:Null } => N [macro]
30+
31+
//if
32+
rule { "type": "IfStatement", "test": O1, "consequent": O2, "alternate": O3 } => if (O1) O2 else O3 [macro]
33+
rule { "type": "BlockStatement", "body": ([Es]):ArrayLiteral } => Es [macro]
34+
35+
//float
36+
rule { "type": "Literal", "value": V } => V [macro]
37+
38+
//post
39+
rule { "type": "UpdateExpression", "operator": V, "argument": O, "prefix": false } => O (V)::Unary [macro]
40+
41+
//pre
42+
rule { "type": "UpdateExpression", "operator": V, "argument": O, "prefix": true } => (V)::Prefix O [macro]
43+
44+
//while
45+
rule { "type": "WhileStatement", "test": O1, "body": O2 } => while (O1) O2 [macro]
46+
//!!!!!!!!!!!!! var type = (operator === '||' || operator === '&&') ? Syntax.LogicalExpression :
47+
// Syntax.BinaryExpression;
48+
rule { "type": "BinaryExpression", "operator": V, "left": O1, "right": O2 } => O1 (V)::Infix O2 [macro]
49+
50+
endmodule

‎json-to-js/json.k‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
//JSON syntax
2+
3+
require "js-on-common.k"
4+
25
module JSON
6+
imports JS-ON-COMMON
7+
/*
8+
//common parts
39
syntax OBJECT ::= "{" "}"
4-
| "{" MEMBERS "}"
10+
| "{" MEMBERS "}"
511
syntax MEMBERS ::= PAIR
6-
| PAIR "," MEMBERS
7-
syntax PAIR ::= String ":" VALUE
12+
| PAIR "," MEMBERS
813
syntax ARRAY ::= "[" "]"
9-
| "[" ELEMENTS "]"
14+
| "[" ELEMENTS "]"
1015
syntax ELEMENTS ::= VALUE
11-
| VALUE "," ELEMENTS
12-
syntax VALUE ::= String
13-
| NUMBER
14-
| OBJECT
15-
| ARRAY
16-
| "true" | "false"
17-
| "null"
18-
16+
| VALUE "," ELEMENTS
17+
*/
18+
19+
syntax PAIR ::= String ":" VALUE
20+
syntax VALUE ::= JValue
21+
| String
22+
| Bool
23+
| Null
24+
25+
/*
26+
//from the actual syntax: json.org
1927
syntax DIGIT ::= Token{"[0-9]"}
2028
syntax DIGIT19 ::= Token{"[1-9]"}
2129

@@ -25,11 +33,5 @@ module JSON
2533
syntax EXP ::= ESCI DIGITS
2634
syntax DIGITS ::= DIGIT | DIGIT DIGITS
2735
syntax ESCI ::= "e0" | "e+" | "e-" | "E0" | "E+" | "E-" // E/e conflicts with dist/include/io/uris.k --> E0/e0
28-
29-
// syntax NUMBER ::= JInt
30-
// | JFloat
31-
//
32-
// syntax JInt ::= Token{"-?[1-9][0-9]*"}
33-
// syntax JFloat ::= Token{"-?[1-9][0-9]*.[0-9]*"}
34-
36+
*/
3537
endmodule

‎json-to-js/jsonprograms/break.json‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "VariableDeclaration",
6+
"declarations": [
7+
{
8+
"type": "VariableDeclarator",
9+
"id": {
10+
"type": "Identifier",
11+
"name": "x"
12+
},
13+
"init": null
14+
},
15+
{
16+
"type": "VariableDeclarator",
17+
"id": {
18+
"type": "Identifier",
19+
"name": "i"
20+
},
21+
"init": {
22+
"type": "Literal",
23+
"value": 0
24+
}
25+
}
26+
],
27+
"kind": "var"
28+
},
29+
{
30+
"type": "ForStatement",
31+
"init": {
32+
"type": "AssignmentExpression",
33+
"operator": "=",
34+
"left": {
35+
"type": "Identifier",
36+
"name": "i"
37+
},
38+
"right": {
39+
"type": "Literal",
40+
"value": 0
41+
}
42+
},
43+
"test": {
44+
"type": "BinaryExpression",
45+
"operator": "<",
46+
"left": {
47+
"type": "Identifier",
48+
"name": "i"
49+
},
50+
"right": {
51+
"type": "Literal",
52+
"value": 10
53+
}
54+
},
55+
"update": {
56+
"type": "UpdateExpression",
57+
"operator": "++",
58+
"argument": {
59+
"type": "Identifier",
60+
"name": "i"
61+
},
62+
"prefix": false
63+
},
64+
"body": {
65+
"type": "BlockStatement",
66+
"body": [
67+
{
68+
"type": "IfStatement",
69+
"test": {
70+
"type": "BinaryExpression",
71+
"operator": "==",
72+
"left": {
73+
"type": "Identifier",
74+
"name": "i"
75+
},
76+
"right": {
77+
"type": "Literal",
78+
"value": 3
79+
}
80+
},
81+
"consequent": {
82+
"type": "BlockStatement",
83+
"body": [
84+
{
85+
"type": "BreakStatement",
86+
"label": null
87+
}
88+
]
89+
},
90+
"alternate": null
91+
},
92+
{
93+
"type": "ExpressionStatement",
94+
"expression": {
95+
"type": "AssignmentExpression",
96+
"operator": "*=",
97+
"left": {
98+
"type": "Identifier",
99+
"name": "x"
100+
},
101+
"right": {
102+
"type": "Identifier",
103+
"name": "i"
104+
}
105+
}
106+
}
107+
]
108+
}
109+
}
110+
]
111+
}

0 commit comments

Comments
 (0)