Skip to content

Commit 9ecbff8

Browse files
committed
Integrate ParseError to compiler-core::Error
1 parent 65df1fe commit 9ecbff8

File tree

8 files changed

+72
-33
lines changed

8 files changed

+72
-33
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ concurrency:
1515
env:
1616
CARGO_ARGS: --no-default-features --features stdlib,zlib,importlib,encodings,ssl,jit
1717
NON_WASM_PACKAGES: >-
18-
-p rustpython-bytecode
1918
-p rustpython-common
19+
-p rustpython-compiler-core
2020
-p rustpython-compiler
21+
-p rustpython-codegen
2122
-p rustpython-parser
2223
-p rustpython-vm
2324
-p rustpython-stdlib

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/core/src/error.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::Location;
2+
3+
#[derive(Debug, PartialEq, Eq)]
4+
pub struct Error<T> {
5+
pub error: T,
6+
pub location: Location,
7+
pub source_path: String,
8+
}
9+
10+
impl<T> std::ops::Deref for Error<T> {
11+
type Target = T;
12+
13+
fn deref(&self) -> &Self::Target {
14+
&self.error
15+
}
16+
}
17+
18+
impl<T> Error<T> {
19+
pub fn error(self) -> T {
20+
self.error
21+
}
22+
}

compiler/core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#![doc(html_root_url = "https://docs.rs/rustpython-compiler-core/")]
33

44
mod bytecode;
5+
mod error;
56
mod location;
67
mod mode;
78

89
pub use bytecode::*;
10+
pub use error::Error;
911
pub use location::Location;
1012
pub use mode::Mode;

compiler/parser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tiny-keccak = { version = "2", features = ["sha3"] }
1616

1717
[dependencies]
1818
rustpython-ast = { path = "../ast" }
19+
rustpython-compiler-core = { path = "../core" }
1920

2021
ahash = "0.7.6"
2122
itertools = "0.10.3"

compiler/parser/src/error.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ impl From<FStringError> for LalrpopError<Location, Tok, LexicalError> {
118118

119119
/// Represents an error during parsing
120120
#[derive(Debug, PartialEq)]
121-
pub struct ParseError {
122-
pub error: ParseErrorType,
123-
pub location: Location,
124-
pub source_path: String,
125-
}
121+
pub struct ParseError(rustpython_compiler_core::Error<ParseErrorType>);
126122

127123
#[derive(Debug, PartialEq)]
128124
pub enum ParseErrorType {
@@ -138,45 +134,59 @@ pub enum ParseErrorType {
138134
Lexical(LexicalErrorType),
139135
}
140136

137+
impl From<ParseError> for rustpython_compiler_core::Error<ParseErrorType> {
138+
fn from(err: ParseError) -> Self {
139+
err.0
140+
}
141+
}
142+
143+
impl From<ParseError> for ParseErrorType {
144+
fn from(err: ParseError) -> Self {
145+
err.0.error
146+
}
147+
}
148+
141149
/// Convert `lalrpop_util::ParseError` to our internal type
142150
impl ParseError {
151+
fn new(error: ParseErrorType, location: Location, source_path: String) -> Self {
152+
Self(rustpython_compiler_core::Error {
153+
error,
154+
location,
155+
source_path,
156+
})
157+
}
158+
143159
pub(crate) fn from_lalrpop(
144160
err: LalrpopError<Location, Tok, LexicalError>,
145161
source_path: &str,
146162
) -> Self {
147163
let source_path = source_path.to_owned();
148164
match err {
149165
// TODO: Are there cases where this isn't an EOF?
150-
LalrpopError::InvalidToken { location } => ParseError {
151-
error: ParseErrorType::Eof,
152-
location,
153-
source_path,
154-
},
155-
LalrpopError::ExtraToken { token } => ParseError {
156-
error: ParseErrorType::ExtraToken(token.1),
157-
location: token.0,
158-
source_path,
159-
},
160-
LalrpopError::User { error } => ParseError {
161-
error: ParseErrorType::Lexical(error.error),
162-
location: error.location,
166+
LalrpopError::InvalidToken { location } => {
167+
ParseError::new(ParseErrorType::Eof, location, source_path)
168+
}
169+
LalrpopError::ExtraToken { token } => {
170+
ParseError::new(ParseErrorType::ExtraToken(token.1), token.0, source_path)
171+
}
172+
LalrpopError::User { error } => ParseError::new(
173+
ParseErrorType::Lexical(error.error),
174+
error.location,
163175
source_path,
164-
},
176+
),
165177
LalrpopError::UnrecognizedToken { token, expected } => {
166178
// Hacky, but it's how CPython does it. See PyParser_AddToken,
167179
// in particular "Only one possible expected token" comment.
168180
let expected = (expected.len() == 1).then(|| expected[0].clone());
169-
ParseError {
170-
error: ParseErrorType::UnrecognizedToken(token.1, expected),
171-
location: token.0,
181+
ParseError::new(
182+
ParseErrorType::UnrecognizedToken(token.1, expected),
183+
token.0,
172184
source_path,
173-
}
185+
)
186+
}
187+
LalrpopError::UnrecognizedEOF { location, .. } => {
188+
ParseError::new(ParseErrorType::Eof, location, source_path)
174189
}
175-
LalrpopError::UnrecognizedEOF { location, .. } => ParseError {
176-
error: ParseErrorType::Eof,
177-
location,
178-
source_path,
179-
},
180190
}
181191
}
182192
}
@@ -229,9 +239,9 @@ impl ParseErrorType {
229239
}
230240

231241
impl std::ops::Deref for ParseError {
232-
type Target = ParseErrorType;
242+
type Target = rustpython_compiler_core::Error<ParseErrorType>;
233243
fn deref(&self) -> &Self::Target {
234-
&self.error
244+
&self.0
235245
}
236246
}
237247

compiler/parser/src/fstring.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'a> FStringParser<'a> {
186186
vec![self.expr(ExprKind::FormattedValue {
187187
value: Box::new(
188188
parse_fstring_expr(&expression)
189-
.map_err(|e| InvalidExpression(Box::new(e.error)))?,
189+
.map_err(|e| InvalidExpression(Box::new(e.into())))?,
190190
),
191191
conversion: conversion as _,
192192
format_spec: spec,
@@ -204,7 +204,7 @@ impl<'a> FStringParser<'a> {
204204
self.expr(ExprKind::FormattedValue {
205205
value: Box::new(
206206
parse_fstring_expr(&expression)
207-
.map_err(|e| InvalidExpression(Box::new(e.error)))?,
207+
.map_err(|e| InvalidExpression(Box::new(e.into())))?,
208208
),
209209
conversion: (if conversion == ConversionFlag::None && spec.is_none()
210210
{

compiler/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustpython_codegen::{compile, symboltable};
22
use rustpython_compiler_core::CodeObject;
33
use rustpython_parser::{
44
ast::{fold::Fold, ConstantOptimizer, Location},
5+
error::ParseErrorType,
56
parser,
67
};
78
use std::fmt;
@@ -48,6 +49,7 @@ impl CompileError {
4849
}
4950
}
5051
fn from_parse(error: rustpython_parser::error::ParseError, source: &str) -> Self {
52+
let error: rustpython_compiler_core::Error<ParseErrorType> = error.into();
5153
Self {
5254
error: error.error.into(),
5355
location: error.location,

0 commit comments

Comments
 (0)