From 107c4496205b05c7f58e0b020f16e90fd97a99f0 Mon Sep 17 00:00:00 2001 From: kalzoo <22137047+kalzoo@users.noreply.github.com> Date: Fri, 9 Jul 2021 13:45:19 -0700 Subject: [PATCH] Breaking: refine public API --- src/lib.rs | 2 +- src/parser/common.rs | 7 +------ src/parser/error.rs | 14 -------------- src/parser/lexer.rs | 11 ----------- src/parser/mod.rs | 17 ++++++++++------- src/program/graph.rs | 19 ++++++++++--------- src/program/mod.rs | 20 ++++++++++---------- 7 files changed, 32 insertions(+), 58 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7496eac3..e5351432 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,5 +16,5 @@ pub mod expression; pub mod instruction; mod macros; -pub mod parser; +pub (crate) mod parser; pub mod program; diff --git a/src/parser/common.rs b/src/parser/common.rs index 42b460e0..108ed27d 100644 --- a/src/parser/common.rs +++ b/src/parser/common.rs @@ -200,9 +200,4 @@ pub fn skip_newlines_and_comments<'a>(input: ParserInput<'a>) -> ParserResult<'a token!(Semicolon), )))(input)?; Ok((input, ())) -} - -/// Parse ahead to the next non-newline token. -pub fn skip_newlines<'a>(input: ParserInput<'a>) -> ParserResult<'a, ()> { - many0(token!(NewLine))(input).map(|(input, _)| (input, ())) -} +} \ No newline at end of file diff --git a/src/parser/error.rs b/src/parser/error.rs index 301cdd8e..3596c9c3 100644 --- a/src/parser/error.rs +++ b/src/parser/error.rs @@ -43,17 +43,6 @@ pub enum ErrorKind { actual: Token, expected: String, }, - /// An unknown identifier was encountered - UnknownIdentifier, - - /// An invalid literal was encountered. - /// - /// When encountered, this generally means a bug exists in the data that - /// was passed in or the parsing logic. - InvalidLiteral, - - /// A full parse was requested, but data was left over after parsing finished. - Partial, /// Tried to parse a kind of command and couldn't /// TODO: Wrap actual error, the string is a lifetime cop-out @@ -62,9 +51,6 @@ pub enum ErrorKind { error: String, }, - /// Tried to parse a gate and couldn't - InvalidGate, - /// Unexpected start of an instruction NotACommandOrGate, diff --git a/src/parser/lexer.rs b/src/parser/lexer.rs index 96f11d61..008b4c00 100644 --- a/src/parser/lexer.rs +++ b/src/parser/lexer.rs @@ -41,11 +41,9 @@ pub enum Token { LBracket, LParenthesis, NonBlocking, - MathematicalFunction(MathematicalFunction), Matrix, Modifier(Modifier), NewLine, - Offset, Operator(Operator), Permutation, RBracket, @@ -121,15 +119,6 @@ pub enum DataType { Integer, } -#[derive(Debug, Clone, PartialEq)] -pub enum MathematicalFunction { - Cis, - Cos, - Exp, - Sin, - Sqrt, -} - #[derive(Debug, Clone, PartialEq)] pub enum Modifier { Controlled, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 6c506328..aafac9fb 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -17,16 +17,19 @@ mod command; mod gate; mod macros; -pub mod common; -pub mod error; -pub mod expression; -pub mod instruction; -pub mod lexer; +mod common; +mod error; +mod expression; +mod instruction; +mod lexer; + +pub (crate) use instruction::parse_instructions; +pub (crate) use lexer::lex; use nom::IResult; use error::Error; use lexer::Token; -pub type ParserInput<'a> = &'a [Token]; -pub type ParserResult<'a, R> = IResult<&'a [Token], R, Error<&'a [Token]>>; +type ParserInput<'a> = &'a [Token]; +type ParserResult<'a, R> = IResult<&'a [Token], R, Error<&'a [Token]>>; diff --git a/src/program/graph.rs b/src/program/graph.rs index 54594e1f..ff420590 100644 --- a/src/program/graph.rs +++ b/src/program/graph.rs @@ -26,11 +26,12 @@ use indexmap::IndexMap; #[derive(Debug, Clone)] pub enum ScheduleErrorVariant { DuplicateLabel, - DurationNotRealConstant, - DurationNotApplicable, - InvalidFrame, UncalibratedInstruction, - UnscheduleableInstruction, + UnscheduableInstruction, + // Note: these may be restored once enforced + // DurationNotRealConstant, + // DurationNotApplicable, + // InvalidFrame, } #[derive(Debug, Clone)] @@ -39,7 +40,7 @@ pub struct ScheduleError { variant: ScheduleErrorVariant, } -type ScheduleResult = Result; +pub type ScheduleResult = Result; #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Hash, Ord)] pub enum ScheduledGraphNode { @@ -211,7 +212,7 @@ impl InstructionBlock { InstructionRole::RFControl => { let frames = Self::get_frames(instruction, program).ok_or(ScheduleError { instruction: instruction.clone(), - variant: ScheduleErrorVariant::UnscheduleableInstruction, + variant: ScheduleErrorVariant::UnscheduableInstruction, })?; // Mark a dependency on @@ -225,11 +226,11 @@ impl InstructionBlock { } InstructionRole::ControlFlow => Err(ScheduleError { instruction: instruction.clone(), - variant: ScheduleErrorVariant::UnscheduleableInstruction, + variant: ScheduleErrorVariant::UnscheduableInstruction, }), InstructionRole::ProgramComposition => Err(ScheduleError { instruction: instruction.clone(), - variant: ScheduleErrorVariant::UnscheduleableInstruction, + variant: ScheduleErrorVariant::UnscheduableInstruction, }), }?; @@ -413,7 +414,7 @@ impl ScheduledProgram { | Instruction::MeasureCalibrationDefinition { .. } | Instruction::WaveformDefinition { .. } => Err(ScheduleError { instruction: instruction.clone(), - variant: ScheduleErrorVariant::UnscheduleableInstruction, + variant: ScheduleErrorVariant::UnscheduableInstruction, }), Instruction::Pragma { .. } => { diff --git a/src/program/mod.rs b/src/program/mod.rs index 652f2f02..f42a2499 100644 --- a/src/program/mod.rs +++ b/src/program/mod.rs @@ -16,20 +16,20 @@ use std::collections::HashMap; use std::str::FromStr; -use frame::FrameSet; - use crate::{ instruction::{FrameIdentifier, Instruction, Waveform}, - parser::{instruction::parse_instructions, lexer}, + parser::{lex, parse_instructions}, }; -use self::calibration::CalibrationSet; -use self::memory::MemoryRegion; +mod calibration; +mod frame; +mod graph; +mod memory; -pub mod calibration; -pub mod frame; -pub mod graph; -pub mod memory; +pub use self::calibration::CalibrationSet; +pub use self::frame::FrameSet; +pub use self::graph::{InstructionBlock, ScheduleError, ScheduleResult, ScheduledProgram}; +pub use self::memory::MemoryRegion; /// A Quil Program instance describes a quantum program with metadata used in execution. /// @@ -170,7 +170,7 @@ impl Program { impl FromStr for Program { type Err = nom::Err; fn from_str(s: &str) -> Result { - let lexed = lexer::lex(s); + let lexed = lex(s); let (_, instructions) = parse_instructions(&lexed).map_err(|err| match err { nom::Err::Incomplete(_) => nom::Err::Error("incomplete".to_owned()), nom::Err::Error(error) => nom::Err::Error(format!("{:?}", error)),