2 GTSAM Copyright 2010-2020, Georgia Tech Research Corporation, 3 Atlanta, Georgia 30332-0415 6 See LICENSE for the license information 8 Parser classes and rules for parsing C++ classes. 10 Author: Duy Nguyen Ta, Fan Jiang, Matthew Sklar, Varun Agrawal, and Frank Dellaert 13 from typing
import Iterable, List, Union
15 from pyparsing
import Literal, Optional, ZeroOrMore
17 from .enum
import Enum
18 from .function
import ArgumentList, ReturnType
19 from .template
import Template
20 from .tokens
import (CLASS, COLON, CONST, IDENT, LBRACE, LPAREN, OPERATOR,
21 RBRACE, RPAREN, SEMI_COLON, STATIC, VIRTUAL)
22 from .type
import TemplatedType, Typename
23 from .utils
import collect_namespaces
24 from .variable
import Variable
29 Rule to parse a method in a class. 34 void sayHello() const; 39 Optional(Template.rule(
"template"))
40 + ReturnType.rule(
"return_type")
43 + ArgumentList.rule(
"args_list")
45 + Optional(
CONST(
"is_const"))
47 ).setParseAction(
lambda t:
Method(t.template, t.name, t.return_type, t.
48 args_list, t.is_const))
53 return_type: ReturnType,
56 parent: Union[str,
"Class"] =
''):
66 return "Method: {} {} {}({}){}".format(
77 Rule to parse all the static methods in a class. 82 static void changeGreeting(); 88 + ReturnType.rule(
"return_type")
91 + ArgumentList.rule(
"args_list")
95 lambda t:
StaticMethod(t.name, t.return_type, t.args_list))
99 return_type: ReturnType,
101 parent: Union[str,
"Class"] =
''):
112 """Generate the C++ code for wrapping.""" 118 Rule to parse the class constructor. 119 Can have 0 or more arguments. 124 + ArgumentList.rule(
"args_list")
127 ).setParseAction(
lambda t:
Constructor(t.name, t.args_list))
132 parent: Union[
"Class", str] =
''):
139 return "Constructor: {}".format(self.
name)
144 Rule for parsing operator overloads. 149 Vector2 operator+(const Vector2 &v) const; 153 ReturnType.rule(
"return_type")
154 + Literal(
"operator")(
"name")
157 + ArgumentList.rule(
"args_list")
161 ).setParseAction(
lambda t:
Operator(t.name, t.operator, t.return_type, t.
162 args_list, t.is_const))
167 return_type: ReturnType,
170 parent: Union[str,
"Class"] =
''):
182 raise ValueError(
"Invalid unary operator {} used for {}".format(
186 assert 0 <=
len(args) < 2, \
187 "Operator overload should be at most 1 argument, " \
188 "{} arguments provided".format(
len(args))
191 if len(args) == 1
and self.
operator not in (
"()",
"[]"):
192 assert args.args_list[0].ctype.typename.name == return_type.type1.typename.name, \
193 "Mixed type overloading not supported. Both arg and return type must be the same." 196 return "Operator: {}{}{}({}) {}".format(
207 Rule to parse a class defined in the interface file. 218 Rule for all the members within a class. 220 rule = ZeroOrMore(Constructor.rule
229 members: List[Union[Constructor, Method, StaticMethod,
230 Variable, Operator]]):
241 self.methods.append(m)
243 self.static_methods.append(m)
245 self.properties.append(m)
247 self.operators.append(m)
251 _parent = COLON + (TemplatedType.rule ^ Typename.rule)(
"parent_class")
253 Optional(Template.rule(
"template"))
254 + Optional(
VIRTUAL(
"is_virtual"))
259 + Members.rule(
"members")
262 ).setParseAction(
lambda t:
Class(
269 t.members.static_methods,
270 t.members.properties,
281 ctors: List[Constructor],
282 methods: List[Method],
283 static_methods: List[StaticMethod],
284 properties: List[Variable],
285 operators: List[Operator],
295 parent_class = parent_class[0]
300 parent_class = parent_class.typename
316 for ctor
in self.
ctors:
317 if ctor.name != self.
name:
318 raise ValueError(
"Error in constructor name! {} != {}".format(
319 ctor.name, self.
name))
321 for ctor
in self.
ctors:
326 static_method.parent = self
328 _property.parent = self
331 """Get the namespaces which this class is nested under as a list.""" 335 return "Class: {self.name}".format(self=self)
bool isinstance(handle obj)
def collect_namespaces(obj)