antlr3 1.6.0 → 1.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,61 +11,54 @@ uses Readline (if available) or standard IO#gets to fetch data on demand.
11
11
  =end
12
12
 
13
13
  class InteractiveStringStream < StringStream
14
- def initialize(options = {}, &block)
14
+ def initialize( options = {}, &block )
15
15
  @data = ''
16
- @size = @position = @column = @mark_depth = 0
16
+ @position = @column = @mark_depth = 0
17
17
  @line = 1
18
18
  @markers = []
19
+ mark
19
20
  @initialized = @eof = false
20
- @last_marker = @name = nil
21
- @readline = block or raise(ArgumentError, "no block was provided")
22
- @name = options.fetch(:name, '(interactive)')
23
- end
24
-
25
- def <<(data)
26
- tail = data.to_s
27
- @size += tail.length
28
- @data << tail
21
+ @readline = block or raise( ArgumentError, "no line-reading block was provided" )
22
+ @name = options.fetch( :name, '(interactive)' )
29
23
  end
30
24
 
31
25
  def consume
32
- @position < @size and return(super)
26
+ @position < @data.size and return( super )
33
27
  unless @eof
34
28
  readline
35
29
  consume
36
30
  end
37
31
  end
38
32
 
39
- def peek(i = 1)
33
+ def peek( i = 1 )
40
34
  i.zero? and return 0
41
35
  i += 1 if i < 0
42
36
  index = @position + i - 1
43
37
  index < 0 and return 0
44
38
 
45
- if index < @size
46
- char = @data[index]
39
+ if index < @data.size
40
+ char = @data[ index ]
47
41
  elsif readline
48
- peek(i)
42
+ peek( i )
49
43
  else EOF
50
44
  end
51
45
  end
52
46
 
53
- def look(i = 1)
54
- peek(i).chr rescue EOF
47
+ def look( i = 1 )
48
+ peek( i ).chr rescue EOF
55
49
  end
56
50
 
57
- alias >> peek
58
-
59
- def substring(start, stop)
60
- fill_through(stop)
61
- @data[start..stop]
51
+ def substring( start, stop )
52
+ fill_through( stop )
53
+ @data[ start..stop ]
62
54
  end
63
55
 
64
- private
65
- def fill_through(position)
56
+ private
57
+
58
+ def fill_through( position )
66
59
  @eof and return
67
60
  if position < 0 then fill_out
68
- else readline until (@size > position or @eof)
61
+ else readline until ( @data.size > position or @eof )
69
62
  end
70
63
  end
71
64
 
@@ -73,7 +66,7 @@ class InteractiveStringStream < StringStream
73
66
  @initialized = true
74
67
  unless @eof
75
68
  if line = @readline.call
76
- self << line.to_s
69
+ @data << line.to_s
77
70
  return true
78
71
  else
79
72
  @eof = true
@@ -4,7 +4,7 @@
4
4
  =begin LICENSE
5
5
 
6
6
  [The "BSD licence"]
7
- Copyright (c) 2009 Kyle Yetter
7
+ Copyright (c) 2009-2010 Kyle Yetter
8
8
  All rights reserved.
9
9
 
10
10
  Redistribution and use in source and binary forms, with or without
@@ -59,8 +59,8 @@ define specific implementations of stream edits.
59
59
 
60
60
  =end
61
61
 
62
- unless defined?(RewriteOperation)
63
- RewriteOperation = Struct.new(:stream, :location, :text)
62
+ unless defined?( RewriteOperation )
63
+ RewriteOperation = Struct.new( :stream, :location, :text )
64
64
  end
65
65
 
66
66
  class RewriteOperation
@@ -79,7 +79,7 @@ class RewriteOperation
79
79
 
80
80
  # TODO: document
81
81
  def inspect
82
- return "(%s @ %p : %p)" % [name, location, text]
82
+ return "(%s @ %p : %p)" % [ name, location, text ]
83
83
  end
84
84
  end
85
85
 
@@ -98,9 +98,9 @@ class InsertBefore < RewriteOperation
98
98
  alias index location
99
99
  alias index= location=
100
100
 
101
- def execute(buffer)
101
+ def execute( buffer )
102
102
  buffer << text.to_s
103
- token = stream[location]
103
+ token = stream[ location ]
104
104
  buffer << token.text.to_s if token
105
105
  return location + 1
106
106
  end
@@ -117,23 +117,23 @@ indexed within the range <tt>op.index .. op.last_index</tt>
117
117
 
118
118
  class Replace < RewriteOperation
119
119
  @operation_name = 'replace'.freeze
120
- def initialize(stream, location, text)
121
- super(stream, nil, text)
120
+ def initialize( stream, location, text )
121
+ super( stream, nil, text )
122
122
  self.location = location
123
123
  end
124
124
 
125
- def location=(val)
125
+ def location=( val )
126
126
  case val
127
- when Range then super(val)
127
+ when Range then super( val )
128
128
  else
129
129
  val = val.to_i
130
- super(val..val)
130
+ super( val..val )
131
131
  end
132
132
  end
133
133
 
134
- def execute(buffer)
134
+ def execute( buffer )
135
135
  buffer << text.to_s unless text.nil?
136
- return(location.end + 1)
136
+ return( location.end + 1 )
137
137
  end
138
138
 
139
139
  def index
@@ -153,45 +153,45 @@ and do not add any text to the rewrite buffer
153
153
 
154
154
  class Delete < Replace
155
155
  @operation_name = 'delete'.freeze
156
- def initialize(stream, location)
157
- super(stream, location, nil)
156
+ def initialize( stream, location )
157
+ super( stream, location, nil )
158
158
  end
159
159
  end
160
160
 
161
161
  class RewriteProgram
162
- def initialize(stream, name = nil)
162
+ def initialize( stream, name = nil )
163
163
  @stream = stream
164
164
  @name = name
165
165
  @operations = []
166
166
  end
167
167
 
168
- def replace(*range_arguments)
169
- range, text = cast_range(range_arguments, 1)
168
+ def replace( *range_arguments )
169
+ range, text = cast_range( range_arguments, 1 )
170
170
 
171
- op = Replace.new(@stream, range, text)
171
+ op = Replace.new( @stream, range, text )
172
172
  @operations << op
173
173
  return op
174
174
  end
175
175
 
176
- def insert_before(index, text)
176
+ def insert_before( index, text )
177
177
  index = index.to_i
178
178
  index < 0 and index += @stream.length
179
- op = InsertBefore.new(@stream, index, text)
179
+ op = InsertBefore.new( @stream, index, text )
180
180
  @operations << op
181
181
  return op
182
182
  end
183
183
 
184
- def insert_after(index, text)
184
+ def insert_after( index, text )
185
185
  index = index.to_i
186
186
  index < 0 and index += @stream.length
187
- op = InsertBefore.new(@stream, index + 1, text)
187
+ op = InsertBefore.new( @stream, index + 1, text )
188
188
  @operations << op
189
189
  return op
190
190
  end
191
191
 
192
- def delete(*range_arguments)
193
- range, = cast_range(range_arguments)
194
- op = Delete.new(@stream, range)
192
+ def delete( *range_arguments )
193
+ range, = cast_range( range_arguments )
194
+ op = Delete.new( @stream, range )
195
195
  @operations << op
196
196
  return op
197
197
  end
@@ -211,12 +211,12 @@ class RewriteProgram
211
211
 
212
212
  case prior_operation
213
213
  when InsertBefore
214
- location.include?(prior_location)
214
+ location.include?( prior_location )
215
215
  when Replace
216
- if location.covers?(prior_location)
216
+ if location.covers?( prior_location )
217
217
  true
218
- elsif location.overlaps?(prior_location)
219
- conflict!(operation, prior_operation)
218
+ elsif location.overlaps?( prior_location )
219
+ conflict!( operation, prior_operation )
220
220
  end
221
221
  end
222
222
  end
@@ -234,35 +234,35 @@ class RewriteProgram
234
234
  if location == prior_location.first
235
235
  prior_operation.text = operation.text << prior_operation.text.to_s
236
236
  operation = nil
