microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
compiler/qsc_parse/src/keyword.rs
190lines · modecode
| 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(super) enum Keyword { |
| 12 | Adj, |
| 13 | Adjoint, |
| 14 | AdjointUpper, |
| 15 | And, |
| 16 | Apply, |
| 17 | As, |
| 18 | Auto, |
| 19 | Body, |
| 20 | Borrow, |
| 21 | Controlled, |
| 22 | ControlledUpper, |
| 23 | Ctl, |
| 24 | Distribute, |
| 25 | Elif, |
| 26 | Else, |
| 27 | Fail, |
| 28 | False, |
| 29 | Fixup, |
| 30 | For, |
| 31 | Function, |
| 32 | If, |
| 33 | In, |
| 34 | Internal, |
| 35 | Intrinsic, |
| 36 | Invert, |
| 37 | Is, |
| 38 | Let, |
| 39 | Mutable, |
| 40 | Namespace, |
| 41 | Newtype, |
| 42 | Not, |
| 43 | One, |
| 44 | Open, |
| 45 | Operation, |
| 46 | Or, |
| 47 | PauliI, |
| 48 | PauliX, |
| 49 | PauliY, |
| 50 | PauliZ, |
| 51 | Repeat, |
| 52 | Return, |
| 53 | Slf, |
| 54 | Set, |
| 55 | True, |
| 56 | Underscore, |
| 57 | Until, |
| 58 | Use, |
| 59 | While, |
| 60 | Within, |
| 61 | Zero, |
| 62 | } |
| 63 | |
| 64 | impl Keyword { |
| 65 | pub(super) fn as_str(self) -> &'static str { |
| 66 | match self { |
| 67 | Self::Adj => "Adj", |
| 68 | Self::Adjoint => "adjoint", |
| 69 | Self::AdjointUpper => "Adjoint", |
| 70 | Self::And => "and", |
| 71 | Self::Apply => "apply", |
| 72 | Self::As => "as", |
| 73 | Self::Auto => "auto", |
| 74 | Self::Body => "body", |
| 75 | Self::Borrow => "borrow", |
| 76 | Self::Controlled => "controlled", |
| 77 | Self::ControlledUpper => "Controlled", |
| 78 | Self::Ctl => "Ctl", |
| 79 | Self::Distribute => "distribute", |
| 80 | Self::Elif => "elif", |
| 81 | Self::Else => "else", |
| 82 | Self::Fail => "fail", |
| 83 | Self::False => "false", |
| 84 | Self::Fixup => "fixup", |
| 85 | Self::For => "for", |
| 86 | Self::Function => "function", |
| 87 | Self::If => "if", |
| 88 | Self::In => "in", |
| 89 | Self::Internal => "internal", |
| 90 | Self::Intrinsic => "intrinsic", |
| 91 | Self::Invert => "invert", |
| 92 | Self::Is => "is", |
| 93 | Self::Let => "let", |
| 94 | Self::Mutable => "mutable", |
| 95 | Self::Namespace => "namespace", |
| 96 | Self::Newtype => "newtype", |
| 97 | Self::Not => "not", |
| 98 | Self::One => "One", |
| 99 | Self::Open => "open", |
| 100 | Self::Operation => "operation", |
| 101 | Self::Or => "or", |
| 102 | Self::PauliI => "PauliI", |
| 103 | Self::PauliX => "PauliX", |
| 104 | Self::PauliY => "PauliY", |
| 105 | Self::PauliZ => "PauliZ", |
| 106 | Self::Repeat => "repeat", |
| 107 | Self::Return => "return", |
| 108 | Self::Slf => "self", |
| 109 | Self::Set => "set", |
| 110 | Self::True => "true", |
| 111 | Self::Underscore => "_", |
| 112 | Self::Until => "until", |
| 113 | Self::Use => "use", |
| 114 | Self::While => "while", |
| 115 | Self::Within => "within", |
| 116 | Self::Zero => "Zero", |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl Display for Keyword { |
| 122 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 123 | f.write_str(self.as_str()) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl FromStr for Keyword { |
| 128 | type Err = (); |
| 129 | |
| 130 | // This is a hot function. Use a match expression so that the Rust compiler |
| 131 | // can optimize the string comparisons better, and order the cases by |
| 132 | // frequency in Q# so that fewer comparisons are needed on average. |
| 133 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 134 | match s { |
| 135 | "let" => Ok(Self::Let), |
| 136 | "operation" => Ok(Self::Operation), |
| 137 | "is" => Ok(Self::Is), |
| 138 | "in" => Ok(Self::In), |
| 139 | "and" => Ok(Self::And), |
| 140 | "for" => Ok(Self::For), |
| 141 | "function" => Ok(Self::Function), |
| 142 | "open" => Ok(Self::Open), |
| 143 | "if" => Ok(Self::If), |
| 144 | "return" => Ok(Self::Return), |
| 145 | "Adj" => Ok(Self::Adj), |
| 146 | "Controlled" => Ok(Self::ControlledUpper), |
| 147 | "controlled" => Ok(Self::Controlled), |
| 148 | "Ctl" => Ok(Self::Ctl), |
| 149 | "set" => Ok(Self::Set), |
| 150 | "use" => Ok(Self::Use), |
| 151 | "as" => Ok(Self::As), |
| 152 | "not" => Ok(Self::Not), |
| 153 | "true" => Ok(Self::True), |
| 154 | "Zero" => Ok(Self::Zero), |
| 155 | "One" => Ok(Self::One), |
| 156 | "namespace" => Ok(Self::Namespace), |
| 157 | "mutable" => Ok(Self::Mutable), |
| 158 | "internal" => Ok(Self::Internal), |
| 159 | "PauliZ" => Ok(Self::PauliZ), |
| 160 | "false" => Ok(Self::False), |
| 161 | "PauliX" => Ok(Self::PauliX), |
| 162 | "PauliI" => Ok(Self::PauliI), |
| 163 | "Adjoint" => Ok(Self::AdjointUpper), |
| 164 | "adjoint" => Ok(Self::Adjoint), |
| 165 | "apply" => Ok(Self::Apply), |
| 166 | "intrinsic" => Ok(Self::Intrinsic), |
| 167 | "or" => Ok(Self::Or), |
| 168 | "elif" => Ok(Self::Elif), |
| 169 | "fail" => Ok(Self::Fail), |
| 170 | "else" => Ok(Self::Else), |
| 171 | "within" => Ok(Self::Within), |
| 172 | "body" => Ok(Self::Body), |
| 173 | "newtype" => Ok(Self::Newtype), |
| 174 | "invert" => Ok(Self::Invert), |
| 175 | "distribute" => Ok(Self::Distribute), |
| 176 | "auto" => Ok(Self::Auto), |
| 177 | "self" => Ok(Self::Slf), |
| 178 | "while" => Ok(Self::While), |
| 179 | "until" => Ok(Self::Until), |
| 180 | "repeat" => Ok(Self::Repeat), |
| 181 | "fixup" => Ok(Self::Fixup), |
| 182 | // The next three were not found or measured |
| 183 | // in the standard library for priority order. |
| 184 | "PauliY" => Ok(Self::PauliY), |
| 185 | "borrow" => Ok(Self::Borrow), |
| 186 | "_" => Ok(Self::Underscore), |
| 187 | _ => Err(()), |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |