Skip to content

Commit

Permalink
Remove cruft from ungram, SyntaxKind, etc. (#96)
Browse files Browse the repository at this point in the history
Removed unused symbols, variants, constructs.
Use better names in some cases.
  • Loading branch information
jlapeyre committed Feb 4, 2024
1 parent 48b306d commit faea94e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 312 deletions.
13 changes: 6 additions & 7 deletions crates/oq3_parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,27 @@ impl BlockLike {
}
}

fn plain_type(p: &mut Parser<'_>) {
assert!(p.current().is_type_name());
fn scalar_type(p: &mut Parser<'_>) {
assert!(p.current().is_scalar_type());
let r = p.start();
p.bump_any();
r.complete(p, PATH_TYPE);
r.complete(p, SCALAR_TYPE);
}

/// Parse the optional return type of a `defcal` or `def` definition.
/// Parse the optional return signature of a `defcal` or `def` definition.
/// Return `true` if the return type was found, else `false.
fn opt_ret_type(p: &mut Parser<'_>) -> bool {
if p.at(T![->]) {
let m = p.start();
p.bump(T![->]);
// types::type_no_bounds(p);
if p.current().is_type_name() {
plain_type(p);
scalar_type(p);
} else {
p.error("Expected return type after ->");
m.abandon(p);
return false;
}
m.complete(p, RET_TYPE);
m.complete(p, RETURN_SIGNATURE);
true
} else {
false
Expand Down
24 changes: 0 additions & 24 deletions crates/oq3_parser/src/syntax_kind/syntax_kind_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,20 @@ pub enum SyntaxKind {
BARRIER,
DEF,
RESET,
RET_TYPE,
CONST,
PAREN_TYPE,
PATH_TYPE,
SLICE_TYPE,
TUPLE_EXPR,
ARRAY_EXPR,
PAREN_EXPR,
PATH_EXPR,
IF_STMT,
WHILE_STMT,
FOR_STMT,
END_STMT,
CONTINUE_STMT,
BREAK_STMT,
LABEL,
BLOCK_EXPR,
STMT_LIST,
RETURN_EXPR,
LET_STMT,
ALIAS_EXPR,
CONCATENATION_EXPR,
BOX_EXPR,
CALL_EXPR,
GATE_CALL_EXPR,
Expand All @@ -173,41 +165,27 @@ pub enum SyntaxKind {
PREFIX_EXPR,
RANGE_EXPR,
BIN_EXPR,
SET_NUM,
EXTERN_ITEM,
ITEM_LIST,
PATH,
PATH_SEGMENT,
LITERAL,
NAME,
EXPR_STMT,
TYPE_SPEC,
TYPE,
NEW_TYPE,
DECLARED_VAR,
TYPE_DECLARATION_STMT,
RETURN_TYPE_ARG,
CONST_PARAM,
CONST_ARG,
PARAM_LIST,
QUBIT_LIST,
FILE_PATH,
PARAM,
ARG_LIST,
GATE_ARG_LIST,
VERSION,
VERSION_STRING,
INCLUDE,
DECLARATION,
DESIGNATOR,
SCALAR_TYPE,
SCALAR_TYPE_NAME,
ARRAY_TYPE,
QUBIT_TYPE,
EXPRESSION_LIST,
RETURN_SIGNATURE,
INT_NUM,
ALIAS_EXPRESSION,
SET_EXPRESSION,
ALIAS_DECLARATION_STATEMENT,
INDEX_OPERATOR,
Expand All @@ -218,8 +196,6 @@ pub enum SyntaxKind {
HARDWARE_QUBIT,
CLASSICAL_DECLARATION_STATEMENT,
ASSIGNMENT_STMT,
DECLARATION_EXPRESSION,
CONST_DECLARATION_STATEMENT,
I_O_DECLARATION_STATEMENT,
GATE_OPERAND,
MEASURE_EXPRESSION,
Expand Down
8 changes: 0 additions & 8 deletions crates/oq3_semantics/src/asg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,6 @@ impl Default for Block {
}
}

// #[test]
// fn test_construct_block() {
// let mut block = Block::new();
// let id = make_ident_expr("x", false);
// let assign = Stmt::Assignment(Assignment { name: id, rhs: make_int_lit_expr(1) });
// block.insert_stmt(assign);
// }

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct GateDeclaration {
name: SymbolIdResult,
Expand Down
8 changes: 4 additions & 4 deletions crates/oq3_syntax/examples/itemparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ fn print_defcal(defcal: ast::DefCal) {
println!("parameters: '{}'", defcal.param_list().unwrap());
}
println!("qubits: '{}'", defcal.qubit_list().unwrap());
if defcal.ret_type().is_some() {
println!("return type: '{}'", defcal.ret_type().unwrap());
if defcal.return_signature().is_some() {
println!("return type: '{}'", defcal.return_signature().unwrap());
}
print!("body: '{}'", defcal.body().unwrap());
}
Expand All @@ -182,8 +182,8 @@ fn print_def(def: ast::Def) {
if def.param_list().is_some() {
println!("parameters: '{}'", def.param_list().unwrap());
}
if def.ret_type().is_some() {
println!("return type: '{}'", def.ret_type().unwrap());
if def.return_signature().is_some() {
println!("return type: '{}'", def.return_signature().unwrap());
}
print!("body: '{}'", def.body().unwrap());
}
Expand Down
36 changes: 5 additions & 31 deletions crates/oq3_syntax/openqasm3.ungram
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This grammar specifies the structure of Rust's concrete syntax tree.
// This grammar specifies the structure of the OpenQASM 3 concrete syntax tree.
// It does not specify parsing rules (ambiguities, precedence, etc are out of scope).
// Tokens are processed -- contextual keywords are recognised, compound operators glued.
//
Expand Down Expand Up @@ -79,6 +79,7 @@ Item =
| GPhaseCallStmt
| LetStmt
| AssignmentStmt
| AliasDeclarationStatement
| Include
| ForStmt
| IfStmt
Expand Down Expand Up @@ -142,12 +143,12 @@ TypeSpec =

// Subroutine definition
Def =
'def' Name ParamList RetType?
'def' Name ParamList ReturnSignature?
(body:BlockExpr | ';')

// Defcal definition
DefCal =
'defcal' Name ParamList QubitList RetType?
'defcal' Name ParamList QubitList ReturnSignature?
(body:BlockExpr | ';')

// Gate definition
Expand All @@ -174,10 +175,6 @@ QubitList =
Param =
Name

// extern is much different in oq3 than rust
// This is a placeholder
ExternItem =
Name

//****************************//
// Statements and Expressions //
Expand Down Expand Up @@ -229,16 +226,6 @@ Literal =
LetStmt =
'let' Name '=' Expr ';'

// FIXME: Only this binary op is turned into a nary-ish op.
// Perhaps this should be removed. Possible issues:
// - Binding power is not expressed here.
// - If this were repeated it might add more boiler plate. Compare BinExpr below.
// So an op that might be considered nary is instead parsed as nested binary ops.
// I recall that Julia's parser changed between nested bin and nary for some ops, perhaps even went back and forth
// a couple times.
ConcatenationExpr =
Expr '++' Expr concat:('++' Expr)*

BlockExpr =
'{'
statements:Stmt*
Expand Down Expand Up @@ -375,18 +362,10 @@ ExpressionList =
ReturnSignature =
'->' ScalarType

RetType =
'->' ScalarType

// Primitive declaration statements.

AliasDeclarationStatement =
'let' Name '=' AliasExpression ';'

// label 'concat' is to work around what is probably a bug
// Parsing this ungram fails with no good diagnostic without it.
AliasExpression =
Expr concat:('++' Expr)*
'let' Name '=' Expr ';'

MeasureExpression =
'measure' GateOperand
Expand Down Expand Up @@ -420,11 +399,6 @@ HardwareQubit =
ClassicalDeclarationStatement =
'const'? (ScalarType | ArrayType) Name ('=' Expr)? ';'

// We can just use ClassicalDeclarationStatement above.
// Or split these into {Scalar,Array}DeclarationStatement, where former includes `const`.
ConstDeclarationStatement =
'const' ScalarType Name '=' Expr ';'

IODeclarationStatement =
('input' | 'output') (ScalarType | ArrayType) Name ';'

Expand Down
Loading

0 comments on commit faea94e

Please sign in to comment.