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 (Literal, Optional, ParseResults,
18 from .template
import Template
19 from .tokens
import (COMMA, DEFAULT_ARG, EQUAL, IDENT, LOPBRACK, LPAREN, PAIR,
20 ROPBRACK, RPAREN, SEMI_COLON)
21 from .type
import TemplatedType, Type
26 The type and name of a function/method argument.
30 void sayHello(/*`s` is the method argument with type `const string&`*/ const string& s);
33 rule = ((Type.rule ^ TemplatedType.rule)(
"ctype")
35 + Optional(EQUAL + DEFAULT_ARG)(
"default")
36 ).setParseAction(
lambda t: Argument(
39 t.default[0]
if isinstance(t.default, ParseResults)
else None))
42 ctype: Union[Type, TemplatedType],
44 default: ParseResults =
None):
50 self.default = default
51 self.parent: Union[ArgumentList,
None] =
None
57 """Return full C++ representation of argument."""
58 return '{} {}'.
format(
repr(self.ctype), self.name)
63 List of Argument objects for all arguments in a function.
65 rule = Optional(delimitedList(Argument.rule)(
"args_list")).setParseAction(
66 lambda t: ArgumentList.from_parse_result(t.args_list))
74 self.parent: Any =
None
78 """Return the result of parsing."""
91 """Return a list of the names of all the arguments."""
92 return [arg.name
for arg
in self.
args_list]
94 def list(self) -> List[Argument]:
95 """Return a list of the names of all the arguments."""
99 """Generate the C++ code for wrapping."""
100 return [arg.ctype.to_cpp()
for arg
in self.
args_list]
105 Rule to parse the return type.
107 The return type can either be a single type or a pair such as <type1, type2>.
110 optional_std = Optional(Literal(
'std::')).
suppress()
112 optional_std + PAIR.suppress()
120 (Type.rule ^ TemplatedType.rule)(
"type1")).setParseAction(
123 def __init__(self, type1: Union[Type, TemplatedType], type2: Type):
129 self.parent: Any =
None
133 Check if the return type is void.
135 return self.
type1.typename.name ==
"void" and not self.
type2
143 Generate the C++ code for wrapping.
145 If there are two return types, we return a pair<>,
146 otherwise we return the regular return type.
149 return "std::pair<{type1},{type2}>".
format(
157 Rule to parse functions defined in the global scope.
160 Optional(Template.rule(
"template")) + ReturnType.rule(
"return_type")
163 + ArgumentList.rule(
"args_list")
166 ).setParseAction(
lambda t:
GlobalFunction(t.name, t.return_type, t.
167 args_list, t.template))
171 return_type: ReturnType,
172 args_list: ArgumentList,
177 self.
args = args_list
182 self.
args.parent = self
189 """Generate the C++ code for wrapping."""