17 if (version.empty()) {
20 size_t next, last = 0;
21 while ((next = version.find(
'.', last)) != std::string::npos) {
22 push_back(std::stoi(version.substr(last, next - last)));
25 push_back(std::stoi(version.substr(last)));
26 for (
auto component : *
this) {
28 throw std::invalid_argument(
"version component below zero");
41 for (
size_t i = 0; i < this->size() || i < other.size(); i++) {
42 auto lhs = i < this->size() ? (*this)[i] : 0;
43 auto rhs = i < other.size() ? other[i] : 0;
44 if (lhs > rhs)
return 1;
45 if (lhs < rhs)
return -1;
66 for (
auto item :
object) {
82 return std::move(
ParseHelper(filename,
"",
true).version);
91 auto retval = std::move(
ParseHelper(filename, file).version);
92 if (fseek(file, 0, SEEK_SET)) {
104 return std::move(
ParseHelper(filename, data,
false).version);
113 ParseHelper::ParseHelper(
114 const std::string &filename,
115 const std::string &data,
117 ) : filename(filename), version(
"") {
120 if (!construct())
return;
124 fptr = fopen(filename.c_str(),
"r");
126 std::ostringstream sb;
127 sb <<
"Failed to open input file " << filename <<
": " 129 push_error(sb.str());
146 ParseHelper::ParseHelper(
147 const std::string &filename,
149 ) : filename(filename), version(
"") {
152 if (!construct())
return;
165 bool ParseHelper::construct() {
168 std::ostringstream sb;
169 sb <<
"Failed to construct scanner: " << strerror(retcode);
170 push_error(sb.str());
180 void ParseHelper::parse() {
183 std::ostringstream sb;
184 sb <<
"Out of memory while parsing " << filename;
185 push_error(sb.str());
187 }
else if (retcode) {
188 std::ostringstream sb;
189 sb <<
"Failed to parse " << filename;
190 push_error(sb.str());
193 if (version.empty()) {
195 "Internal error; no version info nor error info was returned " 196 "by version parser");
Defines utilities for detecting and dealing with cQASM language versions.
Version parse_file(const std::string &filename)
Parse the given file to get its version number.
Toplevel namespace with entry points for the new API.
void cqasm_version_delete_buffer(YY_BUFFER_STATE b, yyscan_t yyscanner)
Destroy the buffer.
void cqasm_versionset_in(FILE *_in_str, yyscan_t yyscanner)
Set the input stream.
YY_BUFFER_STATE cqasm_version_scan_string(yyconst char *yy_str, yyscan_t yyscanner)
Setup the input buffer state to scan a string.
int cqasm_versionparse(yyscan_t scanner, cqasm::version::ParseHelper &helper)
Version(const std::string &version="1.0")
Constructs a version object from a string, defaulting to 1.0.
Version number primitive used within the AST and semantic trees.
std::ostream & operator<<(std::ostream &os, const Version &object)
Stream << overload for version nodes.
Internal helper class for parsing cQASM file versions.
int cqasm_versionlex_destroy(yyscan_t yyscanner)
Contains custom exception objects used by libqasm.
int cqasm_versionlex_init(yyscan_t *scanner)
static void push_error(const std::string &error)
Pushes an error.
virtual ~ParseHelper()
Destroys the parse helper.
Version parse_string(const std::string &data, const std::string &filename)
Parse the given string as a file to get its version number.
int compare(const Version &other) const
Compares this version against the other version.
Parser, generated by Bison.
Exception used for analysis errors.