Permalink
Browse files

Changed to using a special "infix associative" box.

  • Loading branch information...
1 parent df24735 commit a8b0c15ceb49047f85de91dce9d3fa1016a5fa54 @luqui committed Jun 14, 2011
Showing with 49 additions and 28 deletions.
  1. +11 −26 demo/structural.html
  2. +38 −2 structural.js
View
@@ -12,32 +12,17 @@
<script>
$(function() {
-var Exp_eplus_box = Exp_box('eplus', regexp_tokenizer({
- '\\d+': function(m) {
- return Exp_eplus.make(Exp_eatom.make(m[0]), Exp_eplus_cont_box.make())
- }
-}));
-
-var Exp_eplus_cont_box = Exp_box('eplus', regexp_tokenizer({
- '': function(m) {
- return Exp_eplus_cont_box.make();
- },
- '\\+': function(m) {
- return Exp_eplus.make(Exp_plus_token.make(), Exp_eplus_box.make())
- }
-}));
-
-var Exp_E = new EClass({
- cls: 'E'
-});
-
-var Exp_eplus_single = new EClass({
- cls: 'eplus'
-});
-
-var Exp_eplus = new EClass({
- cls: 'eplus'
-});
+var Exp_eplus_box = Infix_assoc_box('eplus',
+ 'eatom', regexp_tokenizer({
+ '\\d+': function(m) {
+ return Exp_eatom.make(m[0]);
+ }
+ }),
+ 'plus_token', regexp_tokenizer({
+ '\\+': function(m) {
+ return Exp_plus_token.make()
+ }
+ }));
var Exp_plus_token = new EClass({
cls: 'plus_token',
View
@@ -195,8 +195,44 @@ var Exp_box = function(cls, tokenizer) {
});
};
-var Infix_assoc_box = function(cls, term_tokenizer, op_tokenizer) {
-
+var Infix_assoc_box = function(cls, term_cls, term_tokenizer, op_cls, op_tokenizer) {
+ return new EClass({
+ cls: cls,
+ parse: function(expr) {
+ var self = this;
+ return function(inp) {
+ // XXX __proto__ instead of prototype?
+ var tokresult = self.__proto__.parse.call(self, expr)(inp);
+ if (tokresult && tokresult[1] !== '') {
+ inp = tokresult[1];
+ var op_tokresult = op_tokenizer(inp);
+ if (op_tokresult) {
+ var term = Exp_box(term_cls, term_tokenizer).make();
+ var r = tokresult[0];
+ return [ new Expr(r.head, r.args.concat([ op_tokresult[0], term ])), op_tokresult[1] ];
+ }
+ else if (inp == '') {
+ return [ tokresult[0], inp ];
+ }
+ else {
+ return null;
+ }
+ }
+ else {
+ return tokresult;
+ }
+ }
+ },
+ make: function() {
+ if (arguments.length == 0) { // can't be empty
+ return this.make(Exp_box(term_cls, term_tokenizer).make());
+ }
+ else {
+ // XXX __proto__ instead of prototype?
+ return this.__proto__.make.apply(this, arguments);
+ }
+ }
+ });
};

0 comments on commit a8b0c15

Please sign in to comment.