Class: Rigrate::Parser
Defined Under Namespace
Modules: LexStatus, StringType, TokenType
Classes: Token
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Migration
#data_source, #join, #migrate, #minus, #self_eval, #union
Instance Attribute Details
#tokens ⇒ Object
Returns the value of attribute tokens.
32
33
34
|
# File 'lib/rigrate/parser.rb', line 32
def tokens
@tokens
end
|
Instance Method Details
#full_parse(tks) ⇒ Object
127
128
129
130
131
|
# File 'lib/rigrate/parser.rb', line 127
def full_parse(tks)
while tks.size > 0
parse_rs_or_migrate_exp(tks)
end
end
|
#get_token_type(str) ⇒ Object
266
267
268
|
# File 'lib/rigrate/parser.rb', line 266
def get_token_type(str)
TokenType.const_get(str.strip.upcase)
end
|
#is_a_token?(str) ⇒ Boolean
262
263
264
|
# File 'lib/rigrate/parser.rb', line 262
def is_a_token?(str)
TokenType.constants.include? str.strip.upcase.to_sym
end
|
#lex(str) ⇒ Object
load script str -> tokens
TODO there is a bug need to fixed, when comment str include FROM key word will praser error.
#parse_condition_exp(tks) ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/rigrate/parser.rb', line 214
def parse_condition_exp(tks)
token = tks.shift
return if token.nil?
if token.type == TokenType::ON
v1 = parse_rs_exp(tks)
if v1.nil?
raise ParserError.new("ON expression should end with a [RUBY_STR]")
end
else
tks.unshift token
return
end
v1
end
|
#parse_migrate_exp(tks) ⇒ Object
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
|
# File 'lib/rigrate/parser.rb', line 146
def parse_migrate_exp(tks)
token = tks.shift
if token.type == TokenType::FROM
v1 = parse_operate_exp(tks)
sub_token = tks.shift
if sub_token.type == TokenType::TO
v2 = parse_rs_exp(tks)
v3 = parse_condition_exp(tks)
v4 = parse_using_exp(tks)
migrate(v1, v2, v3, v4)
else
raise ParserError.new("Syntax Error: need TO expression")
end
end
end
|
#parse_operate_exp(tks) ⇒ Object
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
|
# File 'lib/rigrate/parser.rb', line 177
def parse_operate_exp(tks)
v1 = parse_rs_exp(tks)
while true
token = tks.shift
if token.type != TokenType::UNION &&
token.type != TokenType::JOIN &&
token.type != TokenType::MINUS
tks.unshift token
break
end
v2 = parse_rs_exp(tks)
if token.type == TokenType::UNION
v1 = union(v1, v2)
elsif token.type == TokenType::MINUS
v1 = minus(v1, v2)
elsif token.type == TokenType::JOIN
sub_token = tks.shift
if not sub_token.nil?
if sub_token.type == TokenType::ON
cond = tks.shift
v1 = join(v1, v2, cond.value)
else
tks.unshift sub_token
v1 = join(v1, v2)
end
else
v1 = join(v1, v2)
end
end
end
v1
end
|
#parse_rs_exp(tks) ⇒ Object
TODO return value should change
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/rigrate/parser.rb', line 251
def parse_rs_exp(tks)
token = tks.shift
return if token.nil?
if token.type == TokenType::RUBY_STR
return token.value
else
raise ParserError.new("Invalid Syntax")
end
end
|
#parse_rs_or_migrate_exp(tks) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/rigrate/parser.rb', line 133
def parse_rs_or_migrate_exp(tks)
token = tks.shift
if token.type == TokenType::RUBY_STR
v1 = self_eval token.value
else
tks.unshift token
v1 = parse_migrate_exp(tks)
end
v1
end
|
#parse_using_exp(tks) ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/rigrate/parser.rb', line 232
def parse_using_exp(tks)
token = tks.shift
return if token.nil?
if token.type == TokenType::USING
v1 = parse_rs_exp(tks)
if v1.nil?
raise ParserError.new("USING expression should end with a [RUBY_STR]")
end
else
tks.unshift token
return
end
v1
end
|
#parsing ⇒ Object
121
122
123
|
# File 'lib/rigrate/parser.rb', line 121
def parsing
full_parse tokens.dup
end
|