synvert-core 0.64.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/main.yml +1 -1
  3. data/.gitignore +4 -0
  4. data/CHANGELOG.md +5 -0
  5. data/Guardfile +11 -2
  6. data/README.md +2 -2
  7. data/Rakefile +15 -1
  8. data/lib/synvert/core/array_ext.rb +41 -0
  9. data/lib/synvert/core/engine/erb.rb +9 -8
  10. data/lib/synvert/core/node_ext.rb +47 -44
  11. data/lib/synvert/core/node_query/compiler/array.rb +34 -0
  12. data/lib/synvert/core/node_query/compiler/attribute.rb +51 -0
  13. data/lib/synvert/core/node_query/compiler/attribute_list.rb +24 -0
  14. data/lib/synvert/core/node_query/compiler/boolean.rb +23 -0
  15. data/lib/synvert/core/node_query/compiler/comparable.rb +79 -0
  16. data/lib/synvert/core/node_query/compiler/dynamic_attribute.rb +51 -0
  17. data/lib/synvert/core/node_query/compiler/expression.rb +88 -0
  18. data/lib/synvert/core/node_query/compiler/float.rb +23 -0
  19. data/lib/synvert/core/node_query/compiler/identifier.rb +41 -0
  20. data/lib/synvert/core/node_query/compiler/integer.rb +23 -0
  21. data/lib/synvert/core/node_query/compiler/invalid_operator_error.rb +7 -0
  22. data/lib/synvert/core/node_query/compiler/nil.rb +23 -0
  23. data/lib/synvert/core/node_query/compiler/parse_error.rb +7 -0
  24. data/lib/synvert/core/node_query/compiler/regexp.rb +37 -0
  25. data/lib/synvert/core/node_query/compiler/selector.rb +51 -0
  26. data/lib/synvert/core/node_query/compiler/string.rb +34 -0
  27. data/lib/synvert/core/node_query/compiler/symbol.rb +23 -0
  28. data/lib/synvert/core/node_query/compiler.rb +24 -0
  29. data/lib/synvert/core/node_query/lexer.rex +96 -0
  30. data/lib/synvert/core/node_query/lexer.rex.rb +293 -0
  31. data/lib/synvert/core/node_query/parser.racc.rb +518 -0
  32. data/lib/synvert/core/node_query/parser.y +84 -0
  33. data/lib/synvert/core/node_query.rb +36 -0
  34. data/lib/synvert/core/rewriter/action/delete_action.rb +4 -2
  35. data/lib/synvert/core/rewriter/action/replace_action.rb +4 -2
  36. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +2 -2
  37. data/lib/synvert/core/rewriter/action/wrap_action.rb +3 -2
  38. data/lib/synvert/core/rewriter/action.rb +2 -1
  39. data/lib/synvert/core/rewriter/helper.rb +5 -2
  40. data/lib/synvert/core/rewriter/instance.rb +25 -13
  41. data/lib/synvert/core/rewriter/scope/query_scope.rb +36 -0
  42. data/lib/synvert/core/rewriter/scope/within_scope.rb +1 -1
  43. data/lib/synvert/core/rewriter.rb +1 -0
  44. data/lib/synvert/core/version.rb +1 -1
  45. data/lib/synvert/core.rb +22 -5
  46. data/spec/synvert/core/engine/erb_spec.rb +2 -2
  47. data/spec/synvert/core/node_ext_spec.rb +36 -5
  48. data/spec/synvert/core/node_query/lexer_spec.rb +512 -0
  49. data/spec/synvert/core/node_query/parser_spec.rb +270 -0
  50. data/spec/synvert/core/rewriter/condition/if_only_exist_condition_spec.rb +1 -6
  51. data/spec/synvert/core/rewriter/helper_spec.rb +4 -1
  52. data/spec/synvert/core/rewriter/instance_spec.rb +24 -3
  53. data/spec/synvert/core/rewriter/scope/query_scope_spec.rb +74 -0
  54. data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +12 -9
  55. data/spec/synvert/core/rewriter_spec.rb +4 -2
  56. data/synvert-core-ruby.gemspec +5 -1
  57. metadata +75 -2
