interface_parser/function.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2020, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 Parser classes and rules for parsing C++ functions.
9 
10 Author: Duy Nguyen Ta, Fan Jiang, Matthew Sklar, Varun Agrawal, and Frank Dellaert
11 """
12 
13 from typing import Any, Iterable, List, Union
14 
15 from pyparsing import (Literal, Optional, ParseResults, # type: ignore
16  delimitedList)
17 
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
22 
23 
24 class Argument:
25  """
26  The type and name of a function/method argument.
27 
28  E.g.
29  ```
30  void sayHello(/*`s` is the method argument with type `const string&`*/ const string& s);
31  ```
32  """
33  rule = ((Type.rule ^ TemplatedType.rule)("ctype") #
34  + IDENT("name") #
35  + Optional(EQUAL + DEFAULT_ARG)("default")
36  ).setParseAction(lambda t: Argument(
37  t.ctype, #
38  t.name, #
39  t.default[0] if isinstance(t.default, ParseResults) else None))
40 
41  def __init__(self,
42  ctype: Union[Type, TemplatedType],
43  name: str,
44  default: ParseResults = None):
45  if isinstance(ctype, Iterable):
46  self.ctype = ctype[0] # type: ignore
47  else:
48  self.ctype = ctype
49  self.name = name
50  self.default = default
51  self.parent: Union[ArgumentList, None] = None
52 
53  def __repr__(self) -> str:
54  return self.to_cpp()
55 
56  def to_cpp(self) -> str:
57  """Return full C++ representation of argument."""
58  return '{} {}'.format(repr(self.ctype), self.name)
59 
60 
62  """
63  List of Argument objects for all arguments in a function.
64  """
65  rule = Optional(delimitedList(Argument.rule)("args_list")).setParseAction(
66  lambda t: ArgumentList.from_parse_result(t.args_list))
67 
68  def __init__(self, args_list: List[Argument]):
69  self.args_list = args_list
70  for arg in args_list:
71  arg.parent = self
72  # The parent object which contains the argument list
73  # E.g. Method, StaticMethod, Template, Constructor, GlobalFunction
74  self.parent: Any = None
75 
76  @staticmethod
77  def from_parse_result(parse_result: ParseResults):
78  """Return the result of parsing."""
79  if parse_result:
80  return ArgumentList(parse_result.asList())
81  else:
82  return ArgumentList([])
83 
84  def __repr__(self) -> str:
85  return ",".join([repr(x) for x in self.args_list])
86 
87  def __len__(self) -> int:
88  return len(self.args_list)
89 
90  def names(self) -> List[str]:
91  """Return a list of the names of all the arguments."""
92  return [arg.name for arg in self.args_list]
93 
94  def list(self) -> List[Argument]:
95  """Return a list of the names of all the arguments."""
96  return self.args_list
97 
98  def to_cpp(self) -> List[str]:
99  """Generate the C++ code for wrapping."""
100  return [arg.ctype.to_cpp() for arg in self.args_list]
101 
102 
104  """
105  Rule to parse the return type.
106 
107  The return type can either be a single type or a pair such as <type1, type2>.
108  """
109  # rule to parse optional std:: in front of `pair`
110  optional_std = Optional(Literal('std::')).suppress()
111  _pair = (
112  optional_std + PAIR.suppress() #
113  + LOPBRACK #
114  + Type.rule("type1") #
115  + COMMA #
116  + Type.rule("type2") #
117  + ROPBRACK #
118  )
119  rule = (_pair ^
120  (Type.rule ^ TemplatedType.rule)("type1")).setParseAction( # BR
121  lambda t: ReturnType(t.type1, t.type2))
122 
123  def __init__(self, type1: Union[Type, TemplatedType], type2: Type):
124  # If a TemplatedType, the return is a ParseResults, so we extract out the type.
125  self.type1 = type1[0] if isinstance(type1, ParseResults) else type1
126  self.type2 = type2
127  # The parent object which contains the return type
128  # E.g. Method, StaticMethod, Template, Constructor, GlobalFunction
129  self.parent: Any = None
130 
131  def is_void(self) -> bool:
132  """
133  Check if the return type is void.
134  """
135  return self.type1.typename.name == "void" and not self.type2
136 
137  def __repr__(self) -> str:
138  return "{}{}".format(
139  self.type1, (', ' + self.type2.__repr__()) if self.type2 else '')
140 
141  def to_cpp(self) -> str:
142  """
143  Generate the C++ code for wrapping.
144 
145  If there are two return types, we return a pair<>,
146  otherwise we return the regular return type.
147  """
148  if self.type2:
149  return "std::pair<{type1},{type2}>".format(
150  type1=self.type1.to_cpp(), type2=self.type2.to_cpp())
151  else:
152  return self.type1.to_cpp()
153 
154 
156  """
157  Rule to parse functions defined in the global scope.
158  """
159  rule = (
160  Optional(Template.rule("template")) + ReturnType.rule("return_type") #
161  + IDENT("name") #
162  + LPAREN #
163  + ArgumentList.rule("args_list") #
164  + RPAREN #
165  + SEMI_COLON #
166  ).setParseAction(lambda t: GlobalFunction(t.name, t.return_type, t.
167  args_list, t.template))
168 
169  def __init__(self,
170  name: str,
171  return_type: ReturnType,
172  args_list: ArgumentList,
173  template: Template,
174  parent: Any = ''):
175  self.name = name
176  self.return_type = return_type
177  self.args = args_list
178  self.template = template
179 
180  self.parent = parent
181  self.return_type.parent = self
182  self.args.parent = self
183 
184  def __repr__(self) -> str:
185  return "GlobalFunction: {}{}({})".format(self.return_type, self.name,
186  self.args)
187 
188  def to_cpp(self) -> str:
189  """Generate the C++ code for wrapping."""
190  return self.name
gtwrap.interface_parser.function.ReturnType.type1
type1
Definition: interface_parser/function.py:125
gtwrap.interface_parser.function.ArgumentList.names
List[str] names(self)
Definition: interface_parser/function.py:90
gtwrap.interface_parser.function.GlobalFunction.return_type
return_type
Definition: interface_parser/function.py:171
gtwrap.interface_parser.function.ReturnType.is_void
bool is_void(self)
Definition: interface_parser/function.py:131
gtwrap.interface_parser.function.to_cpp
str to_cpp(self)
Definition: interface_parser/function.py:56
format
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:226
gtwrap.interface_parser.function.ReturnType.__repr__
str __repr__(self)
Definition: interface_parser/function.py:137
gtwrap.interface_parser.function.GlobalFunction.to_cpp
str to_cpp(self)
Definition: interface_parser/function.py:188
gtwrap.interface_parser.function.ArgumentList.__init__
def __init__(self, List[Argument] args_list)
Definition: interface_parser/function.py:68
gtwrap.interface_parser.function.GlobalFunction.args
args
Definition: interface_parser/function.py:172
gtsam.examples.CombinedImuFactorExample.suppress
suppress
Definition: CombinedImuFactorExample.py:31
gtwrap.interface_parser.tokens.IDENT
IDENT
Definition: tokens.py:19
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:825
gtwrap.interface_parser.function.__repr__
str __repr__(self)
Definition: interface_parser/function.py:53
gtwrap.interface_parser.function.ArgumentList.__repr__
str __repr__(self)
Definition: interface_parser/function.py:84
gtwrap.interface_parser.function.GlobalFunction.__repr__
str __repr__(self)
Definition: interface_parser/function.py:184
gtwrap.interface_parser.function.GlobalFunction.template
template
Definition: interface_parser/function.py:173
gtwrap.interface_parser.function.ArgumentList.args_list
args_list
Definition: interface_parser/function.py:69
gtwrap.interface_parser.function.__init__
def __init__(self, Union[Type, TemplatedType] ctype, str name, ParseResults default=None)
Definition: interface_parser/function.py:41
gtwrap.interface_parser.function.GlobalFunction
Definition: interface_parser/function.py:155
gtwrap.interface_parser.function.ArgumentList
Definition: interface_parser/function.py:61
gtwrap.interface_parser.function.ReturnType
Definition: interface_parser/function.py:103
gtwrap.interface_parser.function.ArgumentList.to_cpp
List[str] to_cpp(self)
Definition: interface_parser/function.py:98
gtwrap.interface_parser.function.GlobalFunction.__init__
def __init__(self, str name, ReturnType return_type, ArgumentList args_list, Template template, Any parent='')
Definition: interface_parser/function.py:169
gtwrap.interface_parser.function.ReturnType.to_cpp
str to_cpp(self)
Definition: interface_parser/function.py:141
gtwrap.interface_parser.function.ArgumentList.__len__
int __len__(self)
Definition: interface_parser/function.py:87
gtwrap.interface_parser.function.ReturnType.__init__
def __init__(self, Union[Type, TemplatedType] type1, Type type2)
Definition: interface_parser/function.py:123
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2399
gtwrap.interface_parser.function.ArgumentList.list
List[Argument] list(self)
Definition: interface_parser/function.py:94
gtwrap.interface_parser.function.ArgumentList.from_parse_result
def from_parse_result(ParseResults parse_result)
Definition: interface_parser/function.py:77
gtwrap.interface_parser.function.ReturnType.type2
type2
Definition: interface_parser/function.py:126
gtwrap.interface_parser.function.GlobalFunction.parent
parent
Definition: interface_parser/function.py:175
gtwrap.interface_parser.function.GlobalFunction.name
name
Definition: interface_parser/function.py:170
repr
str repr(handle h)
Definition: pytypes.h:2420


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:02:23