goodguide-gibbon 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57ae9392c5410133c5f4ada1b4246a722a072238
4
- data.tar.gz: 394c30d1756a87b8c9a8792374d0092502b03960
3
+ metadata.gz: 7bfe6e644dc8dbda3f8054e1c36b805956387529
4
+ data.tar.gz: 33de5acdaf0e4a3f489b8cdaab289ec96a63c1eb
5
5
  SHA512:
6
- metadata.gz: 6f565286601f57bd33b64b2078138b72839a97ca5071220c6143fb511fed5ce08ee72841543f34cf83a8c48a93d991c6b26aa77a7cc39a13125953fde3d5c01a
7
- data.tar.gz: 128ce3a70b585711f7d5cfd20f5ca2d4ce41f66d58035c80fedb821564b95ef1aae86e190fdb80ce46f959864c7f8d6d7af647a25fed521555ecddda225dfae4
6
+ metadata.gz: 267c82933e85a791ce21c1a940f68c6ec1983c5cb4144dae9cd523bc602794150f4e717e72e08ea7d20ab16e967a3b5fbbb27edb9dbef78d608a673bb78272bc
7
+ data.tar.gz: e1187c03ea3bbe127376c01fac81e0a9d45870947d7889d5659c93a518d175ba399f126b4a194b63f25097a325ad56ad4920b364b9e89ada50a96177877fc8d7
data/Gemfile CHANGED
@@ -8,7 +8,6 @@ gem 'wrong'
8
8
  gem 'pry'
9
9
 
10
10
  platforms :ruby do
11
- gem 'pry-debugger'
12
11
  gem 'therubyracer', '= 0.12.1'
13
12
  end
14
13
 
@@ -1,7 +1,7 @@
1
1
  module GoodGuide
2
2
  module Gibbon
3
3
  def self.version
4
- '0.10.1'
4
+ '0.10.2'
5
5
  end
6
6
  end
7
7
  end
@@ -116,46 +116,53 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
116
116
  }
117
117
  }
118
118
 
119
- function parseError(stream, result) {
120
- var expected = result.expected;
121
- var i = result.furthest;
119
+ function assertParser(p) {
120
+ if (!(p instanceof Parser)) throw new Error('not a parser: '+p);
121
+ }
122
+
123
+ var formatError = Parsimmon.formatError = function(stream, error) {
124
+ var expected = error.expected;
125
+ var i = error.index;
122
126
 
123
127
  if (i === stream.length) {
124
- var message = 'expected ' + expected + ', got the end of the string';
125
- }
126
- else {
127
- var prefix = (i > 0 ? "'..." : "'");
128
- var suffix = (stream.length - i > 12 ? "...'" : "'");
129
- var message = 'expected ' + expected + ' at character ' + i + ', got '
130
- + prefix + stream.slice(i, i+12) + suffix;
128
+ return 'expected ' + expected + ', got the end of the string';
131
129
  }
132
- throw 'Parse Error: ' + message + "\n parsing: '" + stream + "'";
133
- }
130
+
131
+ var prefix = (i > 0 ? "'..." : "'");
132
+ var suffix = (stream.length - i > 12 ? "...'" : "'");
133
+ return (
134
+ 'expected ' + expected + ' at character ' + i + ', got ' +
135
+ prefix + stream.slice(i, i+12) + suffix
136
+ );
137
+ };
134
138
 
135
139
  _.init = function(body) { this._ = body; };
136
140
 
137
141
  _.parse = function(stream) {
138
142
  var result = this.skip(eof)._(stream, 0);
139
143
 
140
- return result.status ? result.value : parseError(stream, result);
144
+ return result.status ? {
145
+ status: true,
146
+ value: result.value
147
+ } : {
148
+ status: false,
149
+ index: result.furthest,
150
+ expected: result.expected
151
+ };
141
152
  };
142
153
 
143
154
  // -*- primitive combinators -*- //
144
155
  _.or = function(alternative) {
145
- return alt([this, alternative]);
156
+ return alt(this, alternative);
146
157
  };
147
158
 
148
159
  _.then = function(next) {
149
- var self = this;
150
-
151
- return Parser(function(stream, i) {
152
- var result = self._(stream, i);
153
-
154
- if (!result.status) return result;
160
+ if (typeof next === 'function') {
161
+ throw new Error('chaining features of .then are no longer supported');
162
+ }
155
163
 
156
- var nextParser = (next instanceof Parser ? next : next(result.value));
157
- return furthestBacktrackFor(nextParser._(stream, result.index), result);
158
- });
164
+ assertParser(next);
165
+ return seq(this, next).map(function(results) { return results[1]; });
159
166
  };
160
167
 
161
168
  // -*- optimized iterative combinators -*- //
@@ -257,7 +264,7 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
257
264
  _.atMost = function(n) { return this.times(0, n); };
