1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
// Copyright contributors to the openqasm-parser project
// SPDX-License-Identifier: Apache-2.0
//! Low-level OpenQASM3 lexer.
//!
//! This is largely copied from `rustc_lexer`, the lexer for the rust compiler.
//! `rustc_lexer` is avaiable as a crate, and rust-analyzer uses this crate for lexing.
//!
//! The main entity of this crate is the [`TokenKind`] enum which represents common
//! lexeme types.
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
mod cursor;
pub mod unescape;
#[cfg(test)]
mod tests;
pub use crate::cursor::Cursor;
use self::LiteralKind::*;
use self::TokenKind::*;
// Will likely need this:
// use crate::cursor::EOF_CHAR;
use unicode_properties::UnicodeEmoji;
/// Parsed token.
/// It doesn't contain information about data that has been parsed,
/// only the type of the token and its size.
#[derive(Debug)]
pub struct Token {
pub kind: TokenKind,
pub len: u32,
}
impl Token {
fn new(kind: TokenKind, len: u32) -> Token {
Token { kind, len }
}
}
/// Enum representing common lexeme types.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TokenKind {
// Multi-char tokens:
/// "// comment"
LineComment,
/// `/* block comment */`
///
/// Block comments can be recursive, so a sequence like `/* /* */`
/// will not be considered terminated and will result in a parsing error.
BlockComment {
terminated: bool,
},
/// Any whitespace character sequence.
Whitespace,
// ClassicalTypeName,
/// "ident" or "continue"
///
/// At this step, keywords are also considered identifiers.
Ident,
HardwareIdent, // OQ3
/// Like the above, but containing invalid unicode codepoints.
InvalidIdent,
Pragma,
Annotation,
/// Needed for OpenQASM 3 ?
/// An unknown prefix, like `foo#`, `foo'`, `foo"`.
///
/// Note that only the
/// prefix (`foo`) is included in the token, not the separator (which is
/// lexed as its own distinct token). In Rust 2021 and later, reserved
/// prefixes are reported as errors; in earlier editions, they result in a
/// (allowed by default) lint, and are treated as regular identifier
/// tokens.
/// UnknownPrefix,
/// Examples: `1.0e-40`, `1000dt`, `1000ms`. Here `dt` and `ms` are suffixes.
/// OpenQASM3 relevance? -> Note that `_` is an invalid
/// suffix, but may be present here on string and float literals. Users of
/// this type will need to check for and reject that case.
///
/// See [LiteralKind] for more details.
Literal {
kind: LiteralKind,
suffix_start: u32,
},
// One-char tokens:
/// ";"
Semi,
/// ","
Comma,
/// "."
Dot,
/// "("
OpenParen,
/// ")"
CloseParen,
/// "{"
OpenBrace,
/// "}"
CloseBrace,
/// "["
OpenBracket,
/// "]"
CloseBracket,
/// "@"
At,
/// "#"
Pound,
/// "~"
Tilde,
/// "?"
Question,
/// ":"
Colon,
/// "$"
Dollar,
/// "="
Eq,
/// "!"
Bang,
/// "<"
Lt,
/// ">"
Gt,
/// "-"
Minus,
/// "&"
And,
/// "|"
Or,
/// "+"
Plus,
/// "*"
Star,
/// "/"
Slash,
/// "^"
Caret,
/// "%"
Percent,
/// Unknown token, not expected by the lexer, e.g. "№"
Unknown,
/// End of input.
Eof,
}
/// Enum representing the literal types supported by the lexer.
///
/// Note that the suffix is *not* considered when deciding the `LiteralKind` in
/// this type. This means that float literals like `1f32` are classified by this
/// type as `Int`. (Compare against `rustc_ast::token::LitKind` and
/// `rustc_ast::ast::LitKind`).
///
/// Following this same idea, timing literals are also just Int or Float at this stage.
/// But the length of the suffix will be used later to classify them. (GJL Aug 2023)
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum LiteralKind {
/// "12_u8", "0o100", "0b120i99", "1f32".
Int { base: Base, empty_int: bool },
/// "12.34f32", "1e3", but not "1f32".
Float { base: Base, empty_exponent: bool },
/// "b'a'", "b'\\'", "b'''", "b';"
Byte { terminated: bool },
/// ""abc"", ""abc"
Str { terminated: bool },
/// "10011" "100_11"
BitStr {
terminated: bool,
consecutive_underscores: bool,
},
}
/// Base of numeric literal encoding according to its prefix.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Base {
/// Literal starts with "0b".
Binary = 2,
/// Literal starts with "0o".
Octal = 8,
/// Literal doesn't contain a prefix.
Decimal = 10,
/// Literal starts with "0x".
Hexadecimal = 16,
}
/// Creates an iterator that produces tokens from the input string.
pub fn tokenize(input: &str) -> impl Iterator<Item = Token> + '_ {
let mut cursor = Cursor::new(input);
std::iter::from_fn(move || {
let token = cursor.advance_token();
if token.kind != TokenKind::Eof {
Some(token)
} else {
None
}
})
}
/// True if `c` is considered a whitespace according to Rust language definition.
/// See [Rust language reference](https://doc.rust-lang.org/reference/whitespace.html)
/// for definitions of these classes.
pub fn is_whitespace(c: char) -> bool {
// This is Pattern_White_Space.
//
// Note that this set is stable (ie, it doesn't change with different
// Unicode versions), so it's ok to just hard-code the values.
matches!(
c,
// Usual ASCII suspects
'\u{0009}' // \t
| '\u{000A}' // \n
| '\u{000B}' // vertical tab
| '\u{000C}' // form feed
| '\u{000D}' // \r
| '\u{0020}' // space
// NEXT LINE from latin1
| '\u{0085}'
// Bidi markers
| '\u{200E}' // LEFT-TO-RIGHT MARK
| '\u{200F}' // RIGHT-TO-LEFT MARK
// Dedicated whitespace characters from Unicode
| '\u{2028}' // LINE SEPARATOR
| '\u{2029}' // PARAGRAPH SEPARATOR
)
}
/// For OQ3 we take Rust's rules on valid identifiers as a starting point.
/// True if `c` is valid as a first character of an identifier.
/// See [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html) for
/// a formal definition of valid identifier name.
pub fn is_id_start(c: char) -> bool {
// This is XID_Start OR '_' (which formally is not a XID_Start).
// c == '_' || c == '$' || unicode_xid::UnicodeXID::is_xid_start(c)
c == '_' || unicode_xid::UnicodeXID::is_xid_start(c)
}
/// For OQ3 we take Rust's rules on valid identifiers as a starting point.
/// True if `c` is valid as a non-first character of an identifier.
/// See [Rust language reference](https://doc.rust-lang.org/reference/identifiers.html) for
/// a formal definition of valid identifier name.
pub fn is_id_continue(c: char) -> bool {
unicode_xid::UnicodeXID::is_xid_continue(c)
}
/// The passed string is lexically an identifier.
pub fn is_ident(string: &str) -> bool {
let mut chars = string.chars();
if let Some(start) = chars.next() {
is_id_start(start) && chars.all(is_id_continue)
} else {
false
}
}
impl Cursor<'_> {
/// Parses a token from the input string.
pub fn advance_token(&mut self) -> Token {
let first_char = match self.bump() {
Some(c) => c,
None => return Token::new(TokenKind::Eof, 0),
};
let token_kind = match first_char {
// Slash, comment or block comment.
'/' => match self.first() {
'/' => self.line_comment(),
'*' => self.block_comment(),
_ => Slash,
},
// Whitespace sequence.
c if is_whitespace(c) => self.whitespace(),
// Identifier (this should be checked after other variant that can
// start as identifier).
c if is_id_start(c) => self.ident_or_unknown_prefix(),
// Numeric literal.
c @ '0'..='9' => {
let literal_kind = self.number(c);
let suffix_start = self.pos_within_token();
// If this a timing (or duration) literal, we will parse the
// time unit as another token. So we don't eat the suffix if it
// is a time unit.
if !self.has_timing_or_imaginary_suffix() {
self.eat_literal_suffix();
}
TokenKind::Literal {
kind: literal_kind,
suffix_start,
}
}
'#' => {
if self.first() == 'p' {
self.bump();
if self.first() == 'r' {
self.bump();
if self.first() == 'a' {
self.bump();
if self.first() == 'g' {
self.bump();
if self.first() == 'm' {
self.bump();
if self.first() == 'a' {
self.eat_while(|c| c != '\n');
let res = Token::new(Pragma, self.pos_within_token());
self.reset_pos_within_token();
return res;
}
}
}
}
}
}
// Only `#pragma` and `#dim` may begin with a pound character
InvalidIdent
}
'@' => {
if is_id_start(self.first()) {
self.eat_while(|c| c != '\n');
Annotation
} else {
At
}
}
'$' => self.hardware_ident(),
// One-symbol tokens.
';' => Semi,
',' => Comma,
'.' => Dot,
'(' => OpenParen,
')' => CloseParen,
'{' => OpenBrace,
'}' => CloseBrace,
'[' => OpenBracket,
']' => CloseBracket,
'~' => Tilde,
'?' => Question,
':' => Colon,
'=' => Eq,
'!' => Bang,
'<' => Lt,
'>' => Gt,
'-' => Minus,
'&' => And,
'|' => Or,
'+' => Plus,
'*' => Star,
'^' => Caret,
'%' => Percent,
// OQ3 unfortunately follows Python in allowing either ' or " for the same kind of string
// Lifetime or character literal.
// '\'' => self.lifetime_or_char(),
// String literal.
'"' => {
let (terminated, only_ones_and_zeros, consecutive_underscores) =
self.double_quoted_string();
let suffix_start = self.pos_within_token();
if terminated {
self.eat_literal_suffix();
}
let kind = match only_ones_and_zeros {
true => BitStr {
terminated,
consecutive_underscores,
},
false => Str { terminated },
};
Literal { kind, suffix_start }
}
// OQ3 can error on this as well.
// Identifier starting with an emoji. Only lexed for graceful error recovery.
c if !c.is_ascii() && c.is_emoji_char() => self.fake_ident_or_unknown_prefix(),
_ => Unknown,
};
let res = Token::new(token_kind, self.pos_within_token());
self.reset_pos_within_token();
res
}
fn line_comment(&mut self) -> TokenKind {
debug_assert!(self.prev() == '/' && self.first() == '/');
self.bump();
self.eat_while(|c| c != '\n');
LineComment
}
fn block_comment(&mut self) -> TokenKind {
debug_assert!(self.prev() == '/' && self.first() == '*');
self.bump();
let mut depth = 1usize;
while let Some(c) = self.bump() {
match c {
'/' if self.first() == '*' => {
self.bump();
depth += 1;
}
'*' if self.first() == '/' => {
self.bump();
depth -= 1;
if depth == 0 {
// This block comment is closed, so for a construction like "/* */ */"
// there will be a successfully parsed block comment "/* */"
// and " */" will be processed separately.
break;
}
}
_ => (),
}
}
BlockComment {
terminated: depth == 0,
}
}
fn whitespace(&mut self) -> TokenKind {
debug_assert!(is_whitespace(self.prev()));
self.eat_while(is_whitespace);
Whitespace
}
fn ident_or_unknown_prefix(&mut self) -> TokenKind {
debug_assert!(is_id_start(self.prev()));
if self.prev() == 'p' && self.first() == 'r' {
self.bump();
if self.first() == 'a' {
self.bump();
if self.first() == 'g' {
self.bump();
if self.first() == 'm' {
self.bump();
if self.first() == 'a' {
self.eat_while(|c| c != '\n');
return Pragma;
}
}
}
}
}
// Start is already eaten, eat the rest of identifier.
self.eat_while(is_id_continue);
// Known prefixes must have been handled earlier. So if
// we see a prefix here, it is definitely an unknown prefix.
match self.first() {
// '#' | '"' | '\'' => UnknownPrefix,
c if !c.is_ascii() && c.is_emoji_char() => self.fake_ident_or_unknown_prefix(),
_ => Ident,
}
}
fn hardware_ident(&mut self) -> TokenKind {
// Start is already eaten, eat the rest of identifier.
match self.first() {
c if !c.is_ascii() && c.is_emoji_char() => {
self.eat_while(is_id_continue);
self.fake_ident_or_unknown_prefix()
}
_ => {
if !self.eat_decimal_digits() {
Dollar
} else {
HardwareIdent
}
}
}
}
// FIXME. I think this is r-a cruft.
fn fake_ident_or_unknown_prefix(&mut self) -> TokenKind {
// Start is already eaten, eat the rest of identifier.
self.eat_while(|c| {
unicode_xid::UnicodeXID::is_xid_continue(c)
|| (!c.is_ascii() && c.is_emoji_char())
|| c == '\u{200d}'
});
// Known prefixes must have been handled earlier. So if
// we see a prefix here, it is definitely an unknown prefix.
InvalidIdent
// match self.first() {
// // '#' | '"' | '\'' => UnknownPrefix,
// _ => InvalidIdent,
// }
}
// In OQ3, whitespace *is* allowed between an imaginary literal and `im`. So to be consistent, these
// must each be a separate token. Furthermore, if we continue with the way this lexer works for rust,
// `im` will not be recognized as reserved at this stage, just as `for`, `if`, etc. are not. The distinction
// between ordinary identifiers and keywords is made first at the level of the parser.
//
fn number(&mut self, first_digit: char) -> LiteralKind {
debug_assert!('0' <= self.prev() && self.prev() <= '9');
let mut base = Base::Decimal;
if first_digit == '0' {
// Attempt to parse encoding base.
match self.first() {
'b' => {
base = Base::Binary;
self.bump();
if !self.eat_decimal_digits() {
return Int {
base,
empty_int: true,
};
}
}
'o' => {
base = Base::Octal;
self.bump();
if !self.eat_decimal_digits() {
return Int {
base,
empty_int: true,
};
}
}
'x' => {
base = Base::Hexadecimal;
self.bump();
if !self.eat_hexadecimal_digits() {
return Int {
base,
empty_int: true,
};
}
}
// Not a base prefix; consume additional digits.
'0'..='9' | '_' => {
self.eat_decimal_digits();
}
// Also not a base prefix; nothing more to do here.
'.' | 'e' | 'E' => {}
// Just a 0.
_ => {
return Int {
base,
empty_int: false,
}
}
}
} else {
// No base prefix, parse number in the usual way.
self.eat_decimal_digits();
};
match self.first() {
// Don't be greedy if this is actually an
// integer literal followed by field/method access or a range pattern
// (`0..2` and `12.foo()`)
// '.' if self.second() != '.' && !is_id_start(self.second()) => {
// // might have stuff after the ., and if it does, it needs to start
// // with a number
// Above applies in Rust, not OQ3
'.' => {
self.bump();
// n.b. example of `empty_exponent` : 3.4e; This is a syntax error
let mut empty_exponent = false;
// preferred to is_digit(10), in rust lexer
if self.first().is_ascii_digit() {
self.eat_decimal_digits();
match self.first() {
'e' | 'E' => {
self.bump();
empty_exponent = !self.eat_float_exponent();
}
_ => (),
}
}
Float {
base,
empty_exponent,
}
}
'e' | 'E' => {
self.bump();
let empty_exponent = !self.eat_float_exponent();
Float {
base,
empty_exponent,
}
}
_ => Int {
base,
empty_int: false,
},
}
}
// OQ3 will use this. But comment out for now.
// Or, may not hand single-quoted strings this way
// fn single_quoted_string(&mut self) -> bool {
// debug_assert!(self.prev() == '\'');
// // Check if it's a one-symbol literal.
// if self.second() == '\'' && self.first() != '\\' {
// self.bump();
// self.bump();
// return true;
// }
// // Literal has more than one symbol.
// // Parse until either quotes are terminated or error is detected.
// loop {
// match self.first() {
// // Quotes are terminated, finish parsing.
// '\'' => {
// self.bump();
// return true;
// }
// // Probably beginning of the comment, which we don't want to include
// // to the error report.
// '/' => break,
// // Newline without following '\'' means unclosed quote, stop parsing.
// '\n' if self.second() != '\'' => break,
// // FIXME: EOF_CHAR became unused after some edit. why?
// // End of file, stop parsing.
// _EOF_CHAR if self.is_eof() => break,
// // Escaped slash is considered one character, so bump twice.
// '\\' => {
// self.bump();
// self.bump();
// }
// // Skip the character.
// _ => {
// self.bump();
// }
// }
// }
// // String was not terminated.
// false
// }
/// Eats double-quoted string and returns (terminated, only_ones_and_zeros)
/// terminated - true if the string is terminated (with double quote)
/// only_ones_and_zeros - true if string is a bit string literal
/// Note that only_ones_and_zeros allows underscores.
fn double_quoted_string(&mut self) -> (bool, bool, bool) {
debug_assert!(self.prev() == '"');
let mut only_ones_and_zeros = true;
let mut terminated = false;
let mut consecutive_underscores = false;
let mut count_newlines = 0;
let mut prev_char = '\0';
while let Some(c) = self.bump() {
match c {
'"' => {
terminated = true;
if count_newlines > 0 {
only_ones_and_zeros = false;
}
return (terminated, only_ones_and_zeros, consecutive_underscores);
}
'\\' if self.first() == '\\' || self.first() == '"' => {
// Bump again to skip escaped character.
only_ones_and_zeros = false;
self.bump();
}
'\n' => {
count_newlines += 1;
if count_newlines > 1 {
only_ones_and_zeros = false;
}
}
'_' => {
if prev_char == '_' {
consecutive_underscores = true;
}
}
'0' | '1' => (),
_ => {
only_ones_and_zeros = false;
}
}
prev_char = c;
}
// This will skip the case that an unterminated bitstring is the last
// characters in a file. Probably not too common.
if count_newlines > 0 && !(count_newlines == 1 && prev_char == '\n') {
only_ones_and_zeros = false;
}
// End of file reached.
(terminated, only_ones_and_zeros, consecutive_underscores)
}
fn eat_decimal_digits(&mut self) -> bool {
let mut has_digits = false;
loop {
match self.first() {
'_' => {
self.bump();
}
'0'..='9' => {
has_digits = true;
self.bump();
}
_ => break,
}
}
has_digits
}
fn eat_hexadecimal_digits(&mut self) -> bool {
let mut has_digits = false;
loop {
match self.first() {
'_' => {
self.bump();
}
'0'..='9' | 'a'..='f' | 'A'..='F' => {
has_digits = true;
self.bump();
}
_ => break,
}
}
has_digits
}
/// Eats the float exponent. Returns true if at least one digit was met,
/// and returns false otherwise.
fn eat_float_exponent(&mut self) -> bool {
debug_assert!(self.prev() == 'e' || self.prev() == 'E');
if self.first() == '-' || self.first() == '+' {
self.bump();
}
self.eat_decimal_digits()
}
// Eats the suffix of the literal, e.g. "u8".
fn eat_literal_suffix(&mut self) {
self.eat_identifier();
}
fn has_timing_or_imaginary_suffix(&mut self) -> bool {
if self.first() == 's' {
return true;
} else {
// TODO: greek mu is encoded in more than one way. We only get one here.
for (f, s) in [
('d', 't'),
('n', 's'),
('u', 's'),
('m', 's'),
('µ', 's'),
('i', 'm'),
] {
if self.first() == f && self.second() == s {
return true;
}
}
}
false
}
// Eats the identifier. Note: succeeds on `_`, which isn't a valid
// identifier.
fn eat_identifier(&mut self) {
if !is_id_start(self.first()) {
return;
}
self.bump();
self.eat_while(is_id_continue);
}
}