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 Any, 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))
51 template: Union[Template, Any],
53 return_type: ReturnType,
56 parent: Union[
"Class", Any] =
''):
66 """Generate the C++ code for wrapping.""" 70 return "Method: {} {} {}({}){}".
format(
81 Rule to parse all the static methods in a class. 86 static void changeGreeting(); 91 Optional(Template.rule(
"template"))
93 + ReturnType.rule(
"return_type")
96 + ArgumentList.rule(
"args_list")
100 lambda t:
StaticMethod(t.name, t.return_type, t.args_list, t.template))
104 return_type: ReturnType,
106 template: Union[Template, Any] =
None,
107 parent: Union[
"Class", Any] =
''):
119 """Generate the C++ code for wrapping.""" 125 Rule to parse the class constructor. 126 Can have 0 or more arguments. 129 Optional(Template.rule(
"template"))
132 + ArgumentList.rule(
"args_list")
135 ).setParseAction(
lambda t:
Constructor(t.name, t.args_list, t.template))
140 template: Union[Template, Any],
141 parent: Union[
"Class", Any] =
''):
154 Rule for parsing operator overloads. 159 Vector2 operator+(const Vector2 &v) const; 163 ReturnType.rule(
"return_type")
164 + Literal(
"operator")(
"name")
167 + ArgumentList.rule(
"args_list")
171 ).setParseAction(
lambda t:
Operator(t.name, t.operator, t.return_type, t.
172 args_list, t.is_const))
177 return_type: ReturnType,
180 parent: Union[
"Class", Any] =
''):
192 raise ValueError(
"Invalid unary operator {} used for {}".
format(
196 assert 0 <=
len(args) < 2, \
197 "Operator overload should be at most 1 argument, " \
198 "{} arguments provided".
format(
len(args))
201 if len(args) == 1
and self.
operator not in (
"()",
"[]"):
202 assert args.list()[0].ctype.typename.name == return_type.type1.typename.name, \
203 "Mixed type overloading not supported. Both arg and return type must be the same." 206 return "Operator: {}{}{}({}) {}".
format(
217 Rule to parse a class defined in the interface file. 228 Rule for all the members within a class. 230 rule = ZeroOrMore(Constructor.rule
239 members: List[Union[Constructor, Method, StaticMethod,
240 Variable, Operator]]):
246 self.enums: List[Enum] = []
261 _parent = COLON + (TemplatedType.rule ^ Typename.rule)(
"parent_class")
263 Optional(Template.rule(
"template"))
264 + Optional(
VIRTUAL(
"is_virtual"))
269 + Members.rule(
"members")
272 ).setParseAction(
lambda t:
Class(
273 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))
279 template: Union[Template,
None],
283 ctors: List[Constructor],
284 methods: List[Method],
285 static_methods: List[StaticMethod],
286 properties: List[Variable],
287 operators: List[Operator],
297 parent_class = parent_class[0]
318 for ctor
in self.
ctors:
319 if ctor.name != self.
name:
320 raise ValueError(
"Error in constructor name! {} != {}".
format(
321 ctor.name, self.
name))
323 for ctor
in self.
ctors:
328 static_method.parent = self
330 _property.parent = self
333 """Get the namespaces which this class is nested under as a list.""" 337 return "Class: {self.name}".
format(self=self)
bool isinstance(handle obj)
def collect_namespaces(obj)
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
size_t len(handle h)
Get the length of a Python object.