class GetText::RubyParser::POExtractor

Constants

ID
MSGCTXT_ID
MSGCTXT_PLURAL_ID
PLURAL_ID

Attributes

comment_tag[RW]
use_comment[RW]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/gettext/tools/parser/ruby.rb, line 29
def initialize(*args)
  super(*args)
  @in_block_arguments = false
  @ignore_next_comma = false
  @context_stack = []
  @need_definition_name = false
  @current_po_entry = nil
  @current_po_entry_nth_attribute = 0
  @use_comment = false
  @comment_tag = nil
  @last_comment = ""
  @reset_comment = false
  @string_mark_stack = []
  @string_stack = []
end

Public Instance Methods

on_default(event, token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 268
def on_default(event, token, po)
  trace(event, token) do
    process_method = "process_#{event}"
    if respond_to?(process_method)
      __send__(process_method, token, po)
    else
      po
    end
  end
end
process_on_backtick(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 250
def process_on_backtick(token, po)
  @string_mark_stack << "`"
  @string_stack << ""
  po
end
process_on_comma(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 223
def process_on_comma(token, po)
  unless @ignore_next_comma
    if @current_po_entry
      @current_po_entry_nth_attribute += 1
    end
  end
  po
end
process_on_comment(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 103
def process_on_comment(token, po)
  @last_comment = "" if @reset_comment
  @reset_comment = false
  if @last_comment.empty?
    content = token.gsub(/\A#\s*/, "").chomp
    if comment_to_be_extracted?(content)
      @last_comment << content
    end
  else
    content = token.gsub(/\A#/, "").chomp
    @last_comment << "\n"
    @last_comment << content
  end
  po
end
process_on_const(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 93
def process_on_const(token, po)
  case token
  when "N_"," Nn_"
    # TODO: Check the next token is :on_lparen
    process_on_ident(token, po)
  else
    po
  end
end
process_on_embexpr_beg(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 206
def process_on_embexpr_beg(token, po)
  @current_po_entry = nil
  @current_po_entry_nth_attribute = 0
  po
end
process_on_heredoc_beg(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 186
def process_on_heredoc_beg(token, po)
  if token.end_with?("'")
    @string_mark_stack << "'"
  else
    @string_mark_stack << "\""
  end
  @string_stack << ""
  po
end
process_on_heredoc_end(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 196
def process_on_heredoc_end(token, po)
  process_on_tstring_end(token, po)
end
process_on_ident(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 68
def process_on_ident(token, po)
  store_po_entry(po)

  return po if @in_block_arguments
  return po if state.allbits?(Ripper::EXPR_ENDFN)

  case token
  when *ID
    @current_po_entry = POEntry.new(:normal)
  when *PLURAL_ID
    @current_po_entry = POEntry.new(:plural)
  when *MSGCTXT_ID
    @current_po_entry = POEntry.new(:msgctxt)
  when *MSGCTXT_PLURAL_ID
    @current_po_entry = POEntry.new(:msgctxt_plural)
  end
  if @current_po_entry
    @current_po_entry.add_comment(@last_comment) unless @last_comment.empty?
    @last_comment = ""
    @current_po_entry.references << "#{filename}:#{lineno}"
    @current_po_entry_nth_attribute = 0
  end
  po
end
process_on_int(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 218
def process_on_int(token, po)
  @ignore_next_comma = true
  po
end
process_on_kw(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 50
def process_on_kw(token, po)
  store_po_entry(po)
  case token
  when "begin", "case", "do", "for"
    @context_stack.push(token)
  when "class", "def", "module"
    @context_stack.push(token)
  when "if", "unless", "until", "while"
    # postfix case
    unless state.allbits?(Ripper::EXPR_LABEL)
      @context_stack.push(token)
    end
  when "end"
    @context_stack.pop
  end
  po
end
process_on_nl(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 237
def process_on_nl(token, po)
  @reset_comment = true
  po
end
process_on_op(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 45
def process_on_op(token, po)
  @in_block_arguments = !@in_block_arguments if token == "|"
  po
end
process_on_qsymbols_beg(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 256
def process_on_qsymbols_beg(token, po)
  @string_mark_stack << token
  @string_stack << ""
  po
end
process_on_qwords_beg(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 262
def process_on_qwords_beg(token, po)
  @string_mark_stack << token
  @string_stack << ""
  po
end
process_on_regexp_beg(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 200
def process_on_regexp_beg(token, po)
  @string_mark_stack << "\""
  @string_stack << ""
  po
end
process_on_regexp_end(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 212
def process_on_regexp_end(token, po)
  @string_mark_stack.pop
  @string_stack.pop
  po
end
process_on_rparen(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 232
def process_on_rparen(token, po)
  store_po_entry(po)
  po
end
process_on_sp(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 119
def process_on_sp(token, po)
  po
end
process_on_symbeg(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 242
def process_on_symbeg(token, po)
  if token.start_with?("%s") or [":'", ":\""].include?(token)
    @string_mark_stack << ":"
    @string_stack << ""
  end
  po
end
process_on_tstring_beg(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 123
def process_on_tstring_beg(token, po)
  if token.start_with?("%Q")
    @string_mark_stack << "\""
  elsif token.start_with?("%q")
    @string_mark_stack << "'"
  elsif token.start_with?("%")
    @string_mark_stack << "\""
  else
    @string_mark_stack << token
  end
  @string_stack << ""
  po
end
process_on_tstring_content(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 137
def process_on_tstring_content(token, po)
  case @string_mark_stack.last
  when "\"", "`"
    @string_stack.last << token.gsub(/\\./) do |data|
      case data
      when "\\n"
        "\n"
      when "\\t"
        "\t"
      when "\\\\"
        "\\"
      when "\\\""
        "\""
      when "\\\#"
        "#"
      else
        data
      end
    end
  else
    @string_stack.last << token.gsub(/\\./) do |data|
      case data
      when "\\\\"
        "\\"
      when "\\'"
        "'"
      else
        data
      end
    end
  end
  po
end
process_on_tstring_end(token, po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 171
def process_on_tstring_end(token, po)
  @ignore_next_comma = false
  string_mark = @string_mark_stack.pop
  case string_mark
  when "\"", "'"
    last_string = @string_stack.pop
    if @current_po_entry and last_string
      @current_po_entry[@current_po_entry_nth_attribute] =
        (@current_po_entry[@current_po_entry_nth_attribute] || "") +
        last_string
    end
  end
  po
end

Private Instance Methods

comment_to_be_extracted?(comment) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 297
def comment_to_be_extracted?(comment)
  return false unless @use_comment

  return true if @comment_tag.nil?

  comment.start_with?(@comment_tag)
end
debug?() click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 281
def debug?
  @@debug
end
store_po_entry(po) click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 290
def store_po_entry(po)
  return if @current_po_entry.nil?
  po << @current_po_entry if @current_po_entry.msgid
  @current_po_entry = nil
  @current_po_entry_nth_attribute = 0
end
trace(event_name, token) { || ... } click to toggle source
# File lib/gettext/tools/parser/ruby.rb, line 285
def trace(event_name, token)
  pp [event_name, token, state, @context_stack.last] if debug?
  yield
end