Class: MessageFormat::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/message_format/parser.rb

Defined Under Namespace

Classes: SyntaxError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



21
22
23
24
25
# File 'lib/message_format/parser.rb', line 21

def initialize ()
  @pattern = nil
  @length = 0
  @index = 0
end

Class Method Details

.parse(pattern) ⇒ Object



353
354
355
# File 'lib/message_format/parser.rb', line 353

def self.parse ( pattern )
  Parser.new().parse(pattern)
end

Instance Method Details

#error_message(expected = nil, found) ⇒ Object



347
348
349
350
351
# File 'lib/message_format/parser.rb', line 347

def error_message ( expected=nil, found )
  expected ?
    "Expected \"#{ expected }\" but found \"#{ found }\"" :
    "Unexpected \"#{ found }\" found" 
end

#is_digit(char) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/message_format/parser.rb', line 38

def is_digit ( char )
  char == '0' or
  char == '1' or
  char == '2' or
  char == '3' or
  char == '4' or
  char == '5' or
  char == '6' or
  char == '7' or
  char == '8' or
  char == '9'
end

#is_whitespace(char) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/message_format/parser.rb', line 51

def is_whitespace ( char )
  char == "\s" or
  char == "\t" or
  char == "\n" or
  char == "\r" or
  char == "\f" or
  char == "\v" or
  char == "\u00A0" or
  char == "\u2028" or
  char == "\u2029"
end

#parse(pattern) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/message_format/parser.rb', line 27

def parse ( pattern )
  if !pattern.is_a?(String)
    raise_expected('String pattern', pattern.class.to_s)
  end

  @pattern = pattern
  @length = pattern.length
  @index = 0
  parse_message("message")
end

#parse_arg_idObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/message_format/parser.rb', line 179

def parse_arg_id ()
  skip_whitespace()
  id = ''
  while @index < @length
    char = @pattern[@index]
    if char == '{' or char == '#'
      raise_expected('argument id')
    end
    if char == '}' or char == ',' or is_whitespace(char)
      break
    end
    id += char
    @index += 1
  end
  if id.empty?
    raise_expected('argument id')
  end
  skip_whitespace()
  id
end

#parse_arg_typeObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/message_format/parser.rb', line 200

def parse_arg_type ()
  skip_whitespace()
  arg_type = nil
  types = [
    'number', 'date', 'time', 'ordinal', 'duration', 'spellout', 'plural', 'selectordinal', 'select'
  ]
  types.each do |type|
    if @pattern.slice(@index, type.length) == type
      arg_type = type
      @index += type.length
      break
    end
  end
  if !arg_type
    raise_expected(types.join(', '))
  end
  skip_whitespace()
  arg_type
end

#parse_argumentObject



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
# File 'lib/message_format/parser.rb', line 122

def parse_argument ()
  if @pattern[@index] == '#'
    @index += 1 # move passed #
    return [ '#' ]
  end

  @index += 1 # move passed {
  id = parse_arg_id()
  char = @pattern[@index]
  if char == '}' # end argument
    @index += 1 # move passed }
    return [ id ]
  end
  if char != ','
    raise_expected(',')
  end
  @index += 1 # move passed ,

  type = parse_arg_type()
  char = @pattern[@index]
  if char == '}' # end argument
    if (
      type == 'plural' or
      type == 'selectordinal' or
      type == 'select'
    )
      raise_expected(type + ' message options')
    end
    @index += 1 # move passed }
    return [ id, type ]
  end
  if char != ','
    raise_expected(',')
  end
  @index += 1 # move passed ,

  format = nil
  offset = nil
  if type == 'plural' or type == 'selectordinal'
    offset = parse_plural_offset()
    format = parse_sub_messages(type)
  elsif type == 'select'
    format = parse_sub_messages(type)
  else
    format = parse_simple_format()
  end
  char = @pattern[@index]
  if char != '}' # not ended argument
    raise_expected('}')
  end
  @index += 1 # move passed

  (type == 'plural' or type == 'selectordinal') ?
    [ id, type, offset, format ] :
    [ id, type, format ]
end

#parse_message(parent_type) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/message_format/parser.rb', line 310

def parse_message ( parent_type )
  elements = []
  text = parse_text(parent_type)
  if !text.empty?
    elements.push(text)
  end
  while @index < @length
    if @pattern[@index] == '}'
      if parent_type == 'message'
        raise_expected()
      end
      break
    end
    elements.push(parse_argument())
    text = parse_text(parent_type)
    if !text.empty?
      elements.push(text)
    end
  end
  elements
