template.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 Classes and rules for parsing C++ templates and typedefs for template instantiations.
9 
10 Author: Duy Nguyen Ta, Fan Jiang, Matthew Sklar, Varun Agrawal, and Frank Dellaert
11 """
12 
13 from typing import List
14 
15 from pyparsing import Optional, ParseResults, delimitedList # type: ignore
16 
17 from .tokens import (EQUAL, IDENT, LBRACE, LOPBRACK, RBRACE, ROPBRACK,
18  SEMI_COLON, TEMPLATE, TYPEDEF)
19 from .type import TemplatedType, Typename
20 
21 
22 class Template:
23  """
24  Rule to parse templated values in the interface file.
25 
26  E.g.
27  template<POSE> // this is the Template.
28  class Camera { ... };
29  """
31  """
32  Rule to parse the template parameters.
33 
34  template<typename POSE> // POSE is the Instantiation.
35  """
36  rule = (
37  IDENT("typename") #
38  + Optional( #
39  EQUAL #
40  + LBRACE #
41  + ((delimitedList(TemplatedType.rule ^ Typename.rule)
42  ("instantiations"))) #
43  + RBRACE #
44  )).setParseAction(lambda t: Template.TypenameAndInstantiations(
45  t.typename, t.instantiations))
46 
47  def __init__(self, typename: str, instantiations: ParseResults):
48  self.typename = typename
49 
50  self.instantiations = []
51  if instantiations:
52  for inst in instantiations:
53  x = inst.typename if isinstance(inst,
54  TemplatedType) else inst
55  self.instantiations.append(x)
56 
57  rule = ( # BR
58  TEMPLATE #
59  + LOPBRACK #
60  + delimitedList(TypenameAndInstantiations.rule)(
61  "typename_and_instantiations_list") #
62  + ROPBRACK # BR
63  ).setParseAction(
64  lambda t: Template(t.typename_and_instantiations_list.asList()))
65 
66  def __init__(
67  self,
68  typename_and_instantiations_list: List[TypenameAndInstantiations]):
69  ti_list = typename_and_instantiations_list
70  self.typenames = [ti.typename for ti in ti_list]
71  self.instantiations = [ti.instantiations for ti in ti_list]
72 
73  def __repr__(self) -> str:
74  return "<{0}>".format(", ".join(self.typenames))
75 
76 
78  """
79  Rule for parsing typedefs (with templates) within the interface file.
80 
81  E.g.
82  ```
83  typedef SuperComplexName<Arg1, Arg2, Arg3> EasierName;
84  ```
85  """
86  rule = (TYPEDEF + TemplatedType.rule("templated_type") +
87  IDENT("new_name") +
88  SEMI_COLON).setParseAction(lambda t: TypedefTemplateInstantiation(
89  t.templated_type[0], t.new_name))
90 
91  def __init__(self,
92  templated_type: TemplatedType,
93  new_name: str,
94  parent: str = ''):
95  self.typename = templated_type.typename
96  self.new_name = new_name
97  self.parent = parent
98 
99  def __repr__(self):
100  return "Typedef: {new_name} = {typename}".format(
101  new_name=self.new_name, typename=self.typename)
gtwrap.interface_parser.template.Template
Definition: template.py:22
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.template.TypedefTemplateInstantiation.__init__
def __init__(self, TemplatedType templated_type, str new_name, str parent='')
Definition: template.py:91
gtwrap.interface_parser.template.TypedefTemplateInstantiation.parent
parent
Definition: template.py:94
gtwrap.interface_parser.template.Template.typenames
typenames
Definition: template.py:68
gtwrap.interface_parser.template.Template.TypenameAndInstantiations.typename
typename
Definition: template.py:48
gtwrap.interface_parser.tokens.IDENT
IDENT
Definition: tokens.py:19
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:842
gtwrap.interface_parser.template.TypedefTemplateInstantiation
Definition: template.py:77
gtwrap.interface_parser.template.Template.instantiations
instantiations
Definition: template.py:69
gtwrap.interface_parser.template.Template.__repr__
str __repr__(self)
Definition: template.py:73
gtwrap.interface_parser.template.Template.TypenameAndInstantiations.instantiations
instantiations
Definition: template.py:50
gtwrap.interface_parser.template.TypedefTemplateInstantiation.__repr__
def __repr__(self)
Definition: template.py:99
gtwrap.interface_parser.template.TypedefTemplateInstantiation.typename
typename
Definition: template.py:92
gtwrap.interface_parser.template.TypedefTemplateInstantiation.new_name
new_name
Definition: template.py:93
gtwrap.interface_parser.template.Template.TypenameAndInstantiations
Definition: template.py:30
gtwrap.interface_parser.template.Template.TypenameAndInstantiations.__init__
def __init__(self, str typename, ParseResults instantiations)
Definition: template.py:47
gtwrap.interface_parser.template.Template.__init__
def __init__(self, List[TypenameAndInstantiations] typename_and_instantiations_list)
Definition: template.py:66


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:14:23