class Brice::Colours::Tokenizer
Tokenize an inspection string.
Constants
- EVENT_MAP
- IVAR_RE
- OBJECT_ADDR_RE
- OBJECT_CLASS_RE
- OBJECT_RE
- RANGE_RE
Attributes
block[R]
Public Class Methods
new(&block)
click to toggle source
# File lib/brice/colours.rb 330 def initialize(&block) 331 @block = block or raise ArgumentError, 'no block given' 332 end
tokenize(str, &block)
click to toggle source
# File lib/brice/colours.rb 326 def self.tokenize(str, &block) 327 new(&block).tokenize(str) 328 end
Public Instance Methods
tokenize(str)
click to toggle source
# File lib/brice/colours.rb 336 def tokenize(str) 337 return if str.empty? 338 return if enc_event(str) 339 340 lex, prev = Ripper.lex(str), nil 341 342 len = lex[-1][0][-1] + lex[-1][-1].bytesize 343 str, rest = str.byteslice(0, len), str.byteslice(len .. -1) 344 345 return block[:unknown, rest] if str.empty? 346 347 lex.each { |_, event, tok| 348 sym_event(event, tok, prev) || 349 obj_event(event, tok) || 350 rng_event(event, tok) || 351 var_event(event, tok) || 352 map_event(event, tok) 353 354 prev = event 355 } 356 357 tokenize(rest) 358 end
Private Instance Methods
enc_event(str)
click to toggle source
# File lib/brice/colours.rb 362 def enc_event(str) # XXX /\A\s*#.*?coding\s*:\s*./ 363 object($1, $2, $3) if str =~ OBJECT_RE && str.include?('coding') 364 end
map_event(event, tok)
click to toggle source
# File lib/brice/colours.rb 385 def map_event(event, tok) 386 block[EVENT_MAP[event], tok] 387 end
obj_event(event, tok)
click to toggle source
# File lib/brice/colours.rb 366 def obj_event(event, tok) 367 object($1, $2, $3) if event == :on_comment && tok =~ OBJECT_RE 368 end
object(open, str, close)
click to toggle source
# File lib/brice/colours.rb 389 def object(open, str, close) 390 block[:open_object, open] 391 392 if str.sub!(OBJECT_CLASS_RE, '') 393 block[:object_class, $&] 394 395 if str.sub!(OBJECT_ADDR_RE, '') 396 block[:object_addr_prefix, $1] 397 block[:object_addr, $2] 398 399 str = tokenize(str) 400 end 401 end 402 403 block[:unknown, str] if str 404 405 block[:close_object, close] 406 end
rng_event(event, tok)
click to toggle source
# File lib/brice/colours.rb 374 def rng_event(event, tok) 375 block[:range, tok] if event == :on_op && tok =~ RANGE_RE 376 end
sym_event(event, tok, prev)
click to toggle source
# File lib/brice/colours.rb 370 def sym_event(event, tok, prev) 371 block[:symbol, tok] if event == :on_kw && prev == :on_symbeg 372 end
var_event(event, tok)
click to toggle source
# File lib/brice/colours.rb 378 def var_event(event, tok) 379 if event == :on_ivar && tok =~ IVAR_RE 380 block[:object_line_prefix, $1] 381 block[:keyword, $2] 382 end 383 end