Share location between compiler crates

This commit is contained in:
Jeong YunWon 2022-08-22 07:40:15 +09:00
parent b3095c7451
commit a22abc5550
4 changed files with 71 additions and 65 deletions

View file

@ -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))
}
}

View file

@ -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<str>;

View file

@ -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;

51
core/src/location.rs Normal file
View file

@ -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;
}
}