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

Reimplement Parser #2149

Draft
wants to merge 69 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
b0da809
Implement OpenQASM 3 lexer (#2129)
orpuente-MS Feb 2, 2025
fd616c4
Add initial AST and parser infrastructure (#2148)
idavis Feb 4, 2025
83332e6
Add ability to parse quantum decls and lit exprs (#2160)
idavis Feb 5, 2025
c73cdbc
Adding classical & io decls to the parser, stubbing out array decls (…
idavis Feb 7, 2025
17495f7
Fix panic in qasm3 lexer found by the fuzzer (#2170)
orpuente-MS Feb 11, 2025
c4343d0
Add fuzzing for qasm3 (#2167)
idavis Feb 11, 2025
78885b7
Implement Pratt Parsing for Qasm3 parser (#2166)
orpuente-MS Feb 11, 2025
4ea324d
Parse qasm3 switch statements (#2178)
orpuente-MS Feb 12, 2025
7ea14c1
Add subroutine, return, and gate defs, externs, and old style decls (…
idavis Feb 12, 2025
440a42c
Add array and measurement declaration statements (#2180)
orpuente-MS Feb 13, 2025
9c342f6
parse if, for, and while statements
orpuente-MS Feb 19, 2025
c3d7548
fix range expression
orpuente-MS Feb 20, 2025
d4fcdc8
update for loop unit tests
orpuente-MS Feb 20, 2025
2eb0759
add continue statements
orpuente-MS Feb 20, 2025
ba14cb8
update completions unit tests
orpuente-MS Feb 20, 2025
b28ac61
add break statements
orpuente-MS Feb 20, 2025
8b2011f
fix assignment expressions and add expression statements
orpuente-MS Feb 20, 2025
6fc03cf
add end stmt
orpuente-MS Feb 20, 2025
683d132
add alias declarations
orpuente-MS Feb 20, 2025
34c8599
format alias.rs file
orpuente-MS Feb 20, 2025
89b29b4
fixes during PR review with swernli
orpuente-MS Feb 20, 2025
9642239
gate calls
orpuente-MS Feb 24, 2025
4aaf8fd
update expect tests
orpuente-MS Feb 25, 2025
0c39f49
gate-call qubit args should be comma separated
orpuente-MS Feb 25, 2025
6be9453
add barrier, box, calgrammar, cal, defcal, delay, measure arrow, and …
orpuente-MS Feb 25, 2025
c146bd4
update completions tests
orpuente-MS Feb 25, 2025
3ceedb8
add unit tests
orpuente-MS Feb 25, 2025
f42289e
fix formatting
orpuente-MS Feb 25, 2025
ddbb48a
update completions tests
orpuente-MS Feb 25, 2025
ff2db3d
update unit tests
orpuente-MS Feb 25, 2025
dbe31d8
add support for timing literals
orpuente-MS Feb 26, 2025
7aa42ac
improve TODO messages
orpuente-MS Feb 26, 2025
8dd9112
address comments in PR review
orpuente-MS Feb 26, 2025
7df17ce
add gphase
orpuente-MS Feb 27, 2025
f12c6f3
add unit test for box with designator
orpuente-MS Feb 27, 2025
19d0db3
require exactly one angle argument in `gphase`
orpuente-MS Feb 27, 2025
610ff6b
fix parse_include and parse_cal_grammar
orpuente-MS Feb 27, 2025
3a70241
PR review fixes
orpuente-MS Feb 27, 2025
23d2ed6
fixes during PR review
orpuente-MS Feb 28, 2025
6c4f17f
fixes during PR review
orpuente-MS Feb 28, 2025
482d56e
add unit tests for gate_call
orpuente-MS Feb 28, 2025
35528fa
change unit tests names
orpuente-MS Feb 28, 2025
8b4f4f7
format file
orpuente-MS Feb 28, 2025
9371b43
disambiguate between cast expr_stmts and classical decls
orpuente-MS Feb 28, 2025
f6786e9
update cast unit tests
orpuente-MS Feb 28, 2025
8c3eed4
make expr unit tests also run for expr_stmts by adding a semicolon at…
orpuente-MS Feb 28, 2025
7ba15c6
fix formatting
orpuente-MS Feb 28, 2025
be8f57c
fix lexer bug
orpuente-MS Mar 1, 2025
6dcccc7
format code
orpuente-MS Mar 1, 2025
4f6d5c0
fix lexer bug and remove `second` from cooked lexer
orpuente-MS Mar 1, 2025
fa78d24
update test
orpuente-MS Mar 1, 2025
34fa49b
remove extra scope
orpuente-MS Mar 3, 2025
23327c1
Tidy up Display functions in QASM3 parser (#2209)
orpuente-MS Mar 5, 2025
c2d4051
Add initial semantic analysis/transform (#2214)
idavis Mar 7, 2025
ea70d7c
Make Assign and AssignOp valid only in expression statements (#2215)
orpuente-MS Mar 8, 2025
5480691
Make assignment a StmtKind (#2216)
orpuente-MS Mar 11, 2025
e687817
Add grammar docstrings to QASM3 parser (#2222)
orpuente-MS Mar 12, 2025
1c16710
Implement base declarations and binary expression lowering (#2221)
idavis Mar 12, 2025
a8852c7
Make control flow stmts use Stmt instead of List<Stmt> for their bodi…
orpuente-MS Mar 13, 2025
3848f81
Update cross file spans, separate parse/semantic result blurring (#2227)
idavis Mar 13, 2025
4f352af
Lower control flow stmts in QASM3 parser (#2232)
orpuente-MS Mar 17, 2025
95a4249
Adding new compiler, lowerer is now infallible (#2239)
idavis Mar 18, 2025
844be1b
Unify compilation and error propagation. (#2247)
idavis Mar 21, 2025
ae8e060
Add gate calls and the AST items it depends on to the new QASM3 compi…
orpuente-MS Mar 24, 2025
2d67664
Removing old test
idavis Mar 24, 2025
d93f2f8
Parsing now bundles IO source loading errors into parse errors. (#2252)
idavis Mar 25, 2025
313b7af
Compile gphase, gatedef, and control flow statements (#2254)
orpuente-MS Mar 27, 2025
0fd5cd5
QASM3 - Add implicit casts to function and gate arguments (#2257)
orpuente-MS Mar 27, 2025
d74fe1b
QASM3 - simple index expr (#2258)
orpuente-MS Mar 27, 2025
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
Prev Previous commit
Next Next commit
Implement base declarations and binary expression lowering (#2221)
idavis committed Mar 27, 2025
commit 1c16710ec0b0953bc904e2ec1345d35821c1e3c5
1 change: 0 additions & 1 deletion compiler/qsc_qasm3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@
#![allow(dead_code)]

mod angle;
mod ast;
mod ast_builder;
mod compile;
pub use compile::qasm_to_program;
5 changes: 3 additions & 2 deletions compiler/qsc_qasm3/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::ast::{Program, StmtKind};
pub mod ast;
use crate::io::SourceResolver;
use ast::{Program, StmtKind};
use qsc_frontend::compile::SourceMap;
use qsc_frontend::error::WithSource;
use scan::ParserContext;
@@ -231,7 +232,7 @@ where
Ok((program, errors, included))
}

fn parse_includes<R>(program: &crate::ast::Program, resolver: &R) -> miette::Result<Vec<QasmSource>>
fn parse_includes<R>(program: &Program, resolver: &R) -> miette::Result<Vec<QasmSource>>
where
R: SourceResolver,
{
Original file line number Diff line number Diff line change
@@ -481,6 +481,7 @@ pub enum Identifier {
}

impl Identifier {
#[must_use]
pub fn span(&self) -> Span {
match self {
Identifier::Ident(ident) => ident.span,
@@ -878,6 +879,7 @@ pub enum TypeDef {
}

impl TypeDef {
#[must_use]
pub fn span(&self) -> Span {
match self {
TypeDef::Scalar(ident) => ident.span,
@@ -1649,6 +1651,16 @@ impl Display for IndexElement {
}
}

impl IndexElement {
#[must_use]
pub fn span(&self) -> Span {
match self {
IndexElement::DiscreteSet(set) => set.span,
IndexElement::IndexSet(set) => set.span,
}
}
}

#[derive(Clone, Debug, Default)]
pub enum IndexSetItem {
RangeDefinition(RangeDefinition),
@@ -1680,7 +1692,7 @@ impl Display for IndexSetItem {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IOKeyword {
Input,
Output,
@@ -1695,6 +1707,15 @@ impl Display for IOKeyword {
}
}

impl From<IOKeyword> for crate::semantic::symbols::IOKind {
fn from(value: IOKeyword) -> Self {
match value {
IOKeyword::Input => crate::semantic::symbols::IOKind::Input,
IOKeyword::Output => crate::semantic::symbols::IOKind::Output,
}
}
}

#[derive(Clone, Debug)]
pub enum TimeUnit {
Dt,
25 changes: 11 additions & 14 deletions compiler/qsc_qasm3/src/parser/expr.rs
Original file line number Diff line number Diff line change
@@ -12,29 +12,26 @@ use num_traits::Num;
use qsc_data_structures::span::Span;

use crate::{
ast::{
self, list_from_iter, BinOp, BinaryOpExpr, Cast, DiscreteSet, Expr, ExprKind, FunctionCall,
GateOperand, HardwareQubit, Ident, IndexElement, IndexExpr, IndexSet, IndexSetItem,
IndexedIdent, List, Lit, LiteralKind, MeasureExpr, RangeDefinition, TimeUnit, TypeDef,
UnaryOp, ValueExpression, Version,
},
keyword::Keyword,
lex::{
cooked::{ComparisonOp, Literal, TimingLiteralKind},
ClosedBinOp, Delim, Radix, Token, TokenKind,
},
parser::{
completion::WordKinds,
prim::{shorten, token},
scan::ParserContext,
},
};

use crate::parser::Result;

use super::{
ast::{
list_from_iter, BinOp, BinaryOpExpr, Cast, DiscreteSet, Expr, ExprKind, FunctionCall,
GateOperand, HardwareQubit, Ident, IndexElement, IndexExpr, IndexSet, IndexSetItem,
IndexedIdent, List, Lit, LiteralKind, MeasureExpr, RangeDefinition, TimeUnit, TypeDef,
UnaryOp, UnaryOpExpr, ValueExpression, Version,
},
completion::WordKinds,
error::{Error, ErrorKind},
prim::{ident, many, opt, recovering_token, seq, FinalSep},
prim::{ident, many, opt, recovering_token, seq, shorten, token, FinalSep},
scan::ParserContext,
stmt::scalar_or_array_type,
};

@@ -94,7 +91,7 @@ fn expr_op(s: &mut ParserContext, context: OpContext) -> Result<Expr> {
let rhs = expr_op(s, OpContext::Precedence(op.precedence))?;
Expr {
span: s.span(lo),
kind: Box::new(ExprKind::UnaryOp(ast::UnaryOpExpr {
kind: Box::new(ExprKind::UnaryOp(UnaryOpExpr {
op: op.kind,
expr: rhs,
})),
@@ -444,7 +441,7 @@ pub(crate) fn paren_expr(s: &mut ParserContext, lo: u32) -> Result<Expr> {
})
}

fn funcall(s: &mut ParserContext, ident: ast::Ident) -> Result<ExprKind> {
fn funcall(s: &mut ParserContext, ident: Ident) -> Result<ExprKind> {
let lo = ident.span.lo;
let (args, _) = seq(s, expr)?;
token(s, TokenKind::Close(Delim::Paren))?;
2 changes: 1 addition & 1 deletion compiler/qsc_qasm3/src/parser/expr/tests.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

use super::expr;
use crate::{
ast::StmtKind,
parser::ast::StmtKind,
parser::{scan::ParserContext, stmt, tests::check},
};
use expect_test::{expect, Expect};
3 changes: 2 additions & 1 deletion compiler/qsc_qasm3/src/parser/prgm.rs
Original file line number Diff line number Diff line change
@@ -6,11 +6,12 @@ use super::{
stmt, Result,
};
use crate::{
ast::{Program, Stmt, StmtKind, Version},
lex::{Delim, TokenKind},
parser::{completion::WordKinds, expr},
};

use super::ast::{Program, Stmt, StmtKind, Version};

use super::ParserContext;

/// Grammar: `version? statementOrScope* EOF`.
8 changes: 3 additions & 5 deletions compiler/qsc_qasm3/src/parser/prim.rs
Original file line number Diff line number Diff line change
@@ -9,11 +9,9 @@ use super::{
scan::ParserContext,
Parser, Result,
};
use crate::{
ast::{Ident, IncompletePath, Path, PathKind},
lex::TokenKind,
parser::completion::WordKinds,
};
use crate::{lex::TokenKind, parser::completion::WordKinds};

use super::ast::{Ident, IncompletePath, Path, PathKind};

use qsc_data_structures::span::{Span, WithSpan};

2 changes: 1 addition & 1 deletion compiler/qsc_qasm3/src/parser/prim/tests.rs
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@

use super::{ident, opt, seq};
use crate::{
ast::PathKind,
keyword::Keyword,
lex::TokenKind,
parser::ast::PathKind,
parser::{
completion::WordKinds,
error::{Error, ErrorKind},
26 changes: 13 additions & 13 deletions compiler/qsc_qasm3/src/parser/stmt.rs
Original file line number Diff line number Diff line change
@@ -15,23 +15,23 @@ use super::{
Result,
};
use crate::{
ast::{
list_from_iter, AccessControl, AliasDeclStmt, AngleType, Annotation, ArrayBaseTypeKind,
ArrayReferenceType, ArrayType, ArrayTypedParameter, AssignOpStmt, AssignStmt, BarrierStmt,
BitType, Block, BoxStmt, BreakStmt, CalibrationGrammarStmt, CalibrationStmt, Cast,
ClassicalDeclarationStmt, ComplexType, ConstantDeclStmt, ContinueStmt, DefCalStmt, DefStmt,
DelayStmt, EndStmt, EnumerableSet, Expr, ExprKind, ExprStmt, ExternDecl, ExternParameter,
FloatType, ForStmt, FunctionCall, GPhase, GateCall, GateModifierKind, GateOperand,
IODeclaration, IOKeyword, Ident, Identifier, IfStmt, IncludeStmt, IndexElement, IndexExpr,
IndexSetItem, IndexedIdent, IntType, List, LiteralKind, MeasureStmt, Pragma,
QuantumGateDefinition, QuantumGateModifier, QuantumTypedParameter, QubitDeclaration,
RangeDefinition, ResetStmt, ReturnStmt, ScalarType, ScalarTypeKind, ScalarTypedParameter,
Stmt, StmtKind, SwitchCase, SwitchStmt, TypeDef, TypedParameter, UIntType, WhileLoop,
},
keyword::Keyword,
lex::{cooked::Type, Delim, TokenKind},
};

use super::ast::{
list_from_iter, AccessControl, AliasDeclStmt, AngleType, Annotation, ArrayBaseTypeKind,
ArrayReferenceType, ArrayType, ArrayTypedParameter, AssignOpStmt, AssignStmt, BarrierStmt,
BitType, Block, BoxStmt, BreakStmt, CalibrationGrammarStmt, CalibrationStmt, Cast,
ClassicalDeclarationStmt, ComplexType, ConstantDeclStmt, ContinueStmt, DefCalStmt, DefStmt,
DelayStmt, EndStmt, EnumerableSet, Expr, ExprKind, ExprStmt, ExternDecl, ExternParameter,
FloatType, ForStmt, FunctionCall, GPhase, GateCall, GateModifierKind, GateOperand,
IODeclaration, IOKeyword, Ident, Identifier, IfStmt, IncludeStmt, IndexElement, IndexExpr,
IndexSetItem, IndexedIdent, IntType, List, LiteralKind, MeasureStmt, Pragma,
QuantumGateDefinition, QuantumGateModifier, QuantumTypedParameter, QubitDeclaration,
RangeDefinition, ResetStmt, ReturnStmt, ScalarType, ScalarTypeKind, ScalarTypedParameter, Stmt,
StmtKind, SwitchCase, SwitchStmt, TypeDef, TypedParameter, UIntType, WhileLoop,
};
use super::{prim::token, ParserContext};

/// Our implementation differs slightly from the grammar in
Loading