Class: GetText::RubyParser::POExtractor

Inherits:
Ripper::Filter
  • Object
show all
Defined in:
lib/gettext/tools/parser/ruby.rb

Constant Summary collapse

ID =
["gettext", "_", "N_", "sgettext", "s_"]
PLURAL_ID =
["ngettext", "n_", "Nn_", "ns_", "nsgettext"]
MSGCTXT_ID =
["pgettext", "p_"]
MSGCTXT_PLURAL_ID =
["npgettext", "np_"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ POExtractor

Returns a new instance of POExtractor.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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
  @embed_expression_level = 0
  @string_mark_stack = []
  @string_stack = []
end

Instance Attribute Details

#comment_tagObject

Returns the value of attribute comment_tag.



28
29
30
# File 'lib/gettext/tools/parser/ruby.rb', line 28

def comment_tag
  @comment_tag
end

#use_commentObject

Returns the value of attribute use_comment.



27
28
29
# File 'lib/gettext/tools/parser/ruby.rb', line 27

def use_comment
  @use_comment
end

Instance Method Details

#on_default(event, token, po) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/gettext/tools/parser/ruby.rb', line 242

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_comma(token, po) ⇒ Object



223
224
225
226
227
228
229
230
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gettext/tools/parser/ruby.rb', line 109

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) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/gettext/tools/parser/ruby.rb', line 99

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) ⇒ Object



200
201
202
203
204
# File 'lib/gettext/tools/parser/ruby.rb', line 200

def process_on_embexpr_beg(token, po)
  @embed_expression_level += 1
  @string_stack.last << token
  po
end

#process_on_embexpr_end(token, po) ⇒ Object



206
207
208
209
210
# File 'lib/gettext/tools/parser/ruby.rb', line 206

def process_on_embexpr_end(token, po)
  @embed_expression_level -= 1
  @string_stack.last << token
  po
end

#process_on_heredoc_beg(token, po) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/gettext/tools/parser/ruby.rb', line 180

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) ⇒ Object



190
191
192
# File 'lib/gettext/tools/parser/ruby.rb', line 190

def process_on_heredoc_end(token, po)
  process_on_tstring_end(token, po)
end

#process_on_ident(token, po) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gettext/tools/parser/ruby.rb', line 69

def process_on_ident(token, po)
  if @embed_expression_level > 0
    @string_stack.last << token
    return po
  end

  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) ⇒ Object



218
219
220
221
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gettext/tools/parser/ruby.rb', line 51

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) ⇒ Object



237
238
239
240
# 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) ⇒ Object



46
47
48
49
# File 'lib/gettext/tools/parser/ruby.rb', line 46

def process_on_op(token, po)
  @in_block_arguments = !@in_block_arguments if token == "|"
  po
end

#process_on_regexp_beg(token, po) ⇒ Object



194
195
196
197
198
# File 'lib/gettext/tools/parser/ruby.rb', line 194

def process_on_regexp_beg(token, po)
  @string_mark_stack << "\""
  @string_stack << ""
  po
end

#process_on_regexp_end(token, po) ⇒ Object



212
213
214
215
216
# 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) ⇒ Object



232
233
234
235
# 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) ⇒ Object



125
126
127
# File 'lib/gettext/tools/parser/ruby.rb', line 125

def process_on_sp(token, po)
  po
end

#process_on_tstring_beg(token, po) ⇒ Object



129
130
131
132
133
# File 'lib/gettext/tools/parser/ruby.rb', line 129

def process_on_tstring_beg(token, po)
  @string_mark_stack << token
  @string_stack << ""
  po
end

#process_on_tstring_content(token, po) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/gettext/tools/parser/ruby.rb', line 135

def process_on_tstring_content(token, po)
  if @string_mark_stack.last == "\""
    @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) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gettext/tools/parser/ruby.rb', line 168

def process_on_tstring_end(token, po)
  @ignore_next_comma = false
  @string_mark_stack.pop
  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
  po
end