File: po_parser.rb

package info (click to toggle)
ruby-gettext 3.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,284 kB
  • ctags: 1,287
  • sloc: ruby: 9,178; makefile: 8
file content (500 lines) | stat: -rw-r--r-- 10,985 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
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
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
# -*- coding: utf-8 -*-
#
# po_parser.rb - Generate a .mo
#
# Copyright (C) 2003-2009 Masao Mutoh <mutomasa at gmail.com>
# Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
#
# You may redistribute it and/or modify it under the same
# license terms as Ruby or LGPL.

#
# DO NOT MODIFY!!!!
# This file is automatically generated by Racc 1.4.11
# from Racc grammer file "".
#

require 'racc/parser.rb'

require "gettext/po"

module GetText
  class POParser < Racc::Parser

module_eval(<<'...end po_parser.ry/module_eval...', 'po_parser.ry', 123)
  if GetText.respond_to?(:bindtextdomain)
    include GetText
    GetText.bindtextdomain("gettext")
  else
    def _(message_id)
      message_id
    end
    private :_
  end

  attr_writer :ignore_fuzzy, :report_warning
  def initialize
    @ignore_fuzzy = true
    @report_warning = true
  end

  def ignore_fuzzy?
    @ignore_fuzzy
  end

  def report_warning?
    @report_warning
  end

  def unescape(orig)
    ret = orig.gsub(/\\n/, "\n")
    ret.gsub!(/\\t/, "\t")
    ret.gsub!(/\\r/, "\r")
    ret.gsub!(/\\"/, "\"")
    ret
  end
  private :unescape

  def unescape_string(string)
    string.gsub(/\\\\/, "\\")
  end
  private :unescape_string

  def parse(str, data)
    @translator_comments = []
    @extracted_comments = []
    @references = []
    @flags = []
    @previous = []
    @comments = []
    @data = data
    @fuzzy = false
    @msgctxt = nil
    @msgid_plural = nil

    str.strip!
    @q = []
    until str.empty? do
      case str
      when /\A\s+/
        str = $'
      when /\Amsgctxt/
        @q.push [:MSGCTXT, $&]
        str = $'
      when /\Amsgid_plural/
        @q.push [:MSGID_PLURAL, $&]
        str = $'
      when /\Amsgid/
        @q.push [:MSGID, $&]
        str = $'
      when /\Amsgstr/
        @q.push [:MSGSTR, $&]
        str = $'
      when /\A\[(\d+)\]/
        @q.push [:PLURAL_NUM, $1]
        str = $'
      when /\A\#~(.*)/
        if report_warning?
          $stderr.print _("Warning: obsolete msgid exists.\n")
          $stderr.print "         #{$&}\n"
        end
        @q.push [:COMMENT, $&]
        str = $'
      when /\A\#(.*)/
        @q.push [:COMMENT, $&]
        str = $'
      when /\A\"(.*)\"/
        @q.push [:STRING, unescape_string($1)]
        str = $'
      else
        #c = str[0,1]
        #@q.push [:STRING, c]
        str = str[1..-1]
      end
    end
    @q.push [false, "$end"]
    if $DEBUG
      @q.each do |a,b|
      puts "[#{a}, #{b}]"
      end
    end
    @yydebug = true if $DEBUG
    do_parse

    if @comments.size > 0
      @data.set_comment(:last, @comments.join("\n"))
    end
    @data
  end

  def next_token
    @q.shift
  end

  def on_message(msgid, msgstr)
    msgstr = nil if msgstr.empty?

    if @data.instance_of?(PO)
      type = detect_entry_type
      entry = POEntry.new(type)
      entry.translator_comment = format_comment(@translator_comments)
      entry.extracted_comment = format_comment(@extracted_comments)
      entry.flags = @flags
      entry.previous = format_comment(@previous)
      entry.references = @references
      entry.msgctxt = @msgctxt
      entry.msgid = msgid
      entry.msgid_plural = @msgid_plural
      entry.msgstr = msgstr

      @data[@msgctxt, msgid] = entry
    else
      options = {}
      options[:msgctxt] = @msgctxt
      options[:msgid_plural] = @msgid_plural
      @data.store(msgid, msgstr, options)
      @data.set_comment(msgid, format_comment(@comments))
    end

    @translator_comments = []
    @extracted_comments = []
    @references = []
    @flags = []
    @previous = []
    @references = []
    @comments.clear
    @msgctxt = nil
    @msgid_plural = nil
  end

  def format_comment(comments)
    return "" if comments.empty?

    comment = comments.join("\n")
    comment << "\n" if comments.last.empty?
    comment
  end

  def on_comment(comment)
    @fuzzy = true if (/fuzzy/ =~ comment)
    if @data.instance_of?(PO)
      if comment == "#"
        @translator_comments << ""
      elsif /\A(#.)\s*(.*)\z/ =~ comment
        mark = $1
        content = $2
        case mark
        when POFormat::TRANSLATOR_COMMENT_MARK
          @translator_comments << content
        when POFormat::EXTRACTED_COMMENT_MARK
          @extracted_comments << content
        when POFormat::REFERENCE_COMMENT_MARK
          @references.concat(parse_references_line(content))
        when POFormat::FLAG_MARK
          @flags.concat(parse_flags_line(content))
        when POFormat::PREVIOUS_COMMENT_MARK
          @previous << content
        else
          @comments << comment
        end
      end
    else
      @comments << comment
    end
  end

  def parse_file(po_file, data)
    args = [ po_file ]
    # In Ruby 1.9, we must detect proper encoding of a PO file.
    if String.instance_methods.include?(:encode)
      encoding = detect_file_encoding(po_file)
      args << "r:#{encoding}"
    end
    @po_file = po_file
    parse(File.open(*args) {|io| io.read }, data)
  end

  private
  def detect_file_encoding(po_file)
    open(po_file, :encoding => "ASCII-8BIT") do |input|
      in_header = false
      input.each_line do |line|
        case line.chomp
        when /\Amsgid\s+"(.*)"\z/
          id = $1
          break unless id.empty?
          in_header = true
        when /\A"Content-Type:.*\scharset=(.*)\\n"\z/
          charset = $1
          next unless in_header
          break if template_charset?(charset)
          return Encoding.find(charset)
        end
      end
    end
    Encoding.default_external
  end

  def template_charset?(charset)
    charset == "CHARSET"
  end

  def detect_entry_type
    if @msgctxt.nil?
      if @msgid_plural.nil?
        :normal
      else
        :plural
      end
    else
      if @msgid_plural.nil?
        :msgctxt
      else
        :msgctxt_plural
      end
    end
  end

  def parse_references_line(line)
    line.split(/\s+/)
  end

  def parse_flags_line(line)
    line.split(/\s+/)
  end
...end po_parser.ry/module_eval...
##### State transition tables begin ###

racc_action_table = [
     2,    13,    10,     9,     6,    17,    16,    15,    22,    15,
    15,    13,    13,    13,    15,    11,    22,    24,    13,    15 ]

racc_action_check = [
     1,    17,     1,     1,     1,    14,    14,    14,    19,    19,
    12,     6,    16,     9,    18,     2,    20,    22,    24,    25 ]

racc_action_pointer = [
   nil,     0,    15,   nil,   nil,   nil,     4,   nil,   nil,     6,
   nil,   nil,     3,   nil,     0,   nil,     5,    -6,     7,     2,
    10,   nil,     9,   nil,    11,    12 ]

racc_action_default = [
    -1,   -16,   -16,    -2,    -3,    -4,   -16,    -6,    -7,   -16,
   -13,    26,    -5,   -15,   -16,   -14,   -16,   -16,    -8,   -16,
    -9,   -11,   -16,   -10,   -16,   -12 ]

racc_goto_table = [
    12,    21,    23,    14,     4,     5,     3,     7,     8,    20,
    18,    19,     1,   nil,   nil,   nil,   nil,   nil,    25 ]

racc_goto_check = [
     5,     9,     9,     5,     3,     4,     2,     6,     7,     8,
     5,     5,     1,   nil,   nil,   nil,   nil,   nil,     5 ]

racc_goto_pointer = [
   nil,    12,     5,     3,     4,    -6,     6,     7,   -10,   -18 ]

racc_goto_default = [
   nil,   nil,   nil,   nil,   nil,   nil,   nil,   nil,   nil,   nil ]

racc_reduce_table = [
  0, 0, :racc_error,
  0, 10, :_reduce_none,
  2, 10, :_reduce_none,
  2, 10, :_reduce_none,
  2, 10, :_reduce_none,
  2, 12, :_reduce_5,
  1, 13, :_reduce_none,
  1, 13, :_reduce_none,
  4, 15, :_reduce_8,
  5, 16, :_reduce_9,
  2, 17, :_reduce_10,
  1, 17, :_reduce_none,
  3, 18, :_reduce_12,
  1, 11, :_reduce_13,
  2, 14, :_reduce_14,
  1, 14, :_reduce_15 ]

racc_reduce_n = 16

racc_shift_n = 26

racc_token_table = {
  false => 0,
  :error => 1,
  :COMMENT => 2,
  :MSGID => 3,
  :MSGCTXT => 4,
  :MSGID_PLURAL => 5,
  :MSGSTR => 6,
  :STRING => 7,
  :PLURAL_NUM => 8 }

racc_nt_base = 9

racc_use_result_var = true

Racc_arg = [
  racc_action_table,
  racc_action_check,
  racc_action_default,
  racc_action_pointer,
  racc_goto_table,
  racc_goto_check,
  racc_goto_default,
  racc_goto_pointer,
  racc_nt_base,
  racc_reduce_table,
  racc_token_table,
  racc_shift_n,
  racc_reduce_n,
  racc_use_result_var ]

Racc_token_to_s_table = [
  "$end",
  "error",
  "COMMENT",
  "MSGID",
  "MSGCTXT",
  "MSGID_PLURAL",
  "MSGSTR",
  "STRING",
  "PLURAL_NUM",
  "$start",
  "msgfmt",
  "comment",
  "msgctxt",
  "message",
  "string_list",
  "single_message",
  "plural_message",
  "msgstr_plural",
  "msgstr_plural_line" ]

Racc_debug_parser = true

##### State transition tables end #####

# reduce 0 omitted

# reduce 1 omitted

# reduce 2 omitted

# reduce 3 omitted

# reduce 4 omitted

module_eval(<<'.,.,', 'po_parser.ry', 26)
  def _reduce_5(val, _values, result)
        @msgctxt = unescape(val[1])
  
    result
  end
.,.,

# reduce 6 omitted

# reduce 7 omitted

module_eval(<<'.,.,', 'po_parser.ry', 38)
  def _reduce_8(val, _values, result)
        msgid_raw = val[1]
    msgid = unescape(msgid_raw)
    msgstr = unescape(val[3])
    use_message_p = true
    if @fuzzy and not msgid.empty?
      use_message_p = (not ignore_fuzzy?)
      if report_warning?
        if ignore_fuzzy?
          $stderr.print _("Warning: fuzzy message was ignored.\n")
        else
          $stderr.print _("Warning: fuzzy message was used.\n")
        end
        $stderr.print "  #{@po_file}: msgid '#{msgid_raw}'\n"
      end
    end
    @fuzzy = false
    on_message(msgid, msgstr) if use_message_p
    result = ""
  
    result
  end
.,.,

module_eval(<<'.,.,', 'po_parser.ry', 61)
  def _reduce_9(val, _values, result)
        if @fuzzy and ignore_fuzzy?
      if val[1] != ""
        if report_warning?
          $stderr.print _("Warning: fuzzy message was ignored.\n")
          $stderr.print "msgid = '#{val[1]}\n"
        end
      else
        on_message("", unescape(val[3]))
      end
      @fuzzy = false
    else
      @msgid_plural = unescape(val[3])
      on_message(unescape(val[1]), unescape(val[4]))
    end
    result = ""
  
    result
  end
.,.,

module_eval(<<'.,.,', 'po_parser.ry', 82)
  def _reduce_10(val, _values, result)
        if val[0].size > 0
      result = val[0] + "\000" + val[1]
    else
      result = ""
    end
  
    result
  end
.,.,

# reduce 11 omitted

module_eval(<<'.,.,', 'po_parser.ry', 94)
  def _reduce_12(val, _values, result)
        result = val[2]
  
    result
  end
.,.,

module_eval(<<'.,.,', 'po_parser.ry', 101)
  def _reduce_13(val, _values, result)
        on_comment(val[0])
  
    result
  end
.,.,

module_eval(<<'.,.,', 'po_parser.ry', 109)
  def _reduce_14(val, _values, result)
        result = val.delete_if{|item| item == ""}.join
  
    result
  end
.,.,

module_eval(<<'.,.,', 'po_parser.ry', 113)
  def _reduce_15(val, _values, result)
        result = val[0]
  
    result
  end
.,.,

def _reduce_none(val, _values, result)
  val[0]
end

  end   # class POParser
end   # module GetText