237
- break(false)
238
- elsif prior_location.include?(location)
239
- conflict!(operation, prior_operation)
237
+ break( false )
238
+ elsif prior_location.include?( location )
239
+ conflict!( operation, prior_operation )
240
240
  end
241
241
  end
242
242
  end
243
243
  end
244
244
 
245
- reduced.unshift(operation) if operation
245
+ reduced.unshift( operation ) if operation
246
246
  end
247
247
 
248
- @operations.replace(reduced)
248
+ @operations.replace( reduced )
249
249
 
250
- @operations.inject({}) do |map, operation|
251
- other_operaiton = map[operation.index] and
252
- ANTLR3.bug!( Util.tidy(<<-END) % [self.class, operation, other_operaiton] )
250
+ @operations.inject( {} ) do |map, operation|
251
+ other_operaiton = map[ operation.index ] and
252
+ ANTLR3.bug!( Util.tidy( <<-END ) % [ self.class, operation, other_operaiton ] )
253
253
  | %s#reduce! should have left only one operation per index,
254
254
  | but %p conflicts with %p
255
255
  END
256
- map[operation.index] = operation
256
+ map[ operation.index ] = operation
257
257
  map
258
258
  end
259
259
  end
260
260
 
261
- def execute(*range_arguments)
261
+ def execute( *range_arguments )
262
262
  if range_arguments.empty?
263
263
  range = 0 ... @stream.length
264
264
  else
265
- range, = cast_range(range_arguments)
265
+ range, = cast_range( range_arguments )
266
266
  end
267
267
 
268
268
  output = ''
@@ -272,19 +272,19 @@ class RewriteProgram
272
272
  operations = reduce
273
273
 
274
274
  cursor = range.first
275
- while range.include?(cursor)
276
- if operation = operations.delete(cursor)
277
- cursor = operation.execute(output)
275
+ while range.include?( cursor )
276
+ if operation = operations.delete( cursor )
277
+ cursor = operation.execute( output )
278
278
  else
279
- token = tokens[cursor]
279
+ token = tokens[ cursor ]
280
280
  output << token.text if token
281
281
  cursor += 1
282
282
  end
283
283
  end
284
- if operation = operations.delete(cursor) and
285
- operation.is_a?(InsertBefore)
284
+ if operation = operations.delete( cursor ) and
285
+ operation.is_a?( InsertBefore )
286
286
  # catch edge 'insert-after' operations
287
- operation.execute(output)
287
+ operation.execute( output )
288
288
  end
289
289
 
290
290
  return output
@@ -294,51 +294,51 @@ class RewriteProgram
294
294
  @operations.clear
295
295
  end
296
296
 
297
- def undo(number_of_operations = 1)
298
- @operations.pop(number_of_operations)
297
+ def undo( number_of_operations = 1 )
298
+ @operations.pop( number_of_operations )
299
299
  end
300
300
 
301
- def conflict!(current, previous)
302
- message = 'operation %p overlaps with previous operation %p' % [current, previous]
303
- raise(RangeError, message, caller)
301
+ def conflict!( current, previous )
302
+ message = 'operation %p overlaps with previous operation %p' % [ current, previous ]
303
+ raise( RangeError, message, caller )
304
304
  end
305
305
 
306
- def cast_range(args, extra = 0)
306
+ def cast_range( args, extra = 0 )
307
307
  single, pair = extra + 1, extra + 2
308
- case check_arguments(args, single, pair)
308
+ case check_arguments( args, single, pair )
309
309
  when single
310
310
  loc = args.shift
311
311
 
312
- if loc.is_a?(Range)
312
+ if loc.is_a?( Range )
313
313
  first, last = loc.first.to_i, loc.last.to_i
314
314
  loc.exlude_end? and last -= 1
315
- return cast_range(args.unshift(first, last), extra)
315
+ return cast_range( args.unshift( first, last ), extra )
316
316
  else
317
317
  loc = loc.to_i
318
- return cast_range(args.unshift(loc, loc), extra)
318
+ return cast_range( args.unshift( loc, loc ), extra )
319
319
  end
320
320
  when pair
