mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-03 18:29:04 +00:00
Refactor Mode and partial parser/codegen for eval/exec
This commit is contained in:
parent
9c229ebb99
commit
3690272256
5 changed files with 7 additions and 2 deletions
|
@ -18,7 +18,7 @@ use rustpython_ast as ast;
|
|||
use rustpython_bytecode::{self as bytecode, CodeObject, ConstantData, Instruction};
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub use crate::mode::Mode;
|
||||
pub use rustpython_bytecode::Mode;
|
||||
|
||||
type CompileResult<T> = Result<T, CompileError>;
|
||||
|
||||
|
|
|
@ -11,7 +11,6 @@ type IndexSet<T> = indexmap::IndexSet<T, ahash::RandomState>;
|
|||
pub mod compile;
|
||||
pub mod error;
|
||||
pub mod ir;
|
||||
pub mod mode;
|
||||
pub mod symboltable;
|
||||
|
||||
pub use compile::{CompileOpts, Mode};
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
#[derive(Clone, Copy)]
|
||||
pub enum Mode {
|
||||
Exec,
|
||||
Eval,
|
||||
Single,
|
||||
BlockExpr,
|
||||
}
|
||||
|
||||
impl std::str::FromStr for Mode {
|
||||
type Err = ModeParseError;
|
||||
|
||||
// To support `builtins.compile()` `mode` argument
|
||||
fn from_str(s: &str) -> Result<Self, ModeParseError> {
|
||||
match s {
|
||||
"exec" => Ok(Mode::Exec),
|
||||
"eval" => Ok(Mode::Eval),
|
||||
"single" => Ok(Mode::Single),
|
||||
_ => Err(ModeParseError { _priv: () }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ModeParseError {
|
||||
_priv: (),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ModeParseError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, r#"mode should be "exec", "eval", or "single""#)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue