@@ -9,7 +9,7 @@ mod tests;
9
9
10
10
use super :: {
11
11
keyword:: Keyword ,
12
- prim:: { ident, keyword , opt, pat, path, seq, token} ,
12
+ prim:: { ident, opt, pat, path, seq, token} ,
13
13
scan:: Scanner ,
14
14
stmt, Error , Result ,
15
15
} ;
@@ -158,46 +158,46 @@ fn expr_base(s: &mut Scanner) -> Result<Box<Expr>> {
158
158
) ) )
159
159
} else if token ( s, TokenKind :: DotDotDot ) . is_ok ( ) {
160
160
expr_range_prefix ( s)
161
- } else if keyword ( s, Keyword :: Underscore ) . is_ok ( ) {
161
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: Underscore ) ) . is_ok ( ) {
162
162
Ok ( Box :: new ( ExprKind :: Hole ) )
163
- } else if keyword ( s, Keyword :: Fail ) . is_ok ( ) {
163
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: Fail ) ) . is_ok ( ) {
164
164
Ok ( Box :: new ( ExprKind :: Fail ( expr ( s) ?) ) )
165
- } else if keyword ( s, Keyword :: For ) . is_ok ( ) {
165
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: For ) ) . is_ok ( ) {
166
166
let vars = pat ( s) ?;
167
- keyword ( s, Keyword :: In ) ?;
167
+ token ( s, TokenKind :: Keyword ( Keyword :: In ) ) ?;
168
168
let iter = expr ( s) ?;
169
- let body = stmt:: block ( s) ?;
169
+ let body = stmt:: parse_block ( s) ?;
170
170
Ok ( Box :: new ( ExprKind :: For ( vars, iter, body) ) )
171
- } else if keyword ( s, Keyword :: If ) . is_ok ( ) {
171
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: If ) ) . is_ok ( ) {
172
172
expr_if ( s)
173
173
} else if let Some ( components) = opt ( s, expr_interpolate) ? {
174
174
Ok ( Box :: new ( ExprKind :: Interpolate (
175
175
components. into_boxed_slice ( ) ,
176
176
) ) )
177
- } else if keyword ( s, Keyword :: Repeat ) . is_ok ( ) {
178
- let body = stmt:: block ( s) ?;
179
- keyword ( s, Keyword :: Until ) ?;
177
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: Repeat ) ) . is_ok ( ) {
178
+ let body = stmt:: parse_block ( s) ?;
179
+ token ( s, TokenKind :: Keyword ( Keyword :: Until ) ) ?;
180
180
let cond = expr ( s) ?;
181
- let fixup = if keyword ( s, Keyword :: Fixup ) . is_ok ( ) {
182
- Some ( stmt:: block ( s) ?)
181
+ let fixup = if token ( s, TokenKind :: Keyword ( Keyword :: Fixup ) ) . is_ok ( ) {
182
+ Some ( stmt:: parse_block ( s) ?)
183
183
} else {
184
184
None
185
185
} ;
186
186
Ok ( Box :: new ( ExprKind :: Repeat ( body, cond, fixup) ) )
187
- } else if keyword ( s, Keyword :: Return ) . is_ok ( ) {
187
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: Return ) ) . is_ok ( ) {
188
188
Ok ( Box :: new ( ExprKind :: Return ( expr ( s) ?) ) )
189
- } else if keyword ( s, Keyword :: Set ) . is_ok ( ) {
189
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: Set ) ) . is_ok ( ) {
190
190
expr_set ( s)
191
- } else if keyword ( s, Keyword :: While ) . is_ok ( ) {
192
- Ok ( Box :: new ( ExprKind :: While ( expr ( s) ?, stmt:: block ( s) ?) ) )
193
- } else if keyword ( s, Keyword :: Within ) . is_ok ( ) {
194
- let outer = stmt:: block ( s) ?;
195
- keyword ( s, Keyword :: Apply ) ?;
196
- let inner = stmt:: block ( s) ?;
191
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: While ) ) . is_ok ( ) {
192
+ Ok ( Box :: new ( ExprKind :: While ( expr ( s) ?, stmt:: parse_block ( s) ?) ) )
193
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: Within ) ) . is_ok ( ) {
194
+ let outer = stmt:: parse_block ( s) ?;
195
+ token ( s, TokenKind :: Keyword ( Keyword :: Apply ) ) ?;
196
+ let inner = stmt:: parse_block ( s) ?;
197
197
Ok ( Box :: new ( ExprKind :: Conjugate ( outer, inner) ) )
198
198
} else if let Some ( a) = opt ( s, expr_array) ? {
199
199
Ok ( a)
200
- } else if let Some ( b) = opt ( s, stmt:: block ) ? {
200
+ } else if let Some ( b) = opt ( s, stmt:: parse_block ) ? {
201
201
Ok ( Box :: new ( ExprKind :: Block ( b) ) )
202
202
} else if let Some ( l) = lit ( s) ? {
203
203
Ok ( Box :: new ( ExprKind :: Lit ( Box :: new ( l) ) ) )
@@ -220,13 +220,13 @@ fn expr_base(s: &mut Scanner) -> Result<Box<Expr>> {
220
220
221
221
fn expr_if ( s : & mut Scanner ) -> Result < Box < ExprKind > > {
222
222
let cond = expr ( s) ?;
223
- let body = stmt:: block ( s) ?;
223
+ let body = stmt:: parse_block ( s) ?;
224
224
let lo = s. peek ( ) . span . lo ;
225
225
226
- let otherwise = if keyword ( s, Keyword :: Elif ) . is_ok ( ) {
226
+ let otherwise = if token ( s, TokenKind :: Keyword ( Keyword :: Elif ) ) . is_ok ( ) {
227
227
Some ( expr_if ( s) ?)
228
- } else if keyword ( s, Keyword :: Else ) . is_ok ( ) {
229
- Some ( Box :: new ( ExprKind :: Block ( stmt:: block ( s) ?) ) )
228
+ } else if token ( s, TokenKind :: Keyword ( Keyword :: Else ) ) . is_ok ( ) {
229
+ Some ( Box :: new ( ExprKind :: Block ( stmt:: parse_block ( s) ?) ) )
230
230
} else {
231
231
None
232
232
}
@@ -357,14 +357,7 @@ fn lit(s: &mut Scanner) -> Result<Option<Lit>> {
357
357
s. advance ( ) ;
358
358
Ok ( Some ( lit) )
359
359
}
360
- Ok ( None ) if token. kind != TokenKind :: Ident => Ok ( None ) ,
361
- Ok ( None ) => match lit_keyword ( lexeme) {
362
- Some ( lit) => {
363
- s. advance ( ) ;
364
- Ok ( Some ( lit) )
365
- }
366
- None => Ok ( None ) ,
367
- } ,
360
+ Ok ( None ) => Ok ( None ) ,
368
361
Err ( err) => {
369
362
s. advance ( ) ;
370
363
Err ( err)
@@ -410,6 +403,14 @@ fn lit_token(lexeme: &str, token: Token) -> Result<Option<Lit>> {
410
403
} ) ?;
411
404
Ok ( Some ( Lit :: String ( string. into ( ) ) ) )
412
405
}
406
+ TokenKind :: Keyword ( Keyword :: True ) => Ok ( Some ( Lit :: Bool ( true ) ) ) ,
407
+ TokenKind :: Keyword ( Keyword :: Zero ) => Ok ( Some ( Lit :: Result ( ast:: Result :: Zero ) ) ) ,
408
+ TokenKind :: Keyword ( Keyword :: One ) => Ok ( Some ( Lit :: Result ( ast:: Result :: One ) ) ) ,
409
+ TokenKind :: Keyword ( Keyword :: PauliZ ) => Ok ( Some ( Lit :: Pauli ( Pauli :: Z ) ) ) ,
410
+ TokenKind :: Keyword ( Keyword :: False ) => Ok ( Some ( Lit :: Bool ( false ) ) ) ,
411
+ TokenKind :: Keyword ( Keyword :: PauliX ) => Ok ( Some ( Lit :: Pauli ( Pauli :: X ) ) ) ,
412
+ TokenKind :: Keyword ( Keyword :: PauliI ) => Ok ( Some ( Lit :: Pauli ( Pauli :: I ) ) ) ,
413
+ TokenKind :: Keyword ( Keyword :: PauliY ) => Ok ( Some ( Lit :: Pauli ( Pauli :: Y ) ) ) ,
413
414
_ => Ok ( None ) ,
414
415
}
415
416
}
@@ -426,22 +427,6 @@ fn lit_int(lexeme: &str, radix: u32) -> Option<i64> {
426
427
. map ( |( Wrapping ( value) , _) | value)
427
428
}
428
429
429
- #[ allow( clippy:: inline_always) ]
430
- #[ inline( always) ]
431
- fn lit_keyword ( lexeme : & str ) -> Option < Lit > {
432
- match lexeme {
433
- "true" => Some ( Lit :: Bool ( true ) ) ,
434
- "Zero" => Some ( Lit :: Result ( ast:: Result :: Zero ) ) ,
435
- "One" => Some ( Lit :: Result ( ast:: Result :: One ) ) ,
436
- "PauliZ" => Some ( Lit :: Pauli ( Pauli :: Z ) ) ,
437
- "false" => Some ( Lit :: Bool ( false ) ) ,
438
- "PauliX" => Some ( Lit :: Pauli ( Pauli :: X ) ) ,
439
- "PauliI" => Some ( Lit :: Pauli ( Pauli :: I ) ) ,
440
- "PauliY" => Some ( Lit :: Pauli ( Pauli :: Y ) ) ,
441
- _ => None ,
442
- }
443
- }
444
-
445
430
fn prefix_op ( name : OpName ) -> Option < PrefixOp > {
446
431
match name {
447
432
OpName :: Keyword ( Keyword :: Not ) => Some ( PrefixOp {
0 commit comments