|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use enum_iterator::Sequence; |
| 5 | +use std::{ |
| 6 | + fmt::{self, Display, Formatter}, |
| 7 | + str::FromStr, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Sequence)] |
| 11 | +pub enum Keyword { |
| 12 | + Box, |
| 13 | + Break, |
| 14 | + Cal, |
| 15 | + Case, |
| 16 | + Continue, |
| 17 | + Def, |
| 18 | + Default, |
| 19 | + Defcalgrammar, |
| 20 | + Else, |
| 21 | + End, |
| 22 | + Extern, |
| 23 | + For, |
| 24 | + Gate, |
| 25 | + If, |
| 26 | + In, |
| 27 | + Include, |
| 28 | + Let, |
| 29 | + OpenQASM, |
| 30 | + Pragma, |
| 31 | + Return, |
| 32 | + Switch, |
| 33 | + While, |
| 34 | +} |
| 35 | + |
| 36 | +impl Keyword { |
| 37 | + pub(super) fn as_str(self) -> &'static str { |
| 38 | + match self { |
| 39 | + Keyword::Box => "box", |
| 40 | + Keyword::Break => "break", |
| 41 | + Keyword::Cal => "cal", |
| 42 | + Keyword::Case => "case", |
| 43 | + Keyword::Continue => "continue", |
| 44 | + Keyword::Def => "def", |
| 45 | + Keyword::Default => "default", |
| 46 | + Keyword::Defcalgrammar => "defcalgrammar", |
| 47 | + Keyword::Else => "else", |
| 48 | + Keyword::End => "end", |
| 49 | + Keyword::Extern => "extern", |
| 50 | + Keyword::For => "for", |
| 51 | + Keyword::Gate => "gate", |
| 52 | + Keyword::If => "if", |
| 53 | + Keyword::In => "in", |
| 54 | + Keyword::Include => "include", |
| 55 | + Keyword::Let => "let", |
| 56 | + Keyword::OpenQASM => "openqasm", |
| 57 | + Keyword::Pragma => "pragma", |
| 58 | + Keyword::Return => "return", |
| 59 | + Keyword::Switch => "switch", |
| 60 | + Keyword::While => "while", |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl Display for Keyword { |
| 66 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 67 | + f.write_str(self.as_str()) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl FromStr for Keyword { |
| 72 | + type Err = (); |
| 73 | + |
| 74 | + // This is a hot function. Use a match expression so that the Rust compiler |
| 75 | + // can optimize the string comparisons better, and order the cases by |
| 76 | + // frequency in Q# so that fewer comparisons are needed on average. |
| 77 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 78 | + match s { |
| 79 | + "box" => Ok(Self::Box), |
| 80 | + "break" => Ok(Self::Break), |
| 81 | + "cal" => Ok(Self::Cal), |
| 82 | + "case" => Ok(Self::Case), |
| 83 | + "continue" => Ok(Self::Continue), |
| 84 | + "def" => Ok(Self::Def), |
| 85 | + "default" => Ok(Self::Default), |
| 86 | + "defcalgrammar" => Ok(Self::Defcalgrammar), |
| 87 | + "else" => Ok(Self::Else), |
| 88 | + "end" => Ok(Self::End), |
| 89 | + "extern" => Ok(Self::Extern), |
| 90 | + "for" => Ok(Self::For), |
| 91 | + "gate" => Ok(Self::Gate), |
| 92 | + "if" => Ok(Self::If), |
| 93 | + "in" => Ok(Self::In), |
| 94 | + "include" => Ok(Self::Include), |
| 95 | + "let" => Ok(Self::Let), |
| 96 | + "openqasm" => Ok(Self::OpenQASM), |
| 97 | + "pragma" => Ok(Self::Pragma), |
| 98 | + "return" => Ok(Self::Return), |
| 99 | + "switch" => Ok(Self::Switch), |
| 100 | + "while" => Ok(Self::While), |
| 101 | + _ => Err(()), |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments