Skip to content

Commit 65df1fe

Browse files
committed
integrate ast::Location into compilre-core::Location
1 parent 0c456a5 commit 65df1fe

File tree

12 files changed

+34
-99
lines changed

12 files changed

+34
-99
lines changed

compiler/ast/asdl_rs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ def write_ast_def(mod, typeinfo, f):
645645
#![allow(clippy::derive_partial_eq_without_eq)]
646646
647647
pub use crate::constant::*;
648-
pub use crate::location::Location;
648+
pub use crate::Location;
649649
650650
type Ident = String;
651651
\n

compiler/ast/src/ast_gen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(clippy::derive_partial_eq_without_eq)]
44

55
pub use crate::constant::*;
6-
pub use crate::location::Location;
6+
pub use crate::Location;
77

88
type Ident = String;
99

compiler/ast/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ mod constant;
33
#[cfg(feature = "fold")]
44
mod fold_helpers;
55
mod impls;
6-
mod location;
76
#[cfg(feature = "unparse")]
87
mod unparse;
98

109
pub use ast_gen::*;
11-
pub use location::Location;
10+
pub use rustpython_compiler_core::Location;
1211

1312
pub type Suite<U = ()> = Vec<Stmt<U>>;

compiler/ast/src/location.rs

Lines changed: 0 additions & 63 deletions
This file was deleted.

compiler/codegen/src/compile.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use itertools::Itertools;
1515
use num_complex::Complex64;
1616
use num_traits::ToPrimitive;
1717
use rustpython_ast as ast;
18-
use rustpython_compiler_core::{self as bytecode, CodeObject, ConstantData, Instruction};
18+
use rustpython_compiler_core::{self as bytecode, CodeObject, ConstantData, Instruction, Location};
1919
use std::borrow::Cow;
2020

2121
pub use rustpython_compiler_core::Mode;
@@ -64,7 +64,7 @@ struct Compiler {
6464
code_stack: Vec<ir::CodeInfo>,
6565
symbol_table_stack: Vec<SymbolTable>,
6666
source_path: String,
67-
current_source_location: ast::Location,
67+
current_source_location: Location,
6868
qualified_path: Vec<String>,
6969
done_with_future_stmts: bool,
7070
future_annotations: bool,
@@ -220,7 +220,7 @@ impl Compiler {
220220
code_stack: vec![module_code],
221221
symbol_table_stack: Vec::new(),
222222
source_path,
223-
current_source_location: ast::Location::default(),
223+
current_source_location: Location::default(),
224224
qualified_path: Vec::new(),
225225
done_with_future_stmts: false,
226226
future_annotations: false,
@@ -237,7 +237,7 @@ impl Compiler {
237237
fn error(&self, error: CodegenErrorType) -> CodegenError {
238238
self.error_loc(error, self.current_source_location)
239239
}
240-
fn error_loc(&self, error: CodegenErrorType, location: ast::Location) -> CodegenError {
240+
fn error_loc(&self, error: CodegenErrorType, location: Location) -> CodegenError {
241241
CodegenError {
242242
error,
243243
location,
@@ -2617,7 +2617,7 @@ impl Compiler {
26172617
code.current_block = block;
26182618
}
26192619

2620-
fn set_source_location(&mut self, location: ast::Location) {
2620+
fn set_source_location(&mut self, location: Location) {
26212621
self.current_source_location = location;
26222622
}
26232623

@@ -2672,7 +2672,7 @@ fn try_get_constant_string(values: &[ast::Expr]) -> Option<String> {
26722672
}
26732673
}
26742674

2675-
fn compile_location(location: &ast::Location) -> bytecode::Location {
2675+
fn compile_location(location: &Location) -> bytecode::Location {
26762676
bytecode::Location::new(location.row(), location.column())
26772677
}
26782678

compiler/codegen/src/error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use rustpython_ast::Location;
2-
1+
use rustpython_compiler_core::Location;
32
use std::{error::Error, fmt};
43

54
#[derive(Debug)]
@@ -91,7 +90,7 @@ impl Error for CodegenErrorType {}
9190

9291
impl fmt::Display for CodegenError {
9392
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94-
write!(f, "{} at {}", self.error, self.location)
93+
self.location.fmt_with(f, &self.error)
9594
}
9695
}
9796

compiler/codegen/src/symboltable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use crate::{
1111
error::{CodegenError, CodegenErrorType},
1212
IndexMap,
1313
};
14-
use rustpython_ast::{self as ast, Location};
14+
use rustpython_ast as ast;
15+
use rustpython_compiler_core::Location;
1516
use std::{borrow::Cow, fmt};
1617

1718
/// Captures all symbols in the current scope, and has a list of subscopes in this scope.

compiler/core/src/location.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ pub struct Location {
77
pub(super) column: u32,
88
}
99

10+
impl Location {
11+
pub fn fmt_with(
12+
&self,
13+
f: &mut std::fmt::Formatter,
14+
e: &impl std::fmt::Display,
15+
) -> std::fmt::Result {
16+
write!(f, "{} at line {} column {}", e, self.row(), self.column())
17+
}
18+
}
19+
1020
impl Location {
1121
/// Creates a new Location object at the given row and column.
1222
///

compiler/parser/src/error.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
//! Define internal parse error types
22
//! The goal is to provide a matching and a safe error API, maksing errors from LALR
3-
use lalrpop_util::ParseError as LalrpopError;
4-
5-
use crate::ast::Location;
6-
use crate::token::Tok;
73
8-
use std::error::Error;
9-
use std::fmt;
4+
use crate::{ast::Location, token::Tok};
5+
use lalrpop_util::ParseError as LalrpopError;
6+
use std::{error::Error, fmt};
107

118
/// Represents an error during lexical scanning.
129
#[derive(Debug, PartialEq)]
@@ -186,7 +183,7 @@ impl ParseError {
186183

187184
impl fmt::Display for ParseError {
188185
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
189-
write!(f, "{} at {}", self.error, self.location)
186+
self.location.fmt_with(f, &self.error)
190187
}
191188
}
192189

compiler/parser/src/function.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use ahash::RandomState;
2-
use std::collections::HashSet;
3-
41
use crate::ast;
52
use crate::error::{LexicalError, LexicalErrorType};
3+
use ahash::RandomState;
4+
use std::collections::HashSet;
65

76
pub struct ArgumentList {
87
pub args: Vec<ast::Expr>,

0 commit comments

Comments
 (0)