VCDFileParser.cpp
Go to the documentation of this file.
1 /*!
2 @file
3 @brief Definition of the VCDFileParser class
4 */
5 
6 #include "VCDFileParser.hpp"
7 
8 
10 
11 
12  this -> start_time = -std::numeric_limits<decltype(start_time)>::max();
13  this -> end_time = std::numeric_limits<decltype(end_time)>::max() ;
14 
15  this->trace_scanning = false;
16  this->trace_parsing = false;
17 }
18 
20 {
21 }
22 
24 {
25 
26  this->filepath = filepath;
27 
28  scan_begin();
29 
30  this->fh = new VCDFile();
31  VCDFile *tr = this->fh;
32 
33  this->fh->root_scope = new VCDScope;
34  this->fh->root_scope->name = std::string("$root");
35  this->fh->root_scope->type = VCD_SCOPE_ROOT;
36 
37  this->scopes.push(this->fh->root_scope);
38 
39  this -> fh -> root_scope = new VCDScope;
40  this -> fh -> root_scope -> name = std::string("");
41  this -> fh -> root_scope -> type = VCD_SCOPE_ROOT;
42 
43  this -> scopes.push(this -> fh -> root_scope);
44 
45  tr -> add_scope(scopes.top());
46 
47  VCDParser::parser parser(*this);
48 
49  parser.set_debug_level(trace_parsing);
50 
51  int result = parser.parse();
52 
53  scopes.pop();
54 
55  scan_end();
56 
57  if (result == 0)
58  {
59  this->fh = nullptr;
60  return tr;
61  }
62  else
63  {
64  tr = nullptr;
65  delete this->fh;
66  return nullptr;
67  }
68 }
69 
70 void VCDFileParser::error(const VCDParser::location &l, const std::string &m)
71 {
72  std::cerr << "line " << l.begin.line
73  << std::endl;
74  std::cerr << " : " << m << std::endl;
75 }
76 
77 void VCDFileParser::error(const std::string & m){
78  std::cerr << " : "<<m<<std::endl;
79 }
80 
81 #ifdef VCD_PARSER_STANDALONE
82 
83 void print_scope_signals(VCDScope * scope)
84 {
85  for(VCDSignal * signal : scope -> signals) {
86 
87  std::cout << "\t" << signal -> hash << "\t"
88  << signal -> reference;
89 
90  if(signal -> size > 1) {
91  std::cout << "[" << signal -> lindex << ":" << signal -> rindex << "]";
92  } else if (signal -> lindex >= 0) {
93  std::cout << "[" << signal -> lindex << "]";
94  }
95 
96  std::cout << std::endl;
97 
98  }
99 }
100 
101 void traverse_scope(std::string parent, VCDScope * scope)
102 {
103  std::string local_parent = parent;
104 
105  if (parent.length())
106  local_parent += ".";
107  local_parent += scope->name;
108  std::cout << "Scope: " << local_parent << std::endl;
109  print_scope_signals(scope);
110  for (auto child : scope->children) {
111  std::cout << "Child:\n";
112  traverse_scope(local_parent, child);
113  }
114 }
115 /*!
116 @brief Standalone test function to allow testing of the VCD file parser.
117 */
118 int main (int argc, char** argv){
119 
120  std::string infile (argv[1]);
121 
122  std::cout << "Parsing " << infile << std::endl;
123 
124  VCDFileParser parser;
125 
126  VCDFile * trace = parser.parse_file(infile);
127 
128  if(trace) {
129  std::cout << "Parse successful." << std::endl;
130  std::cout << "Version: " << trace -> version << std::endl;
131  std::cout << "Date: " << trace -> date << std::endl;
132  std::cout << "Signal count: " << trace -> get_signals() -> size() <<std::endl;
133  std::cout << "Times Recorded:" << trace -> get_timestamps() -> size() << std::endl;
134 
135  // Print out every signal in every scope.
136  for(VCDScope * scope : *trace -> get_scopes()) {
137  if (scope->parent)
138  continue;
139  traverse_scope(std::string(""), scope);
140  }
141 
142  delete trace;
143 
144  return 0;
145  } else {
146  std::cout << "Parse Failed." << std::endl;
147  return 1;
148  }
149 
150 }
151 
152 #endif
void scan_end()
Utility function for stopping parsing.
VCDTime end_time
Ignore anything after this timepoint.
virtual ~VCDFileParser()
struct vcdscope VCDScope
Definition: VCDTypes.hpp:118
Class for parsing files containing CSP notation.
VCDScope * parent
Parent scope object.
Definition: VCDTypes.hpp:136
bool trace_scanning
Should we debug tokenising?
VCDScope * root_scope
Root scope node of the VCD signals.
Definition: VCDFile.hpp:42
void error(const VCDParser::location &l, const std::string &m)
Reports errors to stderr.
std::vector< VCDScope * > children
Child scope objects.
Definition: VCDTypes.hpp:137
Represents a single signal reference within a VCD file.
Definition: VCDTypes.hpp:121
VCDScopeName name
The short name of the scope.
Definition: VCDTypes.hpp:134
VCDFile * fh
Current file being parsed and constructed.
VCDTime start_time
Ignore anything before this timepoint.
Represents a scope type, scope name pair and all of it&#39;s child signals.
Definition: VCDTypes.hpp:133
Contains the declaration of the parser driver class.
int main(int argc, char **argv)
Standalone test function to allow testing of the VCD file parser.
bool trace_parsing
Should we debug parsing of tokens?
VCDScopeType type
Construct type.
Definition: VCDTypes.hpp:135
std::stack< VCDScope * > scopes
Current stack of scopes being parsed.
Top level object to represent a single VCD file.
Definition: VCDFile.hpp:16
VCDFileParser()
Create a new parser/.
VCDFile * parse_file(const std::string &filepath)
Parse the suppled file.
void scan_begin()
Utility function for starting parsing.
std::string filepath
The current file being parsed.