ohm-js 17.3.0 → 17.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2567,6 +2567,7 @@ Apply.prototype.reallyEval = function (state) {
2567
2567
  }
2568
2568
  if (memoRec) {
2569
2569
  memoRec.failuresAtRightmostPosition = state.cloneRecordedFailures();
2570
+ memoRec.rightmostFailureOffset = state._getRightmostFailureOffset();
2570
2571
  }
2571
2572
  }
2572
2573
 
@@ -5214,9 +5215,10 @@ class GrammarDecl {
5214
5215
  // --------------------------------------------------------------------
5215
5216
 
5216
5217
  class Builder {
5217
- constructor() {
5218
+ constructor(options) {
5218
5219
  this.currentDecl = null;
5219
5220
  this.currentRuleName = null;
5221
+ this.options = options || {};
5220
5222
  }
5221
5223
 
5222
5224
  newGrammar(name) {
@@ -5336,6 +5338,10 @@ class Builder {
5336
5338
  if (!(expr instanceof PExpr)) {
5337
5339
  expr = this.fromRecipe(expr);
5338
5340
  }
5341
+ // For v18 compatibility, where we don't want a binding for lookahead.
5342
+ if (this.options.eliminateLookaheads) {
5343
+ return new Not(new Not(expr));
5344
+ }
5339
5345
  return new Lookahead(expr);
5340
5346
  }
5341
5347
 
@@ -5415,8 +5421,8 @@ function namespaceHas(ns, name) {
5415
5421
  // `tree`, which is the concrete syntax tree of a user-written grammar.
5416
5422
  // The grammar will be assigned into `namespace` under the name of the grammar
5417
5423
  // as specified in the source.
5418
- function buildGrammar(match, namespace, optOhmGrammarForTesting) {
5419
- const builder = new Builder();
5424
+ function buildGrammar(match, namespace, optOhmGrammarForTesting, options) {
5425
+ const builder = new Builder(options);
5420
5426
  let decl;
5421
5427
  let currentRuleName;
5422
5428
  let currentRuleFormals;
@@ -5870,15 +5876,15 @@ const isBuffer = obj =>
5870
5876
  typeof obj.constructor.isBuffer === 'function' &&
5871
5877
  obj.constructor.isBuffer(obj);
5872
5878
 
5873
- function compileAndLoad(source, namespace) {
5879
+ function compileAndLoad(source, namespace, buildOptions) {
5874
5880
  const m = ohmGrammar.match(source, 'Grammars');
5875
5881
  if (m.failed()) {
5876
5882
  throw grammarSyntaxError(m);
5877
5883
  }
5878
- return buildGrammar(m, namespace);
5884
+ return buildGrammar(m, namespace, undefined, buildOptions);
5879
5885
  }
5880
5886
 
5881
- function grammars$1(source, optNamespace) {
5887
+ function _grammars(source, optNamespace, buildOptions) {
5882
5888
  const ns = Object.create(optNamespace || {});
5883
5889
  if (typeof source !== 'string') {
5884
5890
  // For convenience, detect Node.js Buffer objects and automatically call toString().
@@ -5890,10 +5896,14 @@ function grammars$1(source, optNamespace) {
5890
5896
  );
5891
5897
  }
5892
5898
  }
5893
- compileAndLoad(source, ns);
5899
+ compileAndLoad(source, ns, buildOptions);
5894
5900
  return ns;
5895
5901
  }
5896
5902
 
5903
+ function grammars$1(source, optNamespace) {
5904
+ return _grammars(source, optNamespace);
5905
+ }
5906
+
5897
5907
  const grammarsSource = String.raw`
5898
5908
  /*
5899
5909
  Superset of the Ohm grammar that allows examples to be embedded in comments.
@@ -6042,9 +6052,55 @@ function extractExamples(grammarsDef) {
6042
6052
  return semantics(matchResult).examples();
6043
6053
  }
6044
6054
 
6055
+ const isIterSibling = (refNode, n) => {
6056
+ return (
6057
+ n.isIteration() &&
6058
+ n.source.startIdx === refNode.source.startIdx &&
6059
+ n.source.endIdx === refNode.source.endIdx &&
6060
+ n.children.length === refNode.children.length
6061
+ );
6062
+ };
6063
+
6064
+ function groupIterSiblings(nodes) {
6065
+ const groups = [];
6066
+ for (let i = 0; i < nodes.length; i++) {
6067
+ const n = nodes[i];
6068
+ if (!n.isIteration()) {
6069
+ groups.push({kind: 'node', node: n});
6070
+ continue;
6071
+ }
6072
+ const siblings = [n];
6073
+ for (let j = i + 1; j < nodes.length && isIterSibling(n, nodes[j]); j++) {
6074
+ siblings.push(nodes[j]);
6075
+ i = j;
6076
+ }
6077
+ groups.push({kind: 'iter', siblings});
6078
+ }
6079
+ return groups;
6080
+ }
6081
+
6082
+ function recoverSourceOrder(nodes, depth = 0) {
6083
+ const ans = [];
6084
+ for (const group of groupIterSiblings(nodes)) {
6085
+ if (group.kind === 'node') {
6086
+ ans.push(group.node);
6087
+ continue;
6088
+ }
6089
+ const {siblings} = group;
6090
+ const cousins = [];
6091
+ const numRows = siblings[0].children.length;
6092
+ for (let row = 0; row < numRows; row++) {
6093
+ cousins.push(...siblings.map(sib => sib.children[row]));
6094
+ }
6095
+ ans.push(...recoverSourceOrder(cousins, depth + 1));
6096
+ }
6097
+ return ans;
6098
+ }
6099
+
6045
6100
  exports.VisitorFamily = VisitorFamily;
6046
6101
  exports.extractExamples = extractExamples;
6047
6102
  exports.getLineAndColumn = getLineAndColumn;
6048
6103
  exports.getLineAndColumnMessage = getLineAndColumnMessage;
6104
+ exports.recoverSourceOrder = recoverSourceOrder;
6049
6105
  exports.semanticsForToAST = semanticsForToAST;
6050
6106
  exports.toAST = toAST;
@@ -2569,6 +2569,7 @@
2569
2569
  }
2570
2570
  if (memoRec) {
2571
2571
  memoRec.failuresAtRightmostPosition = state.cloneRecordedFailures();
2572
+ memoRec.rightmostFailureOffset = state._getRightmostFailureOffset();
2572
2573
  }
2573
2574
  }
2574
2575
 
@@ -5216,9 +5217,10 @@
5216
5217
  // --------------------------------------------------------------------
5217
5218
 
5218
5219
  class Builder {
5219
- constructor() {
5220
+ constructor(options) {
5220
5221
  this.currentDecl = null;
5221
5222
  this.currentRuleName = null;
5223
+ this.options = options || {};
5222
5224
  }
5223
5225
 
5224
5226
  newGrammar(name) {
@@ -5338,6 +5340,10 @@
5338
5340
  if (!(expr instanceof PExpr)) {
5339
5341
  expr = this.fromRecipe(expr);
5340
5342
  }
5343
+ // For v18 compatibility, where we don't want a binding for lookahead.
5344
+ if (this.options.eliminateLookaheads) {
5345
+ return new Not(new Not(expr));
5346
+ }
5341
5347
  return new Lookahead(expr);
5342
5348
  }
5343
5349
 
@@ -5417,8 +5423,8 @@
5417
5423
  // `tree`, which is the concrete syntax tree of a user-written grammar.
5418
5424
  // The grammar will be assigned into `namespace` under the name of the grammar
5419
5425
  // as specified in the source.
5420
- function buildGrammar(match, namespace, optOhmGrammarForTesting) {
5421
- const builder = new Builder();
5426
+ function buildGrammar(match, namespace, optOhmGrammarForTesting, options) {
5427
+ const builder = new Builder(options);
5422
5428
  let decl;
5423
5429
  let currentRuleName;
5424
5430
  let currentRuleFormals;
@@ -5872,15 +5878,15 @@
5872
5878
  typeof obj.constructor.isBuffer === 'function' &&
5873
5879
  obj.constructor.isBuffer(obj);
5874
5880
 
5875
- function compileAndLoad(source, namespace) {
5881
+ function compileAndLoad(source, namespace, buildOptions) {
5876
5882
  const m = ohmGrammar.match(source, 'Grammars');
5877
5883
  if (m.failed()) {
5878
5884
  throw grammarSyntaxError(m);
5879
5885
  }
5880
- return buildGrammar(m, namespace);
5886
+ return buildGrammar(m, namespace, undefined, buildOptions);
5881
5887
  }
5882
5888
 
5883
- function grammars$1(source, optNamespace) {
5889
+ function _grammars(source, optNamespace, buildOptions) {
5884
5890
  const ns = Object.create(optNamespace || {});
5885
5891
  if (typeof source !== 'string') {
5886
5892
  // For convenience, detect Node.js Buffer objects and automatically call toString().
@@ -5892,10 +5898,14 @@
5892
5898
  );
5893
5899
  }
5894
5900
  }
5895
- compileAndLoad(source, ns);
5901
+ compileAndLoad(source, ns, buildOptions);
5896
5902
  return ns;
5897
5903
  }
5898
5904
 
5905
+ function grammars$1(source, optNamespace) {
5906
+ return _grammars(source, optNamespace);
5907
+ }
5908
+
5899
5909
  const grammarsSource = String.raw`
5900
5910
  /*
5901
5911
  Superset of the Ohm grammar that allows examples to be embedded in comments.
@@ -6044,10 +6054,56 @@
6044
6054
  return semantics(matchResult).examples();
6045
6055
  }
6046
6056
 
6057
+ const isIterSibling = (refNode, n) => {
6058
+ return (
6059
+ n.isIteration() &&
6060
+ n.source.startIdx === refNode.source.startIdx &&
6061
+ n.source.endIdx === refNode.source.endIdx &&
6062
+ n.children.length === refNode.children.length
6063
+ );
6064
+ };
6065
+
6066
+ function groupIterSiblings(nodes) {
6067
+ const groups = [];
6068
+ for (let i = 0; i < nodes.length; i++) {
6069
+ const n = nodes[i];
6070
+ if (!n.isIteration()) {
6071
+ groups.push({kind: 'node', node: n});
6072
+ continue;
6073
+ }
6074
+ const siblings = [n];
6075
+ for (let j = i + 1; j < nodes.length && isIterSibling(n, nodes[j]); j++) {
6076
+ siblings.push(nodes[j]);
6077
+ i = j;
6078
+ }
6079
+ groups.push({kind: 'iter', siblings});
6080
+ }
6081
+ return groups;
6082
+ }
6083
+
6084
+ function recoverSourceOrder(nodes, depth = 0) {
6085
+ const ans = [];
6086
+ for (const group of groupIterSiblings(nodes)) {
6087
+ if (group.kind === 'node') {
6088
+ ans.push(group.node);
6089
+ continue;
6090
+ }
6091
+ const {siblings} = group;
6092
+ const cousins = [];
6093
+ const numRows = siblings[0].children.length;
6094
+ for (let row = 0; row < numRows; row++) {
6095
+ cousins.push(...siblings.map(sib => sib.children[row]));
6096
+ }
6097
+ ans.push(...recoverSourceOrder(cousins, depth + 1));
6098
+ }
6099
+ return ans;
6100
+ }
6101
+
6047
6102
  exports.VisitorFamily = VisitorFamily;
6048
6103
  exports.extractExamples = extractExamples;
6049
6104
  exports.getLineAndColumn = getLineAndColumn;
6050
6105
  exports.getLineAndColumnMessage = getLineAndColumnMessage;
6106
+ exports.recoverSourceOrder = recoverSourceOrder;
6051
6107
  exports.semanticsForToAST = semanticsForToAST;
6052
6108
  exports.toAST = toAST;
6053
6109
 
package/dist/ohm.cjs CHANGED
@@ -2276,6 +2276,7 @@ Apply.prototype.reallyEval = function (state) {
2276
2276
  }
2277
2277
  if (memoRec) {
2278
2278
  memoRec.failuresAtRightmostPosition = state.cloneRecordedFailures();
2279
+ memoRec.rightmostFailureOffset = state._getRightmostFailureOffset();
2279
2280
  }
2280
2281
  }
2281
2282
 
@@ -4949,9 +4950,10 @@ class GrammarDecl {
4949
4950
  // --------------------------------------------------------------------
4950
4951
 
4951
4952
  class Builder {
4952
- constructor() {
4953
+ constructor(options) {
4953
4954
  this.currentDecl = null;
4954
4955
  this.currentRuleName = null;
4956
+ this.options = options || {};
4955
4957
  }
4956
4958
 
4957
4959
  newGrammar(name) {
@@ -5071,6 +5073,10 @@ class Builder {
5071
5073
  if (!(expr instanceof PExpr)) {
5072
5074
  expr = this.fromRecipe(expr);
5073
5075
  }
5076
+ // For v18 compatibility, where we don't want a binding for lookahead.
5077
+ if (this.options.eliminateLookaheads) {
5078
+ return new Not(new Not(expr));
5079
+ }
5074
5080
  return new Lookahead(expr);
5075
5081
  }
5076
5082
 
@@ -5150,8 +5156,8 @@ function namespaceHas(ns, name) {
5150
5156
  // `tree`, which is the concrete syntax tree of a user-written grammar.
5151
5157
  // The grammar will be assigned into `namespace` under the name of the grammar
5152
5158
  // as specified in the source.
5153
- function buildGrammar(match, namespace, optOhmGrammarForTesting) {
5154
- const builder = new Builder();
5159
+ function buildGrammar(match, namespace, optOhmGrammarForTesting, options) {
5160
+ const builder = new Builder(options);
5155
5161
  let decl;
5156
5162
  let currentRuleName;
5157
5163
  let currentRuleFormals;
@@ -5599,7 +5605,7 @@ Object.assign(IndentationSensitive, {
5599
5605
  });
5600
5606
 
5601
5607
  // Generated by scripts/prebuild.js
5602
- const version = '17.3.0';
5608
+ const version = '17.5.0';
5603
5609
 
5604
5610
  Grammar.initApplicationParser(ohmGrammar, buildGrammar);
5605
5611
 
@@ -5608,16 +5614,16 @@ const isBuffer = obj =>
5608
5614
  typeof obj.constructor.isBuffer === 'function' &&
5609
5615
  obj.constructor.isBuffer(obj);
5610
5616
 
5611
- function compileAndLoad(source, namespace) {
5617
+ function compileAndLoad(source, namespace, buildOptions) {
5612
5618
  const m = ohmGrammar.match(source, 'Grammars');
5613
5619
  if (m.failed()) {
5614
5620
  throw grammarSyntaxError(m);
5615
5621
  }
5616
- return buildGrammar(m, namespace);
5622
+ return buildGrammar(m, namespace, undefined, buildOptions);
5617
5623
  }
5618
5624
 
5619
- function grammar(source, optNamespace) {
5620
- const ns = grammars(source, optNamespace);
5625
+ function _grammar(source, optNamespace, buildOptions) {
5626
+ const ns = _grammars(source, optNamespace, buildOptions);
5621
5627
 
5622
5628
  // Ensure that the source contained no more than one grammar definition.
5623
5629
  const grammarNames = Object.keys(ns);
@@ -5634,7 +5640,7 @@ function grammar(source, optNamespace) {
5634
5640
  return ns[grammarNames[0]]; // Return the one and only grammar.
5635
5641
  }
5636
5642
 
5637
- function grammars(source, optNamespace) {
5643
+ function _grammars(source, optNamespace, buildOptions) {
5638
5644
  const ns = Object.create(optNamespace || {});
5639
5645
  if (typeof source !== 'string') {
5640
5646
  // For convenience, detect Node.js Buffer objects and automatically call toString().
@@ -5646,10 +5652,18 @@ function grammars(source, optNamespace) {
5646
5652
  );
5647
5653
  }
5648
5654
  }
5649
- compileAndLoad(source, ns);
5655
+ compileAndLoad(source, ns, buildOptions);
5650
5656
  return ns;
5651
5657
  }
5652
5658
 
5659
+ function grammar(source, optNamespace) {
5660
+ return _grammar(source, optNamespace);
5661
+ }
5662
+
5663
+ function grammars(source, optNamespace) {
5664
+ return _grammars(source, optNamespace);
5665
+ }
5666
+
5653
5667
  exports.ExperimentalIndentationSensitive = IndentationSensitive;
5654
5668
  exports._buildGrammar = buildGrammar;
5655
5669
  exports.grammar = grammar;