n3 2.0.0-beta.1 → 2.0.0-beta.2

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/lib/N3Parser.js CHANGED
@@ -45,6 +45,9 @@ class N3Parser {
45
45
  });
46
46
  // Disable explicit quantifiers by default
47
47
  this._explicitQuantifiers = !!options.explicitQuantifiers;
48
+ // Disable parsing of unsupported versions by default
49
+ this._parseUnsupportedVersions = !!options.parseUnsupportedVersions;
50
+ this._version = options.version;
48
51
  }
49
52
 
50
53
  // ## Static class methods
@@ -121,6 +124,12 @@ class N3Parser {
121
124
  }
122
125
  }
123
126
 
127
+ // ### `_readBeforeTopContext` is called once only at the start of parsing.
128
+ _readBeforeTopContext(token) {
129
+ if (this._version && !this._isValidVersion(this._version)) return this._error(`Detected unsupported version as media type parameter: "${this._version}"`, token);
130
+ return this._readInTopContext(token);
131
+ }
132
+
124
133
  // ### `_readInTopContext` reads a token when in the top context
125
134
  _readInTopContext(token) {
126
135
  switch (token.type) {
@@ -728,11 +737,17 @@ class N3Parser {
728
737
  return this._readDeclarationPunctuation;
729
738
  }
730
739
 
740
+ // ### `_isValidVersion` checks if the given version is valid for this parser to handle.
741
+ _isValidVersion(version) {
742
+ return this._parseUnsupportedVersions || N3Parser.SUPPORTED_VERSIONS.includes(version);
743
+ }
744
+
731
745
  // ### `_readVersion` reads version string declaration
732
746
  _readVersion(token) {
733
747
  if (token.type !== 'literal') return this._error('Expected literal to follow version declaration', token);
734
748
  if (token.end - token.start !== token.value.length + 2) return this._error('Version declarations must use single quotes', token);
735
- this._prefixCallback(token.value);
749
+ this._versionCallback(token.value);
750
+ if (!this._isValidVersion(token.value)) return this._error(`Detected unsupported version: "${token.value}"`, token);
736
751
  return this._readDeclarationPunctuation;
737
752
  }
738
753
 
@@ -1106,7 +1121,7 @@ class N3Parser {
1106
1121
  }
1107
1122
  // The read callback is the next function to be executed when a token arrives.
1108
1123
  // We start reading in the top context.
1109
- this._readCallback = this._readInTopContext;
1124
+ this._readCallback = this._readBeforeTopContext;
1110
1125
  this._sparqlStyle = false;
1111
1126
  this._prefixes = Object.create(null);
1112
1127
  this._prefixes._ = this._blankNodePrefix ? this._blankNodePrefix.substr(2) : `b${blankNodePrefix++}_`;
@@ -1173,4 +1188,5 @@ function initDataFactory(parser, factory) {
1173
1188
  };
1174
1189
  parser.QUANTIFIERS_GRAPH = factory.namedNode('urn:n3:quantifiers');
1175
1190
  }
1191
+ N3Parser.SUPPORTED_VERSIONS = ['1.2', '1.2-basic', '1.1'];
1176
1192
  initDataFactory(N3Parser.prototype, _N3DataFactory.default);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.2",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
package/src/N3Parser.js CHANGED
@@ -35,6 +35,9 @@ export default class N3Parser {
35
35
  this._lexer = options.lexer || new N3Lexer({ lineMode: isLineMode, n3: isN3 });
36
36
  // Disable explicit quantifiers by default
37
37
  this._explicitQuantifiers = !!options.explicitQuantifiers;
38
+ // Disable parsing of unsupported versions by default
39
+ this._parseUnsupportedVersions = !!options.parseUnsupportedVersions;
40
+ this._version = options.version;
38
41
  }
39
42
 
40
43
  // ## Static class methods
@@ -112,6 +115,13 @@ export default class N3Parser {
112
115
  }
113
116
  }
114
117
 
118
+ // ### `_readBeforeTopContext` is called once only at the start of parsing.
119
+ _readBeforeTopContext(token) {
120
+ if (this._version && !this._isValidVersion(this._version))
121
+ return this._error(`Detected unsupported version as media type parameter: "${this._version}"`, token);
122
+ return this._readInTopContext(token);
123
+ }
124
+
115
125
  // ### `_readInTopContext` reads a token when in the top context
116
126
  _readInTopContext(token) {
117
127
  switch (token.type) {
@@ -782,13 +792,20 @@ export default class N3Parser {
782
792
  return this._readDeclarationPunctuation;
783
793
  }
784
794
 
795
+ // ### `_isValidVersion` checks if the given version is valid for this parser to handle.
796
+ _isValidVersion(version) {
797
+ return this._parseUnsupportedVersions || N3Parser.SUPPORTED_VERSIONS.includes(version);
798
+ }
799
+
785
800
  // ### `_readVersion` reads version string declaration
786
801
  _readVersion(token) {
787
802
  if (token.type !== 'literal')
788
803
  return this._error('Expected literal to follow version declaration', token);
789
804
  if ((token.end - token.start) !== token.value.length + 2)
790
805
  return this._error('Version declarations must use single quotes', token);
791
- this._prefixCallback(token.value);
806
+ this._versionCallback(token.value);
807
+ if (!this._isValidVersion(token.value))
808
+ return this._error(`Detected unsupported version: "${token.value}"`, token);
792
809
  return this._readDeclarationPunctuation;
793
810
  }
794
811
 
@@ -1181,7 +1198,7 @@ export default class N3Parser {
1181
1198
  }
1182
1199
  // The read callback is the next function to be executed when a token arrives.
1183
1200
  // We start reading in the top context.
1184
- this._readCallback = this._readInTopContext;
1201
+ this._readCallback = this._readBeforeTopContext;
1185
1202
  this._sparqlStyle = false;
1186
1203
  this._prefixes = Object.create(null);
1187
1204
  this._prefixes._ = this._blankNodePrefix ? this._blankNodePrefix.substr(2)
@@ -1256,4 +1273,9 @@ function initDataFactory(parser, factory) {
1256
1273
  };
1257
1274
  parser.QUANTIFIERS_GRAPH = factory.namedNode('urn:n3:quantifiers');
1258
1275
  }
1276
+ N3Parser.SUPPORTED_VERSIONS = [
1277
+ '1.2',
1278
+ '1.2-basic',
1279
+ '1.1',
1280
+ ];
1259
1281
  initDataFactory(N3Parser.prototype, N3DataFactory);