Skip to content

Commit

Permalink
numerous refactoring
Browse files Browse the repository at this point in the history
- Split parser core and compiler core. Fix RustPython#14
- AST int type to `u32`
- Updated asdl_rs.py and update_asdl.sh fix RustPython#6
- Use `ruff_python_ast::SourceLocation` for Python source location. Deleted our own Location.
- Renamed ast::Located to ast::Attributed to distinguish terms for TextSize and SourceLocation
- `ast::<Node>`s for TextSize located ast. `ast::located::<Node>` for Python source located ast.
- And also strictly renaming `located` to refer only python location related interfaces.
- `SourceLocator` to convert locations.
- New `source-code` features of to disable python locations when unnecessary.
- Also including fully merging astral-sh/RustPython#4 closes RustPython#9
  • Loading branch information
youknowone committed May 9, 2023
1 parent 09a6afd commit fc4bfbf
Show file tree
Hide file tree
Showing 29 changed files with 9,699 additions and 11,961 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ members = [
]

[workspace.dependencies]
rustpython-ast = { path = "ast", version = "0.2.0" }
rustpython-parser-core = { path = "core", version = "0.2.0" }
rustpython-literal = { path = "literal", version = "0.2.0" }

ahash = "0.7.6"
anyhow = "1.0.45"
ascii = "1.0"
bitflags = "1.3.2"
bstr = "0.2.17"
cfg-if = "1.0"
insta = "1.14.0"
itertools = "0.10.3"
Expand All @@ -32,7 +33,7 @@ rand = "0.8.5"
serde = "1.0"
static_assertions = "1.1"
unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unicode_names2.git", rev = "4ce16aa85cbcdd9cc830410f1a72ef9a235f2fde" }
ruff_python_ast = { git = "https://github.com/youknowone/ruff.git", rev = "583df5c1fa43b2732896219f8ab425116c140c80" }
ruff_python_ast = { git = "https://github.com/youknowone/ruff.git", rev = "088958e8fda2f74f1ebf315c75db13c232409b13" }
# ruff_python_ast = { path = "../ruff/crates/ruff_python_ast" }

[profile.dev.package."*"]
Expand Down
8 changes: 4 additions & 4 deletions ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ repository = "https://github.com/RustPython/RustPython"
license = "MIT"

[features]
default = ["constant-optimization", "fold", "location"]
default = ["constant-optimization", "fold", "source-code"]
constant-optimization = ["fold"]
location = []
source-code = ["fold"]
fold = []
unparse = ["rustpython-literal"]

[dependencies]
rustpython-compiler-core = { path = "../core", version = "0.2.0" }
rustpython-literal = { path = "../literal", version = "0.2.0", optional = true }
rustpython-parser-core = { workspace = true }
rustpython-literal = { workspace = true, optional = true }

num-bigint = { workspace = true }
32 changes: 23 additions & 9 deletions ast/asdl_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from argparse import ArgumentParser
from pathlib import Path
from typing import Optional, Dict
from attr import dataclass

import asdl

Expand All @@ -18,7 +17,7 @@
builtin_type_mapping = {
"identifier": "Ident",
"string": "String",
"int": "usize",
"int": "u32",
"constant": "Constant",
}
assert builtin_type_mapping.keys() == asdl.builtin_types
Expand Down Expand Up @@ -391,7 +390,18 @@ def visitModule(self, mod, depth):
depth + 1,
)
self.emit(
"fn map_located<T>(&mut self, located: Attributed<T, U>) -> Result<Attributed<T, Self::TargetU>, Self::Error> { let custom = self.map_user(located.custom)?; Ok(Attributed { range: located.range, custom, node: located.node }) }",
"""
fn map_located<T>(&mut self, located: Attributed<T, U>) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
let custom = self.map_user(located.custom)?;
Ok(Attributed { range: located.range, custom, node: located.node })
}""",
depth + 1,
)
self.emit(
"""
fn fold<X: Foldable<U, Self::TargetU>>(&mut self, node: X) -> Result<X::Mapped, Self::Error> {
node.fold(self)
}""",
depth + 1,
)
for dfn in mod.dfns:
Expand Down Expand Up @@ -761,11 +771,15 @@ def gen_construction(self, cons_path, cons, name, depth):
def extract_location(self, typename, depth):
row = self.decode_field(asdl.Field("int", "lineno"), typename)
column = self.decode_field(asdl.Field("int", "col_offset"), typename)
self.emit(f"""let _location = {{
let row = try_location_field({row}, _vm)?;
let column = try_location_field({column}, _vm)?;
SourceLocation {{ row, column }}
}};""", depth)
self.emit(
f"""
let _location = {{
let row = {row};
let column = {column};
try_location(row, column, _vm)?
}};""",
depth,
)

def decode_field(self, field, typename):
name = json.dumps(field.name)
Expand Down Expand Up @@ -805,7 +819,7 @@ def write_located_def(typeinfo, f):
f.write(
textwrap.dedent(
"""
use crate::location::SourceRange;
use rustpython_parser_core::source_code::SourceRange;
pub type Located<T> = super::generic::Attributed<T, SourceRange>;
"""
Expand Down
6 changes: 4 additions & 2 deletions ast/src/attributed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::location::{SourceLocation, SourceRange};
use rustpython_compiler_core::text_size::{TextRange, TextSize};
use rustpython_parser_core::{
source_code::{SourceLocation, SourceRange},
text_size::{TextRange, TextSize},
};

#[derive(Clone, Debug, PartialEq)]
pub struct Attributed<T, U = ()> {
Expand Down
3 changes: 1 addition & 2 deletions ast/src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use num_bigint::BigInt;
pub use rustpython_compiler_core::ConversionFlag;

#[derive(Clone, Debug, PartialEq)]
pub enum Constant {
Expand Down Expand Up @@ -137,7 +136,7 @@ impl<U> crate::fold::Fold<U> for ConstantOptimizer {
#[cfg(test)]
mod tests {
use super::*;
use rustpython_compiler_core::text_size::TextRange;
use rustpython_parser_core::text_size::TextRange;

#[cfg(feature = "constant-optimization")]
#[test]
Expand Down
2 changes: 1 addition & 1 deletion ast/src/fold_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ macro_rules! simple_fold {
};
}

simple_fold!(usize, String, bool, constant::Constant);
simple_fold!(u32, String, bool, constant::Constant);
18 changes: 13 additions & 5 deletions ast/src/gen/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub struct StmtAnnAssign<U = ()> {
pub target: Box<Expr<U>>,
pub annotation: Box<Expr<U>>,
pub value: Option<Box<Expr<U>>>,
pub simple: usize,
pub simple: u32,
}

impl<U> From<StmtAnnAssign<U>> for StmtKind<U> {
Expand Down Expand Up @@ -328,7 +328,7 @@ impl<U> From<StmtImport<U>> for StmtKind<U> {
pub struct StmtImportFrom<U = ()> {
pub module: Option<Ident>,
pub names: Vec<Alias<U>>,
pub level: Option<usize>,
pub level: Option<u32>,
}

impl<U> From<StmtImportFrom<U>> for StmtKind<U> {
Expand Down Expand Up @@ -610,7 +610,7 @@ impl<U> From<ExprCall<U>> for ExprKind<U> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprFormattedValue<U = ()> {
pub value: Box<Expr<U>>,
pub conversion: usize,
pub conversion: u32,
pub format_spec: Option<Box<Expr<U>>>,
}

Expand Down Expand Up @@ -819,7 +819,7 @@ pub struct Comprehension<U = ()> {
pub target: Expr<U>,
pub iter: Expr<U>,
pub ifs: Vec<Expr<U>>,
pub is_async: usize,
pub is_async: u32,
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -996,7 +996,7 @@ pub type Pattern<U = ()> = Attributed<PatternKind<U>, U>;

#[derive(Clone, Debug, PartialEq)]
pub struct TypeIgnoreTypeIgnore {
pub lineno: usize,
pub lineno: u32,
pub tag: String,
}

Expand All @@ -1019,6 +1019,7 @@ pub mod fold {
type TargetU;
type Error;
fn map_user(&mut self, user: U) -> Result<Self::TargetU, Self::Error>;

fn map_located<T>(
&mut self,
located: Attributed<T, U>,
Expand All @@ -1030,6 +1031,13 @@ pub mod fold {
node: located.node,
})
}

fn fold<X: Foldable<U, Self::TargetU>>(
&mut self,
node: X,
) -> Result<X::Mapped, Self::Error> {
node.fold(self)
}
fn fold_mod(&mut self, node: Mod<U>) -> Result<Mod<Self::TargetU>, Self::Error> {
fold_mod(self, node)
}
Expand Down
2 changes: 1 addition & 1 deletion ast/src/gen/located.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File automatically generated by ast/asdl_rs.py.

use crate::location::SourceRange;
use rustpython_parser_core::source_code::SourceRange;

pub type Located<T> = super::generic::Attributed<T, SourceRange>;
pub type Mod = super::generic::Mod<SourceRange>;
Expand Down
50 changes: 9 additions & 41 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,21 @@ mod generic {
include!("gen/generic.rs");
}
mod impls;
#[cfg(feature = "location")]
pub mod located {
include!("gen/located.rs");
}
#[cfg(feature = "location")]
mod locator;
#[cfg(feature = "location")]
pub use crate::locator::locate;
#[cfg(feature = "location")]
pub use rustpython_compiler_core::SourceLocator;

#[cfg(feature = "source-code")]
mod source_locator;
#[cfg(feature = "unparse")]
mod unparse;

pub use attributed::Attributed;
pub use constant::{Constant, ConversionFlag};
pub use constant::Constant;
pub use generic::*;
pub use rustpython_parser_core::{text_size, ConversionFlag};

pub type Suite<U = ()> = Vec<Stmt<U>>;

pub mod location {
pub use rustpython_compiler_core::source_code::{OneIndexed, SourceLocation};

#[derive(Debug)]
pub struct SourceRange {
pub start: SourceLocation,
pub end: Option<SourceLocation>,
}

impl SourceRange {
pub fn new(start: SourceLocation, end: SourceLocation) -> Self {
Self {
start,
end: Some(end),
}
}
pub fn unwrap_end(&self) -> SourceLocation {
self.end.unwrap()
}
}

impl From<std::ops::Range<SourceLocation>> for SourceRange {
fn from(value: std::ops::Range<SourceLocation>) -> Self {
Self {
start: value.start,
end: Some(value.end),
}
}
}
#[cfg(feature = "source-code")]
pub mod located {
include!("gen/located.rs");
}

pub use rustpython_parser_core::source_code;
8 changes: 1 addition & 7 deletions ast/src/locator.rs → ast/src/source_locator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
use crate::attributed::Attributed;
use crate::fold_helpers::Foldable;
use crate::location::SourceRange;
use rustpython_compiler_core::SourceLocator;

pub fn locate<X: Foldable<(), SourceRange>>(locator: &mut SourceLocator, ast: X) -> X::Mapped {
ast.fold(locator).unwrap()
}
use rustpython_parser_core::source_code::{SourceLocator, SourceRange};

impl crate::fold::Fold<()> for SourceLocator<'_> {
type TargetU = SourceRange;
Expand Down
10 changes: 4 additions & 6 deletions ast/src/unparse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag, Expr, ExprKind,
Operator,
};
use crate::ConversionFlag;
use crate::{Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Expr, ExprKind, Operator};
use std::fmt;

mod precedence {
Expand Down Expand Up @@ -452,7 +450,7 @@ impl<'a> Unparser<'a> {
fn unparse_formatted<U>(
&mut self,
val: &Expr<U>,
conversion: usize,
conversion: u32,
spec: Option<&Expr<U>>,
) -> fmt::Result {
let buffered = to_string_fmt(|f| Unparser::new(f).unparse_expr(val, precedence::TEST + 1));
Expand All @@ -466,7 +464,7 @@ impl<'a> Unparser<'a> {
self.p(&buffered)?;
drop(buffered);

if conversion != ConversionFlag::None as usize {
if conversion != ConversionFlag::None as u32 {
self.p("!")?;
let buf = &[conversion as u8];
let c = std::str::from_utf8(buf).unwrap();
Expand Down
7 changes: 5 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustpython-compiler-core"
description = "RustPython specific bytecode."
name = "rustpython-parser-core"
description = "RustPython parser data types."
version = "0.2.0"
authors = ["RustPython Team"]
edition = "2021"
Expand All @@ -18,3 +18,6 @@ ruff_python_ast = { workspace = true }

lz4_flex = "0.9.2"

[features]
default = ["source-code"]
source-code = []
Loading

0 comments on commit fc4bfbf

Please sign in to comment.