integrate ast::Location into compilre-core::Location

This commit is contained in:
Jeong YunWon 2022-08-22 08:38:21 +09:00
parent bfac0355dc
commit 904fc477f1
8 changed files with 21 additions and 83 deletions

View file

@ -645,7 +645,7 @@ def write_ast_def(mod, typeinfo, f):
#![allow(clippy::derive_partial_eq_without_eq)] #![allow(clippy::derive_partial_eq_without_eq)]
pub use crate::constant::*; pub use crate::constant::*;
pub use crate::location::Location; pub use crate::Location;
type Ident = String; type Ident = String;
\n \n

View file

@ -3,7 +3,7 @@
#![allow(clippy::derive_partial_eq_without_eq)] #![allow(clippy::derive_partial_eq_without_eq)]
pub use crate::constant::*; pub use crate::constant::*;
pub use crate::location::Location; pub use crate::Location;
type Ident = String; type Ident = String;

View file

@ -3,11 +3,10 @@ mod constant;
#[cfg(feature = "fold")] #[cfg(feature = "fold")]
mod fold_helpers; mod fold_helpers;
mod impls; mod impls;
mod location;
#[cfg(feature = "unparse")] #[cfg(feature = "unparse")]
mod unparse; mod unparse;
pub use ast_gen::*; pub use ast_gen::*;
pub use location::Location; pub use rustpython_compiler_core::Location;
pub type Suite<U = ()> = Vec<Stmt<U>>; pub type Suite<U = ()> = Vec<Stmt<U>>;

View file

@ -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<D: fmt::Display> 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))
}
}

View file

@ -7,6 +7,16 @@ pub struct Location {
pub(super) column: u32, 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 { impl Location {
/// Creates a new Location object at the given row and column. /// Creates a new Location object at the given row and column.
/// ///

View file

@ -1,12 +1,9 @@
//! Define internal parse error types //! Define internal parse error types
//! The goal is to provide a matching and a safe error API, maksing errors from LALR //! 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 lalrpop_util::ParseError as LalrpopError;
use std::{error::Error, fmt};
use crate::ast::Location;
use crate::token::Tok;
use std::error::Error;
use std::fmt;
/// Represents an error during lexical scanning. /// Represents an error during lexical scanning.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -186,7 +183,7 @@ impl ParseError {
impl fmt::Display for ParseError { impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} at {}", self.error, self.location) self.location.fmt_with(f, &self.error)
} }
} }

View file

@ -1,8 +1,7 @@
use ahash::RandomState;
use std::collections::HashSet;
use crate::ast; use crate::ast;
use crate::error::{LexicalError, LexicalErrorType}; use crate::error::{LexicalError, LexicalErrorType};
use ahash::RandomState;
use std::collections::HashSet;
pub struct ArgumentList { pub struct ArgumentList {
pub args: Vec<ast::Expr>, pub args: Vec<ast::Expr>,

View file

@ -5,13 +5,9 @@
//! parse a whole program, a single statement, or a single //! parse a whole program, a single statement, or a single
//! expression. //! expression.
use std::iter;
use crate::ast;
use crate::error::ParseError;
use crate::lexer;
pub use crate::mode::Mode; pub use crate::mode::Mode;
use crate::python; use crate::{ast, error::ParseError, lexer, python};
use std::iter;
/* /*
* Parse python code. * Parse python code.