1+ # coding=utf-8
12from __future__ import unicode_literals
3+ import re
24import sys
35import json
6+ import six
47
58
69class Visitor (object ):
@@ -247,12 +250,6 @@ def __init__(self, id, value, attributes=None,
247250 self .comment = comment
248251
249252
250- class VariantList (SyntaxNode ):
251- def __init__ (self , variants , ** kwargs ):
252- super (VariantList , self ).__init__ (** kwargs )
253- self .variants = variants
254-
255-
256253class Pattern (SyntaxNode ):
257254 def __init__ (self , elements , ** kwargs ):
258255 super (Pattern , self ).__init__ (** kwargs )
@@ -279,29 +276,64 @@ class Expression(SyntaxNode):
279276 """An abstract base class for expressions."""
280277
281278
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."""
290281 def __init__ (self , value , ** kwargs ):
291- super (NumberLiteral , self ).__init__ (** kwargs )
282+ super (Literal , self ).__init__ (** kwargs )
292283 self .value = value
293284
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+
294323
295324class MessageReference (Expression ):
296- def __init__ (self , id , ** kwargs ):
325+ def __init__ (self , id , attribute = None , ** kwargs ):
297326 super (MessageReference , self ).__init__ (** kwargs )
298327 self .id = id
328+ self .attribute = attribute
299329
300330
301331class TermReference (Expression ):
302- def __init__ (self , id , ** kwargs ):
332+ def __init__ (self , id , attribute = None , arguments = None , ** kwargs ):
303333 super (TermReference , self ).__init__ (** kwargs )
304334 self .id = id
335+ self .attribute = attribute
336+ self .arguments = arguments
305337
306338
307339class VariableReference (Expression ):
@@ -311,9 +343,10 @@ def __init__(self, id, **kwargs):
311343
312344
313345class FunctionReference (Expression ):
314- def __init__ (self , id , ** kwargs ):
346+ def __init__ (self , id , arguments , ** kwargs ):
315347 super (FunctionReference , self ).__init__ (** kwargs )
316348 self .id = id
349+ self .arguments = arguments
317350
318351
319352class SelectExpression (Expression ):
@@ -323,26 +356,11 @@ def __init__(self, selector, variants, **kwargs):
323356 self .variants = variants
324357
325358
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
346364
347365
348366class Attribute (SyntaxNode ):
@@ -412,8 +430,8 @@ def __init__(self, start, end, **kwargs):
412430
413431
414432class Annotation (SyntaxNode ):
415- def __init__ (self , code , args = None , message = None , ** kwargs ):
433+ def __init__ (self , code , arguments = None , message = None , ** kwargs ):
416434 super (Annotation , self ).__init__ (** kwargs )
417435 self .code = code
418- self .args = args or []
436+ self .arguments = arguments or []
419437 self .message = message
0 commit comments