12
12
13
13
from typing import Any , Iterable , List , Union
14
14
15
- from pyparsing import Literal , Optional , ZeroOrMore # type: ignore
15
+ from pyparsing import ZeroOrMore # type: ignore
16
+ from pyparsing import Literal , Optional , Word , alphas
16
17
17
18
from .enum import Enum
18
19
from .function import ArgumentList , ReturnType
19
20
from .template import Template
20
- from .tokens import (CLASS , COLON , CONST , IDENT , LBRACE , LPAREN , OPERATOR ,
21
- RBRACE , RPAREN , SEMI_COLON , STATIC , VIRTUAL )
21
+ from .tokens import (CLASS , COLON , CONST , DUNDER , IDENT , LBRACE , LPAREN ,
22
+ OPERATOR , RBRACE , RPAREN , SEMI_COLON , STATIC , VIRTUAL )
22
23
from .type import TemplatedType , Typename
23
24
from .utils import collect_namespaces
24
25
from .variable import Variable
@@ -212,6 +213,26 @@ def __repr__(self) -> str:
212
213
)
213
214
214
215
216
+ class DunderMethod :
217
+ """Special Python double-underscore (dunder) methods, e.g. __iter__, __contains__"""
218
+ rule = (
219
+ DUNDER #
220
+ + (Word (alphas ))("name" ) #
221
+ + DUNDER #
222
+ + LPAREN #
223
+ + ArgumentList .rule ("args_list" ) #
224
+ + RPAREN #
225
+ + SEMI_COLON # BR
226
+ ).setParseAction (lambda t : DunderMethod (t .name , t .args_list ))
227
+
228
+ def __init__ (self , name : str , args : ArgumentList ):
229
+ self .name = name
230
+ self .args = args
231
+
232
+ def __repr__ (self ) -> str :
233
+ return f"DunderMethod: __{ self .name } __({ self .args } )"
234
+
235
+
215
236
class Class :
216
237
"""
217
238
Rule to parse a class defined in the interface file.
@@ -223,23 +244,26 @@ class Hello {
223
244
};
224
245
```
225
246
"""
247
+
226
248
class Members :
227
249
"""
228
250
Rule for all the members within a class.
229
251
"""
230
- rule = ZeroOrMore (Constructor .rule #
252
+ rule = ZeroOrMore (DunderMethod .rule #
253
+ ^ Constructor .rule #
231
254
^ Method .rule #
232
255
^ StaticMethod .rule #
233
256
^ Variable .rule #
234
257
^ Operator .rule #
235
258
^ Enum .rule #
236
259
).setParseAction (lambda t : Class .Members (t .asList ()))
237
260
238
- def __init__ (self ,
239
- members : List [ Union [ Constructor , Method , StaticMethod ,
240
- Variable , Operator ]]):
261
+ def __init__ (self , members : List [ Union [ Constructor , Method ,
262
+ StaticMethod , Variable ,
263
+ Operator , Enum , DunderMethod ]]):
241
264
self .ctors = []
242
265
self .methods = []
266
+ self .dunder_methods = []
243
267
self .static_methods = []
244
268
self .properties = []
245
269
self .operators = []
@@ -251,6 +275,8 @@ def __init__(self,
251
275
self .methods .append (m )
252
276
elif isinstance (m , StaticMethod ):
253
277
self .static_methods .append (m )
278
+ elif isinstance (m , DunderMethod ):
279
+ self .dunder_methods .append (m )
254
280
elif isinstance (m , Variable ):
255
281
self .properties .append (m )
256
282
elif isinstance (m , Operator ):
@@ -271,8 +297,8 @@ def __init__(self,
271
297
+ SEMI_COLON # BR
272
298
).setParseAction (lambda t : Class (
273
299
t .template , t .is_virtual , t .name , t .parent_class , t .members .ctors , t .
274
- members .methods , t .members .static_methods , t .members .properties , t .
275
- members .operators , t .members .enums ))
300
+ members .methods , t .members .static_methods , t .members .dunder_methods , t .
301
+ members .properties , t . members . operators , t .members .enums ))
276
302
277
303
def __init__ (
278
304
self ,
@@ -283,6 +309,7 @@ def __init__(
283
309
ctors : List [Constructor ],
284
310
methods : List [Method ],
285
311
static_methods : List [StaticMethod ],
312
+ dunder_methods : List [DunderMethod ],
286
313
properties : List [Variable ],
287
314
operators : List [Operator ],
288
315
enums : List [Enum ],
@@ -308,6 +335,7 @@ def __init__(
308
335
self .ctors = ctors
309
336
self .methods = methods
310
337
self .static_methods = static_methods
338
+ self .dunder_methods = dunder_methods
311
339
self .properties = properties
312
340
self .operators = operators
313
341
self .enums = enums
@@ -326,6 +354,8 @@ def __init__(
326
354
method .parent = self
327
355
for static_method in self .static_methods :
328
356
static_method .parent = self
357
+ for dunder_method in self .dunder_methods :
358
+ dunder_method .parent = self
329
359
for _property in self .properties :
330
360
_property .parent = self
331
361
0 commit comments