258
265
  _.atLeast = function(n) {
259
266
  var self = this;
260
- return seq([this.times(n), this.many()]).map(function(results) {
267
+ return seq(this.times(n), this.many()).map(function(results) {
261
268
  return results[0].concat(results[1]);
262
269
  });
263
270
  };
@@ -272,11 +279,11 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
272
279
  };
273
280
 
274
281
  _.skip = function(next) {
275
- return seq([this, next]).map(function(results) { return results[0]; });
282
+ return seq(this, next).map(function(results) { return results[0]; });
276
283
  };
277
284
 
278
285
  _.mark = function() {
279
- return seq([index, this, index]).map(function(results) {
286
+ return seq(index, this, index).map(function(results) {
280
287
  return { start: results[0], value: results[1], end: results[2] };
281
288
  });
282
289
  };
@@ -357,12 +364,15 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
357
364
  };
358
365
 
359
366
  // [Parser a] -> Parser [a]
360
- var seq = Parsimmon.seq = function(parsers) {
367
+ var seq = Parsimmon.seq = function() {
368
+ var parsers = [].slice.call(arguments);
369
+ var numParsers = parsers.length;
370
+
361
371
  return Parser(function(stream, i) {
362
372
  var result;
363
- var accum = new Array(parsers.length);
373
+ var accum = new Array(numParsers);
364
374
 
365
- for (var j = 0; j < parsers.length; j += 1) {
375
+ for (var j = 0; j < numParsers; j += 1) {
366
376
  result = furthestBacktrackFor(parsers[j]._(stream, i), result);
367
377
  if (!result.status) return result;
368
378
  accum[j] = result.value
@@ -373,7 +383,11 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
373
383
  });
374
384
  };
375
385
 
376
- var alt = Parsimmon.alt = function(parsers) {
386
+ var alt = Parsimmon.alt = function() {
387
+ var parsers = [].slice.call(arguments);
388
+ var numParsers = parsers.length;
389
+ if (numParsers === 0) return fail('zero alternates')
390
+
377
391
  return Parser(function(stream, i) {
378
392
  var result;
379
393
  for (var j = 0; j < parsers.length; j += 1) {
@@ -398,13 +412,21 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
398
412
  _.of = Parser.of = Parsimmon.of = succeed
399
413
 
400
414
  _.ap = function(other) {
401
- return seq([this, other]).map(function(results) {
415
+ return seq(this, other).map(function(results) {
402
416
  return results[0](results[1]);
403
417
  });
404
418
  };
405
419
 
406
420
  //- Monad
407
- _.chain = _.then;
421
+ _.chain = function(f) {
422
+ var self = this;
423
+ return Parser(function(stream, i) {
424
+ var result = self._(stream, i);
425
+ if (!result.status) return result;
426
+ var nextParser = f(result.value);
427
+ return furthestBacktrackFor(nextParser._(stream, result.index), result);
428
+ });
429
+ };
408
430
  });
409
431
  return Parsimmon;
410
432
  })()
@@ -1145,7 +1167,7 @@ Gibbon.TypeAST = TypeAST = (function(_super) {
1145
1167
  })(Variant);
1146
1168
 
1147
1169
  parse = Gibbon.parse = (function() {
1148
- var accessor, accessorExpr, arrow, arrowType, assertString, blankLines, blockExpr, blockType, comma, commaSepFlows, comment, component, concrete, decimal, decimalExpr, defaulted, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, identifier, innerFrame, integer, integerExpr, isString, label, labelVal, lazy, lbrace, lbrack, lexeme, lexical, lexicalExpr, lines, listExpr, listType, lparen, lsplat, metadata, multiline, name, nonDefaultedFlow, nonPairedFlow, numericExpr, opt, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, query, queryArg, queryExpr, rbrace, rbrack, regex, rparen, rsplat, seq, signature, simpleType, singletonFlow, spanLoc, squishListExpr, str, string, stringExpr, substExpr, succeed, tag, tassign, type, typeVar, variable, whitespace, wildType, wildcard, withLoc,
1170
+ var accessor, accessorExpr, arrow, arrowType, assertString, blankLines, blockExpr, blockType, comma, commaSepFlows, comment, component, concrete, decimal, decimalExpr, defaulted, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, handleResult, identifier, innerFrame, integer, integerExpr, isString, label, labelVal, lazy, lbrace, lbrack, lexeme, lexical, lexicalExpr, lines, listExpr, listType, lparen, lsplat, metadata, multiline, name, nonDefaultedFlow, nonPairedFlow, numericExpr, opt, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, query, queryArg, queryExpr, rbrace, rbrack, regex, rparen, rsplat, seq, signature, simpleType, singletonFlow, spanLoc, squishListExpr, str, string, stringExpr, substExpr, succeed, tag, tassign, type, typeVar, variable, whitespace, wildType, wildcard, withLoc,
1149
1171
  _this = this;
1150
1172
  tag = function(name, parser) {
1151
1173
  return Parsimmon.Parser(function(stream, i) {
@@ -1245,9 +1267,9 @@ parse = Gibbon.parse = (function() {
1245
1267
  lexicalExpr = withLoc(lexical, function(loc, name) {
1246
1268
  return AST.lexical(loc, name, []);
1247
1269
  });
1248
- queryExpr = query.then(function(q) {
1270
+ queryExpr = query.chain(function(q) {
1249
1271
  var args;
1250
- args = seq([lbrack, queryArg.many(), rbrack]).map(function(_arg) {
1272
+ args = seq(lbrack, queryArg.many(), rbrack).map(function(_arg) {
1251
1273
  var a, args, loc, r, _;
1252
1274
  _ = _arg[0], args = _arg[1], r = _arg[2];
1253
1275
  loc = spanLoc(q, r);
@@ -1268,28 +1290,28 @@ parse = Gibbon.parse = (function() {
1268
1290
  return lparen.then(flow).skip(rparen);
1269
1291
  });
1270
1292
  substExpr = lazy(function() {
1271
- return seq([lparen, flow, rparen]).map(function(_arg) {
1293
+ return seq(lparen, flow, rparen).map(function(_arg) {
1272
1294
  var fl, l, r;
1273
1295
  l = _arg[0], fl = _arg[1], r = _arg[2];
1274
1296
  return AST.subst(spanLoc(l, r), fl);
1275
1297
  });
1276
1298
  });
1277
1299
  listExpr = lazy(function() {
1278
- return seq([lbrack, commaSepFlows, rbrack]).map(function(_arg) {
1300
+ return seq(lbrack, commaSepFlows, rbrack).map(function(_arg) {
1279
1301
  var els, l, r;
1280
1302
  l = _arg[0], els = _arg[1], r = _arg[2];
1281
1303
  return AST.list(spanLoc(l, r), els, false);
1282
1304
  });
1283
1305
  });
1284
1306
  squishListExpr = lazy(function() {
1285
- return seq([lsplat, commaSepFlows, rsplat]).map(function(_arg) {
1307
+ return seq(lsplat, commaSepFlows, rsplat).map(function(_arg) {
1286
1308
  var els, l, r;
1287
1309
  l = _arg[0], els = _arg[1], r = _arg[2];
1288
1310
  return AST.list(spanLoc(l, r), els, true);
1289
1311
  });
1290
1312
  });
1291
1313
  blockExpr = lazy(function() {
1292
- return seq([lbrace, flow, rbrace]).map(function(_arg) {
1314
+ return seq(lbrace, flow, rbrace).map(function(_arg) {
1293
1315
  var flow, l, r;
1294
1316
  l = _arg[0], flow = _arg[1], r = _arg[2];
1295
1317
  return AST.block(spanLoc(l, r), flow);
@@ -1299,7 +1321,7 @@ parse = Gibbon.parse = (function() {
1299
1321
  singletonFlow = expr.map(function(e) {
1300
1322
  return AST.flow(e.loc, e, null);
1301
1323
  });
1302
- func = seq([name, parenFlow.or(singletonFlow).many()]).map(function(_arg) {
1324
+ func = seq(name, parenFlow.or(singletonFlow).many()).map(function(_arg) {
1303
1325
  var args, loc, name;
1304
1326
  name = _arg[0], args = _arg[1];
1305
1327
  loc = {
@@ -1312,7 +1334,7 @@ parse = Gibbon.parse = (function() {
1312
1334
  return AST.func(loc, name.value, args);
1313
1335
  });
1314
1336
  component = expr.or(func).skip(lines);
1315
- nonPairedFlow = seq([component, arrow.then(component).many()]).map(function(_arg) {
1337
+ nonPairedFlow = seq(component, arrow.then(component).many()).map(function(_arg) {
1316
1338
  var comp, cursor, first, rest, _i, _len;
1317
1339
  first = _arg[0], rest = _arg[1];
1318
1340
  cursor = AST.flow(first.loc, first, null);
@@ -1336,7 +1358,7 @@ parse = Gibbon.parse = (function() {
1336
1358
  return AST.flow(loc, AST.defaulted(loc, body, alternative), null);
1337
1359
  });
1338
1360
  }));
1339
- commaSepFlows = seq([flow.skip(seq([opt(comma, lines)])).many(), opt(flow)]).map(function(_arg) {
1361
+ commaSepFlows = seq(flow.skip(seq(opt(comma, lines))).many(), opt(flow)).map(function(_arg) {
1340
1362
  var els, final;
1341
1363
  els = _arg[0], final = _arg[1];
1342
1364
  if (final) {
@@ -1344,24 +1366,24 @@ parse = Gibbon.parse = (function() {
1344
1366
  }
1345
1367
  return els;
1346
1368
  });
1347
- metadata = seq([label, labelVal]).map(function(key, text) {
1369
+ metadata = seq(label, labelVal).map(function(key, text) {
1348
1370
  return AST.metadata(spanLoc(key, text), key.value, text.value);
1349
1371
  });
1350
1372
  definition = lazy(function() {
1351
- return seq([metadata.many(), name, define.then(innerFrame)]).map(function(_arg) {
1373
+ return seq(metadata.many(), name, define.then(innerFrame)).map(function(_arg) {
1352
1374
  var fl, loc, md, n, _ref5;
1353
1375
  md = _arg[0], n = _arg[1], fl = _arg[2];
1354
1376
  loc = spanLoc(((_ref5 = md[0]) != null ? _ref5.loc : void 0) || n, fl);
1355
1377
  return AST.definition(loc, md, n.value, fl);
1356
1378
  });
1357
1379
  });
1358
- frame = seq([definition.many(), flow]).map(function(_arg) {
1380
+ frame = seq(definition.many(), flow).map(function(_arg) {
1359
1381
  var defs, flow, loc;
1360
1382
  defs = _arg[0], flow = _arg[1];
1361
1383
  loc = spanLoc((defs[0] || flow).loc, flow.loc);
1362
1384
  return AST.frame(loc, defs, flow);
1363
1385
  });
1364
- parenFrame = seq([lparen, frame, rparen, lines]).map(function(_arg) {
1386
+ parenFrame = seq(lparen, frame, rparen, lines).map(function(_arg) {
1365
1387
  var fr, l, r, _;
1366
1388
  l = _arg[0], fr = _arg[1], r = _arg[2], _ = _arg[3];
1367
1389
  fr.loc = spanLoc(l, r);
@@ -1381,7 +1403,7 @@ parse = Gibbon.parse = (function() {
1381
1403
  });
1382
1404
  wildType = wildcard.result(TypeAST.wildcard());
1383
1405
  listType = lazy(function() {
1384
- return seq([lbrack, type, rbrack]).map(function(_arg) {
1406
+ return seq(lbrack, type, rbrack).map(function(_arg) {
1385
1407
  var t, _, __;
1386
1408
  _ = _arg[0], t = _arg[1], __ = _arg[2];
1387
1409
  return TypeAST.list(t);
@@ -1401,12 +1423,12 @@ parse = Gibbon.parse = (function() {
1401
1423
  return TypeAST.pair(first, second);
1402
1424
  });
1403
1425
  });
1404
- arrowType = seq([type, arrow, type]).map(function(_arg) {
1426
+ arrowType = seq(type, arrow, type).map(function(_arg) {
1405
1427
  var first, second, _;
1406
1428
  first = _arg[0], _ = _arg[1], second = _arg[2];
1407
1429
  return TypeAST.arrow(first, second);
1408
1430
  });
1409
- signature = seq([name, type.many(), tassign.then(arrowType)]).map(function(_arg) {
1431
+ signature = seq(name, type.many(), tassign.then(arrowType)).map(function(_arg) {
1410
1432
  var argTypes, ftype, name;
1411
1433
  name = _arg[0], argTypes = _arg[1], ftype = _arg[2];
1412
1434
  return TypeAST.func(ftype.from, argTypes, ftype.to);
@@ -1420,13 +1442,20 @@ parse = Gibbon.parse = (function() {
1420
1442
  throw 'can only parse strings';
1421
1443
  }
1422
1444
  };
1445
+ handleResult = function(result) {
1446
+ if (result.status) {
1447
+ return result.value;
1448
+ } else {
1449
+ throw new Error(Parsimmon.formatError(result));
1450
+ }
1451
+ };
1423
1452
  parse = function(str) {
1424
1453
  assertString(str);
1425
- return program.parse(str);
1454
+ return handleResult(program.parse(str));
1426
1455
  };
1427
1456
  parse.type = function(str) {
1428
1457
  assertString(str);
1429
- return fullSignature.parse(str);
1458
+ return handleResult(fullSignature.parse(str));
1430
1459
  };
1431
1460
  return parse;
1432
1461
  })();
@@ -3522,36 +3551,157 @@ Gibbon.optimize = (function() {
3522
3551
  });
3523
3552
  };
3524
3553
  insertBindings = (function() {
3525
- var findLastCommon, genSubstitutions, makeCrumbs, simplify;
3554
+ var SubstTree, findLastCommon, genSubstitutions, insertSubstitutions, makeCrumbs, _ref10;
3555
+ SubstTree = (function(_super) {
3556
+ __extends(SubstTree, _super);
3557
+
3558
+ function SubstTree() {
3559
+ _ref10 = SubstTree.__super__.constructor.apply(this, arguments);
3560
+ return _ref10;
3561
+ }
3562
+
3563
+ SubstTree.variants({
3564
+ subst: ['bindings', 'expr']
3565
+ });
3566
+
3567
+ SubstTree.prototype.toCore = function() {
3568
+ return this.__toCore || (this.__toCore = this.expr.map(function(x) {
3569
+ return x.toCore();
3570
+ }));
3571
+ };
3572
+
3573
+ SubstTree.prototype.inspect = function() {
3574
+ var e;
3575
+ if (!this.bindings.length) {
3576
+ return this.expr.inspect();
3577
+ }
3578
+ return "(subst [" + (((function() {
3579
+ var _i, _len, _ref11, _results;
3580
+ _ref11 = this.bindings;
3581
+ _results = [];
3582
+ for (_i = 0, _len = _ref11.length; _i < _len; _i++) {
3583
+ e = _ref11[_i];
3584
+ _results.push(e.inspect());
3585
+ }
3586
+ return _results;
3587
+ }).call(this)).join(' ')) + "] " + (this.expr.inspect()) + ")";
3588
+ };
3589
+
3590
+ SubstTree.prototype.map = function(f) {
3591
+ var e;
3592
+ return SubstTree.subst((function() {
3593
+ var _i, _len, _ref11, _results;
3594
+ _ref11 = this.bindings;
3595
+ _results = [];
3596
+ for (_i = 0, _len = _ref11.length; _i < _len; _i++) {
3597
+ e = _ref11[_i];
3598
+ _results.push(e.map(f));
3599
+ }
3600
+ return _results;
3601
+ }).call(this), this.expr.map(f));
3602
+ };
3603
+
3604
+ SubstTree.prototype.substituteWith = function(names, bindableExprs) {
3605
+ var bindable, core, hash, i, _i, _len;
3606
+ core = this.toCore();
3607
+ hash = core.hash();
3608
+ for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3609
+ bindable = bindableExprs[i];
3610
+ if (hash === bindable.toCore().hash()) {
3611
+ return SubstTree.unit(Core.variable(names[i]));
3612
+ }
3613
+ }
3614
+ return this.map(function(x) {
3615
+ return x.substituteWith(names, bindableExprs);
3616
+ });
3617
+ };
3618
+
3619
+ SubstTree.prototype.simplify = function() {
3620
+ var bindableExprs, expr;
3621
+ bindableExprs = this.bindings;
3622
+ expr = this.expr;
3623
+ return expr.cases({
3624
+ rescue: function() {
3625
+ return SubstTree.unit(this.map(function(x) {
3626
+ return insertSubstitutions(x.toCore());
3627
+ }));
3628
+ },
3629
+ squishList: function(list) {
3630
+ list = list.toCore();
3631
+ if (list._tag !== 'list') {
3632
+ throw 'invalid squish';
3633
+ }
3634
+ return SubstTree.unit(Core.squishList(SubstTree.unit(list.map(insertSubstitutions))));
3635
+ },
3636
+ other: function() {
3637
+ var bindable, i, names, out, substituted, _, _i, _len;
3638
+ substituted = expr.map(function(t) {
3639
+ return t.simplify();
3640
+ });
3641
+ names = (function() {
3642
+ var _i, _len, _results;
3643
+ _results = [];
3644
+ for (_i = 0, _len = bindableExprs.length; _i < _len; _i++) {
3645
+ _ = bindableExprs[_i];
3646
+ _results.push(nameGen('b'));
3647
+ }
3648
+ return _results;
3649
+ })();
3650
+ if (!bindableExprs.length) {
3651
+ return SubstTree.unit(substituted);
3652
+ }
3653
+ out = SubstTree.unit(substituted).substituteWith(names, bindableExprs);
3654
+ for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3655
+ bindable = bindableExprs[i];
3656
+ out = SubstTree.unit(Core.bind(names[i], bindable, out));
3657
+ }
3658
+ DEBUG.log("=> " + (out.inspect()));
3659
+ return out;
3660
+ }
3661
+ });
3662
+ };
3663
+
3664
+ SubstTree.unit = function(expr) {
3665
+ return SubstTree.subst([], expr);
3666
+ };
3667
+
3668
+ return SubstTree;
3669
+
3670
+ })(Variant);
3526
3671
  makeCrumbs = function(trace) {
3527
3672
  return trace.mapArray(function(e) {
3528
3673
  return e.hash();
3529
3674
  }).reverse();
3530
3675
  };
3531
3676
  genSubstitutions = function(expr) {
3532
- var newTrace, occurrences, queue, sub, substitutions, trace, _i, _len, _ref10, _ref11, _ref12;
3677
+ var occurrences, queue, recurse, substitutions;
3533
3678
  occurrences = new ObjHash;
3534
3679
  substitutions = new Hash;
3535
3680
  queue = [[expr, List.empty()]];
3536
3681
  while (queue.length) {
3537
- _ref10 = queue.shift(), expr = _ref10[0], trace = _ref10[1];
3538
- if (expr.isSimple() || expr.alwaysFails()) {
3539
- continue;
3540
- }
3541
- if (occurrences.has(expr)) {
3542
- occurrences.get(expr).push(makeCrumbs(trace));
3543
- } else {
3544
- occurrences.set(expr, [makeCrumbs(trace)]);
3545
- newTrace = trace.cons(expr);
3546
- if ((_ref11 = expr._tag) === 'rescue' || _ref11 === 'squishList') {
3547
- continue;
3682
+ (function(_arg) {
3683
+ var expr, newTrace, sub, trace, _i, _len, _ref11, _ref12, _results;
3684
+ expr = _arg[0], trace = _arg[1];
3685
+ if (expr.isSimple() || expr.alwaysFails()) {
3686
+ return;
3548
3687
  }
3549
- _ref12 = expr.subtrees();
3550
- for (_i = 0, _len = _ref12.length; _i < _len; _i++) {
3551
- sub = _ref12[_i];
3552
- queue.push([sub, newTrace]);
3688
+ if (occurrences.has(expr)) {
3689
+ return occurrences.get(expr).push(makeCrumbs(trace));
3690
+ } else {
3691
+ occurrences.set(expr, [makeCrumbs(trace)]);
3692
+ newTrace = trace.cons(expr);
3693
+ if ((_ref11 = expr._tag) === 'rescue' || _ref11 === 'squishList') {
3694
+ return;
3695
+ }
3696
+ _ref12 = expr.subtrees();
3697
+ _results = [];
3698
+ for (_i = 0, _len = _ref12.length; _i < _len; _i++) {
3699
+ sub = _ref12[_i];
3700
+ _results.push(queue.push([sub, newTrace]));
3701
+ }
3702
+ return _results;
3553
3703
  }
3554
- }
3704
+ })(queue.shift());
3555
3705
  }
3556
3706
  occurrences.each(function(expr, crumbs) {
3557
3707
  var insertionPoint;
@@ -3564,7 +3714,22 @@ Gibbon.optimize = (function() {
3564
3714
  });
3565
3715
  return substitutions.get(insertionPoint).push(expr);
3566
3716
  });
3567
- return substitutions;
3717
+ return (recurse = function(expr) {
3718
+ var bindableExprs, e;
3719
+ bindableExprs = substitutions.get(expr.hash());
3720
+ if (!bindableExprs) {
3721
+ return SubstTree.unit(expr.map(recurse));
3722
+ }
3723
+ return SubstTree.subst((function() {
3724
+ var _i, _len, _results;
3725
+ _results = [];
3726
+ for (_i = 0, _len = bindableExprs.length; _i < _len; _i++) {
3727
+ e = bindableExprs[_i];
3728
+ _results.push(recurse(e));
3729
+ }
3730
+ return _results;
3731
+ })(), expr.map(recurse));
3732
+ })(expr);
3568
3733
  };
3569
3734
  findLastCommon = function(_arg) {
3570
3735
  var i, last, refCrumb, reference, rest, testCrumbs, _i, _j, _len, _len1;
@@ -3582,66 +3747,11 @@ Gibbon.optimize = (function() {
3582
3747
  }
3583
3748
  return refCrumb;
3584
3749
  };
3585
- simplify = function(expr, substitutions) {
3586
- var recurse;
3587
- return (recurse = function(expr) {
3588
- var bindable, bindableExprs, i, names, out, recurseInner, _, _i, _len;
3589
- bindableExprs = substitutions.get(expr.hash());
3590
- if (!bindableExprs) {
3591
- return expr.map(recurse);
3592
- }
3593
- names = (function() {
3594
- var _i, _len, _results;
3595
- _results = [];
3596
- for (_i = 0, _len = bindableExprs.length; _i < _len; _i++) {
3597
- _ = bindableExprs[_i];
3598
- _results.push(nameGen('b'));
3599
- }
3600
- return _results;
3601
- })();
3602
- DEBUG(function() {
3603
- var bindable, i, _i, _len, _results;
3604
- _results = [];
3605
- for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3606
- bindable = bindableExprs[i];
3607
- _results.push(DEBUG.log("binding " + (bindable.inspect()) + " as " + names[i]));
3608
- }
3609
- return _results;
3610
- });
3611
- out = (recurseInner = function(boundExpr) {
3612
- var bindable, hash, i, _i, _len;
3613
- hash = boundExpr.hash();
3614
- for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3615
- bindable = bindableExprs[i];
3616
- if (hash === bindable.hash()) {
3617
- return Core.variable(names[i]);
3618
- }
3619
- }
3620
- return boundExpr.cases({
3621
- rescue: function(expr, default_) {
3622
- return Core.rescue(insertBindings(expr), insertBindings(default_));
3623
- },
3624
- squishList: function(list) {
3625
- if (list._tag !== 'list') {
3626
- throw 'invalid squish';
3627
- }
3628
- return Core.squishList(list.map(insertBindings));
3629
- },
3630
- other: function() {
3631
- return this.map(recurseInner);
3632
- }
3633
- });
3634
- })(expr);
3635
- for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3636
- bindable = bindableExprs[i];
3637
- out = Core.bind(names[i], bindable, out);
3638
- }
3639
- DEBUG.log("=> " + (out.inspect()));
3640
- return out.map(recurse);
3641
- })(expr);
3750
+ insertSubstitutions = function(expr) {
3751
+ return genSubstitutions(expr).simplify();
3642
3752
  };
3643
3753
  return function(expr) {
3644
- return simplify(expr, genSubstitutions(expr));
3754
+ return insertSubstitutions(expr).toCore();
3645
3755
  };
3646
3756
  })();
3647
3757
  return function(expr) {
@@ -5066,7 +5176,7 @@ Gibbon.reduce = (function() {
5066
5176
  })();
5067
5177
 
5068
5178
  Gibbon.JS = JS = (function(_super) {
5069
- var inspectString, oldBlock, validIdent;
5179
+ var inspectString, validIdent;
5070
5180
 
5071
5181
  __extends(JS, _super);
5072
5182
 
@@ -5095,15 +5205,6 @@ Gibbon.JS = JS = (function(_super) {
5095
5205
  varDecl: ['name']
5096
5206
  });
5097
5207
 
5098
- oldBlock = JS.block;
5099
-
5100
- JS.block = function(s) {
5101
- if (!isArray(s)) {
5102
- throw 'lol';
5103
- }
5104
- return oldBlock(s);
5105
- };
5106
-
5107
5208
  JS.prototype.toFunction = function() {
5108
5209
  return this.cases({
5109
5210
  func: function(name, args, block) {
@@ -116,46 +116,53 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
116
116
  }
117
117
  }
118
118
 
119
- function parseError(stream, result) {
120
- var expected = result.expected;
121
- var i = result.furthest;
119
+ function assertParser(p) {
120
+ if (!(p instanceof Parser)) throw new Error('not a parser: '+p);
121
+ }
122
+
123
+ var formatError = Parsimmon.formatError = function(stream, error) {
124
+ var expected = error.expected;
125
+ var i = error.index;
122
126
 
123
127
  if (i === stream.length) {
124
- var message = 'expected ' + expected + ', got the end of the string';
125
- }
126
- else {
127
- var prefix = (i > 0 ? "'..." : "'");
128
- var suffix = (stream.length - i > 12 ? "...'" : "'");
129
- var message = 'expected ' + expected + ' at character ' + i + ', got '
130
- + prefix + stream.slice(i, i+12) + suffix;
128
+ return 'expected ' + expected + ', got the end of the string';
131
129
  }
132
- throw 'Parse Error: ' + message + "\n parsing: '" + stream + "'";
133
- }
130
+
131
+ var prefix = (i > 0 ? "'..." : "'");
132
+ var suffix = (stream.length - i > 12 ? "...'" : "'");
133
+ return (
134
+ 'expected ' + expected + ' at character ' + i + ', got ' +
135
+ prefix + stream.slice(i, i+12) + suffix
136
+ );
137
+ };
134
138
 
135
139
  _.init = function(body) { this._ = body; };
136
140
 
137
141
  _.parse = function(stream) {
138
142
  var result = this.skip(eof)._(stream, 0);
139
143
 
140
- return result.status ? result.value : parseError(stream, result);
144
+ return result.status ? {
145
+ status: true,
146
+ value: result.value
147
+ } : {
148
+ status: false,
149
+ index: result.furthest,
150
+ expected: result.expected
151
+ };
141
152
  };
142
153
 
143
154
  // -*- primitive combinators -*- //
144
155
  _.or = function(alternative) {
145
- return alt([this, alternative]);
156
+ return alt(this, alternative);
146
157
  };
147
158
 
148
159
  _.then = function(next) {
149
- var self = this;
150
-
151
- return Parser(function(stream, i) {
152
- var result = self._(stream, i);
153
-
154
- if (!result.status) return result;
160
+ if (typeof next === 'function') {
161
+ throw new Error('chaining features of .then are no longer supported');
162
+ }
155
163
 
156
- var nextParser = (next instanceof Parser ? next : next(result.value));
157
- return furthestBacktrackFor(nextParser._(stream, result.index), result);
158
- });
164
+ assertParser(next);
165
+ return seq(this, next).map(function(results) { return results[1]; });
159
166
  };
160
167
 
161
168
  // -*- optimized iterative combinators -*- //
@@ -257,7 +264,7 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
257
264
  _.atMost = function(n) { return this.times(0, n); };
258
265
  _.atLeast = function(n) {
259
266
  var self = this;
260
- return seq([this.times(n), this.many()]).map(function(results) {
267
+ return seq(this.times(n), this.many()).map(function(results) {
261
268
  return results[0].concat(results[1]);
262
269
  });
263
270
  };
@@ -272,11 +279,11 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
272
279
  };
273
280
 
274
281
  _.skip = function(next) {
275
- return seq([this, next]).map(function(results) { return results[0]; });
282
+ return seq(this, next).map(function(results) { return results[0]; });
276
283
  };
277
284
 
278
285
  _.mark = function() {
279
- return seq([index, this, index]).map(function(results) {
286
+ return seq(index, this, index).map(function(results) {
280
287
  return { start: results[0], value: results[1], end: results[2] };
281
288
  });
282
289
  };
@@ -357,12 +364,15 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
357
364
  };
358
365
 
359
366
  // [Parser a] -> Parser [a]
360
- var seq = Parsimmon.seq = function(parsers) {
367
+ var seq = Parsimmon.seq = function() {
368
+ var parsers = [].slice.call(arguments);
369
+ var numParsers = parsers.length;
370
+
361
371
  return Parser(function(stream, i) {
362
372
  var result;
363
- var accum = new Array(parsers.length);
373
+ var accum = new Array(numParsers);
364
374
 
365
- for (var j = 0; j < parsers.length; j += 1) {
375
+ for (var j = 0; j < numParsers; j += 1) {
366
376
  result = furthestBacktrackFor(parsers[j]._(stream, i), result);
367
377
  if (!result.status) return result;
368
378
  accum[j] = result.value
@@ -373,7 +383,11 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
373
383
  });
374
384
  };
375
385
 
376
- var alt = Parsimmon.alt = function(parsers) {
386
+ var alt = Parsimmon.alt = function() {
387
+ var parsers = [].slice.call(arguments);
388
+ var numParsers = parsers.length;
389
+ if (numParsers === 0) return fail('zero alternates')
390
+
377
391
  return Parser(function(stream, i) {
378
392
  var result;
379
393
  for (var j = 0; j < parsers.length; j += 1) {
@@ -398,13 +412,21 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
398
412
  _.of = Parser.of = Parsimmon.of = succeed
399
413
 
400
414
  _.ap = function(other) {
401
- return seq([this, other]).map(function(results) {
415
+ return seq(this, other).map(function(results) {
402
416
  return results[0](results[1]);
403
417
  });
404
418
  };
405
419
 
406
420
  //- Monad
407
- _.chain = _.then;
421
+ _.chain = function(f) {
422
+ var self = this;
423
+ return Parser(function(stream, i) {
424
+ var result = self._(stream, i);
425
+ if (!result.status) return result;
426
+ var nextParser = f(result.value);
427
+ return furthestBacktrackFor(nextParser._(stream, result.index), result);
428
+ });
429
+ };
408
430
  });
409
431
  return Parsimmon;
410
432
  })()
@@ -1135,7 +1157,7 @@ Gibbon.TypeAST = TypeAST = (function(_super) {
1135
1157
  })(Variant);
1136
1158
 
1137
1159
  parse = Gibbon.parse = (function() {
1138
- var accessor, accessorExpr, arrow, arrowType, assertString, blankLines, blockExpr, blockType, comma, commaSepFlows, comment, component, concrete, decimal, decimalExpr, defaulted, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, identifier, innerFrame, integer, integerExpr, isString, label, labelVal, lazy, lbrace, lbrack, lexeme, lexical, lexicalExpr, lines, listExpr, listType, lparen, lsplat, metadata, multiline, name, nonDefaultedFlow, nonPairedFlow, numericExpr, opt, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, query, queryArg, queryExpr, rbrace, rbrack, regex, rparen, rsplat, seq, signature, simpleType, singletonFlow, spanLoc, squishListExpr, str, string, stringExpr, substExpr, succeed, tag, tassign, type, typeVar, variable, whitespace, wildType, wildcard, withLoc,
1160
+ var accessor, accessorExpr, arrow, arrowType, assertString, blankLines, blockExpr, blockType, comma, commaSepFlows, comment, component, concrete, decimal, decimalExpr, defaulted, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, handleResult, identifier, innerFrame, integer, integerExpr, isString, label, labelVal, lazy, lbrace, lbrack, lexeme, lexical, lexicalExpr, lines, listExpr, listType, lparen, lsplat, metadata, multiline, name, nonDefaultedFlow, nonPairedFlow, numericExpr, opt, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, query, queryArg, queryExpr, rbrace, rbrack, regex, rparen, rsplat, seq, signature, simpleType, singletonFlow, spanLoc, squishListExpr, str, string, stringExpr, substExpr, succeed, tag, tassign, type, typeVar, variable, whitespace, wildType, wildcard, withLoc,
1139
1161
  _this = this;
1140
1162
  tag = function(name, parser) {
1141
1163
  return Parsimmon.Parser(function(stream, i) {
@@ -1235,9 +1257,9 @@ parse = Gibbon.parse = (function() {
1235
1257
  lexicalExpr = withLoc(lexical, function(loc, name) {
1236
1258
  return AST.lexical(loc, name, []);
1237
1259
  });
1238
- queryExpr = query.then(function(q) {
1260
+ queryExpr = query.chain(function(q) {
1239
1261
  var args;
1240
- args = seq([lbrack, queryArg.many(), rbrack]).map(function(_arg) {
1262
+ args = seq(lbrack, queryArg.many(), rbrack).map(function(_arg) {
1241
1263
  var a, args, loc, r, _;
1242
1264
  _ = _arg[0], args = _arg[1], r = _arg[2];
1243
1265
  loc = spanLoc(q, r);
@@ -1258,28 +1280,28 @@ parse = Gibbon.parse = (function() {
1258
1280
  return lparen.then(flow).skip(rparen);
1259
1281
  });
1260
1282
  substExpr = lazy(function() {
1261
- return seq([lparen, flow, rparen]).map(function(_arg) {
1283
+ return seq(lparen, flow, rparen).map(function(_arg) {
1262
1284
  var fl, l, r;
1263
1285
  l = _arg[0], fl = _arg[1], r = _arg[2];
1264
1286
  return AST.subst(spanLoc(l, r), fl);
1265
1287
  });
1266
1288
  });
1267
1289
  listExpr = lazy(function() {
1268
- return seq([lbrack, commaSepFlows, rbrack]).map(function(_arg) {
1290
+ return seq(lbrack, commaSepFlows, rbrack).map(function(_arg) {
1269
1291
  var els, l, r;
1270
1292
  l = _arg[0], els = _arg[1], r = _arg[2];
1271
1293
  return AST.list(spanLoc(l, r), els, false);
1272
1294
  });
1273
1295
  });
1274
1296
  squishListExpr = lazy(function() {
1275
- return seq([lsplat, commaSepFlows, rsplat]).map(function(_arg) {
1297
+ return seq(lsplat, commaSepFlows, rsplat).map(function(_arg) {
1276
1298
  var els, l, r;
1277
1299
  l = _arg[0], els = _arg[1], r = _arg[2];
1278
1300
  return AST.list(spanLoc(l, r), els, true);
1279
1301
  });
1280
1302
  });
1281
1303
  blockExpr = lazy(function() {
1282
- return seq([lbrace, flow, rbrace]).map(function(_arg) {
1304
+ return seq(lbrace, flow, rbrace).map(function(_arg) {
1283
1305
  var flow, l, r;
1284
1306
  l = _arg[0], flow = _arg[1], r = _arg[2];
1285
1307
  return AST.block(spanLoc(l, r), flow);
@@ -1289,7 +1311,7 @@ parse = Gibbon.parse = (function() {
1289
1311
  singletonFlow = expr.map(function(e) {
1290
1312
  return AST.flow(e.loc, e, null);
1291
1313
  });
1292
- func = seq([name, parenFlow.or(singletonFlow).many()]).map(function(_arg) {
1314
+ func = seq(name, parenFlow.or(singletonFlow).many()).map(function(_arg) {
1293
1315
  var args, loc, name;
1294
1316
  name = _arg[0], args = _arg[1];
1295
1317
  loc = {
@@ -1302,7 +1324,7 @@ parse = Gibbon.parse = (function() {
1302
1324
  return AST.func(loc, name.value, args);
1303
1325
  });
1304
1326
  component = expr.or(func).skip(lines);
1305
- nonPairedFlow = seq([component, arrow.then(component).many()]).map(function(_arg) {
1327
+ nonPairedFlow = seq(component, arrow.then(component).many()).map(function(_arg) {
1306
1328
  var comp, cursor, first, rest, _i, _len;
1307
1329
  first = _arg[0], rest = _arg[1];
1308
1330
  cursor = AST.flow(first.loc, first, null);
@@ -1326,7 +1348,7 @@ parse = Gibbon.parse = (function() {
1326
1348
  return AST.flow(loc, AST.defaulted(loc, body, alternative), null);
1327
1349
  });
1328
1350
  }));
1329
- commaSepFlows = seq([flow.skip(seq([opt(comma, lines)])).many(), opt(flow)]).map(function(_arg) {
1351
+ commaSepFlows = seq(flow.skip(seq(opt(comma, lines))).many(), opt(flow)).map(function(_arg) {
1330
1352
  var els, final;
1331
1353
  els = _arg[0], final = _arg[1];
1332
1354
  if (final) {
@@ -1334,24 +1356,24 @@ parse = Gibbon.parse = (function() {
1334
1356
  }
1335
1357
  return els;
1336
1358
  });
1337
- metadata = seq([label, labelVal]).map(function(key, text) {
1359
+ metadata = seq(label, labelVal).map(function(key, text) {
1338
1360
  return AST.metadata(spanLoc(key, text), key.value, text.value);
1339
1361
  });
1340
1362
  definition = lazy(function() {
1341
- return seq([metadata.many(), name, define.then(innerFrame)]).map(function(_arg) {
1363
+ return seq(metadata.many(), name, define.then(innerFrame)).map(function(_arg) {
1342
1364
  var fl, loc, md, n, _ref5;
1343
1365
  md = _arg[0], n = _arg[1], fl = _arg[2];
1344
1366
  loc = spanLoc(((_ref5 = md[0]) != null ? _ref5.loc : void 0) || n, fl);
1345
1367
  return AST.definition(loc, md, n.value, fl);
1346
1368
  });
1347
1369
  });
1348
- frame = seq([definition.many(), flow]).map(function(_arg) {
1370
+ frame = seq(definition.many(), flow).map(function(_arg) {
1349
1371
  var defs, flow, loc;
1350
1372
  defs = _arg[0], flow = _arg[1];
1351
1373
  loc = spanLoc((defs[0] || flow).loc, flow.loc);
1352
1374
  return AST.frame(loc, defs, flow);
1353
1375
  });
1354
- parenFrame = seq([lparen, frame, rparen, lines]).map(function(_arg) {
1376
+ parenFrame = seq(lparen, frame, rparen, lines).map(function(_arg) {
1355
1377
  var fr, l, r, _;
1356
1378
  l = _arg[0], fr = _arg[1], r = _arg[2], _ = _arg[3];
1357
1379
  fr.loc = spanLoc(l, r);
@@ -1371,7 +1393,7 @@ parse = Gibbon.parse = (function() {
1371
1393
  });
1372
1394
  wildType = wildcard.result(TypeAST.wildcard());
1373
1395
  listType = lazy(function() {
1374
- return seq([lbrack, type, rbrack]).map(function(_arg) {
1396
+ return seq(lbrack, type, rbrack).map(function(_arg) {
1375
1397
  var t, _, __;
1376
1398
  _ = _arg[0], t = _arg[1], __ = _arg[2];
1377
1399
  return TypeAST.list(t);
@@ -1391,12 +1413,12 @@ parse = Gibbon.parse = (function() {
1391
1413
  return TypeAST.pair(first, second);
1392
1414
  });
1393
1415
  });
1394
- arrowType = seq([type, arrow, type]).map(function(_arg) {
1416
+ arrowType = seq(type, arrow, type).map(function(_arg) {
1395
1417
  var first, second, _;
1396
1418
  first = _arg[0], _ = _arg[1], second = _arg[2];
1397
1419
  return TypeAST.arrow(first, second);
1398
1420
  });
1399
- signature = seq([name, type.many(), tassign.then(arrowType)]).map(function(_arg) {
1421
+ signature = seq(name, type.many(), tassign.then(arrowType)).map(function(_arg) {
1400
1422
  var argTypes, ftype, name;
1401
1423
  name = _arg[0], argTypes = _arg[1], ftype = _arg[2];
1402
1424
  return TypeAST.func(ftype.from, argTypes, ftype.to);
@@ -1410,13 +1432,20 @@ parse = Gibbon.parse = (function() {
1410
1432
  throw 'can only parse strings';
1411
1433
  }
1412
1434
  };
1435
+ handleResult = function(result) {
1436
+ if (result.status) {
1437
+ return result.value;
1438
+ } else {
1439
+ throw new Error(Parsimmon.formatError(result));
1440
+ }
1441
+ };
1413
1442
  parse = function(str) {
1414
1443
  assertString(str);
1415
- return program.parse(str);
1444
+ return handleResult(program.parse(str));
1416
1445
  };
1417
1446
  parse.type = function(str) {
1418
1447
  assertString(str);
1419
- return fullSignature.parse(str);
1448
+ return handleResult(fullSignature.parse(str));
1420
1449
  };
1421
1450
  return parse;
1422
1451
  })();
@@ -3490,36 +3519,157 @@ Gibbon.optimize = (function() {
3490
3519
  });
3491
3520
  };
3492
3521
  insertBindings = (function() {
3493
- var findLastCommon, genSubstitutions, makeCrumbs, simplify;
3522
+ var SubstTree, findLastCommon, genSubstitutions, insertSubstitutions, makeCrumbs, _ref10;
3523
+ SubstTree = (function(_super) {
3524
+ __extends(SubstTree, _super);
3525
+
3526
+ function SubstTree() {
3527
+ _ref10 = SubstTree.__super__.constructor.apply(this, arguments);
3528
+ return _ref10;
3529
+ }
3530
+
3531
+ SubstTree.variants({
3532
+ subst: ['bindings', 'expr']
3533
+ });
3534
+
3535
+ SubstTree.prototype.toCore = function() {
3536
+ return this.__toCore || (this.__toCore = this.expr.map(function(x) {
3537
+ return x.toCore();
3538
+ }));
3539
+ };
3540
+
3541
+ SubstTree.prototype.inspect = function() {
3542
+ var e;
3543
+ if (!this.bindings.length) {
3544
+ return this.expr.inspect();
3545
+ }
3546
+ return "(subst [" + (((function() {
3547
+ var _i, _len, _ref11, _results;
3548
+ _ref11 = this.bindings;
3549
+ _results = [];
3550
+ for (_i = 0, _len = _ref11.length; _i < _len; _i++) {
3551
+ e = _ref11[_i];
3552
+ _results.push(e.inspect());
3553
+ }
3554
+ return _results;
3555
+ }).call(this)).join(' ')) + "] " + (this.expr.inspect()) + ")";
3556
+ };
3557
+
3558
+ SubstTree.prototype.map = function(f) {
3559
+ var e;
3560
+ return SubstTree.subst((function() {
3561
+ var _i, _len, _ref11, _results;
3562
+ _ref11 = this.bindings;
3563
+ _results = [];
3564
+ for (_i = 0, _len = _ref11.length; _i < _len; _i++) {
3565
+ e = _ref11[_i];
3566
+ _results.push(e.map(f));
3567
+ }
3568
+ return _results;
3569
+ }).call(this), this.expr.map(f));
3570
+ };
3571
+
3572
+ SubstTree.prototype.substituteWith = function(names, bindableExprs) {
3573
+ var bindable, core, hash, i, _i, _len;
3574
+ core = this.toCore();
3575
+ hash = core.hash();
3576
+ for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3577
+ bindable = bindableExprs[i];
3578
+ if (hash === bindable.toCore().hash()) {
3579
+ return SubstTree.unit(Core.variable(names[i]));
3580
+ }
3581
+ }
3582
+ return this.map(function(x) {
3583
+ return x.substituteWith(names, bindableExprs);
3584
+ });
3585
+ };
3586
+
3587
+ SubstTree.prototype.simplify = function() {
3588
+ var bindableExprs, expr;
3589
+ bindableExprs = this.bindings;
3590
+ expr = this.expr;
3591
+ return expr.cases({
3592
+ rescue: function() {
3593
+ return SubstTree.unit(this.map(function(x) {
3594
+ return insertSubstitutions(x.toCore());
3595
+ }));
3596
+ },
3597
+ squishList: function(list) {
3598
+ list = list.toCore();
3599
+ if (list._tag !== 'list') {
3600
+ throw 'invalid squish';
3601
+ }
3602
+ return SubstTree.unit(Core.squishList(SubstTree.unit(list.map(insertSubstitutions))));
3603
+ },
3604
+ other: function() {
3605
+ var bindable, i, names, out, substituted, _, _i, _len;
3606
+ substituted = expr.map(function(t) {
3607
+ return t.simplify();
3608
+ });
3609
+ names = (function() {
3610
+ var _i, _len, _results;
3611
+ _results = [];
3612
+ for (_i = 0, _len = bindableExprs.length; _i < _len; _i++) {
3613
+ _ = bindableExprs[_i];
3614
+ _results.push(nameGen('b'));
3615
+ }
3616
+ return _results;
3617
+ })();
3618
+ if (!bindableExprs.length) {
3619
+ return SubstTree.unit(substituted);
3620
+ }
3621
+ out = SubstTree.unit(substituted).substituteWith(names, bindableExprs);
3622
+ for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3623
+ bindable = bindableExprs[i];
3624
+ out = SubstTree.unit(Core.bind(names[i], bindable, out));
3625
+ }
3626
+
3627
+ return out;
3628
+ }
3629
+ });
3630
+ };
3631
+
3632
+ SubstTree.unit = function(expr) {
3633
+ return SubstTree.subst([], expr);
3634
+ };
3635
+
3636
+ return SubstTree;
3637
+
3638
+ })(Variant);
3494
3639
  makeCrumbs = function(trace) {
3495
3640
  return trace.mapArray(function(e) {
3496
3641
  return e.hash();
3497
3642
  }).reverse();
3498
3643
  };
3499
3644
  genSubstitutions = function(expr) {
3500
- var newTrace, occurrences, queue, sub, substitutions, trace, _i, _len, _ref10, _ref11, _ref12;
3645
+ var occurrences, queue, recurse, substitutions;
3501
3646
  occurrences = new ObjHash;
3502
3647
  substitutions = new Hash;
3503
3648
  queue = [[expr, List.empty()]];
3504
3649
  while (queue.length) {
3505
- _ref10 = queue.shift(), expr = _ref10[0], trace = _ref10[1];
3506
- if (expr.isSimple() || expr.alwaysFails()) {
3507
- continue;
3508
- }
3509
- if (occurrences.has(expr)) {
3510
- occurrences.get(expr).push(makeCrumbs(trace));
3511
- } else {
3512
- occurrences.set(expr, [makeCrumbs(trace)]);
3513
- newTrace = trace.cons(expr);
3514
- if ((_ref11 = expr._tag) === 'rescue' || _ref11 === 'squishList') {
3515
- continue;
3650
+ (function(_arg) {
3651
+ var expr, newTrace, sub, trace, _i, _len, _ref11, _ref12, _results;
3652
+ expr = _arg[0], trace = _arg[1];
3653
+ if (expr.isSimple() || expr.alwaysFails()) {
3654
+ return;
3516
3655
  }
3517
- _ref12 = expr.subtrees();
3518
- for (_i = 0, _len = _ref12.length; _i < _len; _i++) {
3519
- sub = _ref12[_i];
3520
- queue.push([sub, newTrace]);
3656
+ if (occurrences.has(expr)) {
3657
+ return occurrences.get(expr).push(makeCrumbs(trace));
3658
+ } else {
3659
+ occurrences.set(expr, [makeCrumbs(trace)]);
3660
+ newTrace = trace.cons(expr);
3661
+ if ((_ref11 = expr._tag) === 'rescue' || _ref11 === 'squishList') {
3662
+ return;
3663
+ }
3664
+ _ref12 = expr.subtrees();
3665
+ _results = [];
3666
+ for (_i = 0, _len = _ref12.length; _i < _len; _i++) {
3667
+ sub = _ref12[_i];
3668
+ _results.push(queue.push([sub, newTrace]));
3669
+ }
3670
+ return _results;
3521
3671
  }
3522
- }
3672
+ })(queue.shift());
3523
3673
  }
3524
3674
  occurrences.each(function(expr, crumbs) {
3525
3675
  var insertionPoint;
@@ -3532,7 +3682,22 @@ Gibbon.optimize = (function() {
3532
3682
  });
3533
3683
  return substitutions.get(insertionPoint).push(expr);
3534
3684
  });
3535
- return substitutions;
3685
+ return (recurse = function(expr) {
3686
+ var bindableExprs, e;
3687
+ bindableExprs = substitutions.get(expr.hash());
3688
+ if (!bindableExprs) {
3689
+ return SubstTree.unit(expr.map(recurse));
3690
+ }
3691
+ return SubstTree.subst((function() {
3692
+ var _i, _len, _results;
3693
+ _results = [];
3694
+ for (_i = 0, _len = bindableExprs.length; _i < _len; _i++) {
3695
+ e = bindableExprs[_i];
3696
+ _results.push(recurse(e));
3697
+ }
3698
+ return _results;
3699
+ })(), expr.map(recurse));
3700
+ })(expr);
3536
3701
  };
3537
3702
  findLastCommon = function(_arg) {
3538
3703
  var i, last, refCrumb, reference, rest, testCrumbs, _i, _j, _len, _len1;
@@ -3550,58 +3715,11 @@ Gibbon.optimize = (function() {
3550
3715
  }
3551
3716
  return refCrumb;
3552
3717
  };
3553
- simplify = function(expr, substitutions) {
3554
- var recurse;
3555
- return (recurse = function(expr) {
3556
- var bindable, bindableExprs, i, names, out, recurseInner, _, _i, _len;
3557
- bindableExprs = substitutions.get(expr.hash());
3558
- if (!bindableExprs) {
3559
- return expr.map(recurse);
3560
- }
3561
- names = (function() {
3562
- var _i, _len, _results;
3563
- _results = [];
3564
- for (_i = 0, _len = bindableExprs.length; _i < _len; _i++) {
3565
- _ = bindableExprs[_i];
3566
- _results.push(nameGen('b'));
3567
- }
3568
- return _results;
3569
- })();
3570
-
3571
- out = (recurseInner = function(boundExpr) {
3572
- var bindable, hash, i, _i, _len;
3573
- hash = boundExpr.hash();
3574
- for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3575
- bindable = bindableExprs[i];
3576
- if (hash === bindable.hash()) {
3577
- return Core.variable(names[i]);
3578
- }
3579
- }
3580
- return boundExpr.cases({
3581
- rescue: function(expr, default_) {
3582
- return Core.rescue(insertBindings(expr), insertBindings(default_));
3583
- },
3584
- squishList: function(list) {
3585
- if (list._tag !== 'list') {
3586
- throw 'invalid squish';
3587
- }
3588
- return Core.squishList(list.map(insertBindings));
3589
- },
3590
- other: function() {
3591
- return this.map(recurseInner);
3592
- }
3593
- });
3594
- })(expr);
3595
- for (i = _i = 0, _len = bindableExprs.length; _i < _len; i = ++_i) {
3596
- bindable = bindableExprs[i];
3597
- out = Core.bind(names[i], bindable, out);
3598
- }
3599
-
3600
- return out.map(recurse);
3601
- })(expr);
3718
+ insertSubstitutions = function(expr) {
3719
+ return genSubstitutions(expr).simplify();
3602
3720
  };
3603
3721
  return function(expr) {
3604
- return simplify(expr, genSubstitutions(expr));
3722
+ return insertSubstitutions(expr).toCore();
3605
3723
  };
3606
3724
  })();
3607
3725
  return function(expr) {
@@ -5026,7 +5144,7 @@ Gibbon.reduce = (function() {
5026
5144
  })();
5027
5145
 
5028
5146
  Gibbon.JS = JS = (function(_super) {
5029
- var inspectString, oldBlock, validIdent;
5147
+ var inspectString, validIdent;
5030
5148
 
5031
5149
  __extends(JS, _super);
5032
5150
 
@@ -5055,15 +5173,6 @@ Gibbon.JS = JS = (function(_super) {
5055
5173
  varDecl: ['name']
5056
5174
  });
5057
5175
 
5058
- oldBlock = JS.block;
5059
-
5060
- JS.block = function(s) {
5061
- if (!isArray(s)) {
5062
- throw 'lol';
5063
- }
5064
- return oldBlock(s);
5065
- };
5066
-
5067
5176
  JS.prototype.toFunction = function() {
5068
5177
  return this.cases({
5069
5178
  func: function(name, args, block) {
@@ -16,7 +16,7 @@
16
16
  "node-inspector": "0.2.x"
17
17
  },
18
18
  "dependencies": {
19
- "parsimmon": "0.2.x",
19
+ "parsimmon": "0.4.x",
20
20
  "falafel": "0.2.x"
21
21
  },
22
22
  "scripts": {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goodguide-gibbon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.10.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Adkisson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-03 00:00:00.000000000 Z
11
+ date: 2014-04-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Run and analyze gibbon code from ruby or a browser (via a ruby app).
14
14
  email:
@@ -45,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  version: '0'
46
46
  requirements: []
47
47
  rubyforge_project: goodguide-gibbon
48
- rubygems_version: 2.2.0
48
+ rubygems_version: 2.2.2
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: Ruby bindings for the gibbon data language