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++ functions. 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 Optional, ParseResults, delimitedList
17 from .template
import Template
18 from .tokens
import (COMMA, DEFAULT_ARG, EQUAL, IDENT, LOPBRACK, LPAREN, PAIR,
19 ROPBRACK, RPAREN, SEMI_COLON)
20 from .type
import TemplatedType, Type
25 The type and name of a function/method argument. 29 void sayHello(/*`s` is the method argument with type `const string&`*/ const string& s); 32 rule = ((Type.rule ^ TemplatedType.rule)(
"ctype") +
IDENT(
"name") + \
33 Optional(EQUAL + (DEFAULT_ARG ^ Type.rule ^ TemplatedType.rule) + \
34 Optional(LPAREN + RPAREN)
36 ).setParseAction(
lambda t:
Argument(t.ctype, t.name, t.default))
39 ctype: Union[Type, TemplatedType],
41 default: ParseResults =
None):
51 elif len(default) > 1:
52 default =
tuple(default.asList())
58 self.parent: Union[ArgumentList,
None] =
None 64 """Return full C++ representation of argument.""" 70 List of Argument objects for all arguments in a function. 72 rule = Optional(delimitedList(Argument.rule)(
"args_list")).setParseAction(
73 lambda t: ArgumentList.from_parse_result(t.args_list))
85 """Return the result of parsing.""" 92 return self.args_list.__repr__()
98 """Return a list of the names of all the arguments.""" 99 return [arg.name
for arg
in self.
args_list]
101 def to_cpp(self, use_boost: bool) -> List[str]:
102 """Generate the C++ code for wrapping.""" 103 return [arg.ctype.to_cpp(use_boost)
for arg
in self.
args_list]
108 Rule to parse the return type. 110 The return type can either be a single type or a pair such as <type1, type2>. 121 (Type.rule ^ TemplatedType.rule)(
"type1")).setParseAction(
124 def __init__(self, type1: Union[Type, TemplatedType], type2: Type):
134 Check if the return type is void. 136 return self.type1.typename.name ==
"void" and not self.
type2 139 return "{}{}".format(
140 self.
type1, (
', ' + self.type2.__repr__())
if self.
type2 else '')
142 def to_cpp(self, use_boost: bool) -> str:
144 Generate the C++ code for wrapping. 146 If there are two return types, we return a pair<>, 147 otherwise we return the regular return type. 150 return "std::pair<{type1},{type2}>".format(
151 type1=self.type1.to_cpp(use_boost),
152 type2=self.type2.to_cpp(use_boost))
154 return self.type1.to_cpp(use_boost)
159 Rule to parse functions defined in the global scope. 162 Optional(Template.rule(
"template")) + ReturnType.rule(
"return_type")
165 + ArgumentList.rule(
"args_list")
168 ).setParseAction(
lambda t:
GlobalFunction(t.name, t.return_type, t.
169 args_list, t.template))
173 return_type: ReturnType,
174 args_list: ArgumentList,
183 self.return_type.parent = self
184 self.args.parent = self
191 """Generate the C++ code for wrapping."""
bool isinstance(handle obj)