PromelaCodeAnalyzer.h
Go to the documentation of this file.
1 
20 #ifndef PROMELACODEANALYZER_H_E89FF519
21 #define PROMELACODEANALYZER_H_E89FF519
22 
23 #include "uscxml/transform/Trie.h"
25 #include "uscxml/plugins/datamodel/promela/parser/promela.tab.hpp"
26 
27 #include <set>
28 
29 namespace uscxml {
30 
31 class ChartToPromela;
32 
33 class USCXML_API PromelaCodeAnalyzer {
34 public:
36  public:
37  PromelaTypedef() {}
38  std::string name;
39  std::string type;
40  size_t arraySize = 0;
41  int minValue = 0;
42  int maxValue = 0;
43  std::map<std::string, PromelaTypedef> types;
44  std::set<ChartToPromela*> occurrences;
45 
46  bool operator==(const PromelaTypedef& other) const {
47  return name == other.name;
48  }
49 
50  };
51 
52  PromelaCodeAnalyzer() : _eventTrie(".") {}
53 
54  void analyze(ChartToPromela* interpreter);
55 
56  void addCode(const std::string& code, ChartToPromela* interpreter);
57  void addLiteral(const std::string& stateName, int forceIndex = -1);
58 
59  bool usesComplexEventStruct() {
60  return _typeDefs.types.find("_event") != _typeDefs.types.end() && _typeDefs.types["_event"].types.size() > 0;
61  }
62  bool usesEventField(const std::string& fieldName) {
63  if (usesComplexEventStruct() && _typeDefs.types["_event"].types.find(fieldName) != _typeDefs.types["_event"].types.end())
64  return true;
65  return false;
66  }
67  bool usesCancel(const std::string& elementName) {
68  return _usesCancel;
69  }
70 
71  bool usesEventDataField(const std::string& fieldName) {
72  if (usesComplexEventStruct() &&
73  _typeDefs.types["_event"].types.find("data") != _typeDefs.types["_event"].types.end() &&
74  _typeDefs.types["_event"].types["data"].types.find(fieldName) != _typeDefs.types["_event"].types["data"].types.end())
75  return true;
76  return false;
77  }
78 
79  size_t largestDelay = 0;
80 
81  std::string getTypeAssignment(const std::string& varTo, const std::string& varFrom, const PromelaTypedef& type, size_t indent = 0);
82  std::string getTypeReset(const std::string& var, const PromelaTypedef& type, size_t indent = 0);
83 
84  bool usesInPredicate() {
85  return _usesInPredicate;
86  }
87  void usesInPredicate(bool value) {
88  _usesInPredicate = value;
89  }
90  bool usesPlatformVars() {
91  return _usesPlatformVars;
92  }
93 
94  bool hasIndexLessLoops() {
95  return _hasIndexLessLoops;
96  }
97 
98  std::string macroForLiteral(const std::string& literal);
99  int indexForLiteral(const std::string& literal);
100 
101  std::set<std::string> getLiterals() {
102  return _literals;
103  }
104  std::set<std::string> getEventsWithPrefix(const std::string& prefix);
105 
106  Trie& getTrie() {
107  return _eventTrie;
108  }
109 
110  std::string adaptCode(const std::string& code, const std::string& prefix);
111 
112  static std::string prefixIdentifiers(const std::string& expr, const std::string& prefix);
113  static std::list<std::pair<size_t, size_t> > getTokenPositions(const std::string& expr, int type, PromelaParserNode* ast);
114 
115  PromelaTypedef& getTypes() {
116  return _typeDefs;
117  }
118 
119  PromelaTypedef& getType(const std::string& typeName) {
120  return _typeDefs.types.at(typeName);
121  }
122 
123  std::string sanitizeCode(const std::string& code);
124  void addEvent(const std::string& eventName);
125  std::string createMacroName(const std::string& literal);
126 
127 protected:
128  void addState(const std::string& stateName, size_t index);
129 
130  int enumerateLiteral(const std::string& literal, int forceIndex = -1);
131 
132  std::map<std::string, std::string> _strMacros; // macronames for string literals
133  std::map<std::string, int> _strIndex; // integer enumeration for string
134  std::set<std::string> _literals;
135 
136  PromelaTypedef _typeDefs;
137  Trie _eventTrie;
138 
139 private:
140  std::set<std::string> _macroNameSet; // helper set for uniqueness of macros
141  int _lastStrIndex = 1;
142  bool _usesCancel = false;
143  bool _usesInPredicate = false;
144  bool _usesPlatformVars = false;
145  bool _hasIndexLessLoops = false;
146 };
147 
148 
149 
150 }
151 
152 #endif /* end of include guard: PROMELACODEANALYZER_H_E89FF519 */
Definition: Breakpoint.cpp:26
Definition: PromelaCodeAnalyzer.h:33
Definition: PromelaCodeAnalyzer.h:35
Definition: ChartToPromela.h:34