Skip to content

Commit 47ca717

Browse files
committed
Simplify and fix line mode.
Fixes #156.
1 parent 6bf31b9 commit 47ca717

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

lib/N3Lexer.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,40 @@ var escapeReplacements = {
1717
};
1818
var illegalIriChars = /[\x00-\x20<>\\"\{\}\|\^\`]/;
1919

20+
var lineModeRegExps = {
21+
_iri: true,
22+
_unescapedIri: true,
23+
_unescapedQuote: true,
24+
_singleQuote: true,
25+
_langcode: true,
26+
_blank: true,
27+
_newline: true,
28+
_comment: true,
29+
_whitespace: true,
30+
_endOfFile: true,
31+
};
32+
var invalidRegExp = /$0^/;
33+
2034
// ## Constructor
2135
function N3Lexer(options) {
2236
if (!(this instanceof N3Lexer))
2337
return new N3Lexer(options);
2438
options = options || {};
2539

2640
// In line mode (N-Triples or N-Quads), only simple features may be parsed
27-
if (options.lineMode) {
41+
if (this._lineMode = !!options.lineMode) {
42+
this._n3Mode = false;
2843
// Don't tokenize special literals
29-
this._tripleApos = this._tripleQuote = this._number = this._boolean = /$0^/;
30-
// Swap the tokenize method for a restricted version
31-
var self = this;
32-
this._tokenize = this.tokenize;
33-
this.tokenize = function (input, callback) {
34-
this._tokenize(input, function (error, token) {
35-
if (!error && /^(?:IRI|blank|literal|langcode|typeIRI|\.|eof)$/.test(token.type))
36-
callback && callback(error, token);
37-
else
38-
callback && callback(error || self._syntaxError(token.type, callback = null));
39-
});
40-
};
44+
for (var key in this) {
45+
if (!(key in lineModeRegExps) && this[key] instanceof RegExp)
46+
this[key] = invalidRegExp;
47+
}
48+
}
49+
// When not in line mode, enable N3 functionality by default
50+
else {
51+
this._n3Mode = options.n3 !== false;
4152
}
42-
// Enable N3 functionality by default
43-
this._n3Mode = options.n3 !== false;
44-
// Disable comment tokens by default
53+
// Don't output comment tokens by default
4554
this._comments = !!options.comments;
4655
}
4756

@@ -302,9 +311,10 @@ N3Lexer.prototype = {
302311
case ')':
303312
case '{':
304313
case '}':
305-
// The next token is punctuation
306-
matchLength = 1;
307-
type = firstChar;
314+
if (!this._lineMode) {
315+
matchLength = 1;
316+
type = firstChar;
317+
}
308318
break;
309319

310320
default:

test/N3Parser-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ describe('N3Parser', function () {
11261126
shouldNotParse(parser, '<urn:a:a> <= <urn:b:b>.', 'Unexpected "<=" on line 1.'));
11271127

11281128
it('should not parse a formula as object',
1129-
shouldNotParse(parser, '<urn:a:a> <urn:b:b> {}.', 'Unexpected "{" on line 1.'));
1129+
shouldNotParse(parser, '<urn:a:a> <urn:b:b> {}.', 'Unexpected "{}." on line 1.'));
11301130

11311131
it('should not parse @forSome',
11321132
shouldNotParse(parser, '@forSome <x>.', 'Unexpected "@forSome" on line 1.'));
@@ -1165,7 +1165,7 @@ describe('N3Parser', function () {
11651165
shouldNotParse(parser, '<urn:a:a> <= <urn:b:b>.', 'Unexpected "<=" on line 1.'));
11661166

11671167
it('should not parse a formula as object',
1168-
shouldNotParse(parser, '<urn:a:a> <urn:b:b> {}.', 'Unexpected "{" on line 1.'));
1168+
shouldNotParse(parser, '<urn:a:a> <urn:b:b> {}.', 'Unexpected "{}." on line 1.'));
11691169

11701170
it('should not parse @forSome',
11711171
shouldNotParse(parser, '@forSome <x>.', 'Unexpected "@forSome" on line 1.'));

0 commit comments

Comments
 (0)