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
|
/* --- Some enhancements --------------------------------------------------- */
// http://www.xenoveritas.org/blog/xeno/the-correct-way-to-clone-javascript-arrays
// Array.prototype.clone = function(){
// return this.slice(0);
// }
/* --- Main Editor object --------------------------------------------------- */
function Editor(server,opts) {
var t = this;
/* --- Configuration ---------------------------------------------------- */
// default values for options:
this.options={
target: "editor"
}
// Apply supplied options
if(opts) for(var o in opts) this.options[o]=opts[o];
/* --- Creating UI components ------------------------------------------- */
var main = document.getElementById(this.options.target);
this.ui = {
menubar: div_class("menu"),
tree: div_id("tree"),
refinements: div_id("refinements"),
lin: div_id("linearisations")
};
with(this.ui) {
appendChildren(main, [menubar, tree, refinements, lin]);
}
/* --- Client state initialisation -------------------------------------- */
this.server = server;
this.ast = null;
this.grammar = null;
this.languages = [];
this.local = {}; // local settings which may override grammar
/* --- Main program, this gets things going ----------------------------- */
this.menu = new EditorMenu(this);
}
/* --- API for getting and setting state ------------------------------------ */
Editor.prototype.get_ast=function() {
return this.ast.toString();
}
Editor.prototype.get_startcat=function() {
return this.local.startcat || this.grammar.startcat;
}
/* --- These get called from EditorMenu, or some custom code */
Editor.prototype.change_grammar=function(grammar_info) {
with(this) {
grammar = grammar_info;
local.startcat = null;
start_fresh();
}
}
Editor.prototype.change_startcat=function(startcat) {
with(this) {
local.startcat = startcat;
start_fresh();
}
}
// Called after changing grammar or startcat
Editor.prototype.start_fresh=function () {
with(this) {
ast = new AST(null, get_startcat());
redraw_tree();
update_current_node();
get_refinements();
clear(ui.lin);
}
}
/* --- Functions for handling tree manipulation ----------------------------- */
Editor.prototype.get_refinements=function(cat) {
var t = this;
if (cat == undefined)
cat = t.ast.getCat();
var args = {
id: cat,
format: "json"
};
var cont = function(data){
clear(t.ui.refinements);
for (pi in data.producers) {
var opt = span_class("refinement", text(data.producers[pi]));
opt.onclick = bind(function(){ t.select_refinement(this.innerHTML) }, opt);
t.ui.refinements.appendChild(opt);
}
};
var err = function(data){
clear(t.ui.refinements);
alert("Error");
};
t.server.browse(args, cont, err);
}
Editor.prototype.select_refinement=function(fun) {
with (this) {
ui.refinements.innerHTML = "...";
ast.removeChildren();
ast.setFun(fun);
var args = {
id: fun,
format: "json"
};
var err = function(data){
alert("Error");
};
server.browse(args, bind(complete_refinement,this), err);
}
}
Editor.prototype.complete_refinement=function(data) {
if (!data) return;
with (this) {
// Parse out function arguments
var def = data.def;
def = def.substr(def.lastIndexOf(":")+1);
var fun_args = map(function(s){return s.trim()}, def.split("->"))
fun_args = fun_args.slice(0,-1);
if (fun_args.length > 0) {
// Add placeholders
for (ci in fun_args) {
ast.add(null, fun_args[ci]);
}
}
// Update ui
redraw_tree();
update_linearisation();
// Select next hole & get its refinements
ast.toNextHole();
update_current_node();
}
}
Editor.prototype.update_current_node=function(newID) {
with(this) {
if (newID)
ast.current = new NodeID(newID);
redraw_tree();
get_refinements();
}
}
Editor.prototype.redraw_tree=function() {
var t = this;
var elem = node; // function from support.js
function visit(container, id, node) {
var container2 = empty_class("div", "node");
var label =
((node.value.fun) ? node.value.fun : "?") + " : " +
((node.value.cat) ? node.value.cat : "?");
var current = id.equals(t.ast.current);
var element = elem("a", {class:(current?"current":"")}, [text(label)]);
element.onclick = function() {
t.update_current_node(id);
}
container2.appendChild( element );
for (i in node.children) {
var newid = new NodeID(id);
newid.add(parseInt(i));
visit(container2, newid, node.children[i]);
}
container.appendChild(container2);
}
with(this) {
clear(ui.tree);
visit(ui.tree, new NodeID(), ast.tree.root);
}
}
Editor.prototype.update_linearisation=function(){
function langpart(conc,abs) { // langpart("FoodsEng","Foods") == "Eng"
return hasPrefix(conc,abs) ? conc.substr(abs.length) : conc;
}
var t = this;
with (this) {
var args = {
tree: ast.toString()
};
server.linearize(args, function(data){
clear(t.ui.lin);
for (i in data) {
var lang = data[i].to;
var langname = langpart(lang, t.grammar.name);
if (t.languages.length < 1 || elem(lang, t.languages)) {
var div_lang = empty("div");
div_lang.appendChild(span_class("lang", text(langname)));
div_lang.appendChild(
span_class("lin", [text(data[i].text)])
);
t.ui.lin.appendChild(div_lang);
}
}
});
}
}
//
Editor.prototype.delete_refinement = function() {
var t = this;
t.ast.removeChildren();
t.ast.setFun(null);
t.redraw_tree();
// t.get_refinements();
}
// Generate random subtree from current node
Editor.prototype.generate_random = function() {
var t = this;
t.ui.refinements.innerHTML = "...";
t.ast.removeChildren();
var args = {
cat: t.ast.getCat(),
limit: 1
};
var cont = function(data){
// Build tree of just fun, then populate with cats
var tree = t.ast.parseTree(data[0].tree);
tree.traverse(function(node){
var info = t.lookup_fun(node.value.fun);
node.value.cat = info.cat;
});
t.ast.setSubtree(tree);
t.redraw_tree();
};
var err = function(data){
alert("Error");
};
server.get_random(args, cont, err);
}
// Look up information for a function, hopefully from cache
Editor.prototype.lookup_fun = function(fun) {
// TODO
return {
cat: null
}
}
|