File: ruby.rb

package info (click to toggle)
libgettext-ruby 1.7.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,892 kB
  • ctags: 1,198
  • sloc: ruby: 6,738; ansic: 67; makefile: 38; sql: 14; sh: 6
file content (209 lines) | stat: -rw-r--r-- 4,329 bytes parent folder | download
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
#!/usr/bin/ruby
=begin
  parser/ruby.rb - parser for ruby script

  Copyright (C) 2003-2005  Masao Mutoh
  Copyright (C) 2005       speakillof
  Copyright (C) 2001,2002  Yasushi Shoji, Masao Mutoh
 
  You may redistribute it and/or modify it under the same
  license terms as Ruby.

  $Id: ruby.rb,v 1.7 2006/06/11 15:36:20 mutoh Exp $
=end

require 'irb/ruby-lex.rb'
require 'stringio'

class RubyLexX < RubyLex  # :nodoc: all
  
  def initialize
    super
    @prompt = nil
    @here_header = nil
    @lex_state = nil
  end
  
  def read_escape
    case ch = getc
    when "\n", "\r", "\f"
    when "\\", "n", "t", "r", "f", "v", "a", "e", "b" #"
      return "\\".concat(ch)
    when /[0-7]/
      ungetc ch
      3.times do
        case ch = getc
        when /[0-7]/
        when nil
          break
        else
          ungetc
          break
        end
      end
      
    when "x"
      2.times do
        case ch = getc
        when /[0-9a-fA-F]/
        when nil
          break
        else
          ungetc
          break
        end
      end
      
    when "M"
      if (ch = getc) != '-'
        ungetc
      else
        if (ch = getc) == "\\" #"
          read_escape
        end
      end
      
    when "C", "c", "^"
      if ch == "C" and (ch = getc) != "-"
        ungetc
      elsif (ch = getc) == "\\" #"
        read_escape
      end
    when '#'
      return ch
    else
      # other characters
    end
  end

  # Parser#parse resemlbes RubyLex#lex
  def parse
    until (  (tk = token).kind_of?(RubyToken::TkEND_OF_SCRIPT) && !@continue or tk.nil?  )
      s = get_readed
 
      if RubyToken::TkSTRING === tk
        def tk.value
          @value 
        end
        
        def tk.value=(s)
          @value = s
        end
        
        if @here_header
          s = s.sub(/\A.*?\n/, '').sub(/^.*\n\Z/, '')
        else
	  begin
	    s = eval(s)
	  rescue Exception
	    # Do nothing.
	  end
        end
        
        tk.value = s
      end
      
      if $DEBUG
        if tk.is_a? TkSTRING
          $stderr.puts("#{tk}: #{tk.value}")
        elsif tk.is_a? TkIDENTIFIER
          $stderr.puts("#{tk}: #{tk.name}")
        else
          $stderr.puts(tk)
        end
      end
      
      yield tk
    end
    
    return nil
  end

end

module GetText
  module RubyParser
    ID = ['gettext', '_', 'N_', 'sgettext', 's_']
    PLURAL_ID = ['ngettext', 'n_', 'Nn_']

    module_function
    def parse(file, targets = [])  # :nodoc:
      lines = IO.readlines(file)
      parse_lines(file, lines, targets)
    end

    def parse_lines(file_name, lines, targets)  # :nodoc:
      file = StringIO.new(lines.join + "\n")
      rl = RubyLexX.new
      rl.set_input(file)
      rl.skip_space = true
      #rl.readed_auto_clean_up = true

      target = nil
      msgid = nil
      line_no = nil
      
      rl.parse do |tk|
        case tk
        when RubyToken::TkIDENTIFIER, RubyToken::TkCONSTANT
          if ID.include?(tk.name)
            target = :normal
          elsif PLURAL_ID.include?(tk.name)
            target = :plural
          else
            target = nil
          end
          line_no = tk.line_no.to_s
        when RubyToken::TkSTRING
          if target
            if msgid
              msgid += tk.value
            else
              msgid = tk.value 
            end
          end
        when RubyToken::TkPLUS, RubyToken::TkNL
          #do nothing
        when RubyToken::TkCOMMA
          if msgid and target == :plural
            msgid += "\000"
            target = :normal
          end
        else
          if msgid
            key_existed = targets.assoc(msgid.gsub(/\n/, '\n'))
            if key_existed 
              targets[targets.index(key_existed)] = key_existed <<
                file_name + ":" + line_no
            else
              targets << [msgid.gsub(/\n/, '\n'), file_name + ":" + line_no]
            end
            msgid = nil
            target = nil
          end
        end
        targets
      end
      targets
    end

    def target?(file)  # :nodoc:
      true # always true, as default parser.
    end
    
  end 
end



if __FILE__ == $0
  require 'pp'
  ARGV.each do |file|
    pp GetText::RubyParser.parse(file)
  end
  
  #rl = RubyLexX.new; rl.set_input(ARGF)  
  #rl.parse do |tk|
    #p tk
  #end  
end