Skip to content

Commit 2f5a777

Browse files
committed
Raise specific exception #6
1 parent d5545a5 commit 2f5a777

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

src/exceptions.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function GrammarParseError() {
2+
var temp = Error.apply(this, arguments);
3+
temp.name = this.name = 'GrammarParseError';
4+
this.stack = temp.stack;
5+
this.message = temp.message;
6+
}
7+
GrammarParseError.prototype = Object.create(Error.prototype, {
8+
constructor: {
9+
value: GrammarParseError,
10+
writable: true,
11+
configurable: true
12+
}
13+
});
14+
15+
function TextParseError() {
16+
var temp = Error.apply(this, arguments);
17+
temp.name = this.name = 'TextParseError';
18+
this.stack = temp.stack;
19+
this.message = temp.message;
20+
}
21+
TextParseError.prototype = Object.create(Error.prototype, {
22+
constructor: {
23+
value: TextParseError,
24+
writable: true,
25+
configurable: true
26+
}
27+
});
28+
29+
30+
module.exports = {
31+
GrammarParseError: GrammarParseError,
32+
TextParseError: TextParseError
33+
}

src/speg.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var SPEG_actions_visitor = require('./speg_visitor').SPEG_actions_visitor;
22
var SPEG_parser = require('./speg_parser');
33
var rd = require('./rd_parser');
4+
var ex = require('./exceptions');
45

56
function SPEG() {
67
this.parser = new SPEG_parser();
@@ -14,7 +15,7 @@ SPEG.prototype.parse_grammar = function(grammar) {
1415
if (speg_ast) {
1516
this.speg_parser = this.visitor.visit(speg_ast);
1617
} else {
17-
throw Error('Failed to parse grammar: \n\n' + this.parser.get_last_error());
18+
throw new ex.GrammarParseError('Failed to parse grammar: \n\n' + this.parser.get_last_error());
1819
}
1920
};
2021

@@ -28,7 +29,7 @@ SPEG.prototype.parse_text = function(text) {
2829
if (ast) {
2930
return ast;
3031
} else {
31-
throw Error('Failed to parse text: \n\n' + rd.get_last_error(state))
32+
throw new ex.TextParseError('Failed to parse text: \n\n' + rd.get_last_error(state))
3233
}
3334
} else {
3435
throw Error('You need grammar to parse text. Call parseGrammar first');
@@ -47,10 +48,10 @@ SPEG.prototype.parse = function(grammar, text) {
4748
if (ast) {
4849
return ast;
4950
} else {
50-
throw Error('Failed to parse text: \n\n' + rd.get_last_error(state))
51+
throw new ex.TextParseError('Failed to parse text: \n\n' + rd.get_last_error(state))
5152
}
5253
} else {
53-
throw Error('Failed to parse grammar: \n\n' + this.parser.get_last_error())
54+
throw new ex.GrammarParseError('Failed to parse grammar: \n\n' + this.parser.get_last_error())
5455
}
5556
};
5657

test/exceptions.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var should = require('chai').should();
2+
var expect = require('chai').expect;
3+
var simplepeg = require('./../src/speg');
4+
var ex = require('./../src/exceptions');
5+
6+
describe('exceptions - ', function() {
7+
it('should throw correct error when grammar is wrong',function() {
8+
expect(function() {
9+
var parser = new simplepeg.SPEG();
10+
parser.parse_grammar('!!!');
11+
}).to.throw(ex.GrammarParseError);
12+
});
13+
it('should throw correct error when text is wrong',function() {
14+
expect(function() {
15+
var parser = new simplepeg.SPEG();
16+
parser.parse_grammar('GRAMMAR test a -> "A";');
17+
parser.parse_text('B');
18+
}).to.throw(ex.TextParseError);
19+
});
20+
it('should throw when text called before grammar',function() {
21+
expect(function() {
22+
var parser = new simplepeg.SPEG();
23+
parser.parse_text('B');
24+
}).to.throw(Error);
25+
});
26+
it('should (speg.parse) throw correct error when grammar is wrong',function() {
27+
expect(function() {
28+
var parser = new simplepeg.SPEG();
29+
parser.parse('!!!', 'B');
30+
}).to.throw(ex.GrammarParseError);
31+
});
32+
it('should (speg.parse) throw correct error when text is wrong',function() {
33+
expect(function() {
34+
var parser = new simplepeg.SPEG();
35+
parser.parse('GRAMMAR test a -> "A";', 'B');
36+
}).to.throw(ex.TextParseError);
37+
});
38+
});

0 commit comments

Comments
 (0)