libqasm
library for handling cQASM files
cqasm-v1-parse-helper.cpp
Go to the documentation of this file.
1 
6 #include "cqasm-v1-parser.hpp"
7 #include "cqasm-v1-lexer.hpp"
8 
9 namespace cqasm {
10 namespace v1 {
11 namespace parser {
12 
16 ParseResult parse_file(const std::string &filename) {
17  return std::move(ParseHelper(filename, "", true).result);
18 }
19 
23 ParseResult parse_file(FILE *file, const std::string &filename) {
24  return std::move(ParseHelper(filename, file).result);
25 }
26 
31 ParseResult parse_string(const std::string &data, const std::string &filename) {
32  return std::move(ParseHelper(filename, data, false).result);
33 }
34 
41 ParseHelper::ParseHelper(
42  const std::string &filename,
43  const std::string &data,
44  bool use_file
45 ) : filename(filename) {
46 
47  // Create the scanner.
48  if (!construct()) return;
49 
50  // Open the file or pass the data buffer to flex.
51  if (use_file) {
52  fptr = fopen(filename.c_str(), "r");
53  if (!fptr) {
54  std::ostringstream sb;
55  sb << "Failed to open input file " << filename << ": "
56  << strerror(errno);
57  push_error(sb.str());
58  return;
59  }
60  cqasm_v1set_in(fptr, (yyscan_t)scanner);
61  } else {
62  buf = cqasm_v1_scan_string(data.c_str(), (yyscan_t)scanner);
63  }
64 
65  // Do the actual parsing.
66  parse();
67 
68 }
69 
74 ParseHelper::ParseHelper(
75  const std::string &filename,
76  FILE *fptr
77 ) : filename(filename) {
78 
79  // Create the scanner.
80  if (!construct()) return;
81 
82  // Open the file or pass the data buffer to flex.
83  cqasm_v1set_in(fptr, (yyscan_t)scanner);
84 
85  // Do the actual parsing.
86  parse();
87 
88 }
89 
93 bool ParseHelper::construct() {
94  int retcode = cqasm_v1lex_init((yyscan_t*)&scanner);
95  if (retcode) {
96  std::ostringstream sb;
97  sb << "Failed to construct scanner: " << strerror(retcode);
98  push_error(sb.str());
99  return false;
100  } else {
101  return true;
102  }
103 }
104 
108 void ParseHelper::parse() {
109  int retcode = cqasm_v1parse((yyscan_t) scanner, *this);
110  if (retcode == 2) {
111  std::ostringstream sb;
112  sb << "Out of memory while parsing " << filename;
113  push_error(sb.str());
114  return;
115  } else if (retcode) {
116  std::ostringstream sb;
117  sb << "Failed to parse " << filename;
118  push_error(sb.str());
119  return;
120  }
121  if (result.errors.empty() && !result.root.is_well_formed()) {
122  std::cerr << *result.root;
123  throw std::runtime_error("internal error: no parse errors returned, but AST is incomplete. AST was dumped.");
124  }
125 }
126 
131  if (fptr) {
132  fclose(fptr);
133  }
134  if (buf) {
136  }
137  if (scanner) {
139  }
140 }
141 
145 void ParseHelper::push_error(const std::string &error) {
146  result.errors.push_back(error);
147 }
148 
149 } // namespace parser
150 } // namespace v1
151 } // namespace cqasm
Parser, generated by Bison.
void * buf
Flex data buffer, if data was specified.
int cqasm_v1lex_destroy(yyscan_t yyscanner)
void push_error(const std::string &error)
Pushes an error.
Toplevel namespace with entry points for the new API.
YY_BUFFER_STATE cqasm_v1_scan_string(yyconst char *yy_str, yyscan_t yyscanner)
Setup the input buffer state to scan a string.
virtual ~ParseHelper()
Destroys the parse helper.
void * yyscan_t
std::vector< std::string > errors
List of accumulated errors.
Internal helper class for parsing cQASM files.
Contains helper classes and objects for the lexer and parser generated by flex/bison, as well as the entry points for invoking the parser directly, in case you don&#39;t need semantic analysis.
void cqasm_v1set_in(FILE *_in_str, yyscan_t yyscanner)
Set the input stream.
Namespace for the "new" cQASM 1.x API.
void cqasm_v1_delete_buffer(YY_BUFFER_STATE b, yyscan_t yyscanner)
Destroy the buffer.
ast::One< ast::Root > root
Root node of the AST, if analysis was sufficiently successful.
std::string filename
Name of the file being parsed.
ParseResult result
The parse result.
void * scanner
Flex reentrant scanner data.
ParseResult parse_string(const std::string &data, const std::string &filename)
Parse the given string.
int cqasm_v1lex_init(yyscan_t *scanner)
ParseResult parse_file(const std::string &filename)
Parse the given file.
int cqasm_v1parse(yyscan_t scanner, cqasm::v1::parser::ParseHelper &helper)