4
4
5
5
6
6
def to_json (value ):
7
- if isinstance (value , Node ):
7
+ if isinstance (value , BaseNode ):
8
8
return value .to_json ()
9
9
if isinstance (value , list ):
10
10
return list (map (to_json , value ))
@@ -19,25 +19,21 @@ def from_json(value):
19
19
k : from_json (v )
20
20
for k , v in value .items ()
21
21
if k != 'type'
22
- if k != 'span'
23
22
}
24
- node = cls (** args )
25
-
26
- # Spans need to be added via add_span, not __init__.
27
- if 'span' in value :
28
- span = value ['span' ]
29
- # Message and section comments don't have their own spans.
30
- if span is not None :
31
- node .add_span (span ['start' ], span ['end' ])
32
-
33
- return node
23
+ return cls (** args )
34
24
if isinstance (value , list ):
35
25
return list (map (from_json , value ))
36
26
else :
37
27
return value
38
28
39
29
40
- class Node (object ):
30
+ class BaseNode (object ):
31
+ """Base class for all Fluent AST nodes.
32
+
33
+ All productions described in the ASDL subclass BaseNode, including Span and
34
+ Annotation. Implements __str__, to_json and traverse.
35
+ """
36
+
41
37
def traverse (self , fun ):
42
38
"""Postorder-traverse this node and apply `fun` to all child nodes.
43
39
@@ -49,7 +45,7 @@ def traverse(self, fun):
49
45
50
46
def visit (value ):
51
47
"""Call `fun` on `value` and its descendants."""
52
- if isinstance (value , Node ):
48
+ if isinstance (value , BaseNode ):
53
49
return value .traverse (fun )
54
50
if isinstance (value , list ):
55
51
return fun (list (map (visit , value )))
@@ -79,162 +75,165 @@ def __str__(self):
79
75
return json .dumps (self .to_json ())
80
76
81
77
82
- class Resource (Node ):
83
- def __init__ (self , body = None , comment = None ):
84
- super (Resource , self ).__init__ ()
85
- self .body = body or []
86
- self .comment = comment
87
-
78
+ class SyntaxNode (BaseNode ):
79
+ """Base class for AST nodes which can have Spans."""
88
80
89
- class Entry (Node ):
90
- def __init__ (self , span = None , annotations = None ):
91
- super (Entry , self ).__init__ ()
81
+ def __init__ (self , span = None , ** kwargs ):
82
+ super (SyntaxNode , self ).__init__ (** kwargs )
92
83
self .span = span
93
- self .annotations = annotations or []
94
84
95
85
def add_span (self , start , end ):
96
86
self .span = Span (start , end )
97
87
88
+
89
+ class Resource (SyntaxNode ):
90
+ def __init__ (self , body = None , comment = None , ** kwargs ):
91
+ super (Resource , self ).__init__ (** kwargs )
92
+ self .body = body or []
93
+ self .comment = comment
94
+
95
+
96
+ class Entry (SyntaxNode ):
97
+ def __init__ (self , annotations = None , ** kwargs ):
98
+ super (Entry , self ).__init__ (** kwargs )
99
+ self .annotations = annotations or []
100
+
98
101
def add_annotation (self , annot ):
99
102
self .annotations .append (annot )
100
103
101
104
102
105
class Message (Entry ):
103
- def __init__ (
104
- self , id , value = None , attributes = None , tags = None , comment = None ,
105
- span = None , annotations = None ):
106
- super (Message , self ).__init__ (span , annotations )
106
+ def __init__ (self , id , value = None , attributes = None , tags = None ,
107
+ comment = None , ** kwargs ):
108
+ super (Message , self ).__init__ (** kwargs )
107
109
self .id = id
108
110
self .value = value
109
- self .attributes = attributes
110
- self .tags = tags
111
+ self .attributes = attributes or []
112
+ self .tags = tags or []
111
113
self .comment = comment
112
114
113
- class Pattern (Node ):
114
- def __init__ (self , elements ):
115
- super (Pattern , self ).__init__ ()
115
+ class Pattern (SyntaxNode ):
116
+ def __init__ (self , elements , ** kwargs ):
117
+ super (Pattern , self ).__init__ (** kwargs )
116
118
self .elements = elements
117
119
118
- class TextElement (Node ):
119
- def __init__ (self , value ):
120
- super (TextElement , self ).__init__ ()
120
+ class TextElement (SyntaxNode ):
121
+ def __init__ (self , value , ** kwargs ):
122
+ super (TextElement , self ).__init__ (** kwargs )
121
123
self .value = value
122
124
123
- class Expression (Node ):
124
- def __init__ (self ):
125
- super (Expression , self ).__init__ ()
125
+ class Expression (SyntaxNode ):
126
+ def __init__ (self , ** kwargs ):
127
+ super (Expression , self ).__init__ (** kwargs )
126
128
127
129
class StringExpression (Expression ):
128
- def __init__ (self , value ):
129
- super (StringExpression , self ).__init__ ()
130
+ def __init__ (self , value , ** kwargs ):
131
+ super (StringExpression , self ).__init__ (** kwargs )
130
132
self .value = value
131
133
132
134
class NumberExpression (Expression ):
133
- def __init__ (self , value ):
134
- super (NumberExpression , self ).__init__ ()
135
+ def __init__ (self , value , ** kwargs ):
136
+ super (NumberExpression , self ).__init__ (** kwargs )
135
137
self .value = value
136
138
137
139
class MessageReference (Expression ):
138
- def __init__ (self , id ):
139
- super (MessageReference , self ).__init__ ()
140
+ def __init__ (self , id , ** kwargs ):
141
+ super (MessageReference , self ).__init__ (** kwargs )
140
142
self .id = id
141
143
142
144
class ExternalArgument (Expression ):
143
- def __init__ (self , id ):
144
- super (ExternalArgument , self ).__init__ ()
145
+ def __init__ (self , id , ** kwargs ):
146
+ super (ExternalArgument , self ).__init__ (** kwargs )
145
147
self .id = id
146
148
147
149
class SelectExpression (Expression ):
148
- def __init__ (self , expression , variants ):
149
- super (SelectExpression , self ).__init__ ()
150
+ def __init__ (self , expression , variants , ** kwargs ):
151
+ super (SelectExpression , self ).__init__ (** kwargs )
150
152
self .expression = expression
151
153
self .variants = variants
152
154
153
155
class AttributeExpression (Expression ):
154
- def __init__ (self , id , name ):
155
- super (AttributeExpression , self ).__init__ ()
156
+ def __init__ (self , id , name , ** kwargs ):
157
+ super (AttributeExpression , self ).__init__ (** kwargs )
156
158
self .id = id
157
159
self .name = name
158
160
159
161
class VariantExpression (Expression ):
160
- def __init__ (self , id , key ):
161
- super (VariantExpression , self ).__init__ ()
162
+ def __init__ (self , id , key , ** kwargs ):
163
+ super (VariantExpression , self ).__init__ (** kwargs )
162
164
self .id = id
163
165
self .key = key
164
166
165
167
class CallExpression (Expression ):
166
- def __init__ (self , callee , args ):
167
- super (CallExpression , self ).__init__ ()
168
+ def __init__ (self , callee , args , ** kwargs ):
169
+ super (CallExpression , self ).__init__ (** kwargs )
168
170
self .callee = callee
169
171
self .args = args
170
172
171
- class Attribute (Node ):
172
- def __init__ (self , id , value ):
173
- super (Attribute , self ).__init__ ()
173
+ class Attribute (SyntaxNode ):
174
+ def __init__ (self , id , value , ** kwargs ):
175
+ super (Attribute , self ).__init__ (** kwargs )
174
176
self .id = id
175
177
self .value = value
176
178
177
- class Tag (Node ):
178
- def __init__ (self , name ):
179
- super (Tag , self ).__init__ ()
179
+ class Tag (SyntaxNode ):
180
+ def __init__ (self , name , ** kwargs ):
181
+ super (Tag , self ).__init__ (** kwargs )
180
182
self .name = name
181
183
182
- class Variant (Node ):
183
- def __init__ (self , key , value , default = False ):
184
- super (Variant , self ).__init__ ()
184
+ class Variant (SyntaxNode ):
185
+ def __init__ (self , key , value , default = False , ** kwargs ):
186
+ super (Variant , self ).__init__ (** kwargs )
185
187
self .key = key
186
188
self .value = value
187
189
self .default = default
188
190
189
- class NamedArgument (Node ):
190
- def __init__ (self , name , val ):
191
- super (NamedArgument , self ).__init__ ()
191
+ class NamedArgument (SyntaxNode ):
192
+ def __init__ (self , name , val , ** kwargs ):
193
+ super (NamedArgument , self ).__init__ (** kwargs )
192
194
self .name = name
193
195
self .val = val
194
196
195
- class Identifier (Node ):
196
- def __init__ (self , name ):
197
- super (Identifier , self ).__init__ ()
197
+ class Identifier (SyntaxNode ):
198
+ def __init__ (self , name , ** kwargs ):
199
+ super (Identifier , self ).__init__ (** kwargs )
198
200
self .name = name
199
201
200
202
class Symbol (Identifier ):
201
- def __init__ (self , name ):
202
- super (Symbol , self ).__init__ (name )
203
+ def __init__ (self , name , ** kwargs ):
204
+ super (Symbol , self ).__init__ (name , ** kwargs )
203
205
204
206
class Comment (Entry ):
205
- def __init__ (self , content = None , span = None , annotations = None ):
206
- super (Comment , self ).__init__ (span , annotations )
207
+ def __init__ (self , content = None , ** kwargs ):
208
+ super (Comment , self ).__init__ (** kwargs )
207
209
self .content = content
208
210
209
211
class Section (Entry ):
210
- def __init__ (self , name , comment = None , span = None , annotations = None ):
211
- super (Section , self ).__init__ (span , annotations )
212
+ def __init__ (self , name , comment = None , ** kwargs ):
213
+ super (Section , self ).__init__ (** kwargs )
212
214
self .name = name
213
215
self .comment = comment
214
216
215
217
class Function (Identifier ):
216
- def __init__ (self , name ):
217
- super (Function , self ).__init__ (name )
218
+ def __init__ (self , name , ** kwargs ):
219
+ super (Function , self ).__init__ (name , ** kwargs )
218
220
219
221
class Junk (Entry ):
220
- def __init__ (self , content = None , span = None , annotations = None ):
221
- super (Junk , self ).__init__ (span , annotations )
222
+ def __init__ (self , content = None , ** kwargs ):
223
+ super (Junk , self ).__init__ (** kwargs )
222
224
self .content = content
223
225
224
226
225
- class Span (Node ):
226
- def __init__ (self , start , end ):
227
- super (Span , self ).__init__ ()
227
+ class Span (BaseNode ):
228
+ def __init__ (self , start , end , ** kwargs ):
229
+ super (Span , self ).__init__ (** kwargs )
228
230
self .start = start
229
231
self .end = end
230
232
231
233
232
- class Annotation (Node ):
233
- def __init__ (self , code , args = None , message = None ):
234
- super (Annotation , self ).__init__ ()
234
+ class Annotation (SyntaxNode ):
235
+ def __init__ (self , code , args = None , message = None , ** kwargs ):
236
+ super (Annotation , self ).__init__ (** kwargs )
235
237
self .code = code
236
238
self .args = args or []
237
239
self .message = message
238
-
239
- def add_span (self , start , end ):
240
- self .span = Span (start , end )
0 commit comments