forked from wbbaddons/Tims-PackageServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversionComparator.jison
More file actions
113 lines (96 loc) · 3.49 KB
/
versionComparator.jison
File metadata and controls
113 lines (96 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright (C) 2013 - 2015 Tim Düsterhus
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
%lex
%options case-insensitive flex
%%
"&&" return '&&'
"||" return '||'
"(" return '('
")" return ')'
">=" return '>='
"<=" return '<='
"!=" return '!='
"!~" return '!~'
">" return '>'
"<" return '<'
"==" return '='
"=" return '='
"~" return '~'
"*" return '*'
"$v" return '$v'
[0-9]+\.[0-9]+\.[0-9]+(?:\s(?:a|alpha|b|beta|d|dev|rc|pl)\s[0-9]+)? return 'VERSION'
(a(?:lpha)?|b(?:eta)|d(?:dev)|rc|pl) return 'WORD'
\s+ /* skip */
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
%left '&&' '||'
%left '='
%left '<=' '>=' '=' '<' '>' '!=' '!~' '~'
%start ruleset
%%
// A valid ruleset is either an asterisk or an expression
ruleset :
'*' EOF { return "return true;"; }
| E EOF {
return "$v = $v || \"\"; \
$vv = $v.replace(/[ _]/g, '.').replace(/a(?:lpha)/i, -3).replace(/b(?:eta)?/i, -2).replace(/d(?:ev)?/i, -4).replace(/rc/i, -1).replace(/pl/i, 1).split(/\\./).map(function (item) { \
return parseInt(item, 10); \
}); \
for (var i = 0; i < 5; i++) if ($vv[i] == null) $vv[i] = 0; \
var helper = function helper(v1, v2) { \
for (var i = 0; i < 5; i++) { \
if ((parseInt(v1[i])) === parseInt(v2[i])) continue; \
if ((parseInt(v1[i])) < parseInt(v2[i])) return -1; \
if ((parseInt(v1[i])) > parseInt(v2[i])) return 1; \
} \
return 0; \
}; \
return " + $1 + ";"
}
;
// A valid version is either $v or a valid version number
V :
'$v' { $$ = '$vv'; }
| VERSION {
var version = $1.replace(/[ _]/g, '.').replace(/a(?:lpha)/i, -3).replace(/b(?:eta)?/i, -2).replace(/d(?:ev)?/i, -4).replace(/rc/i, -1).replace(/pl/i, 1).split(/\./).map(function (item) {
return parseInt(item, 10);
});
for (var i = 0; i < 5; i++) if (version[i] == null) version[i] = 0;
$$ = JSON.stringify(version);
}
;
// A valid expression is either a chain of ANDs, a chain of ORs or a valid subexpression
E : AND | OR | e ;
// A valid chain of ANDs is a subexpression followed by &&, followed by either a chain of ANDs or a subexpression
AND :
e '&&' e { $$ = '(' + $1 + $2 + $3 + ')'; }
| e '&&' AND { $$ = '(' + $1 + $2 + $3 + ')'; }
;
// A valid chain of ORs is a subexpression followed by ||, followed by either a chain of ORs or a subexpression
OR :
e '||' e { $$ = '(' + $1 + $2 + $3 + ')'; }
| e '||' OR { $$ = '(' + $1 + $2 + $3 + ')'; }
;
// A valid subexpression is either an expression in parentheses or a comparison
e :
'(' E ')' { $$ = $1 + $2 + $3; }
| '$v' '!~' WORD { $$ = '(!/' + $3 + '/i.test(' + $1 + '))'; }
| '$v' '~' WORD { $$ = '(/' + $3 + '/i.test(' + $1 + '))'; }
| V '!=' V { $$ = '(helper(' + $1 + ',' + $3 + ')' + $2 + '0)'; }
| V RELATION V RELATION V { $$ = '(helper(' + $1 + ',' + $3 + ')' + $2 + '0)&&(helper(' + $3 + ',' + $5 + ')' + $4 + '0)'; }
| V RELATION V { $$ = '(helper(' + $1 + ',' + $3 + ')' + $2 + '0)'; }
| V '=' V { $$ = '(helper(' + $1 + ',' + $3 + ')==0)'; }
;
RELATION : '<' | '>' | '<=' | '>=' ;