Skip to content

Commit 81b32b4

Browse files
committed
Fix #363 by removing imports of ohm-js from extras (it’s not required)
1 parent 9a6c27f commit 81b32b4

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

packages/ohm-js/extras/semantics-toAST.js

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
'use strict';
22

3-
// --------------------------------------------------------------------
4-
// Imports
5-
// --------------------------------------------------------------------
6-
7-
const pexprs = require('../src/pexprs');
8-
const MatchResult = require('../src/MatchResult');
9-
const Grammar = require('../src/Grammar');
10-
113
// --------------------------------------------------------------------
124
// Operations
135
// --------------------------------------------------------------------
@@ -23,11 +15,6 @@ const defaultOperation = {
2315

2416
// without customization
2517
if (!Object.prototype.hasOwnProperty.call(mapping, ctorName)) {
26-
// intermediate node
27-
if (this._node instanceof pexprs.Alt || this._node instanceof pexprs.Apply) {
28-
return children[0].toAST(mapping);
29-
}
30-
3118
// lexical rule
3219
if (this.isLexical()) {
3320
return this.sourceString;
@@ -111,8 +98,8 @@ const defaultOperation = {
11198
// The optional `mapping` parameter can be used to customize how the nodes of the CST
11299
// are mapped to the AST (see /doc/extras.md#toastmatchresult-mapping).
113100
function toAST(res, mapping) {
114-
if (!(res instanceof MatchResult) || res.failed()) {
115-
throw new Error('toAST() expects a succesfull MatchResult as first parameter');
101+
if (typeof res.failed !== 'function' || res.failed()) {
102+
throw new Error('toAST() expects a succesful MatchResult as first parameter');
116103
}
117104

118105
mapping = Object.assign({}, mapping);
@@ -130,7 +117,7 @@ function toAST(res, mapping) {
130117

131118
// Returns a semantics containg the toAST(mapping) operation for the given grammar g.
132119
function semanticsForToAST(g) {
133-
if (!(g instanceof Grammar)) {
120+
if (typeof g.createSemantics !== 'function') {
134121
throw new Error('semanticsToAST() expects a Grammar as parameter');
135122
}
136123

packages/ohm-js/src/errors.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ function invalidCodePoint(applyWrapper) {
218218
// Get an interval that covers all of the hex digits.
219219
const digitIntervals = applyWrapper.children.slice(1, -1).map(d => d.source);
220220
const fullInterval = digitIntervals[0].coverageWith(...digitIntervals.slice(1));
221-
return createError(`U+${fullInterval.contents} is not a valid Unicode code point`, fullInterval);
221+
return createError(
222+
`U+${fullInterval.contents} is not a valid Unicode code point`,
223+
fullInterval
224+
);
222225
}
223226

224227
// ----------------- Kleene operators -----------------

packages/ohm-js/test/extras/test-toAST.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const fs = require('fs');
88
const test = require('ava');
99

1010
const ohm = require('../..');
11-
const {toAST} = require('../../extras');
12-
const {semanticsForToAST} = require('../../extras');
11+
const {semanticsForToAST, toAST} = require('../../extras');
1312

1413
const g = ohm.grammar(fs.readFileSync('test/data/arithmetic.ohm'));
1514

@@ -253,3 +252,15 @@ test('real examples (combinations)', t => {
253252
};
254253
t.deepEqual(ast, expected, 'proper AST for arithmetic example #2');
255254
});
255+
256+
test('usage errors', t => {
257+
t.throws(() => toAST(g.match('doesnotmatch')), {
258+
message: /toAST\(\) expects a succesful MatchResult as first parameter/,
259+
});
260+
t.throws(() => toAST({}), {
261+
message: /toAST\(\) expects a succesful MatchResult as first parameter/,
262+
});
263+
t.throws(() => semanticsForToAST({}), {
264+
message: /semanticsToAST\(\) expects a Grammar as parameter/,
265+
});
266+
});

packages/ohm-js/test/test-recipes.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,5 @@ test('semantics recipes w/ method shorthand', t => {
295295

296296
test('recipes with astral plane code units', t => {
297297
const g = ohm.grammar(String.raw`G { start = "\u{1F920}" }`);
298-
t.truthy(
299-
ohm.makeRecipe(g.toRecipe()).match('🤠').succeeded());
298+
t.truthy(ohm.makeRecipe(g.toRecipe()).match('🤠').succeeded());
300299
});

0 commit comments

Comments
 (0)