libqasm
library for handling cQASM files
cqasm-v1-analyzer.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <cstdio>
13 #include <functional>
14 #include "cqasm-v1-ast.hpp"
15 #include "cqasm-v1-semantic.hpp"
16 #include "cqasm-v1-resolver.hpp"
18 
19 namespace cqasm {
20 namespace v1 {
21 
26 namespace analyzer {
27 
32 class AnalysisFailed: public std::runtime_error {
33 public:
34  AnalysisFailed() : std::runtime_error("cQASM analysis failed") {};
35 };
36 
56 public:
57 
66 
71  std::vector<std::string> errors;
72 
79  ast::One<semantic::Program> unwrap(std::ostream &out = std::cerr) const;
80 
81 };
82 
101 class Analyzer {
102 private:
103  friend class AnalyzerHelper;
104 
108  primitives::Version api_version;
109 
114  resolver::MappingTable mappings;
115 
125  resolver::FunctionTable functions;
126 
135  resolver::InstructionTable instruction_set;
136 
143  bool resolve_instructions;
144 
152  resolver::ErrorModelTable error_models;
153 
160  bool resolve_error_model;
161 
162 public:
163 
167  Analyzer(const std::string &api_version = "1.0");
168 
172  Analyzer(const primitives::Version &api_version);
173 
177  void register_mapping(const std::string &name, const values::Value &value);
178 
190  void register_function(
191  const std::string &name,
192  const types::Types &param_types,
193  const resolver::FunctionImpl &impl
194  );
195 
201  void register_function(
202  const std::string &name,
203  const std::string &param_types,
204  const resolver::FunctionImpl &impl
205  );
206 
212  void register_default_functions_and_mappings();
213 
220  void register_instruction(const instruction::Instruction &instruction);
221 
226  void register_instruction(
227  const std::string &name,
228  const std::string &param_types = "",
229  bool allow_conditional = true,
230  bool allow_parallel = true,
231  bool allow_reused_qubits = false,
232  bool allow_different_index_sizes = false
233  );
234 
240  template <typename T>
242  T &&annotation,
243  const std::string &name,
244  const std::string &param_types = "",
245  bool allow_conditional = true,
246  bool allow_parallel = true,
247  bool allow_reused_qubits = false,
248  bool allow_different_index_sizes = false
249  ) {
251  name,
252  param_types,
253  allow_conditional,
254  allow_parallel,
255  allow_reused_qubits,
256  allow_different_index_sizes
257  };
258  insn.set_annotation<T>(std::forward<T>(annotation));
259  register_instruction(insn);
260  }
261 
268  void register_error_model(const error_model::ErrorModel &error_model);
269 
274  void register_error_model(
275  const std::string &name,
276  const std::string &param_types = ""
277  );
278 
284  template <typename T>
286  T &&annotation,
287  const std::string &name,
288  const std::string &param_types = ""
289  ) {
291  name,
292  param_types
293  };
294  model.set_annotation<T>(std::forward<T>(annotation));
295  register_error_model(model);
296  }
297 
301  AnalysisResult analyze(const ast::Program &program) const;
302 
308  AnalysisResult analyze(const parser::ParseResult &parse_result) const;
309 
314  const std::function<version::Version()> &version_parser,
315  const std::function<parser::ParseResult()> &file_parser
316  ) const;
317 
321  AnalysisResult analyze(const std::string &filename) const;
322 
327  AnalysisResult analyze(FILE *file, const std::string &filename = "<unknown>") const;
328 
333  AnalysisResult analyze_string(const std::string &data, const std::string &filename = "<unknown>") const;
334 
335 };
336 
337 } // namespace analyzer
338 } // namespace v1
339 } // namespace cqasm
Main class used for analyzing cQASM files.
tree::One< semantic::Program > analyze_string(const std::string &data, const std::string &filename, const std::string &api_version)
Parses and analyzes the given string with the default analyzer, dumping error messages to stderr and ...
Definition: cqasm-v1.cpp:43
Defines the types for the cQASM abstract syntax tree, based on the classes from cqasm::tree.
Exception thrown by AnalysisResult::unwrap() when the cQASM file fails to parse.
std::vector< std::string > errors
List of accumulated errors.
Toplevel namespace with entry points for the new API.
tree::One< semantic::Program > analyze(const std::string &filename, const std::string &api_version)
Parses and analyzes the given file with the default analyzer, dumping error messages to stderr and th...
Definition: cqasm-v1.cpp:18
void register_error_model_with_annotation(T &&annotation, const std::string &name, const std::string &param_types="")
Convenience method for registering an error model with a single user-specified annotation.
STL namespace.
Table of the supported instructions and their overloads.
Defines the types for the cQASM semantic tree, based on the classes from cqasm::tree.
Version number primitive used within the AST and semantic trees.
ast::One< semantic::Program > root
Root node of the semantic tree, if analysis was successful.
Representation of an error model.
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.
Representation of an available instruction (also known as gate) in the instruction set...
cqasm::tree::One< T > One
Table of all overloads of all constant propagation functions.
Table of the supported instructions and their overloads.
Namespace for the "new" cQASM 1.x API.
tree::One< Node > Value
A cQASM value, either known at compile-time or an expression for something only known at runtime...
Table of all mappings within a certain scope.
tree::Any< TypeBase > Types
Zero or more cQASM types.
Contains MappingTable, FunctionTable, and ErrorModelTable, representing the various cQASM namespaces ...
A complete program.
Helper class for analyzing a single AST.
void register_instruction_with_annotation(T &&annotation, const std::string &name, const std::string &param_types="", bool allow_conditional=true, bool allow_parallel=true, bool allow_reused_qubits=false, bool allow_different_index_sizes=false)
Convenience method for registering an instruction type with a single user-specified annotation...
std::function< values::Value(const values::Values &)> FunctionImpl
C++ function representing (one of the overloads of) a function usable in cQASM constant expressions...