Class: ANTLR3::Rewrite::RewriteProgram

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr3/streams/rewrite.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream, name = nil) ⇒ RewriteProgram

Returns a new instance of RewriteProgram.



165
166
167
168
169
# File 'lib/antlr3/streams/rewrite.rb', line 165

def initialize( stream, name = nil )
  @stream = stream
  @name = name
  @operations = []
end

Instance Method Details

#clearObject



296
297
298
# File 'lib/antlr3/streams/rewrite.rb', line 296

def clear
  @operations.clear
end

#delete(*range_arguments) ⇒ Object



195
196
197
198
199
200
# File 'lib/antlr3/streams/rewrite.rb', line 195

def delete( *range_arguments )
  range, = cast_range( range_arguments )
  op = Delete.new( @stream, range )
  @operations << op
  return op
end

#execute(*range_arguments) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/antlr3/streams/rewrite.rb', line 264

def execute( *range_arguments )
  if range_arguments.empty?
    range = 0 ... @stream.length
  else
    range, = cast_range( range_arguments )
  end
  
  output = ''
  
  tokens = @stream.tokens
  
  operations = reduce
  
  cursor = range.first
  while range.include?( cursor )
    if operation = operations.delete( cursor )
      cursor = operation.execute( output )
    else
      token = tokens[ cursor ]
      output << token.text if token
      cursor += 1
    end
  end
  if operation = operations.delete( cursor ) and
     operation.is_a?( InsertBefore )
    # catch edge 'insert-after' operations
    operation.execute( output )
  end
  
  return output
end

#insert_after(index, text) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/antlr3/streams/rewrite.rb', line 187

def insert_after( index, text )
  index = index.to_i
  index < 0 and index += @stream.length
  op = InsertBefore.new( @stream, index + 1, text )
  @operations << op
  return op
end

#insert_before(index, text) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/antlr3/streams/rewrite.rb', line 179

def insert_before( index, text )
  index = index.to_i
  index < 0 and index += @stream.length
  op = InsertBefore.new( @stream, index, text )
  @operations << op
  return op
end

#reduceObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/antlr3/streams/rewrite.rb', line 202

def reduce
  operations = @operations.reverse
  reduced = []
  
  until operations.empty?
    operation = operations.shift
    location = operation.location
    
    case operation
    when Replace
      operations.delete_if do |prior_operation|
        prior_location = prior_operation.location
        
        case prior_operation
        when InsertBefore
          location.include?( prior_location )
        when Replace
          if location.covers?( prior_location )
            true
          elsif location.overlaps?( prior_location )
            conflict!( operation, prior_operation )
          end
        end
      end
    when InsertBefore
      operations.delete_if do |prior_operation|
        prior_location = prior_operation.location
        
        case prior_operation
        when InsertBefore
          if prior_location == location
            operation.text += prior_operation.text
            true
          end
        when Replace
          if location == prior_location.first
            prior_operation.text = operation.text << prior_operation.text.to_s
            operation = nil
            break( false )
          elsif prior_location.include?( location )
            conflict!( operation, prior_operation )
          end
        end
      end
    end
    
    reduced.unshift( operation ) if operation
  end
  
  @operations.replace( reduced )
  
  @operations.inject( {} ) do |map, operation|
    other_operaiton = map[ operation.index ] and
      ANTLR3.bug!( Util.tidy( "      | %s#reduce! should have left only one operation per index,\n      | but %p conflicts with %p\n      END\n    map[ operation.index ] = operation\n    map\n  end\nend\n" ) % [ self.class, operation, other_operaiton ] )

#replace(*range_arguments) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/antlr3/streams/rewrite.rb', line 171

def replace( *range_arguments )
  range, text = cast_range( range_arguments, 1 )
  
  op = Replace.new( @stream, range, text )
  @operations << op
  return op
end

#undo(number_of_operations = 1) ⇒ Object



300
301
302
# File 'lib/antlr3/streams/rewrite.rb', line 300

def undo( number_of_operations = 1 )
  @operations.pop( number_of_operations )
end