diff --git a/ast/src/location.rs b/ast/src/location.rs index 830cb5f..9584320 100644 --- a/ast/src/location.rs +++ b/ast/src/location.rs @@ -4,14 +4,24 @@ use std::fmt; /// A location somewhere in the sourcecode. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct Location { - row: usize, - column: usize, +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) + write!(f, "line {} column {}", self.row(), self.column()) } } @@ -33,7 +43,7 @@ impl Location { "{}\n{}{arrow:>pad$}", self.desc, self.line, - pad = self.loc.column, + pad = self.loc.column(), arrow = "^", ) } @@ -48,32 +58,6 @@ impl Location { impl Location { pub fn new(row: usize, column: usize) -> Self { - Location { row, column } - } - - pub fn row(&self) -> usize { - self.row - } - - pub fn column(&self) -> usize { - self.column - } - - pub fn reset(&mut self) { - self.row = 1; - self.column = 1; - } - - pub fn go_right(&mut self) { - self.column += 1; - } - - pub fn go_left(&mut self) { - self.column -= 1; - } - - pub fn newline(&mut self) { - self.row += 1; - self.column = 1; + Location(rustpython_compiler_core::Location::new(row, column)) } } diff --git a/core/src/bytecode.rs b/core/src/bytecode.rs index 8e9cc75..ae28f6e 100644 --- a/core/src/bytecode.rs +++ b/core/src/bytecode.rs @@ -1,6 +1,7 @@ //! Implement python as a virtual machine with bytecodes. This module //! implements bytecode structure. +use crate::Location; use bitflags::bitflags; use bstr::ByteSlice; use itertools::Itertools; @@ -9,38 +10,6 @@ use num_complex::Complex64; use serde::{Deserialize, Serialize}; use std::{collections::BTreeSet, fmt, hash}; -/// Sourcecode location. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct Location { - row: u32, - column: u32, -} - -impl Location { - /// Creates a new Location object at the given row and column. - /// - /// # Example - /// ``` - /// use rustpython_compiler_core::Location; - /// let loc = Location::new(10, 10); - /// ``` - pub fn new(row: usize, column: usize) -> Self { - let row = row.try_into().expect("Location::row over u32"); - let column = column.try_into().expect("Location::column over u32"); - Location { row, column } - } - - /// Current row - pub fn row(&self) -> usize { - self.row as usize - } - - /// Current column - pub fn column(&self) -> usize { - self.column as usize - } -} - pub trait Constant: Sized { type Name: AsRef; diff --git a/core/src/lib.rs b/core/src/lib.rs index ac798da..0b2e23e 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,8 +1,10 @@ #![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")] #![doc(html_root_url = "https://docs.rs/rustpython-compiler-core/")] -pub mod bytecode; +mod bytecode; +mod location; mod mode; pub use bytecode::*; +pub use location::Location; pub use mode::Mode; diff --git a/core/src/location.rs b/core/src/location.rs new file mode 100644 index 0000000..1763763 --- /dev/null +++ b/core/src/location.rs @@ -0,0 +1,51 @@ +use serde::{Deserialize, Serialize}; + +/// Sourcecode location. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Location { + pub(super) row: u32, + pub(super) column: u32, +} + +impl Location { + /// Creates a new Location object at the given row and column. + /// + /// # Example + /// ``` + /// use rustpython_compiler_core::Location; + /// let loc = Location::new(10, 10); + /// ``` + pub fn new(row: usize, column: usize) -> Self { + let row = row.try_into().expect("Location::row over u32"); + let column = column.try_into().expect("Location::column over u32"); + Location { row, column } + } + + /// Current row + pub fn row(&self) -> usize { + self.row as usize + } + + /// Current column + pub fn column(&self) -> usize { + self.column as usize + } + + pub fn reset(&mut self) { + self.row = 1; + self.column = 1; + } + + pub fn go_right(&mut self) { + self.column += 1; + } + + pub fn go_left(&mut self) { + self.column -= 1; + } + + pub fn newline(&mut self) { + self.row += 1; + self.column = 1; + } +}