mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-15 17:15:24 +00:00
commit
31b6e815f6
3 changed files with 42 additions and 30 deletions
|
@ -6,6 +6,7 @@
|
||||||
//! https://github.com/micropython/micropython/blob/master/py/compile.c
|
//! https://github.com/micropython/micropython/blob/master/py/compile.c
|
||||||
|
|
||||||
use crate::error::{CompileError, CompileErrorType};
|
use crate::error::{CompileError, CompileErrorType};
|
||||||
|
pub use crate::mode::Mode;
|
||||||
use crate::output_stream::{CodeObjectStream, OutputStream};
|
use crate::output_stream::{CodeObjectStream, OutputStream};
|
||||||
use crate::peephole::PeepholeOptimizer;
|
use crate::peephole::PeepholeOptimizer;
|
||||||
use crate::symboltable::{
|
use crate::symboltable::{
|
||||||
|
@ -105,36 +106,6 @@ pub fn compile_program_single(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub enum Mode {
|
|
||||||
Exec,
|
|
||||||
Eval,
|
|
||||||
Single,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::str::FromStr for Mode {
|
|
||||||
type Err = ModeParseError;
|
|
||||||
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""#)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<O> Default for Compiler<O>
|
impl<O> Default for Compiler<O>
|
||||||
where
|
where
|
||||||
O: OutputStream,
|
O: OutputStream,
|
||||||
|
|
|
@ -8,6 +8,7 @@ extern crate log;
|
||||||
|
|
||||||
pub mod compile;
|
pub mod compile;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod mode;
|
||||||
pub(crate) mod output_stream;
|
pub(crate) mod output_stream;
|
||||||
pub mod peephole;
|
pub mod peephole;
|
||||||
pub mod symboltable;
|
pub mod symboltable;
|
||||||
|
|
40
src/mode.rs
Normal file
40
src/mode.rs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
use rustpython_parser::parser;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub enum Mode {
|
||||||
|
Exec,
|
||||||
|
Eval,
|
||||||
|
Single,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::str::FromStr for Mode {
|
||||||
|
type Err = ModeParseError;
|
||||||
|
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: () }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mode {
|
||||||
|
pub fn to_parser_mode(self) -> parser::Mode {
|
||||||
|
match self {
|
||||||
|
Self::Exec | Self::Single => parser::Mode::Program,
|
||||||
|
Self::Eval => parser::Mode::Statement,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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