melbourne 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/HISTORY +3 -0
  2. data/LICENSE +27 -0
  3. data/README.rdoc +38 -0
  4. data/Rakefile +38 -0
  5. data/VERSION.yml +4 -0
  6. data/ext/melbourne/bstring-license.txt +29 -0
  7. data/ext/melbourne/bstrlib.c +2918 -0
  8. data/ext/melbourne/bstrlib.h +302 -0
  9. data/ext/melbourne/extconf.rb +76 -0
  10. data/ext/melbourne/grammar.cpp +11885 -0
  11. data/ext/melbourne/grammar.hpp +14 -0
  12. data/ext/melbourne/grammar.y +6013 -0
  13. data/ext/melbourne/internal.hpp +137 -0
  14. data/ext/melbourne/lex.c.tab +136 -0
  15. data/ext/melbourne/local_state.hpp +41 -0
  16. data/ext/melbourne/melbourne.cpp +37 -0
  17. data/ext/melbourne/node.hpp +262 -0
  18. data/ext/melbourne/node_types.cpp +245 -0
  19. data/ext/melbourne/node_types.hpp +135 -0
  20. data/ext/melbourne/node_types.rb +190 -0
  21. data/ext/melbourne/quark.cpp +52 -0
  22. data/ext/melbourne/quark.hpp +14 -0
  23. data/ext/melbourne/symbols.cpp +219 -0
  24. data/ext/melbourne/symbols.hpp +116 -0
  25. data/ext/melbourne/var_table.cpp +113 -0
  26. data/ext/melbourne/var_table.hpp +33 -0
  27. data/ext/melbourne/visitor.cpp +1052 -0
  28. data/ext/melbourne/visitor.hpp +20 -0
  29. data/lib/melbourne/ast/constants.rb +128 -0
  30. data/lib/melbourne/ast/control_flow.rb +382 -0
  31. data/lib/melbourne/ast/data.rb +19 -0
  32. data/lib/melbourne/ast/definitions.rb +561 -0
  33. data/lib/melbourne/ast/exceptions.rb +182 -0
  34. data/lib/melbourne/ast/file.rb +15 -0
  35. data/lib/melbourne/ast/grapher.rb +75 -0
  36. data/lib/melbourne/ast/literals.rb +268 -0
  37. data/lib/melbourne/ast/node.rb +21 -0
  38. data/lib/melbourne/ast/operators.rb +117 -0
  39. data/lib/melbourne/ast/self.rb +17 -0
  40. data/lib/melbourne/ast/sends.rb +451 -0
  41. data/lib/melbourne/ast/values.rb +74 -0
  42. data/lib/melbourne/ast/variables.rb +251 -0
  43. data/lib/melbourne/ast.rb +22 -0
  44. data/lib/melbourne/parser.rb +38 -0
  45. data/lib/melbourne/processor.rb +460 -0
  46. data/lib/melbourne.rb +46 -0
  47. data/spec/helpers/ast/node.rb +15 -0
  48. data/spec/helpers/ast/reduced_graph.rb +64 -0
  49. data/spec/lib/parser/alias_spec.rb +97 -0
  50. data/spec/lib/parser/and_spec.rb +63 -0
  51. data/spec/lib/parser/array_spec.rb +157 -0
  52. data/spec/lib/parser/attrasgn_spec.rb +401 -0
  53. data/spec/lib/parser/back_ref_spec.rb +20 -0
  54. data/spec/lib/parser/call_spec.rb +958 -0
  55. data/spec/lib/parser/case_spec.rb +577 -0
  56. data/spec/lib/parser/cdecl_spec.rb +108 -0
  57. data/spec/lib/parser/class_spec.rb +221 -0
  58. data/spec/lib/parser/colon2_spec.rb +13 -0
  59. data/spec/lib/parser/colon3_spec.rb +12 -0
  60. data/spec/lib/parser/const_spec.rb +12 -0
  61. data/spec/lib/parser/cvar_spec.rb +55 -0
  62. data/spec/lib/parser/cvasgn_spec.rb +71 -0
  63. data/spec/lib/parser/cvdecl_spec.rb +31 -0
  64. data/spec/lib/parser/defined_spec.rb +353 -0
  65. data/spec/lib/parser/defn_spec.rb +1409 -0
  66. data/spec/lib/parser/defs_spec.rb +247 -0
  67. data/spec/lib/parser/dot2_spec.rb +29 -0
  68. data/spec/lib/parser/dot3_spec.rb +29 -0
  69. data/spec/lib/parser/dregx_spec.rb +127 -0
  70. data/spec/lib/parser/dstr_spec.rb +453 -0
  71. data/spec/lib/parser/dsym_spec.rb +31 -0
  72. data/spec/lib/parser/dxstr_spec.rb +31 -0
  73. data/spec/lib/parser/ensure_spec.rb +279 -0
  74. data/spec/lib/parser/false_spec.rb +12 -0
  75. data/spec/lib/parser/flip2_spec.rb +138 -0
  76. data/spec/lib/parser/flip3_spec.rb +100 -0
  77. data/spec/lib/parser/for_spec.rb +279 -0
  78. data/spec/lib/parser/gasgn_spec.rb +34 -0
  79. data/spec/lib/parser/gvar_spec.rb +33 -0
  80. data/spec/lib/parser/hash_spec.rb +77 -0
  81. data/spec/lib/parser/iasgn_spec.rb +54 -0
  82. data/spec/lib/parser/if_spec.rb +439 -0
  83. data/spec/lib/parser/iter_spec.rb +2582 -0
  84. data/spec/lib/parser/lasgn_spec.rb +1066 -0
  85. data/spec/lib/parser/lit_spec.rb +75 -0
  86. data/spec/lib/parser/masgn_spec.rb +1970 -0
  87. data/spec/lib/parser/match2_spec.rb +47 -0
  88. data/spec/lib/parser/match3_spec.rb +54 -0
  89. data/spec/lib/parser/match_spec.rb +19 -0
  90. data/spec/lib/parser/module_spec.rb +102 -0
  91. data/spec/lib/parser/nil_spec.rb +13 -0
  92. data/spec/lib/parser/not_spec.rb +39 -0
  93. data/spec/lib/parser/nth_ref_spec.rb +12 -0
  94. data/spec/lib/parser/op_asgn_spec.rb +619 -0
  95. data/spec/lib/parser/or_spec.rb +155 -0
  96. data/spec/lib/parser/postexe_spec.rb +31 -0
  97. data/spec/lib/parser/regex_spec.rb +52 -0
  98. data/spec/lib/parser/rescue_spec.rb +1028 -0
  99. data/spec/lib/parser/return_spec.rb +151 -0
  100. data/spec/lib/parser/sclass_spec.rb +172 -0
  101. data/spec/lib/parser/str_spec.rb +162 -0
  102. data/spec/lib/parser/super_spec.rb +276 -0
  103. data/spec/lib/parser/true_spec.rb +12 -0
  104. data/spec/lib/parser/undef_spec.rb +222 -0
  105. data/spec/lib/parser/until_spec.rb +286 -0
  106. data/spec/lib/parser/valias_spec.rb +12 -0
  107. data/spec/lib/parser/while_spec.rb +458 -0
  108. data/spec/lib/parser/xstr_spec.rb +12 -0
  109. data/spec/lib/parser/yield_spec.rb +202 -0
  110. data/spec/lib/parser/zsuper_spec.rb +101 -0
  111. data/spec/matchers/parse_as.rb +27 -0
  112. data/spec/spec_helper.rb +10 -0
  113. metadata +168 -0
