From 904fc477f19a8dc208f4bd1199ef5aed2fb86ecb Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 22 Aug 2022 08:38:21 +0900 Subject: [PATCH] integrate ast::Location into compilre-core::Location --- ast/asdl_rs.py | 2 +- ast/src/ast_gen.rs | 2 +- ast/src/lib.rs | 3 +- ast/src/location.rs | 63 ------------------------------------------ core/src/location.rs | 10 +++++++ parser/src/error.rs | 11 +++----- parser/src/function.rs | 5 ++-- parser/src/parser.rs | 8 ++---- 8 files changed, 21 insertions(+), 83 deletions(-) delete mode 100644 ast/src/location.rs diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index a4c894a..71c9032 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -645,7 +645,7 @@ def write_ast_def(mod, typeinfo, f): #![allow(clippy::derive_partial_eq_without_eq)] pub use crate::constant::*; - pub use crate::location::Location; + pub use crate::Location; type Ident = String; \n diff --git a/ast/src/ast_gen.rs b/ast/src/ast_gen.rs index fda39b8..17900fe 100644 --- a/ast/src/ast_gen.rs +++ b/ast/src/ast_gen.rs @@ -3,7 +3,7 @@ #![allow(clippy::derive_partial_eq_without_eq)] pub use crate::constant::*; -pub use crate::location::Location; +pub use crate::Location; type Ident = String; diff --git a/ast/src/lib.rs b/ast/src/lib.rs index b696998..d668bed 100644 --- a/ast/src/lib.rs +++ b/ast/src/lib.rs @@ -3,11 +3,10 @@ mod constant; #[cfg(feature = "fold")] mod fold_helpers; mod impls; -mod location; #[cfg(feature = "unparse")] mod unparse; pub use ast_gen::*; -pub use location::Location; +pub use rustpython_compiler_core::Location; pub type Suite = Vec>; diff --git a/ast/src/location.rs b/ast/src/location.rs deleted file mode 100644 index 9584320..0000000 --- a/ast/src/location.rs +++ /dev/null @@ -1,63 +0,0 @@ -//! Datatypes to support source location information. - -use std::fmt; - -/// A location somewhere in the sourcecode. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct Location(rustpython_compiler_core::Location); - -impl std::ops::Deref for Location { - type Target = rustpython_compiler_core::Location; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl std::ops::DerefMut for Location { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -impl fmt::Display for Location { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "line {} column {}", self.row(), self.column()) - } -} - -impl Location { - pub fn visualize<'a>( - &self, - line: &'a str, - desc: impl fmt::Display + 'a, - ) -> impl fmt::Display + 'a { - struct Visualize<'a, D: fmt::Display> { - loc: Location, - line: &'a str, - desc: D, - } - impl fmt::Display for Visualize<'_, D> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}\n{}{arrow:>pad$}", - self.desc, - self.line, - pad = self.loc.column(), - arrow = "^", - ) - } - } - Visualize { - loc: *self, - line, - desc, - } - } -} - -impl Location { - pub fn new(row: usize, column: usize) -> Self { - Location(rustpython_compiler_core::Location::new(row, column)) - } -} diff --git a/core/src/location.rs b/core/src/location.rs index 1763763..fedcdcf 100644 --- a/core/src/location.rs +++ b/core/src/location.rs @@ -7,6 +7,16 @@ pub struct Location { pub(super) column: u32, } +impl Location { + pub fn fmt_with( + &self, + f: &mut std::fmt::Formatter, + e: &impl std::fmt::Display, + ) -> std::fmt::Result { + write!(f, "{} at line {} column {}", e, self.row(), self.column()) + } +} + impl Location { /// Creates a new Location object at the given row and column. /// diff --git a/parser/src/error.rs b/parser/src/error.rs index d256e2a..76f02dc 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -1,12 +1,9 @@ //! Define internal parse error types //! The goal is to provide a matching and a safe error API, maksing errors from LALR + +use crate::{ast::Location, token::Tok}; use lalrpop_util::ParseError as LalrpopError; - -use crate::ast::Location; -use crate::token::Tok; - -use std::error::Error; -use std::fmt; +use std::{error::Error, fmt}; /// Represents an error during lexical scanning. #[derive(Debug, PartialEq)] @@ -186,7 +183,7 @@ impl ParseError { impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{} at {}", self.error, self.location) + self.location.fmt_with(f, &self.error) } } diff --git a/parser/src/function.rs b/parser/src/function.rs index 68d890f..2a384dd 100644 --- a/parser/src/function.rs +++ b/parser/src/function.rs @@ -1,8 +1,7 @@ -use ahash::RandomState; -use std::collections::HashSet; - use crate::ast; use crate::error::{LexicalError, LexicalErrorType}; +use ahash::RandomState; +use std::collections::HashSet; pub struct ArgumentList { pub args: Vec, diff --git a/parser/src/parser.rs b/parser/src/parser.rs index 3199701..ff86ef4 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -5,13 +5,9 @@ //! parse a whole program, a single statement, or a single //! expression. -use std::iter; - -use crate::ast; -use crate::error::ParseError; -use crate::lexer; pub use crate::mode::Mode; -use crate::python; +use crate::{ast, error::ParseError, lexer, python}; +use std::iter; /* * Parse python code.