1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
function with_dir(cont) {
function have_dir(dir) {
var unique_id=local.get("unique_id")
if(!unique_id) {
unique_id=dir.substr(10) // skip "/tmp/gfse."
local.put("unique_id",unique_id)
}
cont(dir,unique_id)
}
var dir=local.get("dir","");
if(/^\/tmp\//.test(dir)) have_dir(dir);
else ajax_http_get("/new",
function(dir) {
local.put("dir",dir);
have_dir(dir);
});
}
function remove_cloud_grammar(g) {
var dir=local.get("dir")
if(dir && g.unique_name) {
var path=g.unique_name+".json"
gfcloud("rm",{file:path},debug);
}
}
// Upload the grammar to the server and check it for errors
function old_upload(g) {
function upload2(dir) {
var form=node("form",{method:"post",action:"/cloud"},
[hidden("dir",dir),hidden("command","make"),
hidden(g.basename+".gf",show_abstract(g))])
//var files = [g.basename+".gf"]
for(var i in g.concretes) {
var cname=g.basename+g.concretes[i].langcode+".gf";
//files.push(cname);
form.appendChild(hidden(cname,
show_concrete(g)(g.concretes[i])));
}
editor.appendChild(form);
form.submit();
form.parentNode.removeChild(form);
}
function upload3(message) { if(message) alert(message); }
with_dir(upload2)
}
// Upload the grammar to the server and check it for errors
function upload_grammars(gs,cont) {
function upload2(dir) {
var pre="dir="+encodeURIComponent(dir)
var form= {command:"make"}
for(var aix in gs) {
var g=gs[aix]
form[encodeURIComponent(g.basename+".gf")]=show_abstract(g)
var cnc=g.concretes
for(var i in cnc) {
var cname=g.basename+cnc[i].langcode+".gf";
form[encodeURIComponent(cname)]=show_concrete(g)(cnc[i]);
}
}
ajax_http_post("/cloud",pre+encodeArgs(form),upload3)
}
function upload3(json) {
var res=JSON.parse(json)
if(cont) cont(res)
else alert(res.errorcode+"\n"+res.command+"\n\n"+res.output);
}
if(navigator.onLine) with_dir(upload2)
else cont({errorcode:"Offline",command:"",output:""})
}
function assign_unique_name(g,unique_id) {
if(!g.unique_name) {
g.unique_name=unique_id+"-"+g.index;
save_grammar(g)
}
return g
}
// Upload all grammars to the cloud
function upload_json(cont) {
function upload2(dir,unique_id) {
function upload3(resptext,status) {
local.put("json_uploaded",Date.now());
//debug("Upload complete")
if(cont) cont();
else {
var sharing=element("sharing");
if(sharing) {
if(status==204) {
var a=empty("a");
a.href="share.html#"+dir.substr(5) // skip "/tmp/"
a.innerHTML=a.href;
sharing.innerHTML="";
sharing.appendChild(text("Use the following link for shared access to your grammars from multiple devices: "))
sharing.appendChild(a)
}
else
sharing.innerHTML=resptext;
}
}
}
//debug("New form data");
//var form=new FormData(); // !!! Doesn't work on Android 2.2!
var form={dir:dir};
//debug("Preparing form data");
for(var i=0;i<local.count;i++) {
var g=local.get(i,null);
if(g) {
g=assign_unique_name(g,unique_id)
//form.append(g.unique_name+".json",JSON.stringify(g));
form[encodeURIComponent(g.unique_name+".json")]=JSON.stringify(g)
}
}
ajax_http_post("/cloud","command=upload"+encodeArgs(form),upload3,cont)
}
with_dir(upload2);
}
function remove_public(name,cont,err) {
gfcloud_public_post("rm",{file:name},cont,err)
}
// Publish a single grammar
function publish_json(g,cont) {
function publish2(dir,unique_id) {
var oldname=g.publishedAs
function publish3(resptext,status) {
console.log("publish3")
if(oldname && oldname!=g.basename) {
console.log("old name="+oldname)
var name=oldname+"-"+g.unique_name+".json"
remove_public(name,cont,cont)
}
else cont()
}
g.publishedAs=g.basename;
save_grammar(g);
g=assign_unique_name(g,unique_id)
var name=g.basename+"-"+g.unique_name
var ix=g.index;
delete g.publishedAs
delete g.unique_name
delete g.index
var form={}
form[encodeURIComponent(name+".json")]=JSON.stringify(g)
g=reget_grammar(ix)
gfcloud_public_post("upload",form,publish3,cont)
}
with_dir(publish2);
}
function download_json() {
var dir=local.get("dir");
var downloading=0;
function get_list(ok,err) { gfcloud("ls",{},ok,err) }
function get_file(file,ok,err) {
downloading++;
gfcloud("download",{file:file},ok,err);
}
function file_failed(errormsg,status) {
debug(errormsg)
downloading--;
}
function file_downloaded(grammar) {
downloading--;
var newg=JSON.parse(grammar);
debug("Downloaded "+newg.unique_name)
var i=my_grammar(newg.unique_name);
if(i!=null) merge_grammar(i,newg)
else {
debug("New")
newg.index=null;
save_grammar(newg);
}
if(downloading==0) done()
}
function done() {
setTimeout(function(){location.href="."},2000);
}
function download_files(ls) {
local.put("current",0);
if(ls) {
//debug("Downloading "+ls);
var files=JSON.parse(ls);
cleanup_deleted(files);
for(var i in files) get_file(files[i],file_downloaded,file_failed);
}
else {
debug("No grammars in the cloud")
done()
}
}
get_list(download_files);
}
function link_directories(newdir,cont) {
gfcloud("link_directories",{newdir:newdir},cont)
}
/* -------------------------------------------------------------------------- */
var public_dir="/tmp/public"
// Request GF cloud service in the public directory (using GET)
function gfcloud_public_json(cmd,args,cont,err) {
var enc=encodeURIComponent;
var url="/cloud?dir="+public_dir+"&command="+enc(cmd)+encodeArgs(args)
http_get_json(url,cont,err)
}
// Request GF cloud service in the public directory (using POST)
function gfcloud_public_post(cmd,args,cont,err) {
var enc=encodeURIComponent;
var req="dir="+public_dir+"&command="+enc(cmd)+encodeArgs(args)
ajax_http_post("/cloud",req,cont,err)
}
// Request GF cloud service (using GET, for idempotent requests)
function gfcloud(cmd,args,cont,err) {
with_dir(function(dir) {
var enc=encodeURIComponent;
var url="/cloud?dir="+enc(dir)+"&command="+enc(cmd)+encodeArgs(args)
ajax_http_get(url,cont,err)
})
}
// Reqest GF cloud service (using POST, for state changing requests)
function gfcloud_post(cmd,args,cont,err) {
with_dir(function(dir) {
var enc=encodeURIComponent;
var req="dir="+enc(dir)+"&command="+enc(cmd)+encodeArgs(args)
ajax_http_post("/cloud",req,cont,err)
})
}
// Send a command to the GF shell
function gfshell(cmd,cont) {
with_dir(function(dir) {
var enc=encodeURIComponent;
ajax_http_get("/gfshell?dir="+enc(dir)+"&command="+enc(cmd),cont)
})
}
// Check the syntax of a source module
function check_module(path,source,cont) {
var enc=encodeURIComponent;
//http_get_json("/parse?"+enc(path)+"="+enc(source),cont)
ajax_http_post_json("/parse",enc(path)+"="+enc(source),cont)
}
// Check the syntax of an expression
function check_exp(s,cont) {
function check(gf_message) {
//debug("cc "+s+" = "+gf_message);
cont(/(parse|syntax) error/.test(gf_message) ? "syntax error" : null);
}
gfshell("cc "+s,check);
}
// Lexing/unlexing text
function lextext(txt,cont) { gfshell('ps -lextext "'+txt+'"',cont) }
function unlextext(txt,cont) { gfshell('ps -bind -unlextext "'+txt+'"',cont) }
|