Class: Rich::RubyLexer
Overview
Ruby lexer
Constant Summary collapse
- KEYWORDS =
%w[ def class module end if else elsif unless case when then begin rescue ensure raise return yield do while until for break next redo retry in and or not alias defined? super self nil true false __FILE__ __LINE__ __ENCODING__ require require_relative include extend prepend attr_reader attr_writer attr_accessor private protected public lambda proc loop catch throw ].freeze
- BUILTINS =
%w[ puts print p pp gets chomp to_s to_i to_f to_a to_h length size each map select reject find reduce inject sort sort_by uniq compact flatten reverse join split push pop shift unshift first last min max sum count empty? nil? is_a? kind_of? respond_to? send __send__ method methods instance_variables class superclass ancestors included_modules freeze frozen? dup clone tap then yield_self itself inspect ].freeze
Instance Method Summary collapse
Instance Method Details
#tokenize(line, theme) ⇒ Object
330 331 332 333 334 335 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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/rich/syntax.rb', line 330 def tokenize(line, theme) segments = [] pos = 0 while pos < line.length # Skip whitespace if line[pos].match?(/\s/) ws_end = pos ws_end += 1 while ws_end < line.length && line[ws_end].match?(/\s/) segments << Segment.new(line[pos...ws_end]) pos = ws_end next end # Comment if line[pos] == "#" segments << Segment.new(line[pos..], style: theme[:comment]) break end # String (double quote) if line[pos] == '"' str_end = find_string_end(line, pos, '"') segments << Segment.new(line[pos..str_end], style: theme[:string]) pos = str_end + 1 next end # String (single quote) if line[pos] == "'" str_end = find_string_end(line, pos, "'") segments << Segment.new(line[pos..str_end], style: theme[:string]) pos = str_end + 1 next end # Regex (only when a real closing '/' exists and the literal does not # start with a space, so ordinary division `a / b` is not swallowed) if line[pos] == "/" && (pos == 0 || line[pos - 1].match?(/[\s=({,]/)) && pos + 1 < line.length && line[pos + 1] != " " regex_end = find_closing_delimiter(line, pos, "/") if regex_end segments << Segment.new(line[pos..regex_end], style: theme[:string_regex]) pos = regex_end + 1 next end end # Symbol if line[pos] == ":" if pos + 1 < line.length && line[pos + 1].match?(/[a-zA-Z_]/) sym_end = pos + 1 sym_end += 1 while sym_end < line.length && line[sym_end].match?(/\w/) segments << Segment.new(line[pos...sym_end], style: theme[:string_symbol] || theme[:string]) pos = sym_end next end end # Number if line[pos].match?(/\d/) num_end = pos num_end += 1 while num_end < line.length && line[num_end].match?(/[\d._xXoObB]/) segments << Segment.new(line[pos...num_end], style: theme[:number]) pos = num_end next end # Instance variable if line[pos] == "@" var_end = pos + 1 var_end += 1 if var_end < line.length && line[var_end] == "@" var_end += 1 while var_end < line.length && line[var_end].match?(/\w/) segments << Segment.new(line[pos...var_end], style: theme[:name_variable] || theme[:name]) pos = var_end next end # Global variable if line[pos] == "$" var_end = pos + 1 var_end += 1 while var_end < line.length && line[var_end].match?(/\w/) segments << Segment.new(line[pos...var_end], style: theme[:name_variable] || theme[:name]) pos = var_end next end # Constant/Class name if line[pos].match?(/[A-Z]/) word_end = pos word_end += 1 while word_end < line.length && line[word_end].match?(/\w/) word = line[pos...word_end] if %w[true false nil].include?(word.downcase) segments << Segment.new(word, style: theme[:keyword_constant] || theme[:keyword]) else segments << Segment.new(word, style: theme[:name_class] || theme[:name]) end pos = word_end next end # Identifier/Keyword if line[pos].match?(/[a-z_]/i) word_end = pos word_end += 1 while word_end < line.length && line[word_end].match?(/[\w?!]/) word = line[pos...word_end] style = if KEYWORDS.include?(word) theme[:keyword] elsif BUILTINS.include?(word) theme[:name_builtin] || theme[:name] else theme[:name] end segments << Segment.new(word, style: style) pos = word_end next end # Operators and punctuation if line[pos].match?(/[+\-*\/%&|^~<>=!?:]/) op_end = pos + 1 op_end += 1 while op_end < line.length && line[op_end].match?(/[+\-*\/%&|^~<>=!?:]/) segments << Segment.new(line[pos...op_end], style: theme[:operator]) pos = op_end next end # Punctuation if line[pos].match?(/[(){}\[\].,;]/) segments << Segment.new(line[pos], style: theme[:punctuation]) pos += 1 next end # Default segments << Segment.new(line[pos]) pos += 1 end segments end |