Class: ZLocalize::SourceParser

Inherits:
Object
  • Object
show all
Defined in:
lib/zlocalize/source_parser.rb

Overview

Parses a Ruby (or ERB) file and generates a list of language tokens

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, root, is_erb = false) ⇒ SourceParser

Returns a new instance of SourceParser.



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/zlocalize/source_parser.rb', line 357

def initialize(filename, root, is_erb = false)
  @in_hash = 0
  @translate_calls = []
  @root = File.expand_path(root).downcase
  @filename = File.expand_path(filename).downcase
  @relative_filename = @filename.gsub(@root,'')
  content = File.open(filename, "r") { |f| f.read }
  if is_erb
    content = ERB.new(content).src
  end

  @lex = RDoc::RubyLex.new(content,RDoc::Options.new)
  @token_list = []
  while tk = @lex.token
    @token_list << tk
  end
  @token_index = -1
  @last_token_index = @token_list.size
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



354
355
356
# File 'lib/zlocalize/source_parser.rb', line 354

def filename
  @filename
end

#in_hashObject

Returns the value of attribute in_hash.



353
354
355
# File 'lib/zlocalize/source_parser.rb', line 353

def in_hash
  @in_hash
end

#rootObject

Returns the value of attribute root.



355
356
357
# File 'lib/zlocalize/source_parser.rb', line 355

def root
  @root
end

#translate_callsObject

Returns the value of attribute translate_calls.



352
353
354
# File 'lib/zlocalize/source_parser.rb', line 352

def translate_calls
  @translate_calls
end

Instance Method Details

#check_for_hash_element_expression(expr) ⇒ Object

check if the expression is a hash element declaration (i.e. is it followed by =>)



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/zlocalize/source_parser.rb', line 534

def check_for_hash_element_expression(expr)
  return expr if @in_hash > 0
  if expr.is_a?(SymbolExpression) && expr.text =~ /:$/
    he_expr = HashElementExpression.new(expr.line_no,expr.char_no)
    he_expr.key = expr
    skip_white_space
    he_expr.value = parse_expression
    # get_tk
    return he_expr
  else
    skip_white_space
    if @tk.is_a?(RDoc::RubyToken::TkASSIGN)
      get_tk
      if @tk.is_a?(RDoc::RubyToken::TkGT)
        he_expr = HashElementExpression.new(expr.line_no,expr.char_no)
        he_expr.key = expr
        get_tk
        he_expr.value = parse_expression
        return he_expr
      end
    end
  end
  return expr
end

#check_for_range_expression(expr) ⇒ Object

check if the expression currently being parsed as a range specifier



520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/zlocalize/source_parser.rb', line 520

def check_for_range_expression(expr)
  skip_white_space
  if [RDoc::RubyToken::TkDOT2,RDoc::RubyToken::TkDOT3].include?(@tk.class)
    range_expr = RangeExpression.new(expr.line_no,expr.char_no,'')
    range_expr.low = expr
    get_tk
    range_expr.high = parse_expression
    return range_expr
  else
    return expr
  end
end

#get_tkObject



377
378
379
380
381
382
383
384
385
# File 'lib/zlocalize/source_parser.rb', line 377

def get_tk
  @token_index += 1
  if @token_index < @last_token_index
    @tk = @token_list[@token_index]
  else
    @tk = nil
  end
  @tk
end

#list_tokensObject

simply list the tokens in the source file



403
404
405
406
407
# File 'lib/zlocalize/source_parser.rb', line 403

def list_tokens
  while get_tk
    puts @tk.inspect
  end
end

#parseObject

we simply detect calls to the ZLocalize methods and parse their parameter list



414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/zlocalize/source_parser.rb', line 414

def parse
 rewind
 while get_tk
   if @tk.is_a?(RDoc::RubyToken::TkCONSTANT) && @tk.text = 'ZLocalize'
     get_tk # should be a dot
     get_tk # either 'translate' or 'pluralize'
     parse_translate_call if ['translate','pluralize'].include?(@tk.text)
   elsif @tk.is_a?(RDoc::RubyToken::TkIDENTIFIER)
     parse_translate_call if ['_','n_'].include?(@tk.text)
   end
 end
end

#parse_arrayObject

parse an Array declaration [...]



668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/zlocalize/source_parser.rb', line 668

def parse_array
  get_tk
  array_expr = ArrayExpression.new(@tk.line_no,@tk.char_no)
  skip_white_space
  array_expr.elements << parse_expression
  skip_white_space
  while @tk.is_a?(RDoc::RubyToken::TkCOMMA) do
    get_tk
    array_expr.elements << parse_expression
    skip_white_space
  end
  unless @tk.is_a?(RDoc::RubyToken::TkRBRACK)
    parse_error(@tk,"']' expected but '#{@tk.text}' found")
  end
  get_tk
  array_expr
end

#parse_error(tk, msg) ⇒ Object

Raises:

  • (ParseError)


409
410
411
# File 'lib/zlocalize/source_parser.rb', line 409

def parse_error(tk,msg)
  raise(ParseError,"\n\nIn file #{@filename}, on line #{tk.line_no} at position #{tk.char_no}: " + msg + "\n\n")
end

#parse_expressionObject

parse an Expression, which can be either a simple identifier, a method call with parameters, an operator expression (as in 1 + 1) or an ‘inline-if’ expression



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/zlocalize/source_parser.rb', line 562

def parse_expression
  skip_white_space
  expr1 = parse_operand
  skip_white_space
  if OPERATOR_TOKENS.include?(@tk.class)
    expr = OperatorExpression.new(expr1.line_no,expr1.char_no,'')
    expr.operands << expr1
    while OPERATOR_TOKENS.include?(@tk.class)
      get_tk
      expr.operands << parse_operand
    end
  else
    expr = expr1
  end
  skip_white_space
  if @tk.is_a?(RDoc::RubyToken::TkQUESTION)
    get_tk
    expr2 = ConditionalExpression.new(expr.line_no,expr.char_no)
    expr2.condition = expr1
    expr2.true_expr = parse_expression
    skip_white_space
    unless @tk.is_a?(RDoc::RubyToken::TkCOLON)
      parse_error(@tk,"':' expected but #{@tk.text} found")
    end
    get_tk
    expr2.false_expr = parse_expression
    expr = expr2
  end
  expr
end

#parse_hashObject

Parse a Hash declaration { ... }



687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/zlocalize/source_parser.rb', line 687

def parse_hash
  @in_hash += 1
  get_tk
  hash_expr = HashExpression.new(@tk.line_no,@tk.char_no)
  skip_white_space
  hash_expr.elements << parse_hash_element
  skip_white_space
  while @tk.is_a?(RDoc::RubyToken::TkCOMMA) do
    get_tk
    hash_expr.elements << parse_hash_element
    skip_white_space
  end
  unless @tk.is_a?(RDoc::RubyToken::TkRBRACE)
    parse_error(@tk,"'}' expected but '#{@tk.text}' found")
  end
  get_tk
  @in_hash -= 1
  hash_expr
end

#parse_hash_elementObject

parse a Hash element within a Hash declaration



708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/zlocalize/source_parser.rb', line 708

def parse_hash_element
  el = HashElementExpression.new(@tk.line_no,@tk.char_no)
  el.key = parse_expression
  if el.key.is_a?(SymbolExpression) && el.key.text =~ /:$/
    skip_white_space
    el.value = parse_expression
    # get_tk
    return el
  else
    skip_white_space
    if @tk.is_a?(RDoc::RubyToken::TkASSIGN)
      get_tk
      if @tk.is_a?(RDoc::RubyToken::TkGT)
        get_tk
        skip_white_space
        el.value = parse_expression
        skip_white_space
        return el
      end
    end
  end
  parse_error(@tk,"'=>' expected but '#{@tk.text}' found")
end

#parse_identifierObject

parse identifier(…), identifier or identifier…



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/zlocalize/source_parser.rb', line 460

