1
+ # coding=utf-8
1
2
from __future__ import unicode_literals
3
+ import re
2
4
import sys
3
5
import json
6
+ import six
4
7
5
8
6
9
class Visitor (object ):
@@ -247,12 +250,6 @@ def __init__(self, id, value, attributes=None,
247
250
self .comment = comment
248
251
249
252
250
- class VariantList (SyntaxNode ):
251
- def __init__ (self , variants , ** kwargs ):
252
- super (VariantList , self ).__init__ (** kwargs )
253
- self .variants = variants
254
-
255
-
256
253
class Pattern (SyntaxNode ):
257
254
def __init__ (self , elements , ** kwargs ):
258
255
super (Pattern , self ).__init__ (** kwargs )
@@ -279,29 +276,64 @@ class Expression(SyntaxNode):
279
276
"""An abstract base class for expressions."""
280
277
281
278
282
- class StringLiteral (Expression ):
283
- def __init__ (self , raw , value , ** kwargs ):
284
- super (StringLiteral , self ).__init__ (** kwargs )
285
- self .raw = raw
286
- self .value = value
287
-
288
-
289
- class NumberLiteral (Expression ):
279
+ class Literal (Expression ):
280
+ """An abstract base class for literals."""
290
281
def __init__ (self , value , ** kwargs ):
291
- super (NumberLiteral , self ).__init__ (** kwargs )
282
+ super (Literal , self ).__init__ (** kwargs )
292
283
self .value = value
293
284
285
+ def parse (self ):
286
+ return {'value' : self .value }
287
+
288
+
289
+ class StringLiteral (Literal ):
290
+ def parse (self ):
291
+ def from_escape_sequence (matchobj ):
292
+ c , codepoint4 , codepoint6 = matchobj .groups ()
293
+ if c :
294
+ return c
295
+ codepoint = int (codepoint4 or codepoint6 , 16 )
296
+ if codepoint <= 0xD7FF or 0xE000 <= codepoint :
297
+ return six .unichr (codepoint )
298
+ # Escape sequences reresenting surrogate code points are
299
+ # well-formed but invalid in Fluent. Replace them with U+FFFD
300
+ # REPLACEMENT CHARACTER.
301
+ return '�'
302
+
303
+ value = re .sub (
304
+ r'\\(?:(\\|")|u([0-9a-fA-F]{4})|U([0-9a-fA-F]{6}))' ,
305
+ from_escape_sequence ,
306
+ self .value
307
+ )
308
+ return {'value' : value }
309
+
310
+
311
+ class NumberLiteral (Literal ):
312
+ def parse (self ):
313
+ value = float (self .value )
314
+ decimal_position = self .value .find ('.' )
315
+ precision = 0
316
+ if decimal_position >= 0 :
317
+ precision = len (self .value ) - decimal_position - 1
318
+ return {
319
+ 'value' : value ,
320
+ 'precision' : precision
321
+ }
322
+
294
323
295
324
class MessageReference (Expression ):
296
- def __init__ (self , id , ** kwargs ):
325
+ def __init__ (self , id , attribute = None , ** kwargs ):
297
326
super (MessageReference , self ).__init__ (** kwargs )
298
327
self .id = id
328
+ self .attribute = attribute
299
329
300
330
301
331
class TermReference (Expression ):
302
- def __init__ (self , id , ** kwargs ):
332
+ def __init__ (self , id , attribute = None , arguments = None , ** kwargs ):
303
333
super (TermReference , self ).__init__ (** kwargs )
304
334
self .id = id
335
+ self .attribute = attribute
336
+ self .arguments = arguments
305
337
306
338
307
339
class VariableReference (Expression ):
@@ -311,9 +343,10 @@ def __init__(self, id, **kwargs):
311
343
312
344
313
345
class FunctionReference (Expression ):
314
- def __init__ (self , id , ** kwargs ):
346
+ def __init__ (self , id , arguments , ** kwargs ):
315
347
super (FunctionReference , self ).__init__ (** kwargs )
316
348
self .id = id
349
+ self .arguments = arguments
317
350
318
351
319
352
class SelectExpression (Expression ):
@@ -323,26 +356,11 @@ def __init__(self, selector, variants, **kwargs):
323
356
self .variants = variants
324
357
325
358
326
- class AttributeExpression (Expression ):
327
- def __init__ (self , ref , name , ** kwargs ):
328
- super (AttributeExpression , self ).__init__ (** kwargs )
329
- self .ref = ref
330
- self .name = name
331
-
332
-
333
- class VariantExpression (Expression ):
334
- def __init__ (self , ref , key , ** kwargs ):
335
- super (VariantExpression , self ).__init__ (** kwargs )
336
- self .ref = ref
337
- self .key = key
338
-
339
-
340
- class CallExpression (Expression ):
341
- def __init__ (self , callee , positional = None , named = None , ** kwargs ):
342
- super (CallExpression , self ).__init__ (** kwargs )
343
- self .callee = callee
344
- self .positional = positional or []
345
- self .named = named or []
359
+ class CallArguments (SyntaxNode ):
360
+ def __init__ (self , positional = None , named = None , ** kwargs ):
361
+ super (CallArguments , self ).__init__ (** kwargs )
362
+ self .positional = [] if positional is None else positional
363
+ self .named = [] if named is None else named
346
364
347
365
348
366
class Attribute (SyntaxNode ):
@@ -412,8 +430,8 @@ def __init__(self, start, end, **kwargs):
412
430
413
431
414
432
class Annotation (SyntaxNode ):
415
- def __init__ (self , code , args = None , message = None , ** kwargs ):
433
+ def __init__ (self , code , arguments = None , message = None , ** kwargs ):
416
434
super (Annotation , self ).__init__ (** kwargs )
417
435
self .code = code
418
- self .args = args or []
436
+ self .arguments = arguments or []
419
437
self .message = message
0 commit comments