@@ -0,0 +1,451 @@
1
+ module Melbourne
2
+
3
+ module AST
4
+
5
+ # A method invocation (a message being sent) as in:
6
+ #
7
+ # method()
8
+ #
9
+ class Send < Node
10
+
11
+ # The receiver of the message/ invocation
12
+ #
13
+ attr_accessor :receiver
14
+
15
+ # The message being sent/ method being invoked
16
+ #
17
+ attr_accessor :name
18
+
19
+ attr_accessor :privately #:nodoc:
20
+
21
+ # The block that is passed to the invocation if one is passed
22
+ #
23
+ attr_accessor :block
24
+
25
+ attr_accessor :variable #:nodoc:
26
+
27
+ attr_accessor :check_for_local #:nodoc:
28
+
29
+ def initialize(line, receiver, name, privately=false) #:nodoc:
30
+ @line = line
31
+ @receiver = receiver
32
+ @name = name
33
+ @privately = privately
34
+ @block = nil
35
+ @check_for_local = false
36
+ end
37
+
38
+ end
39
+
40
+ # A method invocation (a message being sent) where arguments are passed as in:
41
+ #
42
+ # method(a, b)
43
+ #
44
+ class SendWithArguments < Send
45
+
46
+ # The arguments that are passed
47
+ #
48
+ attr_accessor :arguments
49
+
50
+ def initialize(line, receiver, name, arguments, privately=false) #:nodoc:
51
+ super line, receiver, name, privately
52
+ @block = nil
53
+ @arguments = ActualArguments.new line, arguments
54
+ end
55
+
56
+ end
57
+
58
+ # An attribute assignment as in:
59
+ #
60
+ # object.attribute = 1
61
+ #
62
+ class AttributeAssignment < SendWithArguments
63
+
64
+ def initialize(line, receiver, name, arguments) #:nodoc:
65
+ @line = line
66
+
67
+ @receiver = receiver
68
+ @privately = receiver.kind_of?(Self) ? true : false
69
+
70
+ @name = :"#{name}="
71
+
72
+ @arguments = ActualArguments.new line, arguments
73
+ end
74
+
75
+ end
76
+
77
+ # An element assignment as in:
78
+ #
79
+ # a[0] = 1
80
+ #
81
+ class ElementAssignment < SendWithArguments
82
+
83
+ def initialize(line, receiver, arguments) #:nodoc:
84
+ @line = line
85
+
86
+ @receiver = receiver
87
+ @privately = receiver.kind_of?(Self) ? true : false
88
+
89
+ @name = :[]=
90
+
91
+ case arguments
92
+ when PushArgs
93
+ @arguments = arguments
94
+ else
95
+ @arguments = ActualArguments.new line, arguments
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ # Arguments of element assignment as in:
102
+ #
103
+ # a[0] = 1 # 1 is the push argument
104
+ #
105
+ class PushArgs < Node
106
+
107
+ # The actual value of the argument
108
+ #
109
+ attr_accessor :value
110
+
111
+ # The arguments used to access the element the push argument is assigned to
112
+ #
113
+ attr_accessor :arguments
114
+
115
+ def initialize(line, arguments, value) #:nodoc:
116
+ @line = line
117
+ @arguments = arguments
118
+ @value = value
119
+ end
120
+
121
+ # Gets the number of push arguments
122
+ #
123
+ # === Example:
124
+ #
125
+ # a[0] = 1 # size is 1
126
+ #
127
+ def size
128
+ splat? ? 1 : @arguments.size + 1
129
+ end
130
+
131
+ # Gets whether the push arguments are a splat (<tt>*some</tt>)
132
+ #
133
+ # === Example:
134
+ #
135
+ # a[0] = *b # the push arguments are a splat here
136
+ #
137
+ def splat?
138
+ @arguments.kind_of? SplatValue
139
+ end
140
+
141
+ end
142
+
143
+ # A block that is passed as a parameter as in:
144
+ #
145
+ # method(&b)
146
+ #
147
+ class BlockPass < Node
148
+
149
+ # The actual block that is passed
150
+ #
151
+ attr_accessor :body
152
+
153
+ def initialize(line, body) #:nodoc:
154
+ @line = line
155
+ @body = body
156
+ end
157
+
158
+ end
159
+
160
+ # The actual arguments that are passed to a method as in:
161
+ #
162
+ # method(1, b) # the actual arguments are a and b
163
+ #
164
+ class ActualArguments < Node
165
+
166
+ # The array of arguments
167
+ #
168
+ attr_accessor :array
169
+
170
+ # A splat (<tt>*some</tt>) if the actual arguments contain one
171
+ #
172
+ attr_accessor :splat
173
+
174
+ def initialize(line, arguments=nil) #:nodoc:
175
+ @line = line
176
+ @splat = nil
177
+
178
+ case arguments
179
+ when SplatValue
180
+ @splat = arguments
181
+ @array = []
182
+ when ConcatArgs
183
+ @array = arguments.array.body
184
+ @splat = SplatValue.new line, arguments.rest
185
+ when ArrayLiteral
186
+ @array = arguments.body
187
+ when nil
188
+ @array = []
189
+ else
190
+ @array = [arguments]
191
+ end
192
+ end
193
+
194
+ # Gets the number of actual arguments.
195
+ #
196
+ # === Example
197
+ #
198
+ # method(1, b) # the number of actual arguments is 2
199
+ #
200
+ def size
201
+ @array.size
202
+ end
203
+
204
+ # Gets whether the actual arguments contain a splat.
205
+ #
206
+ # === Example
207
+ #
208
+ # method(*b) # splat? is true
209
+ #
210
+ def splat?
211
+ not @splat.nil?
212
+ end
213
+
214
+ end
215
+
216
+ # A code block as in:
217
+ #
218
+ # m { some }
219
+ #
220
+ class Iter < Node
221
+
222
+ # The parent node of the code block
223
+ #
224
+ attr_accessor :parent
225
+
226
+ # The arguments of the code block
227
+ #
228
+ attr_accessor :arguments
229
+
230
+ # The body of the code block
231
+ #
232
+ attr_accessor :body
233
+
234
+ def initialize(line, arguments, body) #:nodoc:
235
+ @line = line
236
+ @arguments = IterArguments.new line, arguments
237
+ @body = body || Nil.new(line)
238
+ end
239
+
240
+ end
241
+
242
+ # Arguments of a code block as in:
243
+ #
244
+ # m { |arg| some }
245
+ #
246
+ class IterArguments < Node
247
+
248
+ # TODO: document!
249
+ attr_accessor :prelude #:nodoc:
250
+
251
+ # The number of arguments
252
+ #
253
+ attr_accessor :arity
254
+
255
+ # The optional arguments
256
+ #
257
+ attr_accessor :optional
258
+
259
+ # The actual array of arguments
260
+ #
261
+ attr_accessor :arguments
262
+
263
+ # TODO: document!
264
+ attr_accessor :splat_index #:nodoc:
265
+
266
+ # The required arguments
267
+ #
268
+ attr_accessor :required_args
269
+
270
+ def initialize(line, arguments) #:nodoc:
271
+ @line = line
272
+ @optional = 0
273
+
274
+ @splat_index = -1
275
+ @required_args = 0
276
+ @splat = nil
277
+ @block = nil
278
+
279
+ array = []
280
+ case arguments
281
+ when Fixnum
282
+ @arity = 0
283
+ @prelude = nil
284
+ when MAsgn
285
+ arguments.iter_arguments
286
+
287
+ if arguments.splat
288
+ @splat = arguments.splat = arguments.splat.value
289
+
290
+ @optional = 1
291
+ if arguments.left
292
+ @prelude = :multi
293
+ @arity = -(arguments.left.body.size + 1)
294
+ @required_args = arguments.left.body.size
295
+ else
296
+ @prelude = :splat
297
+ @arity = -1
298
+ end
299
+ elsif arguments.left
300
+ @prelude = :multi
301
+ @arity = arguments.left.body.size
302
+ @required_args = arguments.left.body.size
303
+ else
304
+ @prelude = :multi
305
+ @arity = -1
306
+ end
307
+
308
+ @block = arguments.block
309
+
310
+ @arguments = arguments
311
+ when nil
312
+ @arity = -1
313
+ @splat_index = -2 # -2 means accept the splat, but don't store it anywhere
314
+ @prelude = nil
315
+ when BlockPass
316
+ @arity = -1
317
+ @splat_index = -2
318
+ @prelude = nil
319
+ @block = arguments
320
+ else # Assignment
321
+ @arguments = arguments
322
+ @arity = 1
323
+ @required_args = 1
324
+ @prelude = :single
325
+ end
326
+ end
327
+
328
+ alias_method :total_args, :required_args
329
+
330
+ # TODO: document!
331
+ def names #:nodoc:
332
+ case @arguments
333
+ when MAsgn
334
+ if arguments = @arguments.left.body
335
+ array = arguments.map { |x| x.name }
336
+ else
337
+ array = []
338
+ end
339
+
340
+ if @arguments.splat.kind_of? SplatAssignment
341
+ array << @arguments.splat.name
342
+ end
343
+
344
+ array
345
+ when nil
346
+ []
347
+ else
348
+ [@arguments.name]
349
+ end
350
+ end
351
+
352
+ end
353
+
354
+ # A +for+ statement as in:
355
+ #
356
+ # for o in ary do
357
+ # puts(o)
358
+ # end
359
+ #
360
+ class For < Iter
361
+
362
+ end
363
+
364
+ # A negation as in:
365
+ #
366
+ # -1
367
+ #
368
+ class Negate < Node
369
+
370
+ # The value that is negated
371
+ #
372
+ attr_accessor :value
373
+
374
+ def initialize(line, value) #:nodoc:
375
+ @line = line
376
+ @value = value
377
+ end
378
+
379
+ end
380
+
381
+ # A +super+ statement as in:
382
+ #
383
+ # def method
384
+ # super
385
+ # end
386
+ #
387
+ class Super < SendWithArguments
388
+
389
+ # The name of the node
390
+ #
391
+ attr_accessor :name
392
+
393
+ # A block that is passed to <tt>super()</tt> if one is passed
394
+ #
395
+ attr_accessor :block
396
+
397
+ def initialize(line, arguments) #:nodoc:
398
+ @line = line
399
+ @block = nil
400
+ @arguments = ActualArguments.new line, arguments
401
+ end
402
+
403
+ end
404
+
405
+ # A +yield+ statement as in:
406
+ #
407
+ # def method(&block)
408
+ # yield 1
409
+ # end
410
+ #
411
+ class Yield < SendWithArguments
412
+
413
+ # TODO: document!
414
+ attr_accessor :flags #:nodoc:
415
+
416
+ def initialize(line, arguments, unwrap) #:nodoc:
417
+ @line = line
418
+
419
+ if arguments.kind_of? ArrayLiteral and not unwrap
420
+ arguments = ArrayLiteral.new line, [arguments]
421
+ end
422
+
423
+ @arguments = ActualArguments.new line, arguments
424
+ @argument_count = @arguments.size
425
+ @yield_splat = false
426
+
427
+ if @arguments.splat?
428
+ splat = @arguments.splat.value
429
+ if (splat.kind_of? ArrayLiteral or splat.kind_of? EmptyArray) and not unwrap
430
+ @argument_count += 1
431
+ else
432
+ @yield_splat = true
433
+ end
434
+ end
435
+ end
436
+
437
+ end
438
+
439
+ # TODO: document!
440
+ class ZSuper < Super #:nodoc:
441
+
442
+ def initialize(line)
443
+ @line = line
444
+ @block = nil
445
+ end
446
+
447
+ end
448
+
449
+ end
450
+
451
+ end
@@ -0,0 +1,74 @@
1
+ module Melbourne
2
+
3
+ module AST
4
+
5
+ # A value of a splat (<tt>*some</tt>) as in:
6
+ #
7
+ # *1 # the value of the splat is 1
8
+ #
9
+ class SplatValue < Node
10
+
11
+ # The actual value
12
+ #
13
+ attr_accessor :value
14
+
15
+ def initialize(line, value) #:nodoc:
16
+ @line = line
17
+ @value = value
18
+ end
19
+
20
+ end
21
+
22
+ # TODO: document!
23
+ class ConcatArgs < Node #:nodoc:
24
+
25
+ attr_accessor :array, :rest, :size
26
+
27
+ def initialize(line, array, rest)
28
+ @line = line
29
+ @array = array
30
+ @size = array.body.size
31
+ @rest = rest
32
+ end
33
+
34
+ end
35
+
36
+ # TODO: document!
37
+ class SValue < Node #:nodoc:
38
+
39
+ attr_accessor :value
40
+
41
+ def initialize(line, value)
42
+ @line = line
43
+ @value = value
44
+ end
45
+
46
+ end
47
+
48
+ # TODO: document!
49
+ class ToArray < Node #:nodoc:
50
+
51
+ attr_accessor :value
52
+
53
+ def initialize(line, value)
54
+ @line = line
55
+ @value = value
56
+ end
57
+
58
+ end
59
+
60
+ # TODO: document!
61
+ class ToString < Node #:nodoc:
62
+
63
+ attr_accessor :value
64
+
65
+ def initialize(line, value)
66
+ @line = line
67
+ @value = value
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ end