Class: ZLocalize::SourceParser
- Inherits:
-
Object
- Object
- ZLocalize::SourceParser
- Defined in:
- lib/zlocalize/rdoc_source_parser.rb
Overview
Parses a Ruby (or ERB) file and generates a list of language tokens
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#in_hash ⇒ Object
Returns the value of attribute in_hash.
-
#root ⇒ Object
Returns the value of attribute root.
-
#translate_calls ⇒ Object
Returns the value of attribute translate_calls.
Instance Method Summary collapse
-
#check_for_hash_element_expression(expr) ⇒ Object
check if the expression is a hash element declaration (i.e. is it followed by
=>
). -
#check_for_range_expression(expr) ⇒ Object
check if the expression currently being parsed as a range specifier.
- #get_tk ⇒ Object
-
#initialize(filename, root, is_erb = false) ⇒ SourceParser
constructor
A new instance of SourceParser.
-
#list_tokens ⇒ Object
simply list the tokens in the source file.
-
#parse ⇒ Object
we simply detect calls to the ZLocalize methods and parse their parameter list.
-
#parse_array ⇒ Object
parse an Array declaration
[...]
. - #parse_error(tk, msg) ⇒ Object
-
#parse_expression ⇒ Object
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.
-
#parse_hash ⇒ Object
Parse a
Hash
declaration{ ... }
. -
#parse_hash_element ⇒ Object
parse a Hash element within a
Hash
declaration. -
#parse_identifier ⇒ Object
parse identifier(…), identifier or identifier….
-
#parse_identifier_method_call(ident) ⇒ Object
parse the additional methods in an expression for example: self.method.sub_method(1).another_sub_method(2).
-
#parse_operand ⇒ Object
parse and return an operand to an OperatorExpression.
-
#parse_parameters ⇒ Object
parse the list of actual parameters to a method.
-
#parse_translate_call ⇒ Object
parse a call to one of the translation methods
_(...)
,n_(...)
. - #peek_tk ⇒ Object
- #rewind ⇒ Object
- #skip_white_space ⇒ Object
-
#translation_entries ⇒ Object
return a Hash of all translation entries we collected.
Constructor Details
#initialize(filename, root, is_erb = false) ⇒ SourceParser
Returns a new instance of SourceParser.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 361 def initialize(filename, root, is_erb = false) @in_hash = 0 @translate_calls = [] @root = File.(root).downcase @filename = File.(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
#filename ⇒ Object
Returns the value of attribute filename.
358 359 360 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 358 def filename @filename end |
#in_hash ⇒ Object
Returns the value of attribute in_hash.
357 358 359 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 357 def in_hash @in_hash end |
#root ⇒ Object
Returns the value of attribute root.
359 360 361 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 359 def root @root end |
#translate_calls ⇒ Object
Returns the value of attribute translate_calls.
356 357 358 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 356 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 =>
)
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 538 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
524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 524 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_tk ⇒ Object
381 382 383 384 385 386 387 388 389 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 381 def get_tk @token_index += 1 if @token_index < @last_token_index @tk = @token_list[@token_index] else @tk = nil end @tk end |
#list_tokens ⇒ Object
simply list the tokens in the source file
407 408 409 410 411 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 407 def list_tokens while get_tk puts @tk.inspect end end |
#parse ⇒ Object
we simply detect calls to the ZLocalize methods and parse their parameter list
418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 418 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_array ⇒ Object
parse an Array declaration [...]
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 672 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
413 414 415 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 413 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_expression ⇒ Object
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
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 592 593 594 595 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 566 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_hash ⇒ Object
Parse a Hash
declaration { ... }
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 691 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_element ⇒ Object
parse a Hash element within a Hash
declaration
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 712 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_identifier ⇒ Object
parse identifier(…), identifier or identifier…
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 504 505 506 507 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 464 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)
512 513 514 515 516 517 518 519 520 521 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 512 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_operand ⇒ Object
parse and return an operand to an OperatorExpression
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 666 667 668 669 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 598 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_parameters ⇒ Object
parse the list of actual parameters to a method
448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 448 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_call ⇒ Object
parse a call to one of the translation methods _(...)
, n_(...)
432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 432 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_tk ⇒ Object
391 392 393 394 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 391 def peek_tk peek_index = @token_index + 1 return peek_index < @last_token_index ? @token_list[peek_index] : nil end |
#rewind ⇒ Object
396 397 398 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 396 def rewind @token_index = -1 end |
#skip_white_space ⇒ Object
400 401 402 403 404 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 400 def skip_white_space while [RDoc::RubyToken::TkNL, RDoc::RubyToken::TkSPACE].include?(@tk.class) get_tk end end |
#translation_entries ⇒ Object
return a Hash of all translation entries we collected
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
# File 'lib/zlocalize/rdoc_source_parser.rb', line 737 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 |