Skip to content

Commit f398321

Browse files
committed
Remove parser dependency from codegen
1 parent 7d05f88 commit f398321

8 files changed

Lines changed: 44 additions & 28 deletions

File tree

‎Cargo.lock‎

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎compiler/codegen/Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ license.workspace = true
1515
rustpython-compiler-core = { workspace = true }
1616
rustpython-compiler-source = {workspace = true }
1717
rustpython-wtf8 = { workspace = true }
18-
ruff_python_parser = { workspace = true }
1918
ruff_python_ast = { workspace = true }
2019
ruff_text_size = { workspace = true }
2120
ruff_source_file = { workspace = true }

‎compiler/codegen/src/compile.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,12 +3458,13 @@ impl Compiler<'_> {
34583458

34593459
self.compile_expression(&fstring_expr.expression)?;
34603460

3461-
emit!(
3462-
self,
3463-
Instruction::FormatValue {
3464-
conversion: conversion
3465-
}
3466-
);
3461+
let conversion = match conversion {
3462+
ConversionFlag::None => bytecode::ConversionFlag::None,
3463+
ConversionFlag::Str => bytecode::ConversionFlag::Str,
3464+
ConversionFlag::Ascii => bytecode::ConversionFlag::Ascii,
3465+
ConversionFlag::Repr => bytecode::ConversionFlag::Repr,
3466+
};
3467+
emit!(self, Instruction::FormatValue { conversion });
34673468

34683469
// concatenate formatted string and debug text (if present)
34693470
if debug_text_count > 0 {

‎compiler/core/Cargo.toml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ license.workspace = true
1010

1111
[dependencies]
1212
# rustpython-parser-core = { workspace = true, features=["location"] }
13-
ruff_python_ast = { workspace = true }
14-
ruff_python_parser = { workspace = true }
1513
ruff_source_file = { workspace = true }
1614
rustpython-wtf8 = { workspace = true }
1715

‎compiler/core/src/bytecode.rs‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ use bitflags::bitflags;
55
use itertools::Itertools;
66
use malachite_bigint::BigInt;
77
use num_complex::Complex64;
8-
pub use ruff_python_ast::ConversionFlag;
9-
// use rustpython_parser_core::source_code::{OneIndexed, SourceLocation};
108
use ruff_source_file::{OneIndexed, SourceLocation};
119
use rustpython_wtf8::{Wtf8, Wtf8Buf};
1210
use std::marker::PhantomData;
1311
use std::{collections::BTreeSet, fmt, hash, mem};
1412

15-
// pub use rustpython_parser_core::ConversionFlag;
13+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
14+
#[repr(i8)]
15+
#[allow(clippy::cast_possible_wrap)]
16+
pub enum ConversionFlag {
17+
/// No conversion
18+
None = -1, // CPython uses -1
19+
/// Converts by calling `str(<value>)`.
20+
Str = b's' as i8,
21+
/// Converts by calling `ascii(<value>)`.
22+
Ascii = b'a' as i8,
23+
/// Converts by calling `repr(<value>)`.
24+
Repr = b'r' as i8,
25+
}
1626

1727
pub trait Constant: Sized {
1828
type Name: AsRef<str>;

‎compiler/core/src/mode.rs‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
pub use ruff_python_parser::ModeParseError;
2-
31
#[derive(Clone, Copy)]
42
pub enum Mode {
53
Exec,
64
Eval,
75
Single,
6+
/// Returns the value of the last statement in the statement list.
87
BlockExpr,
98
}
109

@@ -22,14 +21,12 @@ impl std::str::FromStr for Mode {
2221
}
2322
}
2423

25-
impl From<Mode> for ruff_python_parser::Mode {
26-
fn from(mode: Mode) -> Self {
27-
match mode {
28-
Mode::Exec => Self::Module,
29-
Mode::Eval => Self::Expression,
30-
// TODO: Improve ruff API
31-
// ruff does not have an interactive mode
32-
Mode::Single | Mode::BlockExpr => Self::Ipython,
33-
}
24+
/// Returned when a given mode is not valid.
25+
#[derive(Debug)]
26+
pub struct ModeParseError;
27+
28+
impl std::fmt::Display for ModeParseError {
29+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30+
write!(f, r#"mode must be "exec", "eval", or "single""#)
3431
}
3532
}

‎compiler/src/lib.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ fn _compile(
119119
mode: Mode,
120120
opts: CompileOpts,
121121
) -> Result<CodeObject, CompileError> {
122-
let parsed = parser::parse(source_code.text, parser::Mode::from(mode).into())
122+
let parser_mode = match mode {
123+
Mode::Exec => parser::Mode::Module,
124+
Mode::Eval => parser::Mode::Expression,
125+
// ruff does not have an interactive mode, which is fine,
126+
// since these are only different in terms of compilation
127+
Mode::Single | Mode::BlockExpr => parser::Mode::Module,
128+
};
129+
let parsed = parser::parse(source_code.text, parser_mode.into())
123130
.map_err(|err| CompileError::from_ruff_parse_error(err, &source_code))?;
124131
let ast = parsed.into_syntax();
125132
compile::compile_top(ast, source_code, mode, opts).map_err(|e| e.into())

‎vm/src/stdlib/ast/other.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use num_traits::ToPrimitive;
3+
use rustpython_compiler_core::bytecode;
34

45
impl Node for ruff::ConversionFlag {
56
fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
@@ -13,7 +14,13 @@ impl Node for ruff::ConversionFlag {
1314
) -> PyResult<Self> {
1415
i32::try_from_object(vm, object)?
1516
.to_u32()
16-
.and_then(ruff::ConversionFlag::from_op_arg)
17+
.and_then(bytecode::ConversionFlag::from_op_arg)
18+
.map(|flag| match flag {
19+
bytecode::ConversionFlag::None => Self::None,
20+
bytecode::ConversionFlag::Str => Self::Str,
21+
bytecode::ConversionFlag::Ascii => Self::Ascii,
22+
bytecode::ConversionFlag::Repr => Self::Repr,
23+
})
1724
.ok_or_else(|| vm.new_value_error("invalid conversion flag".to_owned()))
1825
}
1926
}

0 commit comments

Comments
 (0)