1
0
forked from GitHub/gf-core

Syntax editor: improvements and fixes with grammar manager

This commit is contained in:
john.j.camilleri
2012-11-27 10:09:40 +00:00
parent 6542f6ffdd
commit 054df2451a
8 changed files with 65 additions and 80 deletions

View File

@@ -13,21 +13,21 @@ An improved version of the [old syntax editor][1].
## Available startup options
var editor_options = {
target: "editor",
initial: {
grammar: "http://localhost:41296/grammars/Foods.pgf",
startcat: "Kind",
languages: ["Eng","Swe","Mlt"],
abstr: "Pred (That Fish) Expensive"
},
show: {
grammar_menu: true,
startcat_menu: true,
to_menu: true,
random_button: true
}
}
|Options|Description|Default|
|:------|:----------|:------|
|target | |"editor"|
|initial.grammar|Initial grammar URL, e.g. `"http://localhost:41296/grammars/Foods.pgf"`|-|
|initial.startcat|Initial startcat|-|
|initial.languages|Initial linearisation languages, e.g. `["Eng","Swe","Mlt"]`|-|
|initial.abstr|Initial abstract tree (as string), e.g. `"Pred (That Fish) Expensive"`|-|
|show.grammar_menu|Show grammar menu?|true|
|show.startcat_menu|Show start category menu?|true|
|show.to_menu|Show languages menu?|true|
|show.random_button|Show random button?|true|
## Example usage
See `editor.html` and `editor_online.js`.
## TODO

View File

@@ -8,7 +8,7 @@ function NodeID(x) {
switch (typeof x) {
case "number": this.id = [x]; break;
case "string": this.id = map(function(s){return parseInt(s)}, x.split(",")); break;
case "object": this.id = x.get().slice(); break; // another NodeID
case "object": this.id = Array.clone(x.get()); break; // another NodeID
}
}
@@ -101,7 +101,7 @@ function AST(fun, cat) {
// set tree at given id
this._setSubtree = function(id, subtree) {
var lid = id.get().slice(); // clone NodeID array
var lid = Array.clone(id.get()); // clone NodeID array
var node = this.root;
if (lid.length==1)
@@ -124,7 +124,7 @@ function AST(fun, cat) {
switch (typeof id) {
case "number": lid = [id]; break;
case "string": lid = id.split(","); break;
case "object": lid = id.get().slice(); break; // clone NodeID array
case "object": lid = Array.clone(id.get()); break; // clone NodeID array
}
var node = this.root;
if (lid[0] == 0) lid.shift();

View File

@@ -11,7 +11,7 @@
<h2>Syntax Editor</h2>
<div id="minibar"></div>
<div id="editor"></div>
<noscript>This page doesn't works unless JavaScript is enabled.</noscript>
<noscript>This page doesn't work unless JavaScript is enabled.</noscript>
<hr />
<small class="modtime">

View File

@@ -1,20 +1,4 @@
/* --- Main Editor object --------------------------------------------------- */
/* When creating the object, stuff gets called in this order:
new EditorMenu
server.get_grammarlists
EditorMenu.show_grammarlist
EditorMenu.change_grammar
server.switch_to_other_grammar
server.get_grammar_info
EditorMenu.update_startcat_menu
EditorMenu.update_language_menu
Editor.change_grammar
Editor.get_grammar_constructors
Editor.start_fresh
Editor.update_current_node();
Editor.redraw_tree();
Editor.get_refinements();
*/
function Editor(gm,opts) {
var t = this;
/* --- Configuration ---------------------------------------------------- */
@@ -64,11 +48,6 @@ function Editor(gm,opts) {
/* --- Register Grammar Manager hooks ----------------------------------- */
this.gm.register_action("change_grammar",function(grammar){
debug("Editor: change grammar");
var startcat0 = t.options.initial.startcat;
if (elem(startcat0, grammar.categories))
t.startcat = startcat0;
else
t.startcat = null;
t.get_grammar_constructors(bind(t.start_fresh,t));
});
this.gm.register_action("change_startcat",function(startcat){
@@ -124,19 +103,19 @@ Editor.prototype.get_startcat=function() {
}
// TODO
Editor.prototype.initialize_from=function(opts) {
var t=this;
if (opts.startcat)
t.options.initial_startcat=opts.startcat;
t.change_grammar();
if (opts.abstr)
t.import_ast(opts.abstr);
}
// Editor.prototype.initialize_from=function(opts) {
// var t=this;
// if (opts.startcat)
// t.options.initial_startcat=opts.startcat;
// t.change_grammar();
// if (opts.abstr)
// t.import_ast(opts.abstr);
// }
// Called after changing grammar or startcat
Editor.prototype.start_fresh=function () {
var t = this;
t.ast = new AST(null, t.get_startcat());
t.ast = new AST(null, t.get_startcat());
if (t.options.initial.abstr) {
t.import_ast(t.options.initial.abstr);
}

View File

@@ -107,7 +107,7 @@ EditorMenu.prototype.hook_onload=function(dir,grammar_names,dir_count) {
appendChildren(t.ui.grammar_menu, map(opt, grammar_names));
function pick_first_grammar() {
if(t.timeout) clearTimeout(t.timeout),t.timeout=null;
var grammar0=t.options.initial.grammar;
var grammar0=t.gm.options.initial.grammar;
if(!grammar0) grammar0=t.grammars[0];
t.ui.grammar_menu.value=grammar0;
// t.change_grammar();
@@ -139,16 +139,11 @@ EditorMenu.prototype.update_startcat_menu=function(grammar) {
menu.innerHTML="";
var cats=grammar.categories;
for(var cat in cats) menu.appendChild(option(cats[cat],cats[cat]))
// var startcat=this.local.get("startcat") || grammar.startcat;
var startcat0 = t.options.initial.startcat;
var startcat0 = t.gm.options.initial.startcat;
if (elem(startcat0, cats))
menu.value = startcat0;
else
menu.value = grammar.startcat;
// else {
// insertFirst(menu,option("Default",""));
// menu.value="";
// }
}
/* --- Langugage (to) menu -------------------------------------------------- */
@@ -168,9 +163,8 @@ EditorMenu.prototype.update_language_menu=function(menu,grammar) {
if(!hasPrefix(ln,"Disamb")) {
var lp=langpart(ln,grammar.name);
var opt=option(lp,ln);
if (elem(lp, t.options.initial.languages)) {
if (elem(lp, t.gm.options.initial.languages)) {
opt.selected=true;
t.editor.languages.push(opt.value);
}
menu.appendChild(opt);
}

View File

@@ -4,12 +4,9 @@ var server_options = {
}
var editor_options = {
target: "editor",
// initial: {
// grammar: "http://localhost:41296/grammars/Phrasebook.pgf",
// startcat: "Proposition",
// languages: ["Eng","Swe","Ita"],
// abstr: "PropOpenDate (SuperlPlace TheMostExpensive School) Tomorrow"
// },
initial: {
// abstr: "PropOpenDate (SuperlPlace TheMostExpensive School) Tomorrow"
},
show: {
grammar_menu: true,
startcat_menu: true,