-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.rb
222 lines (186 loc) · 5.1 KB
/
parser.rb
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require 'strscan'
require 'forwardable'
require 'travis/conditions/v1/boolean'
require 'travis/conditions/v1/helper'
# var = 'type'
# | 'repo'
# | 'head_repo'
# | 'os'
# | 'dist'
# | 'group'
# | 'sudo'
# | 'language'
# | 'sender'
# | 'fork'
# | 'branch'
# | 'head_branch'
# | 'tag'
# | 'commit_message';
#
# func = 'env' || 'concat';
# pred = 'present' | 'blank';
#
# eq = '=' | '==' | '!=';
# re = '=~' | '~=` | '!~';
# in = 'in' | 'not in' | 'IN' | 'NOT IN';
# is = 'is' | 'is not' | 'IS' | 'IS NOT';
# or = 'or' | 'OR' | '||';
# and = 'and' | 'AND' | '&&';
#
# list = oprd | oprd ',' list;
# call = func '(' list ')';
# val = word | quoted;
#
# oprd = var | val | call;
#
# term = oprd is pred
# | oprd in '(' list ')'
# | oprd re regx
# | oprd eq oprd
# | oprd;
#
# expr = expr or expr
# | expr and expr
# | not expr
# | '(' expr ')'
# | term
module Travis
module Conditions
module V1
class Parser # rubocop:disable Metrics/ClassLength
extend Forwardable
include Helper
include Boolean
VAR = /type|repo|head_repo|os|dist|group|sudo|language|sender|fork|draft|
branch|head_branch|tag|commit_message/x
PRED = /present|blank|true|false/i
FUNC = /env|concat/i
IN = /in|not in/i
IS = /is not|is/i
EQ = /==|=/
NEQ = /!=/
RE = /=~|~=/
NRE = /!~/
COMMA = /,/
WORD = /[^\s\(\)"',=!]+/
CONT = /\\\s*[\n\r]/
OP = {
'=' => :eq,
'==' => :eq,
'!=' => :not_eq,
'=~' => :match,
'~=' => :match,
'!~' => :not_match
}.freeze
MSGS = { # rubocop:disable
invalid: 'Invalid condition: %p',
parse_error: 'Could not parse %s',
shell_var: 'Variable names cannot start with a dollar (shell code does not work). If you are trying to compare to an env var, please use env("name")',
shell_str: 'Strings cannot start with a dollar (shell code does not work). This can be bypassed by quoting the string.'
}.freeze
def_delegators :str, :rest, :scan, :skip, :string, :pos, :peek
attr_reader :str
def initialize(str)
raise ArgumentError, MSGS[:invalid] % [str] unless str.is_a?(String)
@str = StringScanner.new(filter(str))
end
def filter(str)
str.gsub(CONT, ' ')
end
def parse
res = expr
error(:parse_error, string.inspect) unless res && !str.rest?
res
end
def term
lft = operand
lst = in_list(lft) and return lst
prd = is_pred(lft) and return prd
op = re and return [op, lft, regex]
op = eq and return [op, lft, operand]
lft
end
def operand
op = space { var || call || val }
op or err('an operand')
end
def regex
val = call
return [:reg, val] if val
return unless (reg = space { Regex.new(rest).scan })
str.pos = str.pos + reg.size
[:reg, reg.gsub(%r{^/|/$}, '')] # or err('an operand')
end
def eq
op = space { scan(EQ) || scan(NEQ) } and OP[op]
end
def re
op = space { scan(RE) || scan(NRE) } and OP[op]
end
def var
pos = str.pos
var = scan(VAR)
error(:shell_var) if var && var[0] == '$'
return [:var, var.downcase.to_sym] if var && boundary?
str.pos = pos
nil
end
BOUND = /[\s,=)|]/
def boundary?
peek(1) =~ BOUND || str.eos?
end
def call
return unless (name = func)
args = parens { list }
args or return
[:call, name.to_sym, args]
end
def val
val = quoted || word and [:val, val]
end
def is_pred(term)
op = is or return
pr = pred or return
[op.downcase.sub(' ', '_').to_sym, term, pr.downcase.to_sym]
end
def in_list(term)
op = self.in or return
list = parens { list! }
[op.downcase.sub(' ', '_').to_sym, term, list]
end
def list!
list.tap { |list| err 'a list of values' if list.empty? }
end
def list
return [] unless (item = var || call || val)
list = comma ? [item] + self.list : [item]
skip(COMMA)
list.compact
end
def func
space { scan(FUNC) }
end
def pred
space { scan(PRED)&.to_sym }
end
def word
str = space { scan(WORD) }
error(:shell_str) if str && str[0] == '$'
str
end
def in
space { scan(IN) }
end
def is
space { scan(IS) }
end
def comma
space { scan(COMMA) }
end
def error(key, *vals)
raise ParseError, MSGS[key] % vals
end
end
end
end
end