def parse_identifier
  case @tk
    when RDoc::RubyToken::TkLPAREN
      ident_text = "(Method From Stack)"
    when RDoc::RubyToken::TkfLBRACK
      ident_text = "(Array Access)"
    else
      ident_text = @tk.text
      line, char = @tk.line_no, @tk.char_no
      get_tk
      # check for a Ruby 2.0 Hash key symbol (identifier directly followed by a COLON)
      if @tk.is_a?(RDoc::RubyToken::TkCOLON) || @tk.is_a?(RDoc::RubyToken::TkSYMBEG)
        expr = SymbolExpression.new(line,char,'')
        expr.set_text(ident_text + ':')
        expr.name = ident_text + ':'
        get_tk
        return expr
      end
  end
  ident = IdentifierExpression.new(@tk.line_no,@tk.char_no,ident_text)
  while(@tk.is_a?(RDoc::RubyToken::TkCOLON2))
    get_tk
    ident.name << '::' + @tk.text
    get_tk
  end

  case @tk
    when RDoc::RubyToken::TkLPAREN
      get_tk
      ident.parameters = parse_parameters
      unless @tk.is_a?(RDoc::RubyToken::TkRPAREN)
        parse_error(@tk,"')' expected but '#{@tk.text}' found")
      end
      get_tk
    when RDoc::RubyToken::TkfLBRACK
      get_tk
      ident.parameters = parse_parameters
      unless @tk.is_a?(RDoc::RubyToken::TkRBRACK)
        parse_error(@tk,"']' expected but '#{@tk.text}' found")
      end
      get_tk
  end # case @tk
  parse_identifier_method_call(ident)
end

#parse_identifier_method_call(ident) ⇒ Object

parse the additional methods in an expression for example:

self.method.sub_method(1).another_sub_method(2)


508
509
510
511
512
513
514
515
516
517
# File 'lib/zlocalize/source_parser.rb', line 508

def parse_identifier_method_call(ident)
  case @tk
    when RDoc::RubyToken::TkDOT
      get_tk
      ident.sub_method = parse_identifier
    when RDoc::RubyToken::TkLPAREN, RDoc::RubyToken::TkfLBRACK
      ident.sub_method = parse_identifier
  end
  ident
end

#parse_operandObject

parse and return an operand to an OperatorExpression



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/zlocalize/source_parser.rb', line 594

def parse_operand
  skip_white_space
  while UNARY_OP_TOKENS.include?(@tk.class)
    get_tk
    skip_white_space
  end
  case @tk
    when RDoc::RubyToken::TkLPAREN, RDoc::RubyToken::TkfLPAREN
      get_tk
      expr = parse_expression
      unless @tk.is_a?(RDoc::RubyToken::TkRPAREN)
        parse_error(@tk,"')' expected but #{@tk.text} found (unmatched parenthesis)")
      end
      get_tk
      expr = parse_identifier_method_call(expr)  # ident.something
      expr = check_for_range_expression(expr)
      expr = check_for_hash_element_expression(expr)
    when RDoc::RubyToken::TkSTRING, RDoc::RubyToken::TkDSTRING, RDoc::RubyToken::TkXSTRING, RDoc::RubyToken::TkREGEXP
      expr = StringExpression.new(@tk.line_no,@tk.char_no, @tk.text)
      get_tk
      # check for Ruby 2.2 Hash symbol (string directly followed by a COLON)
      if @tk.is_a?(RDoc::RubyToken::TkCOLON)
        expr = SymbolExpression.new(expr.line_no,expr.char_no,expr.name + ':')
        expr.text = expr.name
        get_tk
      end
      expr = parse_identifier_method_call(expr)
      expr = check_for_range_expression(expr)
      expr = check_for_hash_element_expression(expr)
    when RDoc::RubyToken::TkINTEGER, RDoc::RubyToken::TkFLOAT, RDoc::RubyToken::TkTRUE, RDoc::RubyToken::TkFALSE
      expr = NumberExpression.new(@tk.line_no,@tk.char_no,@tk.text)
      get_tk
      expr = parse_identifier_method_call(expr)
      expr = check_for_range_expression(expr)
      expr = check_for_hash_element_expression(expr)
    when RDoc::RubyToken::TkSYMBEG
      expr = SymbolExpression.new(@tk.line_no,@tk.char_no,'')
      get_tk
      if @tk.text =~ /^[a-z]{1,}[a-z0-9\_]*?$/ #is_a?(RDoc::RubyToken::TkIDENTIFIER)
        expr.set_text(':' + @tk.text)
        expr.name = ':' + @tk.text
        get_tk
        expr = parse_identifier_method_call(expr)
        expr = check_for_hash_element_expression(expr)
      else
        parse_error(@tk,"':' not followed by identifier or operator")
      end
    when RDoc::RubyToken::TkGVAR, RDoc::RubyToken::TkIVAR, RDoc::RubyToken::TkSELF, RDoc::RubyToken::TkNIL
      expr = parse_identifier
    when RDoc::RubyToken::TkCONSTANT, RDoc::RubyToken::TkIDENTIFIER
      expr = parse_identifier
      expr = check_for_hash_element_expression(expr)
    when RDoc::RubyToken::TkLBRACK
      expr = parse_array
      expr = parse_identifier_method_call(expr)
    when RDoc::RubyToken::TkLBRACE
      expr = parse_hash
      expr = parse_identifier_method_call(expr)
    when RDoc::RubyToken::TkKW
      # check for a Ruby 2.0 syntax hash key symbol (such as end: begin: class: case: etc...)
      if peek_tk.is_a?(RDoc::RubyToken::TkCOLON)
        ident_text, line, char = @tk.text, @tk.line_no, @tk.char_no
        get_tk
        expr = SymbolExpression.new(line,char,'')
        expr.set_text(ident_text + ':')
        expr.name = ident_text + ':'
        get_tk
        expr = check_for_hash_element_expression(expr)
      end
  end # case @tk
  expr
