@@ -2,9 +2,13 @@ var ids = require('spdx-license-ids')
2
2
var exceptions = require ( 'spdx-exceptions' )
3
3
4
4
module . exports = function ( ) {
5
+ // Parse the input string all at once.
6
+ // Store the array of tokens.
5
7
this . setInput = function ( string ) {
6
8
this . tokens = lex ( string )
7
9
}
10
+
11
+ // Set the state for the Jison parser on each shift.
8
12
this . lex = function ( ) {
9
13
var token = this . tokens . shift ( )
10
14
this . yylineno = 1
@@ -24,18 +28,19 @@ var LICENSEREF = /^LicenseRef-([0-9A-Za-z-+.]+)$/
24
28
var INVALID_CHARACTER = / [ ^ 0 - 9 A - Z a - z . + \- ( ) ] /
25
29
var SINGLE_CHARACTER_TOKENS = [ '(' , ')' , ':' , '+' ]
26
30
31
+ // Polyfill ECMAScript 2016 Array.prototype.includes.
27
32
var includes = Array . prototype . includes
28
- ? function ( array , element ) {
29
- return array . includes ( element )
30
- }
31
- : function ( array , element ) {
32
- return array . indexOf ( element ) !== - 1
33
- }
33
+ ? function ( array , element ) { return array . includes ( element ) }
34
+ : function ( array , element ) { return array . indexOf ( element ) !== - 1 }
34
35
35
36
function lex ( argument ) {
36
- if ( INVALID_CHARACTER . test ( argument ) ) {
37
- throw new Error ( 'Invalid character' )
37
+ // If there are invalid characters, catch them ahead of time.
38
+ var match = INVALID_CHARACTER . exec ( argument )
39
+ if ( match ) {
40
+ throw new Error ( 'Invalid character at offset ' + match . index )
38
41
}
42
+ // Iterate through the argument string, buffering
43
+ // non-space characters and building tokens.
39
44
var tokens = [ ]
40
45
var characterBuffer = ''
41
46
var startedBuffering = null
@@ -60,6 +65,7 @@ function lex (argument) {
60
65
}
61
66
}
62
67
pushBuffered ( )
68
+ // Add an end-of-string token.
63
69
tokens . push ( {
64
70
type : 'EOS' ,
65
71
string : '' ,
@@ -70,12 +76,14 @@ function lex (argument) {
70
76
71
77
function pushBuffered ( ) {
72
78
if ( characterBuffer ) {
79
+ // Create a token for the buffered characters.
73
80
tokens . push ( {
74
81
type : tokenTypeForString ( characterBuffer , startedBuffering ) ,
75
82
string : characterBuffer ,
76
83
start : startedBuffering ,
77
84
end : startedBuffering + characterBuffer . length
78
85
} )
86
+ // Reset the buffer.
79
87
characterBuffer = ''
80
88
startedBuffering = null
81
89
}
0 commit comments