Skip to content

Commit bfac035

Browse files
committed
Share location between compiler crates
1 parent c16e650 commit bfac035

4 files changed

Lines changed: 71 additions & 65 deletions

File tree

‎ast/src/location.rs‎

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@ use std::fmt;
44

55
/// A location somewhere in the sourcecode.
66
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
7-
pub struct Location {
8-
row: usize,
9-
column: usize,
7+
pub struct Location(rustpython_compiler_core::Location);
8+
9+
impl std::ops::Deref for Location {
10+
type Target = rustpython_compiler_core::Location;
11+
fn deref(&self) -> &Self::Target {
12+
&self.0
13+
}
14+
}
15+
16+
impl std::ops::DerefMut for Location {
17+
fn deref_mut(&mut self) -> &mut Self::Target {
18+
&mut self.0
19+
}
1020
}
1121

1222
impl fmt::Display for Location {
1323
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14-
write!(f, "line {} column {}", self.row, self.column)
24+
write!(f, "line {} column {}", self.row(), self.column())
1525
}
1626
}
1727

@@ -33,7 +43,7 @@ impl Location {
3343
"{}\n{}{arrow:>pad$}",
3444
self.desc,
3545
self.line,
36-
pad = self.loc.column,
46+
pad = self.loc.column(),
3747
arrow = "^",
3848
)
3949
}
@@ -48,32 +58,6 @@ impl Location {
4858

4959
impl Location {
5060
pub fn new(row: usize, column: usize) -> Self {
51-
Location { row, column }
52-
}
53-
54-
pub fn row(&self) -> usize {
55-
self.row
56-
}
57-
58-
pub fn column(&self) -> usize {
59-
self.column
60-
}
61-
62-
pub fn reset(&mut self) {
63-
self.row = 1;
64-
self.column = 1;
65-
}
66-
67-
pub fn go_right(&mut self) {
68-
self.column += 1;
69-
}
70-
71-
pub fn go_left(&mut self) {
72-
self.column -= 1;
73-
}
74-
75-
pub fn newline(&mut self) {
76-
self.row += 1;
77-
self.column = 1;
61+
Location(rustpython_compiler_core::Location::new(row, column))
7862
}
7963
}

‎core/src/bytecode.rs‎

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Implement python as a virtual machine with bytecodes. This module
22
//! implements bytecode structure.
33
4+
use crate::Location;
45
use bitflags::bitflags;
56
use bstr::ByteSlice;
67
use itertools::Itertools;
@@ -9,38 +10,6 @@ use num_complex::Complex64;
910
use serde::{Deserialize, Serialize};
1011
use std::{collections::BTreeSet, fmt, hash};
1112

12-
/// Sourcecode location.
13-
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
14-
pub struct Location {
15-
row: u32,
16-
column: u32,
17-
}
18-
19-
impl Location {
20-
/// Creates a new Location object at the given row and column.
21-
///
22-
/// # Example
23-
/// ```
24-
/// use rustpython_compiler_core::Location;
25-
/// let loc = Location::new(10, 10);
26-
/// ```
27-
pub fn new(row: usize, column: usize) -> Self {
28-
let row = row.try_into().expect("Location::row over u32");
29-
let column = column.try_into().expect("Location::column over u32");
30-
Location { row, column }
31-
}
32-
33-
/// Current row
34-
pub fn row(&self) -> usize {
35-
self.row as usize
36-
}
37-
38-
/// Current column
39-
pub fn column(&self) -> usize {
40-
self.column as usize
41-
}
42-
}
43-
4413
pub trait Constant: Sized {
4514
type Name: AsRef<str>;
4615

‎core/src/lib.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
22
#![doc(html_root_url = "https://docs.rs/rustpython-compiler-core/")]
33

4-
pub mod bytecode;
4+
mod bytecode;
5+
mod location;
56
mod mode;
67

78
pub use bytecode::*;
9+
pub use location::Location;
810
pub use mode::Mode;

‎core/src/location.rs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
/// Sourcecode location.
4+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
5+
pub struct Location {
6+
pub(super) row: u32,
7+
pub(super) column: u32,
8+
}
9+
10+
impl Location {
11+
/// Creates a new Location object at the given row and column.
12+
///
13+
/// # Example
14+
/// ```
15+
/// use rustpython_compiler_core::Location;
16+
/// let loc = Location::new(10, 10);
17+
/// ```
18+
pub fn new(row: usize, column: usize) -> Self {
19+
let row = row.try_into().expect("Location::row over u32");
20+
let column = column.try_into().expect("Location::column over u32");
21+
Location { row, column }
22+
}
23+
24+
/// Current row
25+
pub fn row(&self) -> usize {
26+
self.row as usize
27+
}
28+
29+
/// Current column
30+
pub fn column(&self) -> usize {
31+
self.column as usize
32+
}
33+
34+
pub fn reset(&mut self) {
35+
self.row = 1;
36+
self.column = 1;
37+
}
38+
39+
pub fn go_right(&mut self) {
40+
self.column += 1;
41+
}
42+
43+
pub fn go_left(&mut self) {
44+
self.column -= 1;
45+
}
46+
47+
pub fn newline(&mut self) {
48+
self.row += 1;
49+
self.column = 1;
50+
}
51+
}

0 commit comments

Comments
 (0)