mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-04 02:39:22 +00:00
Share location between compiler crates
This commit is contained in:
parent
b3095c7451
commit
a22abc5550
4 changed files with 71 additions and 65 deletions
|
@ -4,14 +4,24 @@ use std::fmt;
|
||||||
|
|
||||||
/// A location somewhere in the sourcecode.
|
/// A location somewhere in the sourcecode.
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||||
pub struct Location {
|
pub struct Location(rustpython_compiler_core::Location);
|
||||||
row: usize,
|
|
||||||
column: usize,
|
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 {
|
impl fmt::Display for Location {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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$}",
|
"{}\n{}{arrow:>pad$}",
|
||||||
self.desc,
|
self.desc,
|
||||||
self.line,
|
self.line,
|
||||||
pad = self.loc.column,
|
pad = self.loc.column(),
|
||||||
arrow = "^",
|
arrow = "^",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -48,32 +58,6 @@ impl Location {
|
||||||
|
|
||||||
impl Location {
|
impl Location {
|
||||||
pub fn new(row: usize, column: usize) -> Self {
|
pub fn new(row: usize, column: usize) -> Self {
|
||||||
Location { row, column }
|
Location(rustpython_compiler_core::Location::new(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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
//! Implement python as a virtual machine with bytecodes. This module
|
//! Implement python as a virtual machine with bytecodes. This module
|
||||||
//! implements bytecode structure.
|
//! implements bytecode structure.
|
||||||
|
|
||||||
|
use crate::Location;
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use bstr::ByteSlice;
|
use bstr::ByteSlice;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
@ -9,38 +10,6 @@ use num_complex::Complex64;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{collections::BTreeSet, fmt, hash};
|
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 {
|
pub trait Constant: Sized {
|
||||||
type Name: AsRef<str>;
|
type Name: AsRef<str>;
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
|
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
|
||||||
#![doc(html_root_url = "https://docs.rs/rustpython-compiler-core/")]
|
#![doc(html_root_url = "https://docs.rs/rustpython-compiler-core/")]
|
||||||
|
|
||||||
pub mod bytecode;
|
mod bytecode;
|
||||||
|
mod location;
|
||||||
mod mode;
|
mod mode;
|
||||||
|
|
||||||
pub use bytecode::*;
|
pub use bytecode::*;
|
||||||
|
pub use location::Location;
|
||||||
pub use mode::Mode;
|
pub use mode::Mode;
|
||||||
|
|
51
core/src/location.rs
Normal file
51
core/src/location.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue