ohm-js 17.1.0-pre → 17.2.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2022 Alessandro Warth and the Ohm project contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [Ohm](https://ohmjs.org/) · [![NPM](https://img.shields.io/npm/v/ohm-js.svg)](https://www.npmjs.com/package/ohm-js) ![Node.js CI](https://github.com/ohmjs/ohm/workflows/Node.js%20CI/badge.svg?style=flat-square) [![Chat on Discord](https://img.shields.io/badge/chat-on%20discord-7289da.svg?sanitize=true)](https://discord.gg/KwxY5gegRQ)
1
+ # [Ohm](https://ohmjs.org/) · [![NPM](https://img.shields.io/npm/v/ohm-js.svg)](https://www.npmjs.com/package/ohm-js) [![Node.js CI](https://github.com/ohmjs/ohm/actions/workflows/node.js.yml/badge.svg)](https://github.com/ohmjs/ohm/actions/workflows/node.js.yml) [![Chat on Discord](https://img.shields.io/badge/chat-on%20discord-7289da.svg?sanitize=true)](https://discord.gg/KwxY5gegRQ)
2
2
 
3
3
  Ohm is a parsing toolkit consisting of a library and a domain-specific language. You can use it to parse custom file formats or quickly build parsers, interpreters, and compilers for programming languages.
4
4
 
@@ -21,6 +21,19 @@ Some awesome things people have built using Ohm:
21
21
  - A [browser-based tool](https://www.arthurcarabott.com/konnakkol/) that turns written _Konnakkol_ (a South Indian vocal percussion art) into audio.
22
22
  - [Wildcard](https://www.geoffreylitt.com/wildcard/), a browser extension that empowers anyone to modify websites to meet their own specific needs, uses Ohm for its spreadsheet formulas.
23
23
 
24
+ ## Sponsors
25
+
26
+ Since 2017, Ohm has been maintained by [@pdubroy](https://github.com/pdubroy) on an unpaid basis. Please consider [becoming a sponsor](http://github.com/sponsors/pdubroy/)!
27
+
28
+ ### Diamond 💎
29
+
30
+ <table>
31
+ <tr>
32
+ <td><a href="https://shopify.com"><img alt="Shopify" src="./doc/images/shopify-logo.png" height="48"></a></td>
33
+ <td><a href="https://projectsubstrate.org"><img alt="Project Substrate" src="./doc/images/substrate-logo.png" height="128"></a></td>
34
+ </tr>
35
+ </table>
36
+
24
37
  ## Getting Started
25
38
 
26
39
  The easiest way to get started with Ohm is to use the [interactive editor](https://ohmjs.org/editor/). Alternatively, you can play with one of the following examples on JSFiddle:
@@ -203,4 +216,4 @@ our [suggestions for publishing grammars](./doc/publishing-grammars.md).
203
216
  ## Contributing to Ohm
204
217
 
205
218
  Interested in contributing to Ohm? Please read [CONTRIBUTING.md](./CONTRIBUTING.md)
206
- and the [Ohm Contributor Guide](doc/contributing.md).
219
+ and the [Ohm Contributor Guide](doc/contributor-guide.md).
@@ -171,6 +171,13 @@ function unexpectedObjToString(obj) {
171
171
  }
172
172
  }
173
173
 
174
+ function checkNotNull(obj, message = 'unexpected null value') {
175
+ if (obj == null) {
176
+ throw new Error(message);
177
+ }
178
+ return obj;
179
+ }
180
+
174
181
  var common = /*#__PURE__*/Object.freeze({
175
182
  __proto__: null,
176
183
  abstract: abstract,
@@ -187,7 +194,8 @@ var common = /*#__PURE__*/Object.freeze({
187
194
  padLeft: padLeft,
188
195
  StringBuffer: StringBuffer,
189
196
  unescapeCodePoint: unescapeCodePoint,
190
- unexpectedObjToString: unexpectedObjToString
197
+ unexpectedObjToString: unexpectedObjToString,
198
+ checkNotNull: checkNotNull
191
199
  });
192
200
 
193
201
  // --------------------------------------------------------------------
@@ -2897,7 +2905,7 @@ any.substituteParams =
2897
2905
  };
2898
2906
 
2899
2907
  Param.prototype.substituteParams = function(actuals) {
2900
- return actuals[this.index];
2908
+ return checkNotNull(actuals[this.index]);
2901
2909
  };
2902
2910
 
2903
2911
  Alt.prototype.substituteParams = function(actuals) {
@@ -5592,6 +5600,9 @@ function initBuiltInSemantics(builtInRules) {
5592
5600
  nonEmpty(first, _, rest) {
5593
5601
  return this.iteration([first].concat(rest.children));
5594
5602
  },
5603
+ self(..._children) {
5604
+ return this;
5605
+ },
5595
5606
  };
5596
5607
 
5597
5608
  Semantics.BuiltInSemantics = Semantics.createSemantics(builtInRules, null).addOperation(
@@ -5601,6 +5612,7 @@ function initBuiltInSemantics(builtInRules) {
5601
5612
  nonemptyListOf: actions.nonEmpty,
5602
5613
  EmptyListOf: actions.empty,
5603
5614
  NonemptyListOf: actions.nonEmpty,
5615
+ _iter: actions.self,
5604
5616
  },
5605
5617
  );
5606
5618
  }
@@ -5845,13 +5857,7 @@ const grammarsSource = String.raw`
5845
5857
  // Example:
5846
5858
  //+ "//+ \"x\"\nG {\n//- \"\"\nstart = \"x\"}"
5847
5859
  OhmWithExamples <: Ohm {
5848
- // The default start rule for Ohm is 'Grammars', which is syntactic rule.
5849
- // When the start rule is a syntactic rule, there's no way to get access to
5850
- // leading space (including comments). So, for this grammar to be useful,
5851
- // you have to explicit use this rule as the start rule.
5852
- grammarsWithExamples = (exampleComments applySyntactic<Grammar>)*
5853
-
5854
- Grammar := ident SuperGrammar? "{" (#exampleComments Rule)* "}"
5860
+ Grammar := ident SuperGrammar? "{" (#exampleComments Rule)* #exampleComments "}"
5855
5861
 
5856
5862
  exampleComments = (spacesNoExampleComment exampleComment)*
5857
5863
 
@@ -5915,19 +5921,10 @@ const semantics = grammars.OhmWithExamples.createSemantics().addOperation('hasEx
5915
5921
  });
5916
5922
 
5917
5923
  semantics.addOperation('examples', {
5918
- grammarsWithExamples(exampleCommentsIter, grammarIter) {
5919
- const result = [];
5920
- for (const [i, child] of Object.entries(grammarIter.children)) {
5921
- if (exampleCommentsIter.hasExamples()) {
5922
- const defaultExamples = exampleCommentsIter.child(i).examples();
5923
- const grammar = child.grammarName();
5924
- result.push(...defaultExamples.map(ex => ({...ex, grammar, rule: ''})));
5925
- }
5926
- result.push(...child.examples());
5927
- }
5928
- return result;
5924
+ Grammars(grammarIter) {
5925
+ return grammarIter.children.flatMap(c => c.examples());
5929
5926
  },
5930
- Grammar(name, _, _open, exampleCommentsIter, ruleIter, _close) {
5927
+ Grammar(name, _, _open, exampleCommentsIter, ruleIter, trailingCommentsIter, _close) {
5931
5928
  const result = [];
5932
5929
  const grammar = this.grammarName();
5933
5930
  for (let i = 0; i < ruleIter.numChildren; i++) {
@@ -5939,6 +5936,10 @@ semantics.addOperation('examples', {
5939
5936
 
5940
5937
  result.push(...augmentedExamples);
5941
5938
  }
5939
+ if (trailingCommentsIter.hasExamples()) {
5940
+ const defaultExamples = trailingCommentsIter.examples();
5941
+ result.push(...defaultExamples.map(ex => ({...ex, grammar, rule: ''})));
5942
+ }
5942
5943
  return result;
5943
5944
  },
5944
5945
  exampleComments(_, commentIter) {
@@ -5964,7 +5965,7 @@ semantics.addOperation('examples', {
5964
5965
  });
5965
5966
 
5966
5967
  semantics.addOperation('grammarName', {
5967
- Grammar(name, _, _open, exampleCommentsIter, ruleIter, _close) {
5968
+ Grammar(name, _, _open, exampleCommentsIter, ruleIter, trailingCommentsIter, _close) {
5968
5969
  return name.sourceString;
5969
5970
  },
5970
5971
  });
@@ -5988,7 +5989,7 @@ semantics.addOperation('ruleName', {
5988
5989
  * @return {[Example]}
5989
5990
  */
5990
5991
  function extractExamples(grammarsDef) {
5991
- const matchResult = grammars.OhmWithExamples.match(grammarsDef, 'grammarsWithExamples');
5992
+ const matchResult = grammars.OhmWithExamples.match(grammarsDef);
5992
5993
  if (matchResult.failed()) {
5993
5994
  throw new Error(matchResult.message);
5994
5995
  }
@@ -173,6 +173,13 @@
173
173
  }
174
174
  }
175
175
 
176
+ function checkNotNull(obj, message = 'unexpected null value') {
177
+ if (obj == null) {
178
+ throw new Error(message);
179
+ }
180
+ return obj;
181
+ }
182
+
176
183
  var common = /*#__PURE__*/Object.freeze({
177
184
  __proto__: null,
178
185
  abstract: abstract,
@@ -189,7 +196,8 @@
189
196
  padLeft: padLeft,
190
197
  StringBuffer: StringBuffer,
191
198
  unescapeCodePoint: unescapeCodePoint,
192
- unexpectedObjToString: unexpectedObjToString
199
+ unexpectedObjToString: unexpectedObjToString,
200
+ checkNotNull: checkNotNull
193
201
  });
194
202
 
195
203
  // --------------------------------------------------------------------
@@ -2899,7 +2907,7 @@
2899
2907
  };
2900
2908
 
2901
2909
  Param.prototype.substituteParams = function(actuals) {
2902
- return actuals[this.index];
2910
+ return checkNotNull(actuals[this.index]);
2903
2911
  };
2904
2912
 
2905
2913
  Alt.prototype.substituteParams = function(actuals) {
@@ -5594,6 +5602,9 @@
5594
5602
  nonEmpty(first, _, rest) {
5595
5603
  return this.iteration([first].concat(rest.children));
5596
5604
  },
5605
+ self(..._children) {
5606
+ return this;
5607
+ },
5597
5608
  };
5598
5609
 
5599
5610
  Semantics.BuiltInSemantics = Semantics.createSemantics(builtInRules, null).addOperation(
@@ -5603,6 +5614,7 @@
5603
5614
  nonemptyListOf: actions.nonEmpty,
5604
5615
  EmptyListOf: actions.empty,
5605
5616
  NonemptyListOf: actions.nonEmpty,
5617
+ _iter: actions.self,
5606
5618
  },
5607
5619
  );
5608
5620
  }
@@ -5847,13 +5859,7 @@
5847
5859
  // Example:
5848
5860
  //+ "//+ \"x\"\nG {\n//- \"\"\nstart = \"x\"}"
5849
5861
  OhmWithExamples <: Ohm {
5850
- // The default start rule for Ohm is 'Grammars', which is syntactic rule.
5851
- // When the start rule is a syntactic rule, there's no way to get access to
5852
- // leading space (including comments). So, for this grammar to be useful,
5853
- // you have to explicit use this rule as the start rule.
5854
- grammarsWithExamples = (exampleComments applySyntactic<Grammar>)*
5855
-
5856
- Grammar := ident SuperGrammar? "{" (#exampleComments Rule)* "}"
5862
+ Grammar := ident SuperGrammar? "{" (#exampleComments Rule)* #exampleComments "}"
5857
5863
 
5858
5864
  exampleComments = (spacesNoExampleComment exampleComment)*
5859
5865
 
@@ -5917,19 +5923,10 @@
5917
5923
  });
5918
5924
 
5919
5925
  semantics.addOperation('examples', {
5920
- grammarsWithExamples(exampleCommentsIter, grammarIter) {
5921
- const result = [];
5922
- for (const [i, child] of Object.entries(grammarIter.children)) {
5923
- if (exampleCommentsIter.hasExamples()) {
5924
- const defaultExamples = exampleCommentsIter.child(i).examples();
5925
- const grammar = child.grammarName();
5926
- result.push(...defaultExamples.map(ex => ({...ex, grammar, rule: ''})));
5927
- }
5928
- result.push(...child.examples());
5929
- }
5930
- return result;
5926
+ Grammars(grammarIter) {
5927
+ return grammarIter.children.flatMap(c => c.examples());
5931
5928
  },
5932
- Grammar(name, _, _open, exampleCommentsIter, ruleIter, _close) {
5929
+ Grammar(name, _, _open, exampleCommentsIter, ruleIter, trailingCommentsIter, _close) {
5933
5930
  const result = [];
5934
5931
  const grammar = this.grammarName();
5935
5932
  for (let i = 0; i < ruleIter.numChildren; i++) {
@@ -5941,6 +5938,10 @@
5941
5938
 
5942
5939
  result.push(...augmentedExamples);
5943
5940
  }
5941
+ if (trailingCommentsIter.hasExamples()) {
5942
+ const defaultExamples = trailingCommentsIter.examples();
5943
+ result.push(...defaultExamples.map(ex => ({...ex, grammar, rule: ''})));
5944
+ }
5944
5945
  return result;
5945
5946
  },
5946
5947
  exampleComments(_, commentIter) {
@@ -5966,7 +5967,7 @@
5966
5967
  });
5967
5968
 
5968
5969
  semantics.addOperation('grammarName', {
5969
- Grammar(name, _, _open, exampleCommentsIter, ruleIter, _close) {
5970
+ Grammar(name, _, _open, exampleCommentsIter, ruleIter, trailingCommentsIter, _close) {
5970
5971
  return name.sourceString;
5971
5972
  },
5972
5973
  });
@@ -5990,7 +5991,7 @@
5990
5991
  * @return {[Example]}
5991
5992
  */
5992
5993
  function extractExamples(grammarsDef) {
5993
- const matchResult = grammars.OhmWithExamples.match(grammarsDef, 'grammarsWithExamples');
5994
+ const matchResult = grammars.OhmWithExamples.match(grammarsDef);
5994
5995
  if (matchResult.failed()) {
5995
5996
  throw new Error(matchResult.message);
5996
5997
  }
File without changes
package/dist/ohm.cjs CHANGED
@@ -171,6 +171,13 @@ function unexpectedObjToString(obj) {
171
171
  }
172
172
  }
173
173
 
174
+ function checkNotNull(obj, message = 'unexpected null value') {
175
+ if (obj == null) {
176
+ throw new Error(message);
177
+ }
178
+ return obj;
179
+ }
180
+
174
181
  var common = /*#__PURE__*/Object.freeze({
175
182
  __proto__: null,
176
183
  abstract: abstract,
@@ -187,7 +194,8 @@ var common = /*#__PURE__*/Object.freeze({
187
194
  padLeft: padLeft,
188
195
  StringBuffer: StringBuffer,
189
196
  unescapeCodePoint: unescapeCodePoint,
190
- unexpectedObjToString: unexpectedObjToString
197
+ unexpectedObjToString: unexpectedObjToString,
198
+ checkNotNull: checkNotNull
191
199
  });
192
200
 
193
201
  // These are just categories that are used in ES5/ES2015.
@@ -2606,7 +2614,7 @@ any.substituteParams =
2606
2614
  };
2607
2615
 
2608
2616
  Param.prototype.substituteParams = function(actuals) {
2609
- return actuals[this.index];
2617
+ return checkNotNull(actuals[this.index]);
2610
2618
  };
2611
2619
 
2612
2620
  Alt.prototype.substituteParams = function(actuals) {
@@ -5327,6 +5335,9 @@ function initBuiltInSemantics(builtInRules) {
5327
5335
  nonEmpty(first, _, rest) {
5328
5336
  return this.iteration([first].concat(rest.children));
5329
5337
  },
5338
+ self(..._children) {
5339
+ return this;
5340
+ },
5330
5341
  };
5331
5342
 
5332
5343
  Semantics.BuiltInSemantics = Semantics.createSemantics(builtInRules, null).addOperation(
@@ -5336,6 +5347,7 @@ function initBuiltInSemantics(builtInRules) {
5336
5347
  nonemptyListOf: actions.nonEmpty,
5337
5348
  EmptyListOf: actions.empty,
5338
5349
  NonemptyListOf: actions.nonEmpty,
5350
+ _iter: actions.self,
5339
5351
  },
5340
5352
  );
5341
5353
  }
@@ -5541,7 +5553,7 @@ Object.assign(IndentationSensitive, {
5541
5553
  });
5542
5554
 
5543
5555
  // Generated by scripts/prebuild.js
5544
- const version = '17.1.0-pre';
5556
+ const version = '17.2.0';
5545
5557
 
5546
5558
  Grammar.initApplicationParser(ohmGrammar, buildGrammar);
5547
5559