| 1 |
import { Lexer } from '../src/'; |
| 2 |
import queueMicrotask from 'queue-microtask'; |
| 3 |
|
| 4 |
import { EventEmitter } from 'events'; |
| 5 |
|
| 6 |
describe('Lexer', () => { |
| 7 |
describe('The Lexer export', () => { |
| 8 |
it('should be a function', () => { |
| 9 |
Lexer.should.be.a('function'); |
| 10 |
}); |
| 11 |
|
| 12 |
it('should be an Lexer constructor', () => { |
| 13 |
new Lexer().should.be.an.instanceof(Lexer); |
| 14 |
}); |
| 15 |
}); |
| 16 |
|
| 17 |
describe('A Lexer instance', () => { |
| 18 |
it('should tokenize the empty string', |
| 19 |
shouldTokenize('', |
| 20 |
{ type: 'eof', line: 1 })); |
| 21 |
|
| 22 |
|
| 23 |
it('should tokenize byte order mark at beginning as empty string', |
| 24 |
shouldTokenize('\ufeff', { type: 'eof', line: 1 })); |
| 25 |
|
| 26 |
it('should tokenize a whitespace string', |
| 27 |
shouldTokenize(' \t \n ', |
| 28 |
{ type: 'eof', line: 2 })); |
| 29 |
|
| 30 |
it('should tokenize an IRI', |
| 31 |
shouldTokenize('<http://ex.org/?bla#foo>', |
| 32 |
{ type: 'IRI', value: 'http://ex.org/?bla#foo', line: 1 }, |
| 33 |
{ type: 'eof', line: 1 })); |
| 34 |
|
| 35 |
it('should tokenize a split IRI', |
| 36 |
shouldTokenize(streamOf('<', 'http://ex.org/?bla#foo>'), |
| 37 |
{ type: 'IRI', value: 'http://ex.org/?bla#foo', line: 1 }, |
| 38 |
{ type: 'eof', line: 1 })); |
| 39 |
|
| 40 |
it('should not tokenize an IRI with disallowed characters', |
| 41 |
shouldNotTokenize('<http://ex.org/bla"foo>', |
| 42 |
'Unexpected "<http://ex.org/bla"foo>" on line 1.')); |
| 43 |
|
| 44 |
it('should not tokenize an IRI with escaped characters', |
| 45 |
shouldNotTokenize('<http://ex.org/bla\\"foo>', |
| 46 |
'Unexpected "<http://ex.org/bla\\"foo>" on line 1.')); |
| 47 |
|
| 48 |
it('should not tokenize an IRI with disallowed escaped characters', |
| 49 |
shouldNotTokenize('<http://ex.org/bla\\u0020foo>', |
| 50 |
'Unexpected "<http://ex.org/bla\\u0020foo>" on line 1.')); |
| 51 |
|
| 52 |
it('should not tokenize an IRI with invalid 4-digit unicode escapes', |
| 53 |
shouldNotTokenize('<http://ex.org/bla\\uXYZZfoo>', |
| 54 |
'Unexpected "<http://ex.org/bla\\uXYZZfoo>" on line 1.')); |
| 55 |
|
| 56 |
it('should not tokenize an IRI with invalid 8-digit unicode escapes', |
| 57 |
shouldNotTokenize('<http://ex.org/bla\\uXYZZxyzzfoo>', |
| 58 |
'Unexpected "<http://ex.org/bla\\uXYZZxyzzfoo>" on line 1.')); |
| 59 |
|
| 60 |
it('should not tokenize an IRI with a non-numeric 4-digit unicode escapes', done => { |
| 61 |
const stream = new EventEmitter(), lexer = new Lexer(); |
| 62 |
lexer.tokenize(stream, (error, token) => { |
| 63 |
error.should.be.an.instanceof(Error); |
| 64 |
error.message.should.equal('Unexpected "<\\uz234>" on line 1.'); |
| 65 |
done(token); |
| 66 |
}); |
| 67 |
stream.emit('data', '<\\uz234>'); |
| 68 |
}); |
| 69 |
|
| 70 |
it('should not tokenize an IRI with a non-numeric 8-digit unicode escapes', done => { |
| 71 |
const stream = new EventEmitter(), lexer = new Lexer(); |
| 72 |
lexer.tokenize(stream, (error, token) => { |
| 73 |
error.should.be.an.instanceof(Error); |
| 74 |
error.message.should.equal('Unexpected "<\\Uz2345678>" on line 1.'); |
| 75 |
done(token); |
| 76 |
}); |
| 77 |
stream.emit('data', '<\\Uz2345678>'); |
| 78 |
}); |
| 79 |
|
| 80 |
it('should tokenize an IRI with four-digit unicode escapes', |
| 81 |
shouldTokenize('<http://a.example/\\u0073>', |
| 82 |
{ type: 'IRI', value: 'http://a.example/s', line: 1 }, |
| 83 |
{ type: 'eof', line: 1 })); |
| 84 |
|
| 85 |
it('should tokenize an IRI with eight-digit unicode escapes', |
| 86 |
shouldTokenize('<http://a.example/\\U00000073\\U00A00073>', |
| 87 |
{ type: 'IRI', value: 'http://a.example/s\uffc0\udc73', line: 1 }, |
| 88 |
{ type: 'eof', line: 1 })); |
| 89 |
|
| 90 |
it('should not decode an IRI', |
| 91 |
shouldTokenize('<http://a.example/%66oo-bar>', |
| 92 |
{ type: 'IRI', value: 'http://a.example/%66oo-bar', line: 1 }, |
| 93 |
{ type: 'eof', line: 1 })); |
| 94 |
|
| 95 |
it('should tokenize two IRIs separated by whitespace', |
| 96 |
shouldTokenize(' \n\t<http://ex.org/?bla#foo> \n\t<http://ex.org/?bla#bar> \n\t', |
| 97 |
{ type: 'IRI', value: 'http://ex.org/?bla#foo', line: 2 }, |
| 98 |
{ type: 'IRI', value: 'http://ex.org/?bla#bar', line: 3 }, |
| 99 |
{ type: 'eof', line: 4 })); |
| 100 |
|
| 101 |
it('should tokenize a statement with IRIs', |
| 102 |
shouldTokenize(' \n\t<http://ex.org/?bla#foo> \n\t<http://ex.org/?bla#bar> \n\t<http://ex.org/?bla#boo> .', |
| 103 |
{ type: 'IRI', value: 'http://ex.org/?bla#foo', line: 2 }, |
| 104 |
{ type: 'IRI', value: 'http://ex.org/?bla#bar', line: 3 }, |
| 105 |
{ type: 'IRI', value: 'http://ex.org/?bla#boo', line: 4 }, |
| 106 |
{ type: '.', line: 4 }, |
| 107 |
{ type: 'eof', line: 4 })); |
| 108 |
|
| 109 |
it('should tokenize prefixed names', |
| 110 |
shouldTokenize(':a b:c d-dd:e-ee.', |
| 111 |
{ type: 'prefixed', prefix: '', value: 'a', line: 1 }, |
| 112 |
{ type: 'prefixed', prefix: 'b', value: 'c', line: 1 }, |
| 113 |
{ type: 'prefixed', prefix: 'd-dd', value: 'e-ee', line: 1 }, |
| 114 |
{ type: '.', line: 1 }, |
| 115 |
{ type: 'eof', line: 1 })); |
| 116 |
|
| 117 |
it('should tokenize prefixed names with leading digits', |
| 118 |
shouldTokenize('leg:3032571 isbn13:9780136019701 ', |
| 119 |
{ type: 'prefixed', prefix: 'leg', value: '3032571', line: 1 }, |
| 120 |
{ type: 'prefixed', prefix: 'isbn13', value: '9780136019701', line: 1 }, |
| 121 |
{ type: 'eof', line: 1 })); |
| 122 |
|
| 123 |
it('should tokenize prefixed names starting with true', |
| 124 |
shouldTokenize('true:a truer:b ', |
| 125 |
{ type: 'prefixed', prefix: 'true', value: 'a', line: 1 }, |
| 126 |
{ type: 'prefixed', prefix: 'truer', value: 'b', line: 1 }, |
| 127 |
{ type: 'eof', line: 1 })); |
| 128 |
|
| 129 |
it('should tokenize prefixed names starting with false', |
| 130 |
shouldTokenize('false:a falser:b ', |
| 131 |
{ type: 'prefixed', prefix: 'false', value: 'a', line: 1 }, |
| 132 |
{ type: 'prefixed', prefix: 'falser', value: 'b', line: 1 }, |
| 133 |
{ type: 'eof', line: 1 })); |
| 134 |
|
| 135 |
it('should tokenize prefixed names with non-leading colons', |
| 136 |
shouldTokenize('og:video:height ', |
| 137 |
{ type: 'prefixed', prefix: 'og', value: 'video:height', line: 1 }, |
| 138 |
{ type: 'eof', line: 1 })); |
| 139 |
|
| 140 |
it('should tokenize prefixed names with reserved escape sequences', |
| 141 |
shouldTokenize('wgs:lat\\-long ', |
| 142 |
{ type: 'prefixed', prefix: 'wgs', value: 'lat-long', line: 1 }, |
| 143 |
{ type: 'eof', line: 1 })); |
| 144 |
|
| 145 |
it('should tokenize the colon prefixed name', |
| 146 |
shouldTokenize(': : :.', |
| 147 |
{ type: 'prefixed', prefix: '', value: '', line: 1 }, |
| 148 |
{ type: 'prefixed', prefix: '', value: '', line: 1 }, |
| 149 |
{ type: 'prefixed', prefix: '', value: '', line: 1 }, |
| 150 |
{ type: '.', line: 1 }, |
| 151 |
{ type: 'eof', line: 1 })); |
| 152 |
|
| 153 |
it('should tokenize a prefixed name with a dot, split in half while streaming', |
| 154 |
shouldTokenize(streamOf('dbpedia:Anthony_J._Batt', 'aglia '), |
| 155 |
{ type: 'prefixed', prefix: 'dbpedia', value: 'Anthony_J._Battaglia', line: 1 }, |
| 156 |
{ type: 'eof', line: 1 })); |
| 157 |
|
| 158 |
it('should tokenize a prefixed name with a dot, split after the dot while streaming', |
| 159 |
shouldTokenize(streamOf('dbpedia:Anthony_J.', '_Battaglia '), |
| 160 |
{ type: 'prefixed', prefix: 'dbpedia', value: 'Anthony_J._Battaglia', line: 1 }, |
| 161 |
{ type: 'eof', line: 1 })); |
| 162 |
|
| 163 |
it('should tokenize a blank node with a dot, split in half while streaming', |
| 164 |
shouldTokenize(streamOf('_:Anthony_J._Batt', 'aglia '), |
| 165 |
{ type: 'blank', prefix: '_', value: 'Anthony_J._Battaglia', line: 1 }, |
| 166 |
{ type: 'eof', line: 1 })); |
| 167 |
|
| 168 |
it('should tokenize a blank node with a dot, split after the dot while streaming', |
| 169 |
shouldTokenize(streamOf('_:Anthony_J.', '_Battaglia '), |
| 170 |
{ type: 'blank', prefix: '_', value: 'Anthony_J._Battaglia', line: 1 }, |
| 171 |
{ type: 'eof', line: 1 })); |
| 172 |
|
| 173 |
it('should not decode a prefixed name', |
| 174 |
shouldTokenize('ex:%66oo-bar ', |
| 175 |
{ type: 'prefixed', prefix: 'ex', value: '%66oo-bar', line: 1 }, |
| 176 |
{ type: 'eof', line: 1 })); |
| 177 |
|
| 178 |
it('should not tokenize a prefixed name with disallowed characters', |
| 179 |
shouldNotTokenize('ex:bla"foo', |
| 180 |
'Unexpected ""foo" on line 1.')); |
| 181 |
|
| 182 |
it('should not tokenize a prefixed name with escaped characters', |
| 183 |
shouldNotTokenize('ex:bla\\"foo', |
| 184 |
'Unexpected "ex:bla\\"foo" on line 1.')); |
| 185 |
|
| 186 |
it('should not tokenize a prefixed name with disallowed escaped characters', |
| 187 |
shouldNotTokenize('ex:bla\\u0020foo', |
| 188 |
'Unexpected "ex:bla\\u0020foo" on line 1.')); |
| 189 |
|
| 190 |
it('should not tokenize a prefixed name with invalid 4-digit unicode escapes', |
| 191 |
shouldNotTokenize('ex:bla\\uXYZZfoo', |
| 192 |
'Unexpected "ex:bla\\uXYZZfoo" on line 1.')); |
| 193 |
|
| 194 |
it('should not tokenize a prefixed name with invalid 8-digit unicode escapes', |
| 195 |
shouldNotTokenize('ex:bla\\uXYZZxyzzfoo', |
| 196 |
'Unexpected "ex:bla\\uXYZZxyzzfoo" on line 1.')); |
| 197 |
|
| 198 |
it('should not tokenize a prefixed name with four-digit unicode escapes', |
| 199 |
shouldNotTokenize('ex:foo\\u0073bar', |
| 200 |
'Unexpected "ex:foo\\u0073bar" on line 1.')); |
| 201 |
|
| 202 |
it('should not tokenize a prefixed name with eight-digit unicode escapes', |
| 203 |
shouldNotTokenize('ex:foo\\U00000073\\U00A00073bar', |
| 204 |
'Unexpected "ex:foo\\U00000073\\U00A00073bar" on line 1.')); |
| 205 |
|
| 206 |
it('should tokenize two prefixed names separated by whitespace', |
| 207 |
shouldTokenize(' \n\tex:foo \n\tex:bar \n\t', |
| 208 |
{ type: 'prefixed', prefix: 'ex', value: 'foo', line: 2 }, |
| 209 |
{ type: 'prefixed', prefix: 'ex', value: 'bar', line: 3 }, |
| 210 |
{ type: 'eof', line: 4 })); |
| 211 |
|
| 212 |
it('should tokenize a statement with prefixed names', |
| 213 |
shouldTokenize(' \n\tex:foo \n\tex:bar \n\tex:baz .', |
| 214 |
{ type: 'prefixed', prefix: 'ex', value: 'foo', line: 2 }, |
| 215 |
{ type: 'prefixed', prefix: 'ex', value: 'bar', line: 3 }, |
| 216 |
{ type: 'prefixed', prefix: 'ex', value: 'baz', line: 4 }, |
| 217 |
{ type: '.', line: 4 }, |
| 218 |
{ type: 'eof', line: 4 })); |
| 219 |
|
| 220 |
it('should correctly recognize different types of newlines', |
| 221 |
shouldTokenize('<a>\r<b>\n<c>\r\n.', |
| 222 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 223 |
{ type: 'IRI', value: 'b', line: 2 }, |
| 224 |
{ type: 'IRI', value: 'c', line: 3 }, |
| 225 |
{ type: '.', line: 4 }, |
| 226 |
{ type: 'eof', line: 4 })); |
| 227 |
|
| 228 |
it('should tokenize a single comment', |
| 229 |
shouldTokenize('#mycomment', |
| 230 |
{ type: 'eof', line: 1 })); |
| 231 |
|
| 232 |
it('should tokenize a stream with split comment', |
| 233 |
shouldTokenize(streamOf('#mycom', 'ment'), |
| 234 |
{ type: 'eof', line: 1 })); |
| 235 |
|
| 236 |
it('should ignore comments', |
| 237 |
shouldTokenize('<#foo> #mycomment\n <#foo> #mycomment \r# mycomment\n\n<#bla>#', |
| 238 |
{ type: 'IRI', value: '#foo', line: 1 }, |
| 239 |
{ type: 'IRI', value: '#foo', line: 2 }, |
| 240 |
{ type: 'IRI', value: '#bla', line: 5 }, |
| 241 |
{ type: 'eof', line: 5 })); |
| 242 |
|
| 243 |
it('should tokenize comments after abbreviations and local names', |
| 244 |
shouldTokenize('[a#\na:a#\n].', |
| 245 |
{ type: '[', line: 1 }, |
| 246 |
{ type: 'abbreviation', value: 'a', line: 1 }, |
| 247 |
{ type: 'prefixed', prefix: 'a', value: 'a', line: 2 }, |
| 248 |
{ type: ']', line: 3 }, |
| 249 |
{ type: '.', line: 3 }, |
| 250 |
{ type: 'eof', line: 3 })); |
| 251 |
|
| 252 |
it('should tokenize a quoted string literal', |
| 253 |
shouldTokenize('"string" ', |
| 254 |
{ type: 'literal', value: 'string', line: 1 }, |
| 255 |
{ type: 'eof', line: 1 })); |
| 256 |
|
| 257 |
it('should not tokenize a quoted string literal with a newline', |
| 258 |
shouldNotTokenize('"abc\ndef" ', |
| 259 |
'Unexpected ""abc" on line 1.')); |
| 260 |
|
| 261 |
it('should not tokenize a quoted string literal with a carriage return', |
| 262 |
shouldNotTokenize('"abc\rdef" ', |
| 263 |
'Unexpected ""abc" on line 1.')); |
| 264 |
|
| 265 |
it('should tokenize a triple quoted string literal', |
| 266 |
shouldTokenize('"""string"""', |
| 267 |
{ type: 'literal', value: 'string', line: 1 }, |
| 268 |
{ type: 'eof', line: 1 })); |
| 269 |
|
| 270 |
it('should tokenize a triple quoted string literal with quoted newlines inside', |
| 271 |
shouldTokenize('"""st"r\r\ni""\n\rng"""', |
| 272 |
{ type: 'literal', value: 'st"r\r\ni""\n\rng', line: 1 }, |
| 273 |
{ type: 'eof', line: 4 })); |
| 274 |
|
| 275 |
it('should tokenize a string with escape characters', |
| 276 |
shouldTokenize('"\\\\ \\\' \\" \\n \\r \\t \\ua1b2" \n """\\\\ \\\' \\" \\n \\r \\t \\U0000a1b2"""', |
| 277 |
{ type: 'literal', value: '\\ \' " \n \r \t \ua1b2', line: 1 }, |
| 278 |
{ type: 'literal', value: '\\ \' " \n \r \t \ua1b2', line: 2 }, |
| 279 |
{ type: 'eof', line: 2 })); |
| 280 |
|
| 281 |
it('should not tokenize a string with invalid characters', |
| 282 |
shouldNotTokenize('"\\uXYZX" ', |
| 283 |
'Unexpected ""\\uXYZX"" on line 1.')); |
| 284 |
|
| 285 |
it('should not tokenize a double-quoted string ending with an escaped quote', |
| 286 |
shouldNotTokenize('"abc\\"', |
| 287 |
'Unexpected ""abc\\"" on line 1.')); |
| 288 |
|
| 289 |
it('should not tokenize a single-quoted string ending with an escaped quote', |
| 290 |
shouldNotTokenize("'abc\\'", |
| 291 |
'Unexpected "\'abc\\\'" on line 1.')); |
| 292 |
|
| 293 |
it('should not tokenize a double-quoted string ending with a backslash and escaped quote', |
| 294 |
shouldNotTokenize('"abc\\\\\\"', |
| 295 |
'Unexpected ""abc\\\\\\"" on line 1.')); |
| 296 |
|
| 297 |
it('should not tokenize a single-quoted string ending with a backslash and escaped quote', |
| 298 |
shouldNotTokenize("'abc\\\\\\'", |
| 299 |
'Unexpected "\'abc\\\\\\\'" on line 1.')); |
| 300 |
|
| 301 |
it('should tokenize a double-quoted string ending in backslashes', |
| 302 |
shouldTokenize('"abc\\\\" "abc\\\\\\\\"', |
| 303 |
{ type: 'literal', value: 'abc\\', line: 1 }, |
| 304 |
{ type: 'literal', value: 'abc\\\\', line: 1 }, |
| 305 |
{ type: 'eof', line: 1 })); |
| 306 |
|
| 307 |
it('should tokenize a single-quoted string ending in backslashes', |
| 308 |
shouldTokenize("'abc\\\\' 'abc\\\\\\\\'", |
| 309 |
{ type: 'literal', value: 'abc\\', line: 1 }, |
| 310 |
{ type: 'literal', value: 'abc\\\\', line: 1 }, |
| 311 |
{ type: 'eof', line: 1 })); |
| 312 |
|
| 313 |
it('should not tokenize a triple-quoted string with invalid characters', |
| 314 |
shouldNotTokenize("'''\\uXYZX''' ", |
| 315 |
"Unexpected \"'''\\uXYZX'''\" on line 1.")); |
| 316 |
|
| 317 |
it('should tokenize a quoted string literal with language code', |
| 318 |
shouldTokenize('"string"@en "string"@nl-be "string"@EN ', |
| 319 |
{ type: 'literal', value: 'string', line: 1 }, |
| 320 |
{ type: 'langcode', value: 'en', line: 1 }, |
| 321 |
{ type: 'literal', value: 'string', line: 1 }, |
| 322 |
{ type: 'langcode', value: 'nl-be', line: 1 }, |
| 323 |
{ type: 'literal', value: 'string', line: 1 }, |
| 324 |
{ type: 'langcode', value: 'EN', line: 1 }, |
| 325 |
{ type: 'eof', line: 1 })); |
| 326 |
|
| 327 |
it('should tokenize a quoted string literal with type', |
| 328 |
shouldTokenize('"stringA"^^<type> "stringB"^^ns:mytype ', |
| 329 |
{ type: 'literal', value: 'stringA', line: 1 }, |
| 330 |
{ type: 'typeIRI', value: 'type', line: 1 }, |
| 331 |
{ type: 'literal', value: 'stringB', line: 1 }, |
| 332 |
{ type: 'type', value: 'mytype', prefix: 'ns', line: 1 }, |
| 333 |
{ type: 'eof', line: 1 })); |
| 334 |
|
| 335 |
it('should not tokenize a single hat', |
| 336 |
shouldNotTokenize('^', |
| 337 |
'Unexpected "^" on line 1.')); |
| 338 |
|
| 339 |
it('should not tokenize a double hat followed by a non-IRI', |
| 340 |
shouldNotTokenize('^^1', |
| 341 |
'Unexpected "1" on line 1.')); |
| 342 |
|
| 343 |
it('should tokenize a single-quoted string literal', |
| 344 |
shouldTokenize("'string' ", |
| 345 |
{ type: 'literal', value: 'string', line: 1 }, |
| 346 |
{ type: 'eof', line: 1 })); |
| 347 |
|
| 348 |
it('should tokenize a triple single-quoted string literal', |
| 349 |
shouldTokenize("'''string'''", |
| 350 |
{ type: 'literal', value: 'string', line: 1 }, |
| 351 |
{ type: 'eof', line: 1 })); |
| 352 |
|
| 353 |
it('should not tokenize a single-quoted string literal with a newline', |
| 354 |
shouldNotTokenize("'abc\ndef' ", |
| 355 |
'Unexpected "\'abc" on line 1.')); |
| 356 |
|
| 357 |
it('should not tokenize a single-quoted string literal with a carriage return', |
| 358 |
shouldNotTokenize("'abc\rdef' ", |
| 359 |
'Unexpected "\'abc" on line 1.')); |
| 360 |
|
| 361 |
it('should tokenize a triple single-quoted string literal with quotes newlines inside', |
| 362 |
shouldTokenize("'''st'r\ni''ng'''", |
| 363 |
{ type: 'literal', value: 'st\'r\ni\'\'ng', line: 1 }, |
| 364 |
{ type: 'eof', line: 2 })); |
| 365 |
|
| 366 |
it('should tokenize a single-quoted string with escape characters', |
| 367 |
shouldTokenize("'\\\\ \\\" \\' \\n \\r \\t \\ua1b2' \n '''\\\\ \\\" \\' \\n \\r \\t \\U0020a1b2'''", |
| 368 |
{ type: 'literal', value: '\\ " \' \n \r \t \ua1b2', line: 1 }, |
| 369 |
{ type: 'literal', value: '\\ " \' \n \r \t \udfe8\uddb2', line: 2 }, |
| 370 |
{ type: 'eof', line: 2 })); |
| 371 |
|
| 372 |
it('should tokenize a single-quoted string literal with language code', |
| 373 |
shouldTokenize("'string'@en 'string'@nl-be 'string'@EN ", |
| 374 |
{ type: 'literal', value: 'string', line: 1 }, |
| 375 |
{ type: 'langcode', value: 'en', line: 1 }, |
| 376 |
{ type: 'literal', value: 'string', line: 1 }, |
| 377 |
{ type: 'langcode', value: 'nl-be', line: 1 }, |
| 378 |
{ type: 'literal', value: 'string', line: 1 }, |
| 379 |
{ type: 'langcode', value: 'EN', line: 1 }, |
| 380 |
{ type: 'eof', line: 1 })); |
| 381 |
|
| 382 |
it('should tokenize a single-quoted string literal with type', |
| 383 |
shouldTokenize("'stringA'^^<type> 'stringB'^^ns:mytype ", |
| 384 |
{ type: 'literal', value: 'stringA', line: 1 }, |
| 385 |
{ type: 'typeIRI', value: 'type', line: 1 }, |
| 386 |
{ type: 'literal', value: 'stringB', line: 1 }, |
| 387 |
{ type: 'type', value: 'mytype', prefix: 'ns', line: 1 }, |
| 388 |
{ type: 'eof', line: 1 })); |
| 389 |
|
| 390 |
it('should tokenize an integer literal', |
| 391 |
shouldTokenize('10, +20. -30, 40. ', |
| 392 |
{ type: 'literal', value: '10', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 393 |
{ type: ',', line: 1 }, |
| 394 |
{ type: 'literal', value: '+20', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 395 |
{ type: '.', line: 1 }, |
| 396 |
{ type: 'literal', value: '-30', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 397 |
{ type: ',', line: 1 }, |
| 398 |
{ type: 'literal', value: '40', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 399 |
{ type: '.', line: 1 }, |
| 400 |
{ type: 'eof', line: 1 })); |
| 401 |
|
| 402 |
it('should tokenize a decimal literal', |
| 403 |
shouldTokenize('1. 2.0, .3. -0.4, -.5. ', |
| 404 |
{ type: 'literal', value: '1', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 405 |
{ type: '.', line: 1 }, |
| 406 |
{ type: 'literal', value: '2.0', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 407 |
{ type: ',', line: 1 }, |
| 408 |
{ type: 'literal', value: '.3', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 409 |
{ type: '.', line: 1 }, |
| 410 |
{ type: 'literal', value: '-0.4', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 411 |
{ type: ',', line: 1 }, |
| 412 |
{ type: 'literal', value: '-.5', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 413 |
{ type: '.', line: 1 }, |
| 414 |
{ type: 'eof', line: 1 })); |
| 415 |
|
| 416 |
it('should tokenize a double literal', |
| 417 |
shouldTokenize('10e20, +30.40E+50. -60.70e-80. .171e-11.', |
| 418 |
{ type: 'literal', value: '10e20', prefix: 'http://www.w3.org/2001/XMLSchema#double', line: 1 }, |
| 419 |
{ type: ',', line: 1 }, |
| 420 |
{ type: 'literal', value: '+30.40E+50', prefix: 'http://www.w3.org/2001/XMLSchema#double', line: 1 }, |
| 421 |
{ type: '.', line: 1 }, |
| 422 |
{ type: 'literal', value: '-60.70e-80', prefix: 'http://www.w3.org/2001/XMLSchema#double', line: 1 }, |
| 423 |
{ type: '.', line: 1 }, |
| 424 |
{ type: 'literal', value: '.171e-11', prefix: 'http://www.w3.org/2001/XMLSchema#double', line: 1 }, |
| 425 |
{ type: '.', line: 1 }, |
| 426 |
{ type: 'eof', line: 1 })); |
| 427 |
|
| 428 |
it('should not tokenize an invalid number', |
| 429 |
shouldNotTokenize('10-10 ', |
| 430 |
'Unexpected "10-10" on line 1.')); |
| 431 |
|
| 432 |
it('should tokenize booleans', |
| 433 |
shouldTokenize('true false ', |
| 434 |
{ type: 'literal', value: 'true', prefix: 'http://www.w3.org/2001/XMLSchema#boolean', line: 1 }, |
| 435 |
{ type: 'literal', value: 'false', prefix: 'http://www.w3.org/2001/XMLSchema#boolean', line: 1 }, |
| 436 |
{ type: 'eof', line: 1 })); |
| 437 |
|
| 438 |
it('should tokenize statements with shared subjects', |
| 439 |
shouldTokenize('<a> <b> <c>;\n<d> <e>.', |
| 440 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 441 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 442 |
{ type: 'IRI', value: 'c', line: 1 }, |
| 443 |
{ type: ';', line: 1 }, |
| 444 |
{ type: 'IRI', value: 'd', line: 2 }, |
| 445 |
{ type: 'IRI', value: 'e', line: 2 }, |
| 446 |
{ type: '.', line: 2 }, |
| 447 |
{ type: 'eof', line: 2 })); |
| 448 |
|
| 449 |
it('should tokenize statements with shared subjects and predicates', |
| 450 |
shouldTokenize('<a> <b> <c>,\n<d>.', |
| 451 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 452 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 453 |
{ type: 'IRI', value: 'c', line: 1 }, |
| 454 |
{ type: ',', line: 1 }, |
| 455 |
{ type: 'IRI', value: 'd', line: 2 }, |
| 456 |
{ type: '.', line: 2 }, |
| 457 |
{ type: 'eof', line: 2 })); |
| 458 |
|
| 459 |
it('should tokenize statements with shared subjects and predicates and prefixed names', |
| 460 |
shouldTokenize('a:a b:b c:c;d:d e:e,f:f.', |
| 461 |
{ type: 'prefixed', prefix: 'a', value: 'a', line: 1 }, |
| 462 |
{ type: 'prefixed', prefix: 'b', value: 'b', line: 1 }, |
| 463 |
{ type: 'prefixed', prefix: 'c', value: 'c', line: 1 }, |
| 464 |
{ type: ';', line: 1 }, |
| 465 |
{ type: 'prefixed', prefix: 'd', value: 'd', line: 1 }, |
| 466 |
{ type: 'prefixed', prefix: 'e', value: 'e', line: 1 }, |
| 467 |
{ type: ',', line: 1 }, |
| 468 |
{ type: 'prefixed', prefix: 'f', value: 'f', line: 1 }, |
| 469 |
{ type: '.', line: 1 }, |
| 470 |
{ type: 'eof', line: 1 })); |
| 471 |
|
| 472 |
it('should tokenize a stream', |
| 473 |
shouldTokenize(streamOf('<a>\n<b', '> ', '"""', 'c\n', '"""', '.', |
| 474 |
'<d> <e', '> ', '""', '.', |
| 475 |
'<g> <h> "i"', '@e', 'n.'), |
| 476 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 477 |
{ type: 'IRI', value: 'b', line: 2 }, |
| 478 |
{ type: 'literal', value: 'c\n', line: 2 }, |
| 479 |
{ type: '.', line: 3 }, |
| 480 |
{ type: 'IRI', value: 'd', line: 3 }, |
| 481 |
{ type: 'IRI', value: 'e', line: 3 }, |
| 482 |
{ type: 'literal', value: '', line: 3 }, |
| 483 |
{ type: '.', line: 3 }, |
| 484 |
{ type: 'IRI', value: 'g', line: 3 }, |
| 485 |
{ type: 'IRI', value: 'h', line: 3 }, |
| 486 |
{ type: 'literal', value: 'i', line: 3 }, |
| 487 |
{ type: 'langcode', value: 'en', line: 3 }, |
| 488 |
{ type: '.', line: 3 }, |
| 489 |
{ type: 'eof', line: 3 })); |
| 490 |
|
| 491 |
it('should tokenize a stream ending with a digit and a dot', |
| 492 |
shouldTokenize(streamOf('1', '.'), |
| 493 |
{ type: 'literal', value: '1', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 494 |
{ type: '.', line: 1 }, |
| 495 |
{ type: 'eof', line: 1 })); |
| 496 |
|
| 497 |
it('should tokenize a stream containing a decimal without leading digit', |
| 498 |
shouldTokenize(streamOf('.', '1 '), |
| 499 |
{ type: 'literal', value: '.1', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 500 |
{ type: 'eof', line: 1 })); |
| 501 |
|
| 502 |
it('should tokenize a stream containing a decimal with leading digit', |
| 503 |
shouldTokenize(streamOf('1.', '1 '), |
| 504 |
{ type: 'literal', value: '1.1', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 505 |
{ type: 'eof', line: 1 })); |
| 506 |
|
| 507 |
it('should immediately signal an error if a linebreak occurs anywhere outside a triple-quoted literal', |
| 508 |
shouldNotTokenize(streamOf('abc\n', null), 'Unexpected "abc" on line 1.')); |
| 509 |
|
| 510 |
it('should immediately signal an error if a linebreak occurs inside a single-quoted literal', |
| 511 |
shouldNotTokenize(streamOf('"abc\n', null), 'Unexpected ""abc" on line 1.')); |
| 512 |
|
| 513 |
it('should immediately signal an error if a carriage return occurs anywhere outside a triple-quoted literal', |
| 514 |
shouldNotTokenize(streamOf('abc\r', null), 'Unexpected "abc" on line 1.')); |
| 515 |
|
| 516 |
it('should immediately signal an error if a carriage return occurs inside a single-quoted literal', |
| 517 |
shouldNotTokenize(streamOf('"abc\r', null), 'Unexpected ""abc" on line 1.')); |
| 518 |
|
| 519 |
it('should tokenize a split single-quoted string', |
| 520 |
shouldTokenize(streamOf("'abc", "def' "), |
| 521 |
{ type: 'literal', value: 'abcdef', line: 1 }, |
| 522 |
{ type: 'eof', line: 1 })); |
| 523 |
|
| 524 |
it('should tokenize a split single-quoted string followed by a triple-quoted string', |
| 525 |
// Checks whether _literalClosingPos is properly reset |
| 526 |
shouldTokenize(streamOf("'abcdef", "' '''a'''"), |
| 527 |
{ type: 'literal', value: 'abcdef', line: 1 }, |
| 528 |
{ type: 'literal', value: 'a', line: 1 }, |
| 529 |
{ type: 'eof', line: 1 })); |
| 530 |
|
| 531 |
it('should tokenize split triple-quoted strings', |
| 532 |
shouldTokenize(streamOf('"', '""abc""" ', |
| 533 |
'""', '"def""" ', |
| 534 |
'"""', 'ghi""" ', |
| 535 |
'"""jkl', '""" ', |
| 536 |
'"""mno"', '"" ', |
| 537 |
'"""pqr""', '" '), |
| 538 |
{ type: 'literal', value: 'abc', line: 1 }, |
| 539 |
{ type: 'literal', value: 'def', line: 1 }, |
| 540 |
{ type: 'literal', value: 'ghi', line: 1 }, |
| 541 |
{ type: 'literal', value: 'jkl', line: 1 }, |
| 542 |
{ type: 'literal', value: 'mno', line: 1 }, |
| 543 |
{ type: 'literal', value: 'pqr', line: 1 }, |
| 544 |
{ type: 'eof', line: 1 })); |
| 545 |
|
| 546 |
it('should tokenize a triple-quoted string split after a newline', |
| 547 |
shouldTokenize(streamOf('"""abc\n', 'def"""'), |
| 548 |
{ type: 'literal', value: 'abc\ndef', line: 1 }, |
| 549 |
{ type: 'eof', line: 2 })); |
| 550 |
|
| 551 |
it('should tokenize @ keywords', |
| 552 |
shouldTokenize('@prefix @base @forSome @forAll ', |
| 553 |
{ type: '@prefix', line: 1 }, |
| 554 |
{ type: '@base', line: 1 }, |
| 555 |
{ type: '@forSome', line: 1 }, |
| 556 |
{ type: '@forAll', line: 1 }, |
| 557 |
{ type: 'eof', line: 1 })); |
| 558 |
|
| 559 |
it('should tokenize @prefix declarations', |
| 560 |
shouldTokenize('@prefix : <http://iri.org/#>.\n@prefix abc:<http://iri.org/#>.\n@prefix:<http://example/c/>.', |
| 561 |
{ type: '@prefix', line: 1 }, |
| 562 |
{ type: 'prefix', value: '', line: 1 }, |
| 563 |
{ type: 'IRI', value: 'http://iri.org/#', line: 1 }, |
| 564 |
{ type: '.', line: 1 }, |
| 565 |
{ type: '@prefix', line: 2 }, |
| 566 |
{ type: 'prefix', value: 'abc', line: 2 }, |
| 567 |
{ type: 'IRI', value: 'http://iri.org/#', line: 2 }, |
| 568 |
{ type: '.', line: 2 }, |
| 569 |
{ type: '@prefix', line: 3 }, |
| 570 |
{ type: 'prefix', value: '', line: 3 }, |
| 571 |
{ type: 'IRI', value: 'http://example/c/', line: 3 }, |
| 572 |
{ type: '.', line: 3 }, |
| 573 |
{ type: 'eof', line: 3 })); |
| 574 |
|
| 575 |
it('should not tokenize prefixes that end with a dot', |
| 576 |
shouldNotTokenize('@prefix abc.: <def>.', |
| 577 |
'Unexpected "abc.:" on line 1.')); |
| 578 |
|
| 579 |
it('should tokenize @base declarations', |
| 580 |
shouldTokenize('@base <http://iri.org/#>.\n@base <http://iri.org/#>.', |
| 581 |
{ type: '@base', line: 1 }, |
| 582 |
{ type: 'IRI', value: 'http://iri.org/#', line: 1 }, |
| 583 |
{ type: '.', line: 1 }, |
| 584 |
{ type: '@base', line: 2 }, |
| 585 |
{ type: 'IRI', value: 'http://iri.org/#', line: 2 }, |
| 586 |
{ type: '.', line: 2 }, |
| 587 |
{ type: 'eof', line: 2 })); |
| 588 |
|
| 589 |
it('should tokenize PREFIX declarations', |
| 590 |
shouldTokenize('PREFIX : <http://iri.org/#>\npreFiX abc: <http://iri.org/#>', |
| 591 |
{ type: 'PREFIX', line: 1 }, |
| 592 |
{ type: 'prefix', value: '', line: 1 }, |
| 593 |
{ type: 'IRI', value: 'http://iri.org/#', line: 1 }, |
| 594 |
{ type: 'PREFIX', line: 2 }, |
| 595 |
{ type: 'prefix', value: 'abc', line: 2 }, |
| 596 |
{ type: 'IRI', value: 'http://iri.org/#', line: 2 }, |
| 597 |
{ type: 'eof', line: 2 })); |
| 598 |
|
| 599 |
it('should tokenize BASE declarations', |
| 600 |
shouldTokenize('BASE <http://iri.org/#>\nbAsE <http://iri.org/#>', |
| 601 |
{ type: 'BASE', line: 1 }, |
| 602 |
{ type: 'IRI', value: 'http://iri.org/#', line: 1 }, |
| 603 |
{ type: 'BASE', line: 2 }, |
| 604 |
{ type: 'IRI', value: 'http://iri.org/#', line: 2 }, |
| 605 |
{ type: 'eof', line: 2 })); |
| 606 |
|
| 607 |
it('should tokenize blank nodes', |
| 608 |
shouldTokenize('[] [<a> <b>] [a:b "c"^^d:e][a:b[]] _:a:b.', |
| 609 |
{ type: '[', line: 1 }, |
| 610 |
{ type: ']', line: 1 }, |
| 611 |
{ type: '[', line: 1 }, |
| 612 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 613 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 614 |
{ type: ']', line: 1 }, |
| 615 |
{ type: '[', line: 1 }, |
| 616 |
{ type: 'prefixed', prefix: 'a', value: 'b', line: 1 }, |
| 617 |
{ type: 'literal', value: 'c', line: 1 }, |
| 618 |
{ type: 'type', prefix: 'd', value: 'e', line: 1 }, |
| 619 |
{ type: ']', line: 1 }, |
| 620 |
{ type: '[', line: 1 }, |
| 621 |
{ type: 'prefixed', prefix: 'a', value: 'b', line: 1 }, |
| 622 |
{ type: '[', line: 1 }, |
| 623 |
{ type: ']', line: 1 }, |
| 624 |
{ type: ']', line: 1 }, |
| 625 |
{ type: 'blank', prefix: '_', value: 'a', line: 1 }, |
| 626 |
{ type: 'prefixed', prefix: '', value: 'b', line: 1 }, |
| 627 |
{ type: '.', line: 1 }, |
| 628 |
{ type: 'eof', line: 1 })); |
| 629 |
|
| 630 |
it('should not tokenize invalid blank nodes', |
| 631 |
shouldNotTokenize('_::', |
| 632 |
'Unexpected "_::" on line 1.')); |
| 633 |
|
| 634 |
it('should tokenize lists', |
| 635 |
shouldTokenize('() (<a>) (<a> <b>)', |
| 636 |
{ type: '(', line: 1 }, |
| 637 |
{ type: ')', line: 1 }, |
| 638 |
{ type: '(', line: 1 }, |
| 639 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 640 |
{ type: ')', line: 1 }, |
| 641 |
{ type: '(', line: 1 }, |
| 642 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 643 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 644 |
{ type: ')', line: 1 }, |
| 645 |
{ type: 'eof', line: 1 })); |
| 646 |
|
| 647 |
it('should tokenize mixed lists', |
| 648 |
shouldTokenize('<a> <b> (1 "2" :o)(1()(1))', |
| 649 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 650 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 651 |
{ type: '(', line: 1 }, |
| 652 |
{ type: 'literal', value: '1', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 653 |
{ type: 'literal', value: '2', line: 1 }, |
| 654 |
{ type: 'prefixed', value: 'o', line: 1 }, |
| 655 |
{ type: ')', line: 1 }, |
| 656 |
{ type: '(', line: 1 }, |
| 657 |
{ type: 'literal', value: '1', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 658 |
{ type: '(', line: 1 }, |
| 659 |
{ type: ')', line: 1 }, |
| 660 |
{ type: '(', line: 1 }, |
| 661 |
{ type: 'literal', value: '1', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 662 |
{ type: ')', line: 1 }, |
| 663 |
{ type: ')', line: 1 }, |
| 664 |
{ type: 'eof', line: 1 })); |
| 665 |
|
| 666 |
it('should tokenize the "a" predicate', |
| 667 |
shouldTokenize('<x> a <y>.', |
| 668 |
{ type: 'IRI', value: 'x', line: 1 }, |
| 669 |
{ type: 'abbreviation', value: 'a', line: 1 }, |
| 670 |
{ type: 'IRI', value: 'y', line: 1 }, |
| 671 |
{ type: '.', line: 1 }, |
| 672 |
{ type: 'eof', line: 1 })); |
| 673 |
|
| 674 |
it('should tokenize the "a" predicate without spacing', |
| 675 |
shouldTokenize('[a<>].\n[a[]].\n[a()].\n<>a<>.\n<>a[].\n<>a().\n', |
| 676 |
{ type: '[', line: 1 }, |
| 677 |
{ type: 'abbreviation', value: 'a', line: 1 }, |
| 678 |
{ type: 'IRI', line: 1 }, |
| 679 |
{ type: ']', line: 1 }, |
| 680 |
{ type: '.', line: 1 }, |
| 681 |
{ type: '[', line: 2 }, |
| 682 |
{ type: 'abbreviation', value: 'a', line: 2 }, |
| 683 |
{ type: '[', line: 2 }, |
| 684 |
{ type: ']', line: 2 }, |
| 685 |
{ type: ']', line: 2 }, |
| 686 |
{ type: '.', line: 2 }, |
| 687 |
{ type: '[', line: 3 }, |
| 688 |
{ type: 'abbreviation', value: 'a', line: 3 }, |
| 689 |
{ type: '(', line: 3 }, |
| 690 |
{ type: ')', line: 3 }, |
| 691 |
{ type: ']', line: 3 }, |
| 692 |
{ type: '.', line: 3 }, |
| 693 |
{ type: 'IRI', line: 4 }, |
| 694 |
{ type: 'abbreviation', value: 'a', line: 4 }, |
| 695 |
{ type: 'IRI', line: 4 }, |
| 696 |
{ type: '.', line: 4 }, |
| 697 |
{ type: 'IRI', line: 5 }, |
| 698 |
{ type: 'abbreviation', value: 'a', line: 5 }, |
| 699 |
{ type: '[', line: 5 }, |
| 700 |
{ type: ']', line: 5 }, |
| 701 |
{ type: '.', line: 5 }, |
| 702 |
{ type: 'IRI', line: 6 }, |
| 703 |
{ type: 'abbreviation', value: 'a', line: 6 }, |
| 704 |
{ type: '(', line: 6 }, |
| 705 |
{ type: ')', line: 6 }, |
| 706 |
{ type: '.', line: 6 }, |
| 707 |
{ type: 'eof', line: 7 })); |
| 708 |
|
| 709 |
it('should tokenize an empty default graph', |
| 710 |
shouldTokenize('{}', |
| 711 |
{ type: '{', line: 1 }, |
| 712 |
{ type: '}', line: 1 }, |
| 713 |
{ type: 'eof', line: 1 })); |
| 714 |
|
| 715 |
it('should tokenize a non-empty default graph', |
| 716 |
shouldTokenize('{<a> <b> c:d}', |
| 717 |
{ type: '{', line: 1 }, |
| 718 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 719 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 720 |
{ type: 'prefixed', prefix: 'c', value: 'd', line: 1 }, |
| 721 |
{ type: '}', line: 1 }, |
| 722 |
{ type: 'eof', line: 1 })); |
| 723 |
|
| 724 |
it('should tokenize an empty graph identified by an IRI', |
| 725 |
shouldTokenize('<g>{}', |
| 726 |
{ type: 'IRI', value: 'g', line: 1 }, |
| 727 |
{ type: '{', line: 1 }, |
| 728 |
{ type: '}', line: 1 }, |
| 729 |
{ type: 'eof', line: 1 })); |
| 730 |
|
| 731 |
it('should tokenize a non-empty graph identified by an IRI', |
| 732 |
shouldTokenize('<g> {<a> <b> c:d}', |
| 733 |
{ type: 'IRI', value: 'g', line: 1 }, |
| 734 |
{ type: '{', line: 1 }, |
| 735 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 736 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 737 |
{ type: 'prefixed', prefix: 'c', value: 'd', line: 1 }, |
| 738 |
{ type: '}', line: 1 }, |
| 739 |
{ type: 'eof', line: 1 })); |
| 740 |
|
| 741 |
it('should tokenize an empty graph identified by a blank node', |
| 742 |
shouldTokenize('_:g{}', |
| 743 |
{ type: 'blank', prefix: '_', value: 'g', line: 1 }, |
| 744 |
{ type: '{', line: 1 }, |
| 745 |
{ type: '}', line: 1 }, |
| 746 |
{ type: 'eof', line: 1 })); |
| 747 |
|
| 748 |
it('should tokenize a non-empty graph identified by a blank node', |
| 749 |
shouldTokenize('_:g {<a> <b> c:d}', |
| 750 |
{ type: 'blank', prefix: '_', value: 'g', line: 1 }, |
| 751 |
{ type: '{', line: 1 }, |
| 752 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 753 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 754 |
{ type: 'prefixed', prefix: 'c', value: 'd', line: 1 }, |
| 755 |
{ type: '}', line: 1 }, |
| 756 |
{ type: 'eof', line: 1 })); |
| 757 |
|
| 758 |
it('should tokenize an empty graph with the GRAPH keyword', |
| 759 |
shouldTokenize('GRAPH<g>{}', |
| 760 |
{ type: 'GRAPH', line: 1 }, |
| 761 |
{ type: 'IRI', value: 'g', line: 1 }, |
| 762 |
{ type: '{', line: 1 }, |
| 763 |
{ type: '}', line: 1 }, |
| 764 |
{ type: 'eof', line: 1 })); |
| 765 |
|
| 766 |
it('should tokenize a non-empty graph with the GRAPH keyword', |
| 767 |
shouldTokenize('graph <g> {<a> <b> c:d}', |
| 768 |
{ type: 'GRAPH', line: 1 }, |
| 769 |
{ type: 'IRI', value: 'g', line: 1 }, |
| 770 |
{ type: '{', line: 1 }, |
| 771 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 772 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 773 |
{ type: 'prefixed', prefix: 'c', value: 'd', line: 1 }, |
| 774 |
{ type: '}', line: 1 }, |
| 775 |
{ type: 'eof', line: 1 })); |
| 776 |
|
| 777 |
it('should tokenize variables', |
| 778 |
shouldTokenize('?a ?abc ?a_B_c.', |
| 779 |
{ type: 'var', value: '?a', line: 1 }, |
| 780 |
{ type: 'var', value: '?abc', line: 1 }, |
| 781 |
{ type: 'var', value: '?a_B_c', line: 1 }, |
| 782 |
{ type: '.', value: '', line: 1 }, |
| 783 |
{ type: 'eof', line: 1 })); |
| 784 |
|
| 785 |
it('should not tokenize invalid variables', |
| 786 |
shouldNotTokenize('?0a ', 'Unexpected "?0a" on line 1.')); |
| 787 |
|
| 788 |
it('should tokenize the equality sign', |
| 789 |
shouldTokenize('<a> = <b> ', |
| 790 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 791 |
{ type: 'abbreviation', value: '=', line: 1 }, |
| 792 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 793 |
{ type: 'eof', line: 1 })); |
| 794 |
|
| 795 |
it('should tokenize the right implication', |
| 796 |
shouldTokenize('<a> => <b> ', |
| 797 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 798 |
{ type: 'abbreviation', value: '>', line: 1 }, |
| 799 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 800 |
{ type: 'eof', line: 1 })); |
| 801 |
|
| 802 |
it('should tokenize the left implication', |
| 803 |
shouldTokenize('<a> <= <b> ', |
| 804 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 805 |
{ type: 'inverse', value: '>', line: 1 }, |
| 806 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 807 |
{ type: 'eof', line: 1 })); |
| 808 |
|
| 809 |
it('should tokenize a split left implication', |
| 810 |
shouldTokenize(streamOf('<a> <', '= <b> '), |
| 811 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 812 |
{ type: 'inverse', value: '>', line: 1 }, |
| 813 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 814 |
{ type: 'eof', line: 1 })); |
| 815 |
|
| 816 |
it('should tokenize paths', |
| 817 |
shouldTokenize(':joe!fam:mother!loc:office!loc:zip :joe!fam:mother^fam:mother', |
| 818 |
{ type: 'prefixed', prefix: '', value: 'joe', line: 1 }, |
| 819 |
{ type: '!', line: 1 }, |
| 820 |
{ type: 'prefixed', prefix: 'fam', value: 'mother', line: 1 }, |
| 821 |
{ type: '!', line: 1 }, |
| 822 |
{ type: 'prefixed', prefix: 'loc', value: 'office', line: 1 }, |
| 823 |
{ type: '!', line: 1 }, |
| 824 |
{ type: 'prefixed', prefix: 'loc', value: 'zip', line: 1 }, |
| 825 |
|
| 826 |
{ type: 'prefixed', prefix: '', value: 'joe', line: 1 }, |
| 827 |
{ type: '!', line: 1 }, |
| 828 |
{ type: 'prefixed', prefix: 'fam', value: 'mother', line: 1 }, |
| 829 |
{ type: '^', line: 1 }, |
| 830 |
{ type: 'prefixed', prefix: 'fam', value: 'mother', line: 1 }, |
| 831 |
{ type: 'eof', line: 1 })); |
| 832 |
|
| 833 |
it('should not tokenize an invalid document', |
| 834 |
shouldNotTokenize(' \n @!', 'Unexpected "@!" on line 2.')); |
| 835 |
|
| 836 |
it('does not call setEncoding if not available', () => { |
| 837 |
new Lexer().tokenize({ on: function () {} }); |
| 838 |
}); |
| 839 |
|
| 840 |
it('should tokenize an Quadterm start', |
| 841 |
shouldTokenize('<<', |
| 842 |
{ type: '<<', line: 1 }, { type: 'eof', line: 1 })); |
| 843 |
|
| 844 |
it('should tokenize a split Quadterm start', |
| 845 |
shouldTokenize(streamOf('<', '<'), |
| 846 |
{ type: '<<', line: 1 }, { type: 'eof', line: 1 })); |
| 847 |
|
| 848 |
it('should tokenize an Quadterm end', |
| 849 |
shouldTokenize('>>', |
| 850 |
{ type: '>>', line: 1 }, { type: 'eof', line: 1 })); |
| 851 |
|
| 852 |
it('should tokenize an empty Quadterm', |
| 853 |
shouldTokenize('<< >>', |
| 854 |
{ type: '<<', line: 1 }, |
| 855 |
{ type: '>>', line: 1 }, |
| 856 |
{ type: 'eof', line: 1 })); |
| 857 |
|
| 858 |
it('should tokenize an RDF* statement with IRIs', |
| 859 |
shouldTokenize('<<<http://ex.org/?bla#foo> \n\t<http://ex.org/?bla#bar> \n\t<http://ex.org/?bla#boo>>> .', |
| 860 |
{ type: '<<', line: 1 }, |
| 861 |
{ type: 'IRI', value: 'http://ex.org/?bla#foo', line: 1 }, |
| 862 |
{ type: 'IRI', value: 'http://ex.org/?bla#bar', line: 2 }, |
| 863 |
{ type: 'IRI', value: 'http://ex.org/?bla#boo', line: 3 }, |
| 864 |
{ type: '>>', line: 3 }, |
| 865 |
{ type: '.', line: 3 }, |
| 866 |
{ type: 'eof', line: 3 })); |
| 867 |
|
| 868 |
it('should not tokenize a wrongly closed RDF* statement with IRIs', |
| 869 |
shouldNotTokenize('<<<http://ex.org/?bla#foo> \n\t<http://ex.org/?bla#bar> \n\t<http://ex.org/?bla#boo>> .', |
| 870 |
'Unexpected ">" on line 3.')); |
| 871 |
|
| 872 |
it('should tokenize a split RDF* statement with IRIs', |
| 873 |
shouldTokenize(streamOf('<', '<<http://ex.org/?bla#foo> \n\t<http://ex.org/?bla#bar> \n\t<http://ex.org/?bla#boo>>> .'), |
| 874 |
{ type: '<<', line: 1 }, |
| 875 |
{ type: 'IRI', value: 'http://ex.org/?bla#foo', line: 1 }, |
| 876 |
{ type: 'IRI', value: 'http://ex.org/?bla#bar', line: 2 }, |
| 877 |
{ type: 'IRI', value: 'http://ex.org/?bla#boo', line: 3 }, |
| 878 |
{ type: '>>', line: 3 }, |
| 879 |
{ type: '.', line: 3 }, |
| 880 |
{ type: 'eof', line: 3 })); |
| 881 |
|
| 882 |
it('should tokenize an RDF* statement with string literals', |
| 883 |
shouldTokenize('<<"string"@en "string"@nl-be "string"@EN>> .', |
| 884 |
{ type: '<<', line: 1 }, |
| 885 |
{ type: 'literal', value: 'string', line: 1 }, |
| 886 |
{ type: 'langcode', value: 'en', line: 1 }, |
| 887 |
{ type: 'literal', value: 'string', line: 1 }, |
| 888 |
{ type: 'langcode', value: 'nl-be', line: 1 }, |
| 889 |
{ type: 'literal', value: 'string', line: 1 }, |
| 890 |
{ type: 'langcode', value: 'EN', line: 1 }, |
| 891 |
{ type: '>>', line: 1 }, |
| 892 |
{ type: '.', line: 1 }, |
| 893 |
{ type: 'eof', line: 1 })); |
| 894 |
|
| 895 |
it('should tokenize an RDF* statement with integers', |
| 896 |
shouldTokenize('<<1 2 3>>.', |
| 897 |
{ type: '<<', line: 1 }, |
| 898 |
{ type: 'literal', value: '1', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 899 |
{ type: 'literal', value: '2', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 900 |
{ type: 'literal', value: '3', prefix: 'http://www.w3.org/2001/XMLSchema#integer', line: 1 }, |
| 901 |
{ type: '>>', line: 1 }, |
| 902 |
{ type: '.', line: 1 }, |
| 903 |
{ type: 'eof', line: 1 })); |
| 904 |
|
| 905 |
it('should tokenize an RDF* statement with decimals', |
| 906 |
shouldTokenize('<<1.2 3.4 5.6>>.', |
| 907 |
{ type: '<<', line: 1 }, |
| 908 |
{ type: 'literal', value: '1.2', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 909 |
{ type: 'literal', value: '3.4', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 910 |
{ type: 'literal', value: '5.6', prefix: 'http://www.w3.org/2001/XMLSchema#decimal', line: 1 }, |
| 911 |
{ type: '>>', line: 1 }, |
| 912 |
{ type: '.', line: 1 }, |
| 913 |
{ type: 'eof', line: 1 })); |
| 914 |
|
| 915 |
it('should tokenize an RDF* statement with booleans', |
| 916 |
shouldTokenize('<<true false true>>.', |
| 917 |
{ type: '<<', line: 1 }, |
| 918 |
{ type: 'literal', value: 'true', prefix: 'http://www.w3.org/2001/XMLSchema#boolean', line: 1 }, |
| 919 |
{ type: 'literal', value: 'false', prefix: 'http://www.w3.org/2001/XMLSchema#boolean', line: 1 }, |
| 920 |
{ type: 'literal', value: 'true', prefix: 'http://www.w3.org/2001/XMLSchema#boolean', line: 1 }, |
| 921 |
{ type: '>>', line: 1 }, |
| 922 |
{ type: '.', line: 1 }, |
| 923 |
{ type: 'eof', line: 1 })); |
| 924 |
|
| 925 |
it('should tokenize a prefixed iri followed by the end of a QuadTerm', |
| 926 |
shouldTokenize('c:c>> .', |
| 927 |
{ type: 'prefixed', prefix: 'c', value: 'c', line: 1 }, |
| 928 |
{ type: '>>', line: 1 }, |
| 929 |
{ type: '.', line: 1 }, |
| 930 |
{ type: 'eof', line: 1 })); |
| 931 |
|
| 932 |
it('should tokenize an RDF* statement with prefixed names', |
| 933 |
shouldTokenize('<<a:a b:b c:c>> .', |
| 934 |
{ type: '<<', line: 1 }, |
| 935 |
{ type: 'prefixed', prefix: 'a', value: 'a', line: 1 }, |
| 936 |
{ type: 'prefixed', prefix: 'b', value: 'b', line: 1 }, |
| 937 |
{ type: 'prefixed', prefix: 'c', value: 'c', line: 1 }, |
| 938 |
{ type: '>>', line: 1 }, |
| 939 |
{ type: '.', line: 1 }, |
| 940 |
{ type: 'eof', line: 1 })); |
| 941 |
|
| 942 |
it('should tokenize an RDF* statement with blank nodes', |
| 943 |
shouldTokenize('<<_:a _:b _:c>> .', |
| 944 |
{ type: '<<', line: 1 }, |
| 945 |
{ type: 'blank', prefix: '_', value: 'a', line: 1 }, |
| 946 |
{ type: 'blank', prefix: '_', value: 'b', line: 1 }, |
| 947 |
{ type: 'blank', prefix: '_', value: 'c', line: 1 }, |
| 948 |
{ type: '>>', line: 1 }, |
| 949 |
{ type: '.', line: 1 }, |
| 950 |
{ type: 'eof', line: 1 })); |
| 951 |
|
| 952 |
it('should tokenize an RDF* statement with variables', |
| 953 |
shouldTokenize('<<?a ?b ?c>> .', |
| 954 |
{ type: '<<', line: 1 }, |
| 955 |
{ type: 'var', value: '?a', line: 1 }, |
| 956 |
{ type: 'var', value: '?b', line: 1 }, |
| 957 |
{ type: 'var', value: '?c', line: 1 }, |
| 958 |
{ type: '>>', line: 1 }, |
| 959 |
{ type: '.', line: 1 }, |
| 960 |
{ type: 'eof', line: 1 })); |
| 961 |
|
| 962 |
it('should tokenize an RDF* statement with mixed types', |
| 963 |
shouldTokenize('<<<http://ex.org/?bla#foo> "string"@nl-be c:c>> .', |
| 964 |
{ type: '<<', line: 1 }, |
| 965 |
{ type: 'IRI', value: 'http://ex.org/?bla#foo', line: 1 }, |
| 966 |
{ type: 'literal', value: 'string', line: 1 }, |
| 967 |
{ type: 'langcode', value: 'nl-be', line: 1 }, |
| 968 |
{ type: 'prefixed', prefix: 'c', value: 'c', line: 1 }, |
| 969 |
{ type: '>>', line: 1 }, |
| 970 |
{ type: '.', line: 1 }, |
| 971 |
{ type: 'eof', line: 1 })); |
| 972 |
|
| 973 |
it('should tokenize an RDF* statement with mixed types', |
| 974 |
shouldTokenize('<<_:a a:a "string"@EN>> .', |
| 975 |
{ type: '<<', line: 1 }, |
| 976 |
{ type: 'blank', prefix: '_', value: 'a', line: 1 }, |
| 977 |
{ type: 'prefixed', prefix: 'a', value: 'a', line: 1 }, |
| 978 |
{ type: 'literal', value: 'string', line: 1 }, |
| 979 |
{ type: 'langcode', value: 'EN', line: 1 }, |
| 980 |
{ type: '>>', line: 1 }, |
| 981 |
{ type: '.', line: 1 }, |
| 982 |
{ type: 'eof', line: 1 })); |
| 983 |
|
| 984 |
it('should tokenize an RDF* statement with mixed types', |
| 985 |
shouldTokenize('<<"literal"@AU <http://ex.org/?bla#foo> _:a>> .', |
| 986 |
{ type: '<<', line: 1 }, |
| 987 |
{ type: 'literal', value: 'literal', line: 1 }, |
| 988 |
{ type: 'langcode', value: 'AU', line: 1 }, |
| 989 |
{ type: 'IRI', value: 'http://ex.org/?bla#foo', line: 1 }, |
| 990 |
{ type: 'blank', prefix: '_', value: 'a', line: 1 }, |
| 991 |
{ type: '>>', line: 1 }, |
| 992 |
{ type: '.', line: 1 }, |
| 993 |
{ type: 'eof', line: 1 })); |
| 994 |
|
| 995 |
it('should tokenize RDF* statements with shared subjects', |
| 996 |
shouldTokenize('<<<a> <b> <c>;\n<d> <e>>>.', |
| 997 |
{ type: '<<', line: 1 }, |
| 998 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 999 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 1000 |
{ type: 'IRI', value: 'c', line: 1 }, |
| 1001 |
{ type: ';', line: 1 }, |
| 1002 |
{ type: 'IRI', value: 'd', line: 2 }, |
| 1003 |
{ type: 'IRI', value: 'e', line: 2 }, |
| 1004 |
{ type: '>>', line: 2 }, |
| 1005 |
{ type: '.', line: 2 }, |
| 1006 |
{ type: 'eof', line: 2 })); |
| 1007 |
|
| 1008 |
it('should tokenize RDF* statements with shared subjects and predicates', |
| 1009 |
shouldTokenize('<<<a> <b> <c>,\n<d>>>.', |
| 1010 |
{ type: '<<', line: 1 }, |
| 1011 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 1012 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 1013 |
{ type: 'IRI', value: 'c', line: 1 }, |
| 1014 |
{ type: ',', line: 1 }, |
| 1015 |
{ type: 'IRI', value: 'd', line: 2 }, |
| 1016 |
{ type: '>>', line: 2 }, |
| 1017 |
{ type: '.', line: 2 }, |
| 1018 |
{ type: 'eof', line: 2 })); |
| 1019 |
|
| 1020 |
it('should tokenize an RDF* statement with shared subjects and predicates and prefixed names', |
| 1021 |
shouldTokenize('<<a:a b:b c:c;d:d e:e,f:f>> .', |
| 1022 |
{ type: '<<', line: 1 }, |
| 1023 |
{ type: 'prefixed', prefix: 'a', value: 'a', line: 1 }, |
| 1024 |
{ type: 'prefixed', prefix: 'b', value: 'b', line: 1 }, |
| 1025 |
{ type: 'prefixed', prefix: 'c', value: 'c', line: 1 }, |
| 1026 |
{ type: ';', line: 1 }, |
| 1027 |
{ type: 'prefixed', prefix: 'd', value: 'd', line: 1 }, |
| 1028 |
{ type: 'prefixed', prefix: 'e', value: 'e', line: 1 }, |
| 1029 |
{ type: ',', line: 1 }, |
| 1030 |
{ type: 'prefixed', prefix: 'f', value: 'f', line: 1 }, |
| 1031 |
{ type: '>>', line: 1 }, |
| 1032 |
{ type: '.', line: 1 }, |
| 1033 |
{ type: 'eof', line: 1 })); |
| 1034 |
|
| 1035 |
it('should tokenize a QuadTerm followed by other tokens', |
| 1036 |
shouldTokenize('<<_:a <b> "lit"@EN>> _:a b:b.', |
| 1037 |
{ type: '<<', line: 1 }, |
| 1038 |
{ type: 'blank', prefix: '_', value: 'a', line: 1 }, |
| 1039 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 1040 |
{ type: 'literal', value: 'lit', line: 1 }, |
| 1041 |
{ type: 'langcode', value: 'EN', line: 1 }, |
| 1042 |
{ type: '>>', line: 1 }, |
| 1043 |
{ type: 'blank', prefix: '_', value: 'a', line: 1 }, |
| 1044 |
{ type: 'prefixed', prefix: 'b', value: 'b', line: 1 }, |
| 1045 |
{ type: '.', line: 1 }, |
| 1046 |
{ type: 'eof', line: 1 })); |
| 1047 |
|
| 1048 |
it('should tokenize a QuadTerm preceded by other tokens', |
| 1049 |
shouldTokenize('"lit"@DE _:b <<_:a b:b "lit"@EN>>.', |
| 1050 |
{ type: 'literal', value: 'lit', line: 1 }, |
| 1051 |
{ type: 'langcode', value: 'DE', line: 1 }, |
| 1052 |
{ type: 'blank', prefix: '_', value: 'b', line: 1 }, |
| 1053 |
{ type: '<<', line: 1 }, |
| 1054 |
{ type: 'blank', prefix: '_', value: 'a', line: 1 }, |
| 1055 |
{ type: 'prefixed', prefix: 'b', value: 'b', line: 1 }, |
| 1056 |
{ type: 'literal', value: 'lit', line: 1 }, |
| 1057 |
{ type: 'langcode', value: 'EN', line: 1 }, |
| 1058 |
{ type: '>>', line: 1 }, |
| 1059 |
{ type: '.', line: 1 }, |
| 1060 |
{ type: 'eof', line: 1 })); |
| 1061 |
|
| 1062 |
it('should tokenize a nested QuadTerm as subject in a statement', |
| 1063 |
shouldTokenize('<<<<_:b <b> "lit"@DE>> <a> "lit"@EN>>.', |
| 1064 |
{ type: '<<', line: 1 }, |
| 1065 |
{ type: '<<', line: 1 }, |
| 1066 |
{ type: 'blank', prefix: '_', value: 'b', line: 1 }, |
| 1067 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 1068 |
{ type: 'literal', value: 'lit', line: 1 }, |
| 1069 |
{ type: 'langcode', value: 'DE', line: 1 }, |
| 1070 |
{ type: '>>', line: 1 }, |
| 1071 |
{ type: 'IRI', value: 'a', line: 1 }, |
| 1072 |
{ type: 'literal', value: 'lit', line: 1 }, |
| 1073 |
{ type: 'langcode', value: 'EN', line: 1 }, |
| 1074 |
{ type: '>>', line: 1 }, |
| 1075 |
{ type: '.', line: 1 }, |
| 1076 |
{ type: 'eof', line: 1 })); |
| 1077 |
|
| 1078 |
it('should tokenize a nested QuadTerm as object in a statement', |
| 1079 |
shouldTokenize('<<a:a _:a <<_:b <b> "lit"@DE>>>>.', |
| 1080 |
{ type: '<<', line: 1 }, |
| 1081 |
{ type: 'prefixed', prefix: 'a', value: 'a', line: 1 }, |
| 1082 |
{ type: 'blank', prefix: '_', value: 'a', line: 1 }, |
| 1083 |
{ type: '<<', line: 1 }, |
| 1084 |
{ type: 'blank', prefix: '_', value: 'b', line: 1 }, |
| 1085 |
{ type: 'IRI', value: 'b', line: 1 }, |
| 1086 |
{ type: 'literal', value: 'lit', line: 1 }, |
| 1087 |
{ type: 'langcode', value: 'DE', line: 1 }, |
| 1088 |
{ type: '>>', line: 1 }, |
| 1089 |
{ type: '>>', line: 1 }, |
| 1090 |
{ type: '.', line: 1 }, |
| 1091 |
{ type: 'eof', line: 1 })); |
| 1092 |
|
| 1093 |
it('returns start and end index for every token', () => { |
| 1094 |
const tokens = new Lexer().tokenize('<a:a> <b:c> "lit"@EN.'); |
| 1095 |
tokens.should.deep.equal([ |
| 1096 |
{ line: 1, prefix: '', type: 'IRI', value: 'a:a', start: 0, end: 6 }, |
| 1097 |
{ line: 1, prefix: '', type: 'IRI', value: 'b:c', start: 6, end: 12 }, |
| 1098 |
{ line: 1, prefix: '', type: 'literal', value: 'lit', start: 12, end: 17 }, |
| 1099 |
{ line: 1, prefix: '', type: 'langcode', value: 'EN', start: 17, end: 20 }, |
| 1100 |
{ line: 1, prefix: '', type: '.', value: '', start: 20, end: 21 }, |
| 1101 |
{ line: 1, prefix: '', type: 'eof', value: '', start: 21, end: 21 }, |
| 1102 |
]); |
| 1103 |
}); |
| 1104 |
|
| 1105 |
it('returns start and end index relative to line', () => { |
| 1106 |
const tokens = new Lexer().tokenize('<a:a> <b:c> "lit"@EN ; \n <b:d> <d:e> .'); |
| 1107 |
tokens.should.deep.equal([ |
| 1108 |
{ line: 1, prefix: '', type: 'IRI', value: 'a:a', start: 0, end: 6 }, |
| 1109 |
{ line: 1, prefix: '', type: 'IRI', value: 'b:c', start: 6, end: 12 }, |
| 1110 |
{ line: 1, prefix: '', type: 'literal', value: 'lit', start: 12, end: 17 }, |
| 1111 |
{ line: 1, prefix: '', type: 'langcode', value: 'EN', start: 17, end: 20 }, |
| 1112 |
{ line: 1, prefix: '', type: ';', value: '', start: 21, end: 22 }, |
| 1113 |
{ line: 2, prefix: '', type: 'IRI', value: 'b:d', start: 0, end: 6 }, |
| 1114 |
{ line: 2, prefix: '', type: 'IRI', value: 'd:e', start: 6, end: 12 }, |
| 1115 |
{ line: 2, prefix: '', type: '.', value: '', start: 12, end: 13 }, |
| 1116 |
{ line: 2, prefix: '', type: 'eof', value: '', start: 13, end: 13 }, |
| 1117 |
]); |
| 1118 |
}); |
| 1119 |
|
| 1120 |
it('returns index including whitespaces', () => { |
| 1121 |
const tokens = new Lexer().tokenize('<a:a> <b:c> <d:e> .'); |
| 1122 |
tokens.should.deep.equal([ |
| 1123 |
{ line: 1, prefix: '', type: 'IRI', value: 'a:a', start: 0, end: 8 }, |
| 1124 |
{ line: 1, prefix: '', type: 'IRI', value: 'b:c', start: 8, end: 17 }, |
| 1125 |
{ line: 1, prefix: '', type: 'IRI', value: 'd:e', start: 17, end: 24 }, |
| 1126 |
{ line: 1, prefix: '', type: '.', value: '', start: 24, end: 25 }, |
| 1127 |
{ line: 1, prefix: '', type: 'eof', value: '', start: 25, end: 25 }, |
| 1128 |
]); |
| 1129 |
}); |
| 1130 |
|
| 1131 |
it('returns index for comments and eof', () => { |
| 1132 |
const tokens = new Lexer({ comments: true }).tokenize('# some\n<a:a> <b:b> <c:c> . # trailing comment\n# thing'); |
| 1133 |
tokens.should.deep.equal([ |
| 1134 |
{ line: 1, prefix: '', type: 'comment', value: ' some', start: 0, end: 7 }, |
| 1135 |
{ line: 2, prefix: '', type: 'IRI', value: 'a:a', start: 0, end: 6 }, |
| 1136 |
{ line: 2, prefix: '', type: 'IRI', value: 'b:b', start: 6, end: 12 }, |
| 1137 |
{ line: 2, prefix: '', type: 'IRI', value: 'c:c', start: 12, end: 18 }, |
| 1138 |
{ line: 2, prefix: '', type: '.', value: '', start: 18, end: 19 }, |
| 1139 |
{ line: 2, prefix: '', type: 'comment', value: ' trailing comment', start: 19, end: 39 }, |
| 1140 |
{ line: 3, prefix: '', type: 'comment', value: ' thing', start: 0, end: 7 }, |
| 1141 |
{ line: 3, prefix: '', type: 'eof', value: '', start: 7, end: 7 }, |
| 1142 |
]); |
| 1143 |
}); |
| 1144 |
|
| 1145 |
describe('passing data after the stream has been finished', () => { |
| 1146 |
const tokens = []; |
| 1147 |
let error; |
| 1148 |
before(() => { |
| 1149 |
const stream = new EventEmitter(), lexer = new Lexer(); |
| 1150 |
lexer.tokenize(stream, (err, token) => { |
| 1151 |
if (err) |
| 1152 |
error = err; |
| 1153 |
else |
| 1154 |
tokens.push(token); |
| 1155 |
}); |
| 1156 |
stream.emit('data', '<a> '); |
| 1157 |
stream.emit('end'); |
| 1158 |
stream.emit('data', '<b> '); |
| 1159 |
stream.emit('end'); |
| 1160 |
}); |
| 1161 |
|
| 1162 |
it('parses only the first chunk (plus EOF)', () => { |
| 1163 |
tokens.should.have.length(2); |
| 1164 |
}); |
| 1165 |
|
| 1166 |
it('does not emit an error', () => { |
| 1167 |
expect(error).to.not.exist; |
| 1168 |
}); |
| 1169 |
}); |
| 1170 |
|
| 1171 |
describe('passing data after a syntax error', () => { |
| 1172 |
const tokens = []; |
| 1173 |
let error; |
| 1174 |
before(() => { |
| 1175 |
const stream = new EventEmitter(), lexer = new Lexer(); |
| 1176 |
lexer.tokenize(stream, (err, token) => { |
| 1177 |
if (err) |
| 1178 |
error = err; |
| 1179 |
else |
| 1180 |
tokens.push(token); |
| 1181 |
}); |
| 1182 |
stream.emit('data', '<a> '); |
| 1183 |
stream.emit('data', ' error '); |
| 1184 |
stream.emit('end'); |
| 1185 |
stream.emit('data', '<b> '); |
| 1186 |
stream.emit('end'); |
| 1187 |
}); |
| 1188 |
|
| 1189 |
it('parses only the first chunk', () => { |
| 1190 |
tokens.should.have.length(1); |
| 1191 |
}); |
| 1192 |
|
| 1193 |
it('emits the error', () => { |
| 1194 |
expect(error).to.exist; |
| 1195 |
expect(error).to.have.property('message', 'Unexpected "error" on line 1.'); |
| 1196 |
}); |
| 1197 |
}); |
| 1198 |
|
| 1199 |
describe('when the stream errors', () => { |
| 1200 |
const tokens = []; |
| 1201 |
let error; |
| 1202 |
before(() => { |
| 1203 |
const stream = new EventEmitter(), lexer = new Lexer(); |
| 1204 |
lexer.tokenize(stream, (err, token) => { |
| 1205 |
if (err) |
| 1206 |
error = err; |
| 1207 |
else |
| 1208 |
tokens.push(token); |
| 1209 |
}); |
| 1210 |
stream.emit('error', new Error('my error')); |
| 1211 |
}); |
| 1212 |
|
| 1213 |
it('passes the error', () => { |
| 1214 |
expect(error).to.exist; |
| 1215 |
expect(error).to.have.property('message', 'my error'); |
| 1216 |
}); |
| 1217 |
}); |
| 1218 |
|
| 1219 |
describe('called with a string and without callback', () => { |
| 1220 |
const lexer = new Lexer(), |
| 1221 |
tokens = lexer.tokenize('<a> <b> <c>.'); |
| 1222 |
|
| 1223 |
it('returns all tokens synchronously', () => { |
| 1224 |
tokens.should.deep.equal([ |
| 1225 |
{ line: 1, type: 'IRI', value: 'a', prefix: '', start: 0, end: 4 }, |
| 1226 |
{ line: 1, type: 'IRI', value: 'b', prefix: '', start: 4, end: 8 }, |
| 1227 |
{ line: 1, type: 'IRI', value: 'c', prefix: '', start: 8, end: 11 }, |
| 1228 |
{ line: 1, type: '.', value: '', prefix: '', start: 11, end: 12 }, |
| 1229 |
{ line: 1, type: 'eof', value: '', prefix: '', start: 12, end: 12 }, |
| 1230 |
]); |
| 1231 |
}); |
| 1232 |
}); |
| 1233 |
|
| 1234 |
describe('called with an erroneous string and without callback', () => { |
| 1235 |
const lexer = new Lexer(); |
| 1236 |
|
| 1237 |
it('throws an error', () => { |
| 1238 |
(function () { lexer.tokenize('<a> bar'); }) |
| 1239 |
.should.throw('Unexpected "bar" on line 1.'); |
| 1240 |
}); |
| 1241 |
}); |
| 1242 |
}); |
| 1243 |
}); |
| 1244 |
|
| 1245 |
describe('A Lexer instance with the n3 option set to false', () => { |
| 1246 |
function createLexer() { return new Lexer({ n3: false }); } |
| 1247 |
|
| 1248 |
it('should not tokenize a variable', |
| 1249 |
shouldNotTokenize(createLexer(), '?a', 'Unexpected "?a" on line 1.')); |
| 1250 |
|
| 1251 |
it('should not tokenize a right implication', |
| 1252 |
shouldNotTokenize(createLexer(), '<a> => <c>.', 'Unexpected "=>" on line 1.')); |
| 1253 |
|
| 1254 |
it('should not tokenize a left implication', |
| 1255 |
shouldNotTokenize(createLexer(), '<a> <= <c>.', 'Unexpected "<=" on line 1.')); |
| 1256 |
|
| 1257 |
it('should not tokenize an equality', |
| 1258 |
shouldNotTokenize(createLexer(), '<a> = <c>.', 'Unexpected "=" on line 1.')); |
| 1259 |
|
| 1260 |
it('should not tokenize a ! path', |
| 1261 |
shouldNotTokenize(createLexer(), ':joe!fam:mother', 'Unexpected "!fam:mother" on line 1.')); |
| 1262 |
|
| 1263 |
it('should not tokenize a ^ path', |
| 1264 |
shouldNotTokenize(createLexer(), ':joe^fam:father', 'Unexpected "^fam:father" on line 1.')); |
| 1265 |
}); |
| 1266 |
|
| 1267 |
describe('A Lexer instance with the comment option set to true', () => { |
| 1268 |
function createLexer() { return new Lexer({ comments: true }); } |
| 1269 |
|
| 1270 |
it('should tokenize a single comment', |
| 1271 |
shouldTokenize(createLexer(), '#mycomment', |
| 1272 |
{ type: 'comment', value: 'mycomment', line: 1 }, |
| 1273 |
{ type: 'eof', line: 1 })); |
| 1274 |
|
| 1275 |
it('should tokenize a stream with split comment', |
| 1276 |
shouldTokenize(createLexer(), streamOf('#mycom', 'ment'), |
| 1277 |
{ type: 'comment', value: 'mycomment', line: 1 }, |
| 1278 |
{ type: 'eof', line: 1 })); |
| 1279 |
|
| 1280 |
it('should tokenize comments', |
| 1281 |
shouldTokenize(createLexer(), '<#foo> #mycomment\n <#foo> #mycomment \r# mycomment\n\n<#bla>#', |
| 1282 |
{ type: 'IRI', value: '#foo', line: 1 }, |
| 1283 |
{ type: 'comment', value: 'mycomment', line: 1 }, |
| 1284 |
{ type: 'IRI', value: '#foo', line: 2 }, |
| 1285 |
{ type: 'comment', value: 'mycomment ', line: 2 }, |
| 1286 |
{ type: 'comment', value: ' mycomment', line: 3 }, |
| 1287 |
{ type: 'IRI', value: '#bla', line: 5 }, |
| 1288 |
{ type: 'comment', value: '', line: 5 }, |
| 1289 |
{ type: 'eof', line: 5 })); |
| 1290 |
}); |
| 1291 |
|
| 1292 |
function shouldTokenize(lexer, input) { |
| 1293 |
const expected = Array.prototype.slice.call(arguments, 1); |
| 1294 |
const ignoredAttributes = { start: true, end: true }; |
| 1295 |
|
| 1296 |
// Shift parameters as necessary |
| 1297 |
if (lexer instanceof Lexer) |
| 1298 |
expected.shift(); |
| 1299 |
else |
| 1300 |
input = lexer, lexer = new Lexer(); |
| 1301 |
|
| 1302 |
return function (done) { |
| 1303 |
const result = []; |
| 1304 |
lexer.tokenize(input, tokenCallback); |
| 1305 |
|
| 1306 |
function tokenCallback(error, token) { |
| 1307 |
expect(error).not.to.exist; |
| 1308 |
expect(token).to.exist; |
| 1309 |
const expectedItem = expected[result.length]; |
| 1310 |
if (expectedItem) |
| 1311 |
for (const attribute in token) |
| 1312 |
if (typeof expectedItem[attribute] === 'undefined' && |
| 1313 |
(token[attribute] === '' || (ignoredAttributes[attribute]))) |
| 1314 |
delete token[attribute]; |
| 1315 |
result.push(token); |
| 1316 |
if (token.type === 'eof') { |
| 1317 |
result.should.eql(expected); |
| 1318 |
done(null, result); |
| 1319 |
} |
| 1320 |
} |
| 1321 |
}; |
| 1322 |
} |
| 1323 |
|
| 1324 |
function shouldNotTokenize(lexer, input, expectedError) { |
| 1325 |
// Shift parameters if necessary |
| 1326 |
if (!(lexer instanceof Lexer)) |
| 1327 |
expectedError = input, input = lexer, lexer = new Lexer(); |
| 1328 |
|
| 1329 |
return function (done) { |
| 1330 |
lexer.tokenize(input, tokenCallback); |
| 1331 |
function tokenCallback(error, token) { |
| 1332 |
if (error) { |
| 1333 |
expect(token).not.to.exist; |
| 1334 |
error.should.be.an.instanceof(Error); |
| 1335 |
error.message.should.eql(expectedError); |
| 1336 |
done(); |
| 1337 |
} |
| 1338 |
else if (token.type === 'eof') |
| 1339 |
throw new Error(`Expected error ${expectedError}`); |
| 1340 |
} |
| 1341 |
}; |
| 1342 |
} |
| 1343 |
|
| 1344 |
function streamOf() { |
| 1345 |
const elements = Array.prototype.slice.call(arguments), |
| 1346 |
stream = new EventEmitter(); |
| 1347 |
|
| 1348 |
stream.setEncoding = function (encoding) { |
| 1349 |
if (encoding === 'utf8') |
| 1350 |
queueMicrotask(next); |
| 1351 |
}; |
| 1352 |
|
| 1353 |
function next() { |
| 1354 |
if (elements.length) { |
| 1355 |
const element = elements.shift(); |
| 1356 |
// use "null" to stall the stream |
| 1357 |
if (element !== null) { |
| 1358 |
stream.emit('data', element); |
| 1359 |
queueMicrotask(next); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
else { |
| 1363 |
stream.emit('end'); |
| 1364 |
} |
| 1365 |
} |
| 1366 |
return stream; |
| 1367 |
} |