end

#parse_parametersObject

parse the list of actual parameters to a method



444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/zlocalize/source_parser.rb', line 444

def parse_parameters
  parameters = []
  skip_white_space
  return if @tk.is_a?(RDoc::RubyToken::TkRPAREN)

  parameters << parse_expression
  skip_white_space
  while @tk.is_a?(RDoc::RubyToken::TkCOMMA) do
    get_tk
    parameters << parse_expression
    skip_white_space
  end
  parameters
end

#parse_translate_callObject

parse a call to one of the translation methods _(...), n_(...)



428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/zlocalize/source_parser.rb', line 428

def parse_translate_call
  m = IdentifierExpression.new(@tk.line_no,@tk.char_no,@tk.text)
  get_tk
  skip_white_space
  expect_r_paren = @tk.is_a?(RDoc::RubyToken::TkLPAREN)
  get_tk if expect_r_paren
  m.parameters = parse_parameters
  if expect_r_paren && !@tk.is_a?(RDoc::RubyToken::TkRPAREN)
    parse_error(@tk,"')' expected but '#{@tk.text}' (#{@tk.class.name}) found")
  end
  @translate_calls << m
  m
end

#peek_tkObject



387
388
389
390
# File 'lib/zlocalize/source_parser.rb', line 387

def peek_tk
  peek_index = @token_index + 1
  return peek_index < @last_token_index ? @token_list[peek_index] : nil
end

#rewindObject



392
393
394
# File 'lib/zlocalize/source_parser.rb', line 392

def rewind
  @token_index = -1
end

#skip_white_spaceObject



396
397
398
399
400
# File 'lib/zlocalize/source_parser.rb', line 396

def skip_white_space
  while [RDoc::RubyToken::TkNL, RDoc::RubyToken::TkSPACE].include?(@tk.class)
    get_tk
  end
end

#translation_entriesObject

return a Hash of all translation entries we collected



733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/zlocalize/source_parser.rb', line 733

def translation_entries
  entries = {}
  self.translate_calls.each do |c|
    e = c.to_translation_entry(@relative_filename)
    if e.is_a?(Array)
      e.each do |te|
        if entries[te.source]
          entries[te.source].references += te.references
        else
          entries[te.source] = te
        end
      end
      elsif e.is_a?(TranslationEntry)
       if entries[te.source]
          entries[e.source].references += te.references
       else
          entries[e.source] = e
       end
    end
  end
  entries
end