321
- first, last = args.shift(2).map! { |arg| arg.to_i }
321
+ first, last = args.shift( 2 ).map! { |arg| arg.to_i }
322
322
  if first < 0 and last < 0
323
323
  first += @stream.length
324
324
  last += @stream.length
325
325
  else
326
326
  last < 0 and last += @stream.length
327
- first = first.at_least(0)
327
+ first = first.at_least( 0 )
328
328
  end
329
- return(args.unshift(first .. last))
329
+ return( args.unshift( first .. last ) )
330
330
  end
331
331
  end
332
332
 
333
- def check_arguments(args, min, max)
333
+ def check_arguments( args, min, max )
334
334
  n = args.length
335
335
  if n < min
336
336
  raise ArgumentError,
337
- "wrong number of arguments (#{args.length} for #{min})",
337
+ "wrong number of arguments (#{ args.length } for #{ min })",
338
338
  caller
339
339
  elsif n > max
340
340
  raise ArgumentError,
341
- "wrong number of arguments (#{args.length} for #{max})",
341
+ "wrong number of arguments (#{ args.length } for #{ max })",
342
342
  caller
343
343
  else return n
344
344
  end
@@ -349,64 +349,64 @@ end
349
349
 
350
350
  attr_reader :programs
351
351
 
352
- def initialize(token_source, options = {})
353
- super(token_source, options)
352
+ def initialize( token_source, options = {} )
353
+ super( token_source, options )
354
354
 
355
355
  @programs = Hash.new do |programs, name|
356
- if name.is_a?(String)
357
- programs[name] = RewriteProgram.new(self, name)
358
- else programs[name.to_s]
356
+ if name.is_a?( String )
357
+ programs[ name ] = RewriteProgram.new( self, name )
358
+ else programs[ name.to_s ]
359
359
  end
360
360
  end
361
361
 
362
362
  @last_rewrite_token_indexes = {}
363
363
  end
364
364
 
365
- def rewrite(program_name = 'default', range = nil)
366
- program = @programs[program_name]
365
+ def rewrite( program_name = 'default', range = nil )
366
+ program = @programs[ program_name ]
367
367
  if block_given?
368
- yield(program)
369
- program.execute(range)
368
+ yield( program )
369
+ program.execute( range )
370
370
  else program
371
371
  end
372
372
  end
373
373
 
374
- def program(name = 'default')
375
- return @programs[name]
374
+ def program( name = 'default' )
375
+ return @programs[ name ]
376
376
  end
377
377
 
378
- def delete_program(name = 'default')
379
- @programs.delete(name)
378
+ def delete_program( name = 'default' )
379
+ @programs.delete( name )
380
380
  end
381
381
 
382
- def original_string(start = 0, finish = size - 1)
382
+ def original_string( start = 0, finish = size - 1 )
383
383
  @position == -1 and fill_buffer
384
384
 
385
- return(self[start..finish].map { |t| t.text }.join(''))
385
+ return( self[ start..finish ].map { |t| t.text }.join( '' ) )
386
386
  end
387
387
 
388
- def insert_before(*args)
389
- @programs['default'].insert_before(*args)
388
+ def insert_before( *args )
389
+ @programs[ 'default' ].insert_before( *args )
390
390
  end
391
391
 
392
- def insert_after(*args)
393
- @programs['default'].insert_after(*args)
392
+ def insert_after( *args )
393
+ @programs[ 'default' ].insert_after( *args )
394
394
  end
395
395
 
396
- def replace(*args)
397
- @programs['default'].replace(*args)
396
+ def replace( *args )
397
+ @programs[ 'default' ].replace( *args )
398
398
  end
399
399
 
400
- def delete(*args)
401
- @programs['default'].delete(*args)
400
+ def delete( *args )
401
+ @programs[ 'default' ].delete( *args )
402
402
  end
403
403
 
404
- def render(*arguments)
404
+ def render( *arguments )
405
405
  case arguments.first
406
406
  when String, Symbol then name = arguments.shift.to_s
407
407
  else name = 'default'
408
408
  end
409
- @programs[name].execute(*arguments)
409
+ @programs[ name ].execute( *arguments )
410
410
  end
411
411
  end
412
412
  end