42 class Location :
public IDisplay {
43 std::string fname_ =
"";
60 Location(
const std::string& fname, idx line, idx column)
61 : fname_(fname), line_(line), column_(column) {}
68 Location(
const Location& loc)
69 : fname_(loc.fname_), line_(loc.line_), column_(loc.column_) {}
80 std::ostream& display(std::ostream& os)
const {
81 os << fname_ <<
":" << line_ <<
":" << column_;
90 const std::string& get_filename()
const {
return fname_; }
97 idx get_linenum()
const {
return line_; }
104 idx get_column()
const {
return column_; }
113 void advance_line(idx num = 1) {
123 void advance_column(idx num = 1) { column_ += num; }
131 class Token :
public IDisplay {
196 Token(Location loc, Kind k,
const std::string& value)
197 : loc_(loc), kind_(k), value_(value) {}
204 Kind kind()
const {
return kind_; }
212 bool is(Kind k)
const {
return kind_ == k; }
220 bool is_not(Kind k)
const {
return kind_ != k; }
228 template <
typename... Ts>
229 bool is_one_of(Kind k1, Kind k2, Ts... ks)
const {
230 return is(k1) || is_one_of(k2, ks...);
240 operator double() {
return std::stof(value_); }
247 operator std::string() {
return std::string(value_); }
256 operator idx() {
return std::stoi(value_); }
265 operator int() {
return std::stoi(value_); }
272 const Location& location()
const {
return loc_; }
281 friend std::ostream& operator<<(std::ostream& os,
const Kind& k) {
292 case Kind::identifier:
298 case Kind::nninteger:
343 case Kind::semicolon:
346 case Kind::equalequal:
355 case Kind::kw_include:
358 case Kind::kw_barrier:
373 case Kind::kw_measure:
379 case Kind::kw_opaque:
382 case Kind::kw_openqasm:
424 std::ostream& display(std::ostream& os)
const {
427 case Kind::identifier:
428 os <<
"(" << value_ <<
")";
431 os <<
"(" << std::stof(value_) <<
")";
433 case Kind::nninteger:
434 os <<
"(" << std::stoi(value_) <<
")";
437 os <<
"(\"" << value_ <<
"\")";
446 Location loc_ = Location();
447 Kind kind_ = Kind::unknown;
448 std::string value_ =
"";
454 static const std::unordered_map<std::string, Token::Kind> keywords{
455 {
"include", Token::Kind::kw_include},
456 {
"barrier", Token::Kind::kw_barrier},
457 {
"creg", Token::Kind::kw_creg},
458 {
"CX", Token::Kind::kw_cx},
459 {
"gate", Token::Kind::kw_gate},
460 {
"if", Token::Kind::kw_if},
461 {
"measure", Token::Kind::kw_measure},
462 {
"pi", Token::Kind::kw_pi},
463 {
"opaque", Token::Kind::kw_opaque},
464 {
"OPENQASM", Token::Kind::kw_openqasm},
465 {
"qreg", Token::Kind::kw_qreg},
466 {
"reset", Token::Kind::kw_reset},
467 {
"U", Token::Kind::kw_u},
468 {
"sin", Token::Kind::kw_sin},
469 {
"cos", Token::Kind::kw_cos},
470 {
"tan", Token::Kind::kw_tan},
471 {
"exp", Token::Kind::kw_exp},
472 {
"ln", Token::Kind::kw_ln},
473 {
"sqrt", Token::Kind::kw_sqrt}};
Quantum++ main namespace.
Definition: circuits.h:35