Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement OpenQASM 3 lexer #2129

Draft
wants to merge 17 commits into
base: feature/qasm3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion compiler/qsc_qasm3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ version.workspace = true

[dependencies]
bitflags = { workspace = true }
enum-iterator = { workspace = true }
num-bigint = { workspace = true }
miette = { workspace = true }
qsc = { path = "../qsc" }
qsc_data_structures = { path = "../qsc_data_structures" }
rustc-hash = { workspace = true }
thiserror = { workspace = true }
oq3_source_file = { workspace = true }
oq3_syntax = { workspace = true }
oq3_parser = { workspace = true }
oq3_lexer = { workspace = true }
# oq3_lexer = { workspace = true }
oq3_semantics = { workspace = true }

[dev-dependencies]
Expand Down
60 changes: 60 additions & 0 deletions compiler/qsc_qasm3/src/keyword.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use enum_iterator::Sequence;
use std::{
fmt::{self, Display, Formatter},
str::FromStr,
};

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Sequence)]
pub enum Keyword {
OpenQASM,
Include,
Defcalgrammar,
Def,
Cal,
Gate,
Extern,
Box,
Let,

Break,
Continue,
If,
Else,
End,
Return,
For,
While,
In,
Switch,
Case,
Default,

Pragma,
Annotation,
}

impl Keyword {
pub(super) fn as_str(self) -> &'static str {
todo!()
}
}

impl Display for Keyword {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(self.as_str())
}
}

impl FromStr for Keyword {
type Err = ();

// This is a hot function. Use a match expression so that the Rust compiler
// can optimize the string comparisons better, and order the cases by
// frequency in Q# so that fewer comparisons are needed on average.
fn from_str(s: &str) -> Result<Self, Self::Err> {
todo!()
}
}
51 changes: 51 additions & 0 deletions compiler/qsc_qasm3/src/lex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

pub mod cooked;
pub mod raw;

use enum_iterator::Sequence;

pub(super) use cooked::{ClosedBinOp, Error, Lexer, StringToken, Token, TokenKind};

/// A delimiter token.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Sequence)]
pub enum Delim {
/// `{` or `}`
Brace,
/// `[` or `]`
Bracket,
/// `(` or `)`
Paren,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Sequence)]
pub enum Radix {
Binary,
Octal,
Decimal,
Hexadecimal,
}

impl From<Radix> for u32 {
fn from(value: Radix) -> Self {
match value {
Radix::Binary => 2,
Radix::Octal => 8,
Radix::Decimal => 10,
Radix::Hexadecimal => 16,
}
}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Sequence)]
pub enum InterpolatedStart {
DollarQuote,
RBrace,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Sequence)]
pub enum InterpolatedEnding {
Quote,
LBrace,
}
Loading
Loading