From 09c4f8410eba31bafc567a7d4115d62681665938 Mon Sep 17 00:00:00 2001 From: "john.j.camilleri" Date: Tue, 20 Nov 2012 13:56:56 +0000 Subject: Syntax editor: update to use common js files --- src/www/syntax-editor/README.md | 3 +- src/www/syntax-editor/ast.js | 208 ++++++++++++++++++++ src/www/syntax-editor/editor.css | 76 ++++++++ src/www/syntax-editor/editor.html | 17 +- src/www/syntax-editor/editor.js | 297 ++++++++++++++++++++++++++++ src/www/syntax-editor/editor_menu.js | 173 +++++++++++++++++ src/www/syntax-editor/js/ast.js | 204 -------------------- src/www/syntax-editor/js/editor.js | 295 ---------------------------- src/www/syntax-editor/js/editor_menu.js | 173 ----------------- src/www/syntax-editor/js/pgf_online.js | 80 -------- src/www/syntax-editor/js/support.js | 329 -------------------------------- src/www/syntax-editor/ui/style.css | 76 -------- 12 files changed, 763 insertions(+), 1168 deletions(-) create mode 100644 src/www/syntax-editor/ast.js create mode 100644 src/www/syntax-editor/editor.css create mode 100644 src/www/syntax-editor/editor.js create mode 100644 src/www/syntax-editor/editor_menu.js delete mode 100644 src/www/syntax-editor/js/ast.js delete mode 100644 src/www/syntax-editor/js/editor.js delete mode 100644 src/www/syntax-editor/js/editor_menu.js delete mode 100644 src/www/syntax-editor/js/pgf_online.js delete mode 100644 src/www/syntax-editor/js/support.js delete mode 100644 src/www/syntax-editor/ui/style.css (limited to 'src') diff --git a/src/www/syntax-editor/README.md b/src/www/syntax-editor/README.md index 647792d48..dec633476 100644 --- a/src/www/syntax-editor/README.md +++ b/src/www/syntax-editor/README.md @@ -9,7 +9,7 @@ An improved version of the [old syntax editor][1]. ## Notes -Tested with latest Chrome and Firefox. +- Tested with latest Chrome and Firefox (only). ## TODO @@ -19,7 +19,6 @@ Tested with latest Chrome and Firefox. - UI issue with DisambPhrasebookEng - more prominence to Disamb-linearizations - ambiguity: (optionally) parse all the resulting linearizations/variants and point out those which are ambiguous -- random-generate a non-empty tree as a starting point - try to retain subtree when replacing node - add undo/redo (or back/forward) navigation - structure the set of fridge magnets some more. Even though they diff --git a/src/www/syntax-editor/ast.js b/src/www/syntax-editor/ast.js new file mode 100644 index 000000000..143b9836c --- /dev/null +++ b/src/www/syntax-editor/ast.js @@ -0,0 +1,208 @@ +/* --- ID for a node in a tree ---------------------------------------------- */ +function NodeID(x) { + this.id = new Array(); + this.id.push(0); + + // Initialize from input + if (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 + } + } + + // get id + this.get = function() { + return this.id; + } + + // Add child node to id + this.add = function(x) { + this.id.push(parseInt(x)); + return this.id; + } + + // compare with other id + this.equals = function(other) { + return JSON.stringify(this.id)==JSON.stringify(other.id); + } + +} + +/* --- Abstract Syntax Tree (with state)------------------------------------- */ + +function ASTNode(data) { + for(var d in data) this[d]=data[d]; + this.children = []; + for (c in data.children) { + this.children.push( new ASTNode(data.children[c]) ); + } + this.hasChildren = function(){ + return this.children.length > 0; + } + + // generic HOF for traversing tree + this.traverse = function(f) { + function visit(node) { + f(node); + for (i in node.children) { + visit(node.children[i]); + } + } + visit(this); + } + +} + +function AST(fun, cat) { + + // local helper function for building ASTNodes + newNode = function(fun, cat) { + return new ASTNode({ + "fun": fun, + "cat": cat, + "children": [] + }); + } + + this.root = newNode(fun, cat); + + this.current = new NodeID(); // current id in tree + + this.getFun = function() { + return this.find(this.current).fun; + } + this.setFun = function(f) { + this.find(this.current).fun = f; + } + this.getCat = function() { + return this.find(this.current).cat; + } + this.setCat = function(c) { + this.find(this.current).cat = c; + } + + // Add a single fun at current node + this.add = function(fun, cat) { + this._add(this.current, newNode(fun,cat)); + } + + // add node as child of id + this._add = function(id, node) { + var x = this.find(id); + x.children.push(node); + } + + // Set entire subtree at current node + this.setSubtree = function(node) { + this._setSubtree(this.current, node); + } + + // set tree at given id + this._setSubtree = function(id, subtree) { + var lid = id.get().slice(); // clone NodeID array + var node = this.root; + + if (lid.length==1) + // Insert at root + this.root = new ASTNode(subtree); + else { + lid.shift(); // throw away root + while (lid.length>1 && node.hasChildren()) { + node = node.children[lid.shift()]; + } + node.children[lid.shift()] = new ASTNode(subtree); + } + + } + + // id should be a list of child indices [0,1,0] + // or a string separated by commas "0,1,0" + this.find = function(id) { + var lid = undefined + switch (typeof id) { + case "number": lid = [id]; break; + case "string": lid = id.split(","); break; + case "object": lid = id.get().slice(); break; // clone NodeID array + } + var node = this.root; + if (lid[0] == 0) lid.shift(); + while (lid.length>0 && node.children.length>0) { + node = node.children[lid.shift()]; + } + if (lid.length>0) + return undefined; + return node; + } + + // Clear children of current node + this.removeChildren = function() { + this.find(this.current).children = []; + } + + // Move current ID to next hole + this.toNextHole = function() { + var id = new NodeID(this.current); + + // loop until we're at top + while (id.get().length > 0) { + var node = this.find(id); + + // first check children + for (i in node.children) { + var child = node.children[i]; + if (!child.fun) { + var newid = new NodeID(id); + newid.add(i); + this.current = newid; + return; + } + } + + // otherwise go up to parent + id.get().pop(); + } + } + + // Move current id to child number i + this.toChild = function(i) { + this.current.add(i); + } + + // generic HOF for traversing tree + // this.traverse = function(f) { + // this.root.traverse(f); + // } + this.traverse = function(f) { + function visit(id, node) { + f(node); + for (i in node.children) { + var newid = new NodeID(id); + newid.add(parseInt(i)); + visit(newid, node.children[i]); + } + } + visit(new NodeID(), this.root); + } + + // Return tree as string + this.toString = function() { + var s = ""; + function visit(node) { + s += node.fun ? node.fun : "?" ; + if (!node.hasChildren()) +// if (node.children.length == 0) + return; + for (i in node.children) { + s += " ("; + visit(node.children[i]); + s += ")"; + } + } + visit(this.root); + return s; + } + +} + diff --git a/src/www/syntax-editor/editor.css b/src/www/syntax-editor/editor.css new file mode 100644 index 000000000..8640a819f --- /dev/null +++ b/src/www/syntax-editor/editor.css @@ -0,0 +1,76 @@ +body { + background: #ccc url("http://cloud.grammaticalframework.org/minibar/brushed-metal.png"); +} + +.hidden +{ + display:none; +} + +select#to_menu +{ + height: 10em; + position: absolute; + min-width: 5em; + } + +#tree +{ + white-space:pre; + font-family: monospace; + background: rgba(238, 238, 238, 0.6); + padding:0.5em; + margin:0.5em 0; + } + +#tree .node +{ + margin: 0.4em 0 0.4em 1.5em; + } + +#tree .node a +{ + cursor: pointer; + } +#tree .node a:hover +{ + text-decoration: underline; + } +#tree .node a.current +{ + font-weight: bold; + } + +#linearisations +{ + /* background: rgba(170, 170, 170, 0.5); */ + padding:0.5em; + margin:0.5em 0; + } +#linearisations div +{ + padding:0.2em; + } +#linearisations .lang +{ + display: inline-block; + margin-right: 0.5em; + width: 3em; + font-weight: bold; + text-align: center; + } +#linearisations .lin +{ + } + +.refinement +{ + margin: 0 0.1em; + display: inline-block; + cursor: pointer; + border: 1px solid; + padding: 0.2em; + font: 0.9em sans-serif; + background: white; + } + diff --git a/src/www/syntax-editor/editor.html b/src/www/syntax-editor/editor.html index d641998ab..bb6b49fa8 100644 --- a/src/www/syntax-editor/editor.html +++ b/src/www/syntax-editor/editor.html @@ -5,7 +5,7 @@ Syntax Editor - +

Syntax Editor

@@ -17,16 +17,15 @@ John J. Camilleri, November 2012 - - - - - + + + + +