Class: RubyLexer::StringToken

Inherits:
Token
  • Object
show all
Defined in:
lib/rubylexer/token.rb

Overview


Direct Known Subclasses

RenderExactlyStringToken

Constant Summary collapse

DQUOTE_ESCAPE_TABLE =
[
  ["\n",'\n'],
  ["\r",'\r'],
  ["\t",'\t'],
  ["\v",'\v'],
  ["\f",'\f'],
  ["\e",'\e'],
  ["\b",'\b'],
  ["\a",'\a']
]
PREFIXERS =
{ '['=>"%w[", '{'=>'%W{' }
SUFFIXERS =
{ '['=>"]",   '{'=>'}' }

Instance Attribute Summary collapse

Attributes inherited from Token

#allow_ooo_offset, #as, #endline, #ident, #offset, #tag

Instance Method Summary collapse

Methods inherited from Token

#dump, #error, #has_no_block?, #inspect, #orig_inspect, #ws_munge

Constructor Details

#initialize(type = '"', ident = '') ⇒ StringToken

Returns a new instance of StringToken.



309
310
311
312
313
314
315
316
317
# File 'lib/rubylexer/token.rb', line 309

def initialize(type='"',ident='')
   super(ident)
   type=="'" and type='"'
   @char=type
   assert @char[/^[\[{"`\/]$/]  #"
   @elems=[ident.dup]     #why .dup?
   @modifiers=nil
   @endline=nil
end

Instance Attribute Details

#bs_handlerObject

Returns the value of attribute bs_handler.



284
285
286
# File 'lib/rubylexer/token.rb', line 284

def bs_handler
  @bs_handler
end

#charObject (readonly)

Returns the value of attribute char.



279
280
281
# File 'lib/rubylexer/token.rb', line 279

def char
  @char
end

#closeObject

exact seq of (1) char to stop the str



287
288
289
# File 'lib/rubylexer/token.rb', line 287

def close
  @close
end

#elemsObject

Returns the value of attribute elems.



282
283
284
# File 'lib/rubylexer/token.rb', line 282

def elems
  @elems
end

#lvarsObject

names used in named backrefs if this is a regex



289
290
291
# File 'lib/rubylexer/token.rb', line 289

def lvars
  @lvars
end

#modifiersObject

for regex only



281
282
283
# File 'lib/rubylexer/token.rb', line 281

def modifiers
  @modifiers
end

#openObject

exact sequence of chars used to start the str



286
287
288
# File 'lib/rubylexer/token.rb', line 286

def open
  @open
end

#startlineObject

Returns the value of attribute startline.



283
284
285
# File 'lib/rubylexer/token.rb', line 283

def startline
  @startline
end

Instance Method Details

#append(glob) ⇒ Object



384
385
386
387
388
389
390
391
392
# File 'lib/rubylexer/token.rb', line 384

def append(glob)
   #assert @elems.last.kind_of?(String)
   case glob
   when String,Integer then append_str! glob
   when RubyCode then append_code! glob
   else raise "bad string contents: #{glob}, a #{glob.class}"
   end
   #assert @elems.last.kind_of?(String)
end

#append_token(strtok) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/rubylexer/token.rb', line 394

def append_token(strtok)
   assert @elems.last.kind_of?(String)
   #assert strtok.elems.last.kind_of?(String)
   assert strtok.elems.first.kind_of?(String)

   @elems.last << strtok.elems.shift

   first=strtok.elems.first
   assert( first.nil? || first.kind_of?(RubyCode) )

   @elems += strtok.elems
   @ident << strtok.ident

   assert((!@modifiers or !strtok.modifiers))
   @modifiers||=strtok.modifiers

   #assert @elems.last.kind_of?(String)

   @bs_handler ||=strtok.bs_handler

   return self
end

#has_str_inc?Boolean

Returns:

  • (Boolean)


332
333
334
# File 'lib/rubylexer/token.rb', line 332

def has_str_inc?
  elems.size>1 or RubyCode===elems.first
end

#lineObject



306
# File 'lib/rubylexer/token.rb', line 306

def line; @endline end

#line=(l) ⇒ Object



307
# File 'lib/rubylexer/token.rb', line 307

def line= l; @endline=l end

#linecountObject



291
# File 'lib/rubylexer/token.rb', line 291

def linecount; line-startline end

#to_s(transname = :transform) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/rubylexer/token.rb', line 336

def to_s transname=:transform
   assert @char[/[\[{"`\/]/] #"
   #on output, all single-quoted strings become double-quoted
   assert(@elems.length==1)  if @char=='['

   result=open.dup
   starter=result[-1,1]
   ender=close
   elems.each{|e|
     case e
     when String; result<<e
#        strfrag=translate_escapes strfrag if RubyLexer::FASTER_STRING_ESCAPES
#        result << send(transname,strfrag,starter,ender)
     when VarNameToken;
       if /^[$@]/===e.to_s
         result << '#' + e.to_s
       else 
         result << "\#{#{e}}"
       end
     when RubyCode; result << '#' + e.to_s
     else fail
     end
   }
   result << ender

   if @char=='/'
     result << modifiers if modifiers #regex only
     result="%r"+result if RubyLexer::WHSPLF[result[1,1]]
   end

   result<<"\n" if open.empty? and result[-1] != ?\n

   return result
end

#to_termObject



371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/rubylexer/token.rb', line 371

def to_term
   result=[]
   0.step(@elems.length-1,2) { |i|
      result << ConstTerm.new(@elems[i].dup)

      if e=@elems[i+1]
         assert(e.kind_of?(RubyCode))
         result << (RubyTerm.new e)
      end
   }
   return result
end

#translate_escapes(str) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/rubylexer/token.rb', line 417

def translate_escapes(str)
  rl=RubyLexer.new("(string escape translation hack...)",'')
  result=str.dup
  seq=result.to_sequence
  rl.instance_eval{@file=seq}
  repls=[]
  i=0
  #ugly ugly ugly
  while i<result.size and bs_at=result.index(/\\./m,i) 
      seq.pos=$~.end(0)-1
      ch=rl.send(bs_handler,"\\",@open[-1,1],@close)
      result[bs_at...seq.pos]=ch
      i=bs_at+ch.size
  end

  return  result
end

#utf8!Object



297
298
299
# File 'lib/rubylexer/token.rb', line 297

def utf8!
  @utf8=true
end

#utf8?Boolean

Returns:

  • (Boolean)


293
294
295
# File 'lib/rubylexer/token.rb', line 293

def utf8?
  @utf8||=nil
end

#with_line(line) ⇒ Object



301
302
303
304
# File 'lib/rubylexer/token.rb', line 301

def with_line(line)
  @endline=line
  self
end