Skip to content

Commit 0a6afbb

Browse files
authored
Add grammar docstrings to QASM3 parser (#2222)
This PR: 1. Fixes a bug with block statements 2. Adds grammar docstrings to the QASM3 parser. 3. Adds the invalid tests in the reference parser to make sure they are invalid for our parser as well.
1 parent d7e6e38 commit 0a6afbb

17 files changed

+3312
-183
lines changed

compiler/qsc_qasm3/src/parser/expr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ fn unescape(s: &str) -> std::result::Result<String, usize> {
680680
Ok(buf)
681681
}
682682

683+
/// Grammar: `LBRACKET expression RBRACKET`.
683684
pub(super) fn designator(s: &mut ParserContext) -> Result<Expr> {
684685
token(s, TokenKind::Open(Delim::Bracket))?;
685686
let expr = expr(s)?;
@@ -756,6 +757,7 @@ fn hardware_qubit(s: &mut ParserContext) -> Result<HardwareQubit> {
756757
})
757758
}
758759

760+
/// Grammar: `Identifier indexOperator*`.
759761
pub(crate) fn indexed_identifier(s: &mut ParserContext) -> Result<IndexedIdent> {
760762
let lo = s.peek().span.lo;
761763
let name: Ident = ident(s)?;
@@ -768,6 +770,15 @@ pub(crate) fn indexed_identifier(s: &mut ParserContext) -> Result<IndexedIdent>
768770
})
769771
}
770772

773+
/// Grammar:
774+
/// ```g4
775+
/// LBRACKET
776+
/// (
777+
/// setExpression
778+
/// | (expression | rangeExpression) (COMMA (expression | rangeExpression))* COMMA?
779+
/// )
780+
/// RBRACKET
781+
/// ```
771782
fn index_operand(s: &mut ParserContext) -> Result<IndexElement> {
772783
token(s, TokenKind::Open(Delim::Bracket))?;
773784
let index = index_element(s)?;

compiler/qsc_qasm3/src/parser/prgm.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313

1414
use super::ParserContext;
1515

16+
/// Grammar: `version? statementOrScope* EOF`.
1617
pub(super) fn parse(s: &mut ParserContext) -> Result<Program> {
1718
let lo = s.peek().span.lo;
1819
let version = opt(s, parse_version)?;
@@ -29,6 +30,7 @@ pub(super) fn parse(s: &mut ParserContext) -> Result<Program> {
2930
})
3031
}
3132

33+
/// Grammar: `OPENQASM VersionSpecifier SEMICOLON`.
3234
fn parse_version(s: &mut ParserContext<'_>) -> Result<Version> {
3335
s.expect(WordKinds::OpenQASM);
3436
token(s, TokenKind::Keyword(crate::keyword::Keyword::OpenQASM))?;

0 commit comments

Comments
 (0)