@@ -0,0 +1,518 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.6.0
4
+ # from Racc grammar file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+ module Synvert
9
+ module Core
10
+ module NodeQuery
11
+ class Parser < Racc::Parser
12
+
13
+ def initialize
14
+ @lexer = Lexer.new
15
+ end
16
+
17
+ def parse string
18
+ @lexer.parse string
19
+ do_parse
20
+ end
21
+
22
+ def next_token
23
+ @lexer.next_token
24
+ end
25
+ ##### State transition tables begin ###
26
+
27
+ racc_action_table = [
28
+ 6, 18, 19, 9, 59, 20, 21, 8, 6, 28,
29
+ 29, 8, 59, 51, 6, 49, 6, 23, 24, 8,
30
+ 59, 51, 3, 4, 5, 8, 30, 8, 31, 51,
31
+ 32, 44, 52, 53, 54, 55, 56, 57, 58, 8,
32
+ 52, 53, 54, 55, 56, 57, 58, 69, 52, 53,
33
+ 54, 55, 56, 57, 58, 6, 70, 72, 73, 59,
34
+ 77, 84, 85, 6, 86, 87, 8, 59, 51, 6,
35
+ 88, 6, 89, 90, 8, 59, 51, 10, 11, 12,
36
+ 8, nil, 8, nil, 51, nil, nil, 52, 53, 54,
37
+ 55, 56, 57, 58, nil, 52, 53, 54, 55, 56,
38
+ 57, 58, nil, 52, 53, 54, 55, 56, 57, 58,
39
+ 6, nil, nil, nil, 59, nil, nil, nil, 6, nil,
40
+ nil, 8, 59, 51, nil, nil, 6, nil, nil, 8,
41
+ 59, 51, nil, 67, nil, nil, nil, 8, nil, 51,
42
+ nil, nil, 52, 53, 54, 55, 56, 57, 58, nil,
43
+ 52, 53, 54, 55, 56, 57, 58, nil, 52, 53,
44
+ 54, 55, 56, 57, 58, 6, nil, nil, nil, 59,
45
+ nil, nil, nil, 6, nil, nil, 8, 59, 51, nil,
46
+ nil, 74, 6, nil, 8, nil, 51, nil, nil, 78,
47
+ 3, 4, 5, 8, nil, nil, nil, 52, 53, 54,
48
+ 55, 56, 57, 58, nil, 52, 53, 54, 55, 56,
49
+ 57, 58, 6, nil, nil, nil, 59, nil, nil, nil,
50
+ 6, nil, nil, 8, 59, 51, nil, nil, 80, 6,
51
+ 6, 8, nil, 51, 59, nil, 82, 3, 4, 5,
52
+ 8, 8, nil, 51, 52, 53, 54, 55, 56, 57,
53
+ 58, nil, 52, 53, 54, 55, 56, 57, 58, nil,
54
+ nil, 6, 52, 53, 54, 55, 56, 57, 58, 3,
55
+ 4, 5, 8, 6, nil, nil, nil, 6, nil, nil,
56
+ nil, 3, 4, 5, 8, 3, 4, 5, 8, 6,
57
+ nil, nil, nil, 6, nil, nil, nil, 3, 4, 5,
58
+ 8, 3, 4, 5, 8, 6, nil, nil, nil, 6,
59
+ nil, nil, nil, 3, 4, 5, 8, 3, 4, 5,
60
+ 8, 40, 33, 35, 34, 37, 36, 39, 38, 43,
61
+ 42, 41 ]
62
+
63
+ racc_action_check = [
64
+ 33, 6, 6, 1, 33, 7, 7, 6, 34, 17,
65
+ 17, 33, 34, 33, 0, 33, 35, 8, 9, 34,
66
+ 35, 34, 0, 0, 0, 0, 19, 35, 21, 35,
67
+ 22, 29, 33, 33, 33, 33, 33, 33, 33, 32,
68
+ 34, 34, 34, 34, 34, 34, 34, 42, 35, 35,
69
+ 35, 35, 35, 35, 35, 36, 43, 45, 46, 36,
70
+ 51, 71, 75, 37, 76, 77, 36, 37, 36, 2,
71
+ 79, 38, 81, 83, 37, 38, 37, 2, 2, 2,
72
+ 2, nil, 38, nil, 38, nil, nil, 36, 36, 36,
73
+ 36, 36, 36, 36, nil, 37, 37, 37, 37, 37,
74
+ 37, 37, nil, 38, 38, 38, 38, 38, 38, 38,
75
+ 39, nil, nil, nil, 39, nil, nil, nil, 40, nil,
76
+ nil, 39, 40, 39, nil, nil, 41, nil, nil, 40,
77
+ 41, 40, nil, 40, nil, nil, nil, 41, nil, 41,
78
+ nil, nil, 39, 39, 39, 39, 39, 39, 39, nil,
79
+ 40, 40, 40, 40, 40, 40, 40, nil, 41, 41,
80
+ 41, 41, 41, 41, 41, 49, nil, nil, nil, 49,
81
+ nil, nil, nil, 67, nil, nil, 49, 67, 49, nil,
82
+ nil, 49, 3, nil, 67, nil, 67, nil, nil, 67,
83
+ 3, 3, 3, 3, nil, nil, nil, 49, 49, 49,
84
+ 49, 49, 49, 49, nil, 67, 67, 67, 67, 67,
85
+ 67, 67, 69, nil, nil, nil, 69, nil, nil, nil,
86
+ 70, nil, nil, 69, 70, 69, nil, nil, 69, 4,
87
+ 86, 70, nil, 70, 86, nil, 70, 4, 4, 4,
88
+ 4, 86, nil, 86, 69, 69, 69, 69, 69, 69,
89
+ 69, nil, 70, 70, 70, 70, 70, 70, 70, nil,
90
+ nil, 5, 86, 86, 86, 86, 86, 86, 86, 5,
91
+ 5, 5, 5, 10, nil, nil, nil, 11, nil, nil,
92
+ nil, 10, 10, 10, 10, 11, 11, 11, 11, 12,
93
+ nil, nil, nil, 30, nil, nil, nil, 12, 12, 12,
94
+ 12, 30, 30, 30, 30, 31, nil, nil, nil, 44,
95
+ nil, nil, nil, 31, 31, 31, 31, 44, 44, 44,
96
+ 44, 23, 23, 23, 23, 23, 23, 23, 23, 23,
97
+ 23, 23 ]
98
+
99
+ racc_action_pointer = [
100
+ 12, 3, 67, 180, 227, 259, -6, -2, 13, 18,
101
+ 271, 275, 287, nil, nil, nil, nil, 2, nil, 7,
102
+ nil, 9, 16, 300, nil, nil, nil, nil, nil, 12,
103
+ 291, 303, 26, -2, 6, 14, 53, 61, 69, 108,
104
+ 116, 124, 30, 39, 307, 37, 38, nil, nil, 163,
105
+ nil, 27, nil, nil, nil, nil, nil, nil, nil, nil,
106
+ nil, nil, nil, nil, nil, nil, nil, 171, nil, 210,
107
+ 218, 41, nil, nil, nil, 44, 55, 49, nil, 52,
108
+ nil, 54, nil, 55, nil, nil, 228, nil, nil, nil,
109
+ nil, nil ]
110
+
111
+ racc_action_default = [
112
+ -49, -49, -5, -49, -49, -49, -16, -17, -49, -49,
113
+ -49, -49, -49, -4, -6, -7, -8, -15, -10, -49,
114
+ -11, -49, -49, -49, 92, -1, -2, -3, -9, -49,
115
+ -49, -49, -19, -49, -49, -49, -49, -49, -49, -49,
116
+ -49, -49, -49, -49, -49, -49, -49, -18, -20, -49,
117
+ -39, -49, -41, -42, -43, -44, -45, -46, -47, -48,
118
+ -21, -22, -23, -24, -25, -26, -27, -49, -28, -49,
119
+ -49, -49, -13, -14, -29, -49, -38, -49, -30, -49,
120
+ -31, -49, -32, -49, -12, -33, -49, -40, -34, -35,
121
+ -36, -37 ]
122
+
123
+ racc_goto_table = [
124
+ 1, 22, 13, 14, 15, 16, nil, 17, nil, nil,
125
+ 25, 26, 27, nil, 75, 2, nil, 2, 2, 2,
126
+ 2, nil, nil, nil, nil, 2, 2, 2, nil, nil,
127
+ 45, 46, 79, 47, 81, 83, nil, nil, nil, nil,
128
+ nil, nil, nil, nil, 71, 2, 2, nil, nil, nil,
129
+ nil, 91, nil, nil, nil, nil, nil, nil, nil, 2,
130
+ 48, 60, 61, 62, 63, 64, 65, 66, 68 ]
131
+
132
+ racc_goto_check = [
133
+ 1, 4, 1, 1, 1, 1, nil, 3, nil, nil,
134
+ 1, 1, 1, nil, 6, 2, nil, 2, 2, 2,
135
+ 2, nil, nil, nil, nil, 2, 2, 2, nil, nil,
136
+ 1, 1, 6, 3, 6, 6, nil, nil, nil, nil,
137
+ nil, nil, nil, nil, 1, 2, 2, nil, nil, nil,
138
+ nil, 6, nil, nil, nil, nil, nil, nil, nil, 2,
139
+ 5, 5, 5, 5, 5, 5, 5, 5, 5 ]
140
+
141
+ racc_goto_pointer = [
142
+ nil, 0, 15, 1, -7, 27, -35 ]
143
+
144
+ racc_goto_default = [
145
+ nil, nil, 50, 7, nil, 76, nil ]
146
+
147
+ racc_reduce_table = [
148
+ 0, 0, :racc_error,
149
+ 3, 42, :_reduce_1,
150
+ 3, 42, :_reduce_2,
151
+ 3, 42, :_reduce_3,
152
+ 2, 42, :_reduce_4,
153
+ 1, 42, :_reduce_5,
154
+ 2, 42, :_reduce_6,
155
+ 2, 42, :_reduce_7,
156
+ 2, 42, :_reduce_8,
157
+ 3, 43, :_reduce_9,
158
+ 2, 43, :_reduce_10,
159
+ 2, 43, :_reduce_11,
160
+ 6, 43, :_reduce_12,
161
+ 5, 43, :_reduce_13,
162
+ 5, 43, :_reduce_14,
163
+ 2, 43, :_reduce_15,
164
+ 1, 43, :_reduce_16,
165
+ 1, 43, :_reduce_17,
166
+ 4, 44, :_reduce_18,
167
+ 3, 44, :_reduce_19,
168
+ 3, 45, :_reduce_20,
169
+ 3, 45, :_reduce_21,
170
+ 3, 45, :_reduce_22,
171
+ 3, 45, :_reduce_23,
172
+ 3, 45, :_reduce_24,
173
+ 3, 45, :_reduce_25,
174
+ 3, 45, :_reduce_26,
175
+ 3, 45, :_reduce_27,
176
+ 3, 45, :_reduce_28,
177
+ 4, 45, :_reduce_29,
178
+ 4, 45, :_reduce_30,
179
+ 4, 45, :_reduce_31,
180
+ 4, 45, :_reduce_32,
181
+ 5, 45, :_reduce_33,
182
+ 5, 45, :_reduce_34,
183
+ 5, 45, :_reduce_35,
184
+ 5, 45, :_reduce_36,
185
+ 3, 47, :_reduce_37,
186
+ 1, 47, :_reduce_38,
187
+ 1, 46, :_reduce_none,
188
+ 3, 46, :_reduce_40,
189
+ 1, 46, :_reduce_41,
190
+ 1, 46, :_reduce_42,
191
+ 1, 46, :_reduce_43,
192
+ 1, 46, :_reduce_44,
193
+ 1, 46, :_reduce_45,
194
+ 1, 46, :_reduce_46,
195
+ 1, 46, :_reduce_47,
196
+ 1, 46, :_reduce_48 ]
197
+
198
+ racc_reduce_n = 49
199
+
200
+ racc_shift_n = 92
201
+
202
+ racc_token_table = {
203
+ false => 0,
204
+ :error => 1,
205
+ :tNODE_TYPE => 2,
206
+ :tATTRIBUTE => 3,
207
+ :tKEY => 4,
208
+ :tIDENTIFIER => 5,
209
+ :tIDENTIFIER_VALUE => 6,
210
+ :tINDEX => 7,
211
+ :tHAS => 8,
212
+ :tCOMMA => 9,
213
+ :tCHILD => 10,
214
+ :tSUBSEQUENT_SIBLING => 11,
215
+ :tNEXT_SIBLING => 12,
216
+ :tOPEN_ATTRIBUTE => 13,
217
+ :tCLOSE_ATTRIBUTE => 14,
218
+ :tOPEN_DYNAMIC_ATTRIBUTE => 15,
219
+ :tCLOSE_DYNAMIC_ATTRIBUTE => 16,
220
+ :tOPEN_ARRAY => 17,
221
+ :tCLOSE_ARRAY => 18,
222
+ :tOPEN_SELECTOR => 19,
223
+ :tCLOSE_SELECTOR => 20,
224
+ :tEQUAL => 21,
225
+ :tNOT_EQUAL => 22,
226
+ :tMATCH => 23,
227
+ :tNOT_MATCH => 24,
228
+ :tGREATER_THAN => 25,
229
+ :tGREATER_THAN_OR_EQUAL => 26,
230
+ :tLESS_THAN => 27,
231
+ :tLESS_THAN_OR_EQUAL => 28,
232
+ :tIN => 29,
233
+ :tNOT_IN => 30,
234
+ :tINCLUDES => 31,
235
+ :tARRAY_VALUE => 32,
236
+ :tDYNAMIC_ATTRIBUTE => 33,
237
+ :tBOOLEAN => 34,
238
+ :tFLOAT => 35,
239
+ :tINTEGER => 36,
240
+ :tNIL => 37,
241
+ :tREGEXP => 38,
242
+ :tSTRING => 39,
243
+ :tSYMBOL => 40 }
244
+
245
+ racc_nt_base = 41
246
+
247
+ racc_use_result_var = false
248
+
249
+ Racc_arg = [
250
+ racc_action_table,
251
+ racc_action_check,
252
+ racc_action_default,
253
+ racc_action_pointer,
254
+ racc_goto_table,
255
+ racc_goto_check,
256
+ racc_goto_default,
257
+ racc_goto_pointer,
258
+ racc_nt_base,
259
+ racc_reduce_table,
260
+ racc_token_table,
261
+ racc_shift_n,
262
+ racc_reduce_n,
263
+ racc_use_result_var ]
264
+
265
+ Racc_token_to_s_table = [
266
+ "$end",
267
+ "error",
268
+ "tNODE_TYPE",
269
+ "tATTRIBUTE",
270
+ "tKEY",
271
+ "tIDENTIFIER",
272
+ "tIDENTIFIER_VALUE",
273
+ "tINDEX",
274
+ "tHAS",
275
+ "tCOMMA",
276
+ "tCHILD",
277
+ "tSUBSEQUENT_SIBLING",
278
+ "tNEXT_SIBLING",
279
+ "tOPEN_ATTRIBUTE",
280
+ "tCLOSE_ATTRIBUTE",
281
+ "tOPEN_DYNAMIC_ATTRIBUTE",
282
+ "tCLOSE_DYNAMIC_ATTRIBUTE",
283
+ "tOPEN_ARRAY",
284
+ "tCLOSE_ARRAY",
285
+ "tOPEN_SELECTOR",
286
+ "tCLOSE_SELECTOR",
287
+ "tEQUAL",
288
+ "tNOT_EQUAL",
289
+ "tMATCH",
290
+ "tNOT_MATCH",
291
+ "tGREATER_THAN",
292
+ "tGREATER_THAN_OR_EQUAL",
293
+ "tLESS_THAN",
294
+ "tLESS_THAN_OR_EQUAL",
295
+ "tIN",
296
+ "tNOT_IN",
297
+ "tINCLUDES",
298
+ "tARRAY_VALUE",
299
+ "tDYNAMIC_ATTRIBUTE",
300
+ "tBOOLEAN",
301
+ "tFLOAT",
302
+ "tINTEGER",
303
+ "tNIL",
304
+ "tREGEXP",
305
+ "tSTRING",
306
+ "tSYMBOL",
307
+ "$start",
308
+ "expression",
309
+ "selector",
310
+ "attribute_list",
311
+ "attribute",
312
+ "value",
313
+ "array_value" ]
314
+
315
+ Racc_debug_parser = false
316
+
317
+ ##### State transition tables end #####
318
+
319
+ # reduce 0 omitted
320
+
321
+ def _reduce_1(val, _values)
322
+ Compiler::Expression.new(selector: val[0], rest: val[2], relationship: :child)
323
+ end
324
+
325
+ def _reduce_2(val, _values)
326
+ Compiler::Expression.new(selector: val[0], rest: val[2], relationship: :subsequent_sibling)
327
+ end
328
+
329
+ def _reduce_3(val, _values)
330
+ Compiler::Expression.new(selector: val[0], rest: val[2], relationship: :next_sibling)
331
+ end
332
+
333
+ def _reduce_4(val, _values)
334
+ Compiler::Expression.new(selector: val[0], rest: val[1], relationship: :descendant)
335
+ end
336
+
337
+ def _reduce_5(val, _values)
338
+ Compiler::Expression.new(selector: val[0])
339
+ end
340
+
341
+ def _reduce_6(val, _values)
342
+ Compiler::Expression.new(rest: val[1], relationship: :child)
343
+ end
344
+
345
+ def _reduce_7(val, _values)
346
+ Compiler::Expression.new(rest: val[1], relationship: :subsequent_sibling)
347
+ end
348
+
349
+ def _reduce_8(val, _values)
350
+ Compiler::Expression.new(rest: val[1], relationship: :next_sibling)
351
+ end
352
+
353
+ def _reduce_9(val, _values)
354
+ Compiler::Selector.new(node_type: val[0], attribute_list: val[1], index: val[2])
355
+ end
356
+
357
+ def _reduce_10(val, _values)
358
+ Compiler::Selector.new(node_type: val[0], index: val[1])
359
+ end
360
+
361
+ def _reduce_11(val, _values)
362
+ Compiler::Selector.new(attribute_list: val[0], index: val[1])
363
+ end
364
+
365
+ def _reduce_12(val, _values)
366
+ Compiler::Selector.new(node_type: val[0], attribute_list: val[1], has_expression: val[4])
367
+ end
368
+
369
+ def _reduce_13(val, _values)
370
+ Compiler::Selector.new(node_type: val[0], has_expression: val[3])
371
+ end
372
+
373
+ def _reduce_14(val, _values)
374
+ Compiler::Selector.new(attribute_list: val[0], has_expression: val[3])
375
+ end
376
+
377
+ def _reduce_15(val, _values)
378
+ Compiler::Selector.new(node_type: val[0], attribute_list: val[1])
379
+ end
380
+
381
+ def _reduce_16(val, _values)
382
+ Compiler::Selector.new(node_type: val[0])
383
+ end
384
+
385
+ def _reduce_17(val, _values)
386
+ Compiler::Selector.new(attribute_list: val[0])
387
+ end
388
+
389
+ def _reduce_18(val, _values)
390
+ Compiler::AttributeList.new(attribute: val[1], rest: val[3])
391
+ end
392
+
393
+ def _reduce_19(val, _values)
394
+ Compiler::AttributeList.new(attribute: val[1])
395
+ end
396
+
397
+ def _reduce_20(val, _values)
398
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :!=)
399
+ end
400
+
401
+ def _reduce_21(val, _values)
402
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :!~)
403
+ end
404
+
405
+ def _reduce_22(val, _values)
406
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :=~)
407
+ end
408
+
409
+ def _reduce_23(val, _values)
410
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :>=)
411
+ end
412
+
413
+ def _reduce_24(val, _values)
414
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :>)
415
+ end
416
+
417
+ def _reduce_25(val, _values)
418
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :<=)
419
+ end
420
+
421
+ def _reduce_26(val, _values)
422
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :<)
423
+ end
424
+
425
+ def _reduce_27(val, _values)
426
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :==)
427
+ end
428
+
429
+ def _reduce_28(val, _values)
430
+ Compiler::Attribute.new(key: val[0], value: val[2], operator: :includes)
431
+ end
432
+
433
+ def _reduce_29(val, _values)
434
+ Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: :!=)
435
+ end
436
+
437
+ def _reduce_30(val, _values)
438
+ Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: :==)
439
+ end
440
+
441
+ def _reduce_31(val, _values)
442
+ Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: :not_in)
443
+ end
444
+
445
+ def _reduce_32(val, _values)
446
+ Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: :in)
447
+ end
448
+
449
+ def _reduce_33(val, _values)
450
+ Compiler::Attribute.new(key: val[0], value: val[3], operator: :!=)
451
+ end
452
+
453
+ def _reduce_34(val, _values)
454
+ Compiler::Attribute.new(key: val[0], value: val[3], operator: :==)
455
+ end
456
+
457
+ def _reduce_35(val, _values)
458
+ Compiler::Attribute.new(key: val[0], value: val[3], operator: :not_in)
459
+ end
460
+
461
+ def _reduce_36(val, _values)
462
+ Compiler::Attribute.new(key: val[0], value: val[3], operator: :in)
463
+ end
464
+
465
+ def _reduce_37(val, _values)
466
+ Compiler::Array.new(value: val[0], rest: val[2])
467
+ end
468
+
469
+ def _reduce_38(val, _values)
470
+ Compiler::Array.new(value: val[0])
471
+ end
472
+
473
+ # reduce 39 omitted
474
+
475
+ def _reduce_40(val, _values)
476
+ Compiler::DynamicAttribute.new(value: val[1])
477
+ end
478
+
479
+ def _reduce_41(val, _values)
480
+ Compiler::Boolean.new(value: val[0])
481
+ end
482
+
483
+ def _reduce_42(val, _values)
484
+ Compiler::Float.new(value: val[0])
485
+ end
486
+
487
+ def _reduce_43(val, _values)
488
+ Compiler::Integer.new(value: val[0])
489
+ end
490
+
491
+ def _reduce_44(val, _values)
492
+ Compiler::Nil.new(value: val[0])
493
+ end
494
+
495
+ def _reduce_45(val, _values)
496
+ Compiler::Regexp.new(value: val[0])
497
+ end
498
+
499
+ def _reduce_46(val, _values)
500
+ Compiler::String.new(value: val[0])
501
+ end
502
+
503
+ def _reduce_47(val, _values)
504
+ Compiler::Symbol.new(value: val[0])
505
+ end
506
+
507
+ def _reduce_48(val, _values)
508
+ Compiler::Identifier.new(value: val[0])
509
+ end
510
+
511
+ def _reduce_none(val, _values)
512
+ val[0]
513
+ end
514
+
515
+ end # class Parser
516
+ end # module NodeQuery
517
+ end # module Core
518
+ end # module Synvert
@@ -0,0 +1,84 @@
1
+ class Synvert::Core::NodeQuery::Parser
2
+ options no_result_var
3
+ token tNODE_TYPE tATTRIBUTE tKEY tIDENTIFIER tIDENTIFIER_VALUE tINDEX tHAS tCOMMA
4
+ tCHILD tSUBSEQUENT_SIBLING tNEXT_SIBLING
5
+ tOPEN_ATTRIBUTE tCLOSE_ATTRIBUTE tOPEN_DYNAMIC_ATTRIBUTE tCLOSE_DYNAMIC_ATTRIBUTE tOPEN_ARRAY tCLOSE_ARRAY tOPEN_SELECTOR tCLOSE_SELECTOR
6
+ tEQUAL tNOT_EQUAL tMATCH tNOT_MATCH tGREATER_THAN tGREATER_THAN_OR_EQUAL tLESS_THAN tLESS_THAN_OR_EQUAL tIN tNOT_IN tINCLUDES
7
+ tARRAY_VALUE tDYNAMIC_ATTRIBUTE tBOOLEAN tFLOAT tINTEGER tNIL tREGEXP tSTRING tSYMBOL
8
+ rule
9
+ expression
10
+ : selector tCHILD expression { Compiler::Expression.new(selector: val[0], rest: val[2], relationship: :child) }
11
+ | selector tSUBSEQUENT_SIBLING expression { Compiler::Expression.new(selector: val[0], rest: val[2], relationship: :subsequent_sibling) }
12
+ | selector tNEXT_SIBLING expression { Compiler::Expression.new(selector: val[0], rest: val[2], relationship: :next_sibling) }
13
+ | selector expression { Compiler::Expression.new(selector: val[0], rest: val[1], relationship: :descendant) }
14
+ | selector { Compiler::Expression.new(selector: val[0]) }
15
+ | tCHILD expression { Compiler::Expression.new(rest: val[1], relationship: :child) }
16
+ | tSUBSEQUENT_SIBLING expression { Compiler::Expression.new(rest: val[1], relationship: :subsequent_sibling) }
17
+ | tNEXT_SIBLING expression { Compiler::Expression.new(rest: val[1], relationship: :next_sibling) }
18
+
19
+ selector
20
+ : tNODE_TYPE attribute_list tINDEX { Compiler::Selector.new(node_type: val[0], attribute_list: val[1], index: val[2]) }
21
+ | tNODE_TYPE tINDEX { Compiler::Selector.new(node_type: val[0], index: val[1]) }
22
+ | attribute_list tINDEX { Compiler::Selector.new(attribute_list: val[0], index: val[1]) }
23
+ | tNODE_TYPE attribute_list tHAS tOPEN_SELECTOR expression tCLOSE_SELECTOR { Compiler::Selector.new(node_type: val[0], attribute_list: val[1], has_expression: val[4]) }
24
+ | tNODE_TYPE tHAS tOPEN_SELECTOR expression tCLOSE_SELECTOR { Compiler::Selector.new(node_type: val[0], has_expression: val[3]) }
25
+ | attribute_list tHAS tOPEN_SELECTOR expression tCLOSE_SELECTOR { Compiler::Selector.new(attribute_list: val[0], has_expression: val[3]) }
26
+ | tNODE_TYPE attribute_list { Compiler::Selector.new(node_type: val[0], attribute_list: val[1]) }
27
+ | tNODE_TYPE { Compiler::Selector.new(node_type: val[0]) }
28
+ | attribute_list { Compiler::Selector.new(attribute_list: val[0]) }
29
+ ;
30
+
31
+ attribute_list
32
+ : tOPEN_ATTRIBUTE attribute tCLOSE_ATTRIBUTE attribute_list { Compiler::AttributeList.new(attribute: val[1], rest: val[3]) }
33
+ | tOPEN_ATTRIBUTE attribute tCLOSE_ATTRIBUTE { Compiler::AttributeList.new(attribute: val[1]) }
34
+ ;
35
+
36
+ attribute
37
+ : tKEY tNOT_EQUAL value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :!=) }
38
+ | tKEY tNOT_MATCH value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :!~) }
39
+ | tKEY tMATCH value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :=~) }
40
+ | tKEY tGREATER_THAN_OR_EQUAL value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :>=) }
41
+ | tKEY tGREATER_THAN value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :>) }
42
+ | tKEY tLESS_THAN_OR_EQUAL value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :<=) }
43
+ | tKEY tLESS_THAN value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :<) }
44
+ | tKEY tEQUAL value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :==) }
45
+ | tKEY tINCLUDES value { Compiler::Attribute.new(key: val[0], value: val[2], operator: :includes) }
46
+ | tKEY tNOT_EQUAL tOPEN_ARRAY tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: :!=) }
47
+ | tKEY tEQUAL tOPEN_ARRAY tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: :==) }
48
+ | tKEY tNOT_IN tOPEN_ARRAY tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: :not_in) }
49
+ | tKEY tIN tOPEN_ARRAY tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: Compiler::Array.new, operator: :in) }
50
+ | tKEY tNOT_EQUAL tOPEN_ARRAY array_value tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: val[3], operator: :!=) }
51
+ | tKEY tEQUAL tOPEN_ARRAY array_value tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: val[3], operator: :==) }
52
+ | tKEY tNOT_IN tOPEN_ARRAY array_value tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: val[3], operator: :not_in) }
53
+ | tKEY tIN tOPEN_ARRAY array_value tCLOSE_ARRAY { Compiler::Attribute.new(key: val[0], value: val[3], operator: :in) }
54
+
55
+ array_value
56
+ : value tCOMMA array_value { Compiler::Array.new(value: val[0], rest: val[2]) }
57
+ | value { Compiler::Array.new(value: val[0]) }
58
+
59
+ value
60
+ : selector
61
+ | tOPEN_DYNAMIC_ATTRIBUTE tDYNAMIC_ATTRIBUTE tCLOSE_DYNAMIC_ATTRIBUTE { Compiler::DynamicAttribute.new(value: val[1]) }
62
+ | tBOOLEAN { Compiler::Boolean.new(value: val[0]) }
63
+ | tFLOAT { Compiler::Float.new(value: val[0]) }
64
+ | tINTEGER { Compiler::Integer.new(value: val[0])}
65
+ | tNIL { Compiler::Nil.new(value: val[0]) }
66
+ | tREGEXP { Compiler::Regexp.new(value: val[0]) }
67
+ | tSTRING { Compiler::String.new(value: val[0]) }
68
+ | tSYMBOL { Compiler::Symbol.new(value: val[0]) }
69
+ | tIDENTIFIER_VALUE { Compiler::Identifier.new(value: val[0]) }
70
+ end
71
+
72
+ ---- inner
73
+ def initialize
74
+ @lexer = Lexer.new
75
+ end
76
+
77
+ def parse string
78
+ @lexer.parse string
79
+ do_parse
80
+ end
81
+
82
+ def next_token
83
+ @lexer.next_token
84
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # NodeQuery defines a node query language, which is a css like syntax for matching nodes.
4
+ #
5
+ # It supports the following selectors:
6
+ #
7
+ # * AST node type: +.class+, +.send+
8
+ # * attribute value: +[receiver = nil]+, +[message = create]+
9
+ # * attribute regex: <code>[key=~/\A:([^'"]+)\z/]</code>, <code>[key!~/\A:([^'"]+)\z/]</code>
10
+ # * attribute conditions: +[message != nil]+, +[value > 1]+, +[value >= 1]+, +[value < 1]+, +[value <= 1]+
11
+ # * nested attribute: +[caller.message = map]+, +[arguments.size = 2]+
12
+ # * first or last child: +.def:first-child+, +.send:last-child+
13
+ # * nth-child or nth-last-child: +.def:nth-child(2)+, +.send:nth-last-child(2)+
14
+ # * descendant: +.class .send+
15
+ # * child: +.class > .def+
16
+ # * following sibling: <code>.def:first-child + .def</code>
17
+ # * subsequnt sibling: +.def:first-child ~ .def+
18
+ # * has: +.class:has(.def)+
19
+ #
20
+ # It also supports some custom selectors:
21
+ #
22
+ # * nested selector: +.send[arguments = [size = 2][first = .sym][last = .hash]]+
23
+ # * array value: +.send[arguments = (a, b)]+
24
+ # * IN operator: +.send[message IN (try, try!)]+
25
+ # * NOT IN operator: +.send[message NOT IN (create, build)]+
26
+ # * INCLUDES operator: +.send[arguments INCLUDES &block]+
27
+ # * dynamic attribute value: +.hash > .pair[key={{value}}]+
28
+ #
29
+ # @example
30
+ # # it matches methods call nodes, like `puts message` or `p message`
31
+ # Synvert::Core::NodeQuery::Parser.parse('.send[receiver = nil][message IN (puts, p)]').query_nodes(node)
32
+ module Synvert::Core::NodeQuery
33
+ autoload :Compiler, 'synvert/core/node_query/compiler'
34
+ autoload :Lexer, 'synvert/core/node_query/lexer.rex'
35
+ autoload :Parser, 'synvert/core/node_query/parser.racc'
36
+ end
@@ -21,8 +21,10 @@ module Synvert::Core
21
21
 
22
22
  # Calculate the begin and end positions.
23
23
  def calculate_position
24
- @begin_pos = @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:begin_pos).min
25
- @end_pos = @selectors.map { |selector| @node.child_node_range(selector) }.compact.map(&:end_pos).max
24
+ @begin_pos = @selectors.map { |selector| @node.child_node_range(selector) }
25
+ .compact.map(&:begin_pos).min
26
+ @end_pos = @selectors.map { |selector| @node.child_node_range(selector) }
27
+ .compact.map(&:end_pos).max
26
28
  squeeze_spaces
27
29
  remove_comma
28
30
  end
@@ -24,8 +24,10 @@ module Synvert::Core
24
24
 
25
25
  # Calculate the begin the end positions.
26
26
  def calculate_position
27
- @begin_pos = @selectors.map { |selector| @node.child_node_range(selector).begin_pos }.min
28
- @end_pos = @selectors.map { |selector| @node.child_node_range(selector).end_pos }.max
27
+ @begin_pos = @selectors.map { |selector| @node.child_node_range(selector).begin_pos }
28
+ .min
29
+ @end_pos = @selectors.map { |selector| @node.child_node_range(selector).end_pos }
30
+ .max
29
31
  end
30
32
  end
31
33
  end
@@ -17,8 +17,8 @@ module Synvert::Core
17
17
  # @return [String] rewritten code.
18
18
  def rewritten_code
19
19
  @node.loc.expression.source_buffer.source[begin_pos...end_pos]
20
- .sub(Engine::ERUBY_STMT_SPLITTER, '@output_buffer.append= ')
21
- .sub(Engine::ERUBY_STMT_SPLITTER, Engine::ERUBY_EXPR_SPLITTER)
20
+ .sub(Engine::ERUBY_STMT_SPLITTER, '@output_buffer.append= ')
21
+ .sub(Engine::ERUBY_STMT_SPLITTER, Engine::ERUBY_EXPR_SPLITTER)
22
22
  end
23
23
 
24
24
  private
@@ -21,8 +21,9 @@ module Synvert::Core
21
21
  # @return [String] rewritten code.
22
22
  def rewritten_code
23
23
  "#{@code}\n#{' ' * @indent}" +
24
- @node.to_source.split("\n").map { |line| " #{line}" }.join("\n") +
25
- "\n#{' ' * @indent}end"
24
+ @node.to_source.split("\n").map { |line| " #{line}" }
25
+ .join("\n") +
26
+ "\n#{' ' * @indent}end"
26
27
  end
27
28
 
28
29
  private