mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Integrate CompileError to compiler-core::BaseError
This commit is contained in:
1 parent
31f95eeb1e
commit
9aacd144ca
6 files changed
+58
-27
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
pub type CodegenError = rustpython_compiler_core::Error<CodegenErrorType>;
|
||||
pub type CodegenError = rustpython_compiler_core::BaseError<CodegenErrorType>;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
|
||||
@@ -3,13 +3,13 @@ use std::fmt::Display;
|
||||
use crate::Location;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Error<T> {
|
||||
pub struct BaseError<T> {
|
||||
pub error: T,
|
||||
pub location: Location,
|
||||
pub source_path: String,
|
||||
}
|
||||
|
||||
impl<T> std::ops::Deref for Error<T> {
|
||||
impl<T> std::ops::Deref for BaseError<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@@ -17,7 +17,7 @@ impl<T> std::ops::Deref for Error<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::error::Error for Error<T>
|
||||
impl<T> std::error::Error for BaseError<T>
|
||||
where
|
||||
T: std::fmt::Display + std::fmt::Debug,
|
||||
{
|
||||
@@ -26,7 +26,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Display for Error<T>
|
||||
impl<T> Display for BaseError<T>
|
||||
where
|
||||
T: std::fmt::Display,
|
||||
{
|
||||
@@ -35,8 +35,26 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Error<T> {
|
||||
impl<T> BaseError<T> {
|
||||
pub fn error(self) -> T {
|
||||
self.error
|
||||
}
|
||||
|
||||
pub fn from<U>(obj: BaseError<U>) -> Self
|
||||
where
|
||||
U: Into<T>,
|
||||
{
|
||||
Self {
|
||||
error: obj.error.into(),
|
||||
location: obj.location,
|
||||
source_path: obj.source_path,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into<U>(self) -> BaseError<U>
|
||||
where
|
||||
T: Into<U>,
|
||||
{
|
||||
BaseError::from(self)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,6 @@ mod location;
|
||||
mod mode;
|
||||
|
||||
pub use bytecode::*;
|
||||
pub use error::Error;
|
||||
pub use error::BaseError;
|
||||
pub use location::Location;
|
||||
pub use mode::Mode;
|
||||
@@ -118,7 +118,7 @@ impl From<FStringError> for LalrpopError<Location, Tok, LexicalError> {
|
||||
|
||||
/// Represents an error during parsing
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct ParseError(rustpython_compiler_core::Error<ParseErrorType>);
|
||||
pub struct ParseError(rustpython_compiler_core::BaseError<ParseErrorType>);
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum ParseErrorType {
|
||||
@@ -134,7 +134,7 @@ pub enum ParseErrorType {
|
||||
Lexical(LexicalErrorType),
|
||||
}
|
||||
|
||||
impl From<ParseError> for rustpython_compiler_core::Error<ParseErrorType> {
|
||||
impl From<ParseError> for rustpython_compiler_core::BaseError<ParseErrorType> {
|
||||
fn from(err: ParseError) -> Self {
|
||||
err.0
|
||||
}
|
||||
@@ -149,7 +149,7 @@ impl From<ParseError> for ParseErrorType {
|
||||
/// Convert `lalrpop_util::ParseError` to our internal type
|
||||
impl ParseError {
|
||||
fn new(error: ParseErrorType, location: Location, source_path: String) -> Self {
|
||||
Self(rustpython_compiler_core::Error {
|
||||
Self(rustpython_compiler_core::BaseError {
|
||||
error,
|
||||
location,
|
||||
source_path,
|
||||
@@ -239,7 +239,7 @@ impl ParseErrorType {
|
||||
}
|
||||
|
||||
impl std::ops::Deref for ParseError {
|
||||
type Target = rustpython_compiler_core::Error<ParseErrorType>;
|
||||
type Target = rustpython_compiler_core::BaseError<ParseErrorType>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
|
||||
+18
-13
@@ -1,5 +1,5 @@
|
||||
use rustpython_codegen::{compile, symboltable};
|
||||
use rustpython_compiler_core::CodeObject;
|
||||
use rustpython_compiler_core::{BaseError, CodeObject};
|
||||
use rustpython_parser::{
|
||||
ast::{fold::Fold, ConstantOptimizer, Location},
|
||||
error::ParseErrorType,
|
||||
@@ -18,14 +18,21 @@ pub enum CompileErrorType {
|
||||
Parse(#[from] rustpython_parser::error::ParseErrorType),
|
||||
}
|
||||
|
||||
pub type CompileErrorBody = BaseError<CompileErrorType>;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub struct CompileError {
|
||||
pub error: CompileErrorType,
|
||||
pub source_path: String,
|
||||
pub location: Location,
|
||||
pub body: CompileErrorBody,
|
||||
pub statement: Option<String>,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for CompileError {
|
||||
type Target = CompileErrorBody;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.body
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CompileError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let loc = self.location;
|
||||
@@ -41,20 +48,18 @@ impl fmt::Display for CompileError {
|
||||
|
||||
impl CompileError {
|
||||
fn from_codegen(error: rustpython_codegen::error::CodegenError, source: &str) -> Self {
|
||||
let statement = get_statement(source, error.location);
|
||||
Self {
|
||||
error: error.error.into(),
|
||||
location: error.location,
|
||||
source_path: error.source_path,
|
||||
statement: get_statement(source, error.location),
|
||||
body: error.into(),
|
||||
statement,
|
||||
}
|
||||
}
|
||||
fn from_parse(error: rustpython_parser::error::ParseError, source: &str) -> Self {
|
||||
let error: rustpython_compiler_core::Error<ParseErrorType> = error.into();
|
||||
let error: rustpython_compiler_core::BaseError<ParseErrorType> = error.into();
|
||||
let statement = get_statement(source, error.location);
|
||||
Self {
|
||||
error: error.error.into(),
|
||||
location: error.location,
|
||||
source_path: error.source_path,
|
||||
statement: get_statement(source, error.location),
|
||||
body: error.into(),
|
||||
statement,
|
||||
}
|
||||
}
|
||||
fn from_symtable(
|
||||
|
||||
+11
-3
@@ -4,7 +4,7 @@ use rustpython_parser::error::{LexicalErrorType, ParseErrorType};
|
||||
use rustpython_vm::readline::{Readline, ReadlineResult};
|
||||
use rustpython_vm::{
|
||||
builtins::PyBaseExceptionRef,
|
||||
compile::{self, CompileError, CompileErrorType},
|
||||
compile::{self, CompileError, CompileErrorBody, CompileErrorType},
|
||||
scope::Scope,
|
||||
AsObject, PyResult, VirtualMachine,
|
||||
};
|
||||
@@ -22,11 +22,19 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResul
|
||||
Err(err) => ShellExecResult::PyErr(err),
|
||||
},
|
||||
Err(CompileError {
|
||||
error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)),
|
||||
body:
|
||||
CompileErrorBody {
|
||||
error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)),
|
||||
..
|
||||
},
|
||||
..
|
||||
})
|
||||
| Err(CompileError {
|
||||
error: CompileErrorType::Parse(ParseErrorType::Eof),
|
||||
body:
|
||||
CompileErrorBody {
|
||||
error: CompileErrorType::Parse(ParseErrorType::Eof),
|
||||
..
|
||||
},
|
||||
..
|
||||
}) => ShellExecResult::Continue,
|
||||
Err(err) => ShellExecResult::PyErr(vm.new_syntax_error(&err)),
|
||||
|
||||
Reference in new issue
Block a user