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
16 
17 from .tokens import (EQUAL, IDENT, LBRACE, LOPBRACK, RBRACE, ROPBRACK,
18  SEMI_COLON, TEMPLATE, TYPEDEF)
19 from .type import Typename, TemplatedType
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)
bool isinstance(handle obj)
Definition: pytypes.h:384


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:45:07