Permalink
Browse files

Some more keyboard handling improvements and grammar combinators.

  • Loading branch information...
1 parent efaa704 commit 71d08d39758d6f2d4412ce5cf2442553e3f23dd1 @luqui committed Jun 19, 2011
Showing with 32 additions and 8 deletions.
  1. +1 −3 StructuralEditor.js
  2. +24 −3 StructuralGrammar.js
  3. +7 −2 demo/structural.html
View
@@ -69,9 +69,7 @@ event_node.keydown(function(e) {
});
event_node.keypress(function(e) {
- if (e.which == 8) { // backspace
- return false;
- }
+ if (!(32 <= e.which && e.which <= 127)) return; // non-printable
if (typeof(zipper.expr) !== 'string') {
var ch = String.fromCharCode(e.charCode);
View
@@ -180,6 +180,14 @@ $$.sym = function(name) {
return function(grammar) { return grammar(name) }
};
+$$.empty = function(grammar) {
+ var c = new SF.SynClass({
+ open: function() { return this.make([]) },
+ parse_prefix: function(str) { return [ c.make([]), str ] }
+ });
+ return c;
+};
+
$$.cls = function(clsname, syn) {
return function(grammar) {
return wrap_fields({
@@ -198,7 +206,19 @@ $$.literal = function(str) {
},
parse_prefix: string_tokenizer(str, function() { return c.make([str]) })
});
-
+ return c;
+ };
+};
+
+// "variable literal", a literal token that has multiple parsings
+$$.varlit = function(rx, tok) {
+ return function(grammar) {
+ var toks = {};
+ toks[rx.source] = function(m) { return $$.literal(tok)(grammar).open() };
+ var c = new SF.SynClass({
+ open: function() { return box_synclass(c).make([tok]) },
+ parse_prefix: regexp_tokenizer(toks)
+ });
return c;
};
};
@@ -218,12 +238,13 @@ $$.token = function(rx) {
$$.seq = function() {
var xs = arguments_to_array(arguments);
return function(grammar) {
- var subsyms = xs.map(function(x) { return x(grammar) });
+ var get_subsyms = function() { return xs.map(function(x) { return x(grammar) }) };
var c = new SF.SynClass({
open: function() {
- return this.make(subsyms.map(function(x) { return x.open() }));
+ return this.make(get_subsyms().map(function(x) { return x.open() }));
},
parse_prefix: function(str) {
+ var subsyms = get_subsyms();
var rs = subsyms.map(function(x) { return x.open() });
for (var i = 0; i < subsyms.length; i++) {
var tokresult = subsyms[i].parse_prefix(str);
View
@@ -56,11 +56,16 @@
var text_node = function(text) { return document.createTextNode(text) };
// End CodeCatalog Snippet
-var top_class = SG.grammar('stmt', {
+var space = SG.varlit(/\s*/, ' ');
+
+var top_class = SG.grammar('stmts', {
+ 'stmts': SG.choice(
+ SG.empty,
+ SG.seq(SG.sym('stmt'), SG.literal(';'), SG.sym('stmts'))),
'stmt': SG.choice(
SG.sym('var_decl'),
SG.sym('funcall')),
- 'var_decl': SG.seq(SG.literal('var'), SG.sym('identifier'), SG.literal(';')),
+ 'var_decl': SG.seq(SG.literal('var'), space, SG.sym('identifier')),
'funcall': SG.seq(SG.sym('identifier'), SG.literal('('), SG.sym('expr'), SG.literal(')')),
'identifier': SG.token(/[a-zA-Z_$][\w$]*/),
'expr': SG.sym('identifier')

0 comments on commit 71d08d3

Please sign in to comment.