@@ -2,86 +2,98 @@ var Generator = require('jison').Generator
2
2
var options = {
3
3
type : 'slr' ,
4
4
moduleType : 'commonjs' ,
5
- moduleName : 'spdxparse' }
5
+ moduleName : 'spdxparse'
6
+ }
6
7
7
- var words = [ 'AND' , 'OR' , 'WITH' ]
8
+ var words = [ 'AND' , 'OR' , 'WITH' ]
8
9
9
- var quote = function ( argument ) {
10
- return '\'' + argument + '\'' }
10
+ var quote = function ( argument ) {
11
+ return '\'' + argument + '\''
12
+ }
11
13
12
- var regexEscape = function ( s ) {
13
- return s . replace ( / [ \^ \\ $ * + ? . ( ) | { } \[ \] \/ ] / g, '\\$&' ) }
14
+ var regexEscape = function ( s ) {
15
+ return s . replace ( / [ \^ \\ $ * + ? . ( ) | { } \[ \] \/ ] / g, '\\$&' )
16
+ }
14
17
15
- var handleLicensesAndExceptions = function ( ) {
18
+ var handleLicensesAndExceptions = function ( ) {
16
19
var ids = require ( 'spdx-license-ids' )
17
20
var exceptions = require ( 'spdx-exceptions' )
18
21
19
22
// Sort tokens longest-first (both license ids and exception strings)
20
23
var tokens = ids . concat ( exceptions )
21
- tokens . sort ( function ( a , b ) { return ( b . length - a . length ) } )
22
- return tokens . map ( function ( t ) {
23
- var type = ( ( ids . indexOf ( t ) >= 0 ) ? 'LICENSE' : 'EXCEPTION' )
24
- return [ regexEscape ( t ) , ( 'return ' + quote ( type ) ) ] } ) }
24
+ tokens . sort ( function ( a , b ) { return b . length - a . length } )
25
+ return tokens . map ( function ( t ) {
26
+ var type = ( ids . indexOf ( t ) >= 0 ) ? 'LICENSE' : 'EXCEPTION'
27
+ return [ regexEscape ( t ) , 'return ' + quote ( type ) ]
28
+ } )
29
+ }
25
30
26
31
var grammar = {
27
32
lex : {
28
- macros : { } ,
33
+ macros : { } ,
29
34
rules : [
30
- [ '$' , 'return ' + quote ( 'EOS' ) ] ,
31
- [ '\\s+' , '/* skip whitespace */' ] ,
32
- [ '\\+' , 'return ' + quote ( 'PLUS' ) ] ,
33
- [ '\\(' , 'return ' + quote ( 'OPEN' ) ] ,
34
- [ '\\)' , 'return ' + quote ( 'CLOSE' ) ] ,
35
- [ ':' , 'return ' + quote ( 'COLON' ) ] ,
36
- [ 'DocumentRef-([0-9A-Za-z-+.]+)' ,
37
- 'return ' + quote ( 'DOCUMENTREF' ) ] ,
38
- [ 'LicenseRef-([0-9A-Za-z-+.]+)' ,
39
- 'return ' + quote ( 'LICENSEREF' ) ] ]
40
- . concat ( words . map ( function ( word ) {
41
- return [ word , 'return ' + quote ( word ) ] } ) )
42
- . concat ( handleLicensesAndExceptions ( ) ) } ,
35
+ [ '$' , 'return ' + quote ( 'EOS' ) ] ,
36
+ [ '\\s+' , '/* skip whitespace */' ] ,
37
+ [ '\\+' , 'return ' + quote ( 'PLUS' ) ] ,
38
+ [ '\\(' , 'return ' + quote ( 'OPEN' ) ] ,
39
+ [ '\\)' , 'return ' + quote ( 'CLOSE' ) ] ,
40
+ [ ':' , 'return ' + quote ( 'COLON' ) ] ,
41
+ [
42
+ 'DocumentRef-([0-9A-Za-z-+.]+)' ,
43
+ 'return ' + quote ( 'DOCUMENTREF' )
44
+ ] ,
45
+ [
46
+ 'LicenseRef-([0-9A-Za-z-+.]+)' ,
47
+ 'return ' + quote ( 'LICENSEREF' )
48
+ ]
49
+ ]
50
+ . concat ( words . map ( function ( word ) {
51
+ return [ word , 'return ' + quote ( word ) ]
52
+ } ) )
53
+ . concat ( handleLicensesAndExceptions ( ) )
54
+ } ,
43
55
operators : [
44
- [ 'left' , 'OR' ] ,
45
- [ 'left' , 'AND' ] ,
46
- [ 'right' , 'PLUS' , 'WITH' ] ] ,
56
+ [ 'left' , 'OR' ] ,
57
+ [ 'left' , 'AND' ] ,
58
+ [ 'right' , 'PLUS' , 'WITH' ]
59
+ ] ,
47
60
tokens : [
48
61
'CLOSE' ,
49
62
'COLON' ,
50
63
'EXCEPTION' ,
51
64
'LICENSE' ,
52
65
'LICENSEREF' ,
53
66
'OPEN' ,
54
- 'PLUS' ]
55
- . concat ( words )
56
- . join ( ' ' ) ,
67
+ 'PLUS'
68
+ ] . concat ( words ) . join ( ' ' ) ,
57
69
start : 'start' ,
58
70
bnf : {
59
- start : [
60
- [ 'expression EOS' , 'return $$ = $1' ] ] ,
71
+ start : [ [ 'expression EOS' , 'return $$ = $1' ] ] ,
61
72
simpleExpression : [
62
- [ 'LICENSE' ,
63
- '$$ = { license: yytext }' ] ,
64
- [ 'LICENSE PLUS' ,
65
- '$$ = { license: $1, plus: true }' ] ,
66
- [ 'LICENSEREF' ,
67
- '$$ = { license: yytext }' ] ,
68
- [ 'DOCUMENTREF COLON LICENSEREF' ,
69
- '$$ = { license: yytext }' ] ] ,
73
+ [ 'LICENSE' , '$$ = {license: yytext}' ] ,
74
+ [ 'LICENSE PLUS' , '$$ = {license: $1, plus: true}' ] ,
75
+ [ 'LICENSEREF' , '$$ = {license: yytext}' ] ,
76
+ [ 'DOCUMENTREF COLON LICENSEREF' , '$$ = {license: yytext}' ]
77
+ ] ,
70
78
expression : [
71
- [ 'simpleExpression' ,
72
- '$$ = $1' ] ,
73
- [ 'simpleExpression WITH EXCEPTION' ,
74
- [ '$$ = { exception: $3 }' ,
75
- '$$.license = $1.license' ,
76
- 'if ($1.hasOwnProperty(\'plus\')) {' ,
77
- ' $$.plus = $1.plus' ,
78
- '}' ]
79
- . join ( '\n' ) ] ,
80
- [ 'expression AND expression' ,
81
- '$$ = { conjunction: \'and\', left: $1, right: $3 }' ] ,
82
- [ 'expression OR expression' ,
83
- '$$ = { conjunction: \'or\', left: $1, right: $3 }' ] ,
84
- [ 'OPEN expression CLOSE' ,
85
- '$$ = $2' ] ] } }
79
+ [ 'simpleExpression' , '$$ = $1' ] ,
80
+ [ 'simpleExpression WITH EXCEPTION' , [
81
+ '$$ = {exception: $3}' ,
82
+ '$$.license = $1.license' ,
83
+ 'if ($1.hasOwnProperty(\'plus\')) {' ,
84
+ ' $$.plus = $1.plus' ,
85
+ '}' ] . join ( '\n' ) ] ,
86
+ [
87
+ 'expression AND expression' ,
88
+ '$$ = {conjunction: \'and\', left: $1, right: $3}'
89
+ ] ,
90
+ [
91
+ 'expression OR expression' ,
92
+ '$$ = {conjunction: \'or\', left: $1, right: $3}'
93
+ ] ,
94
+ [ 'OPEN expression CLOSE' , '$$ = $2' ]
95
+ ]
96
+ }
97
+ }
86
98
87
99
console . log ( new Generator ( grammar , options ) . generate ( ) )
0 commit comments