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 Any, 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")
34 + Optional(EQUAL + DEFAULT_ARG)(
"default")
38 t.default[0]
if isinstance(t.default, ParseResults)
else None))
41 ctype: Union[Type, TemplatedType],
43 default: ParseResults =
None):
50 self.parent: Union[ArgumentList,
None] =
None 56 """Return full C++ representation of argument.""" 62 List of Argument objects for all arguments in a function. 64 rule = Optional(delimitedList(Argument.rule)(
"args_list")).setParseAction(
65 lambda t: ArgumentList.from_parse_result(t.args_list))
73 self.parent: Any =
None 77 """Return the result of parsing.""" 90 """Return a list of the names of all the arguments.""" 91 return [arg.name
for arg
in self.
args_list]
93 def list(self) -> List[Argument]:
94 """Return a list of the names of all the arguments.""" 98 """Generate the C++ code for wrapping.""" 99 return [arg.ctype.to_cpp()
for arg
in self.
args_list]
104 Rule to parse the return type. 106 The return type can either be a single type or a pair such as <type1, type2>. 117 (Type.rule ^ TemplatedType.rule)(
"type1")).setParseAction(
120 def __init__(self, type1: Union[Type, TemplatedType], type2: Type):
126 self.parent: Any =
None 130 Check if the return type is void. 132 return self.
type1.typename.name ==
"void" and not self.
type2 140 Generate the C++ code for wrapping. 142 If there are two return types, we return a pair<>, 143 otherwise we return the regular return type. 146 return "std::pair<{type1},{type2}>".
format(
154 Rule to parse functions defined in the global scope. 157 Optional(Template.rule(
"template")) + ReturnType.rule(
"return_type")
160 + ArgumentList.rule(
"args_list")
163 ).setParseAction(
lambda t:
GlobalFunction(t.name, t.return_type, t.
164 args_list, t.template))
168 return_type: ReturnType,
169 args_list: ArgumentList,
179 self.
args.parent = self
186 """Generate the C++ code for wrapping."""
bool isinstance(handle 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.