@@ -1090,6 +1090,57 @@ describe('Lexer', () => {
10901090 { type : '.' , line : 1 } ,
10911091 { type : 'eof' , line : 1 } ) ) ;
10921092
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+ } ) ;
10931144
10941145 describe ( 'passing data after the stream has been finished' , ( ) => {
10951146 const tokens = [ ] ;
@@ -1171,11 +1222,11 @@ describe('Lexer', () => {
11711222
11721223 it ( 'returns all tokens synchronously' , ( ) => {
11731224 tokens . should . deep . equal ( [
1174- { line : 1 , type : 'IRI' , value : 'a' , prefix : '' } ,
1175- { line : 1 , type : 'IRI' , value : 'b' , prefix : '' } ,
1176- { line : 1 , type : 'IRI' , value : 'c' , prefix : '' } ,
1177- { line : 1 , type : '.' , value : '' , prefix : '' } ,
1178- { line : 1 , type : 'eof' , value : '' , prefix : '' } ,
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 } ,
11791230 ] ) ;
11801231 } ) ;
11811232 } ) ;
@@ -1240,6 +1291,8 @@ describe('A Lexer instance with the comment option set to true', () => {
12401291
12411292function shouldTokenize ( lexer , input ) {
12421293 const expected = Array . prototype . slice . call ( arguments , 1 ) ;
1294+ const ignoredAttributes = { start : true , end : true } ;
1295+
12431296 // Shift parameters as necessary
12441297 if ( lexer instanceof Lexer )
12451298 expected . shift ( ) ;
@@ -1256,7 +1309,8 @@ function shouldTokenize(lexer, input) {
12561309 const expectedItem = expected [ result . length ] ;
12571310 if ( expectedItem )
12581311 for ( const attribute in token )
1259- if ( token [ attribute ] === '' && expectedItem [ attribute ] !== '' )
1312+ if ( typeof expectedItem [ attribute ] === 'undefined' &&
1313+ ( token [ attribute ] === '' || ( ignoredAttributes [ attribute ] ) ) )
12601314 delete token [ attribute ] ;
12611315 result . push ( token ) ;
12621316 if ( token . type === 'eof' ) {
0 commit comments