end

#parse_plural_offsetObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/message_format/parser.rb', line 230

def parse_plural_offset ()
  skip_whitespace()
  offset = 0
  if @pattern.slice(@index, 7) == 'offset:'
    @index += 7 # move passed offset:
    skip_whitespace()
    start = @index
    while (
      @index < @length and
      is_digit(@pattern[@index])
    )
      @index += 1
    end
    if start == @index
      raise_expected('offset number')
    end
    offset = @pattern[start..@index].to_i
    skip_whitespace()
  end
  offset
end

#parse_selectorObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/message_format/parser.rb', line 275

def parse_selector ()
  selector = ''
  while @index < @length
    char = @pattern[@index]
    if char == '}' or char == ','
      raise_expected('{')
    end
    if char == '{' or is_whitespace(char)
      break
    end
    selector += char
    @index += 1
  end
  if selector.empty?
    raise_expected('selector')
  end
  skip_whitespace()
  selector
end

#parse_simple_formatObject



220
221
222
223
224
225
226
227
228
# File 'lib/message_format/parser.rb', line 220

def parse_simple_format ()
  skip_whitespace()
  style = parse_text('style')
  if style.empty?
    raise_expected('argument style name')
  end
  skip_whitespace()
  style
end

#parse_sub_message(parent_type) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/message_format/parser.rb', line 295

def parse_sub_message ( parent_type )
  char = @pattern[@index]
  if char != '{'
    raise_expected('{')
  end
  @index += 1 # move passed {
  message = parse_message(parent_type)
  char = @pattern[@index]
  if char != '}'
    raise_expected('}')
  end
  @index += 1 # move passed }
  message
end

#parse_sub_messages(parent_type) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/message_format/parser.rb', line 252

def parse_sub_messages ( parent_type )
  skip_whitespace()
  options = {}
  has_subs = false
  while (
    @index < @length and
    @pattern[@index] != '}'
  )
    selector = parse_selector()
    skip_whitespace()
    options[selector] = parse_sub_message(parent_type)
    has_subs = true
    skip_whitespace()
  end
  if !has_subs
    raise_expected(parent_type + ' message options')
  end
  if !options.has_key?('other') # does not have an other selector
    raise_expected(nil, nil, '"other" option must be specified in ' + parent_type)
  end
  options
end

#parse_text(parent_type) ⇒ 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/message_format/parser.rb', line 69

def parse_text ( parent_type )
  is_hash_special = (parent_type == 'plural' or parent_type == 'selectordinal')
  is_arg_style = (parent_type == 'style')
  text = ''
  while @index < @length
    char = @pattern[@index]
    if (
      char == '{' or
      char == '}' or
      (is_hash_special and char == '#') or
      (is_arg_style and is_whitespace(char))
    )
      break
    elsif char == '\''
      @index += 1
      char = @pattern[@index]
      if char == '\'' # double is always 1 '
        text += char
        @index += 1
      elsif (
        # only when necessary
        char == '{' or
        char == '}' or
        (is_hash_special and char == '#') or
        (is_arg_style and is_whitespace(char))
      )
        text += char
        while @index + 1 < @length
          @index += 1
          char = @pattern[@index]
          if @pattern.slice(@index, 2) == '\'\'' # double is always 1 '
            text += char
            @index += 1
          elsif char == '\'' # end of quoted
            @index += 1
            break
          else
            text += char
          end
        end
      else # lone ' is just a '
        text += '\''
        # already incremented
      end
    else
      text += char
      @index += 1
    end
  end

  text
end

#raise_expected(expected = nil, found = nil, message = nil) ⇒ Object

Raises:



332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/message_format/parser.rb', line 332

def raise_expected ( expected=nil, found=nil, message=nil )
  lines = @pattern[0..@index].split(/\r?\n/)
  line = lines.length
  column = lines.last.length
  if !found
    found = @index < @length ? @pattern[@index] : 'end of input'
  end
  if !message
    message = error_message(expected, found)
  end
  message += ' in "' + @pattern.gsub(/\r?\n/, "\n") + '"'

  raise SyntaxError.new(message, expected, found, @index, line, column)
end

#skip_whitespaceObject



63
64
65
66
67
# File 'lib/message_format/parser.rb', line 63

def skip_whitespace ()
  while @index < @length and is_whitespace(@pattern[@index])
    @index += 1
  end
end