mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-27 13:54:55 +00:00
Refactor Mode and partial parser/codegen for eval/exec
This commit is contained in:
parent
53c48bf6b9
commit
a66902406f
2 changed files with 35 additions and 0 deletions
|
@ -4,6 +4,9 @@
|
||||||
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
|
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
|
||||||
#![doc(html_root_url = "https://docs.rs/rustpython-bytecode/")]
|
#![doc(html_root_url = "https://docs.rs/rustpython-bytecode/")]
|
||||||
|
|
||||||
|
mod mode;
|
||||||
|
pub use mode::Mode;
|
||||||
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use bstr::ByteSlice;
|
use bstr::ByteSlice;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
32
bytecode/src/mode.rs
Normal file
32
bytecode/src/mode.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#[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