Remove parser dependency from codegen

This commit is contained in:
Noa committed 2025-03-27 22:09:00 -05:00
1 parent 7d05f881c4
commit f398321b1f
8 files changed
+44 -28

No files matched your search

Generated
-3
View File
@@ -2324,7 +2324,6 @@ dependencies = [
"num-traits",
"ruff_python_ast",
"ruff_python_codegen",
"ruff_python_parser",
"ruff_source_file",
"ruff_text_size",
"rustpython-compiler-core",
@@ -2386,8 +2385,6 @@ dependencies = [
"lz4_flex",
"malachite-bigint",
"num-complex",
"ruff_python_ast",
"ruff_python_parser",
"ruff_source_file",
"rustpython-wtf8",
"serde",
-1
View File
@@ -15,7 +15,6 @@ license.workspace = true
rustpython-compiler-core = { workspace = true }
rustpython-compiler-source = {workspace = true }
rustpython-wtf8 = { workspace = true }
ruff_python_parser = { workspace = true }
ruff_python_ast = { workspace = true }
ruff_text_size = { workspace = true }
ruff_source_file = { workspace = true }
+7 -6
View File
@@ -3458,12 +3458,13 @@ impl Compiler<'_> {
self.compile_expression(&fstring_expr.expression)?;
emit!(
self,
Instruction::FormatValue {
conversion: conversion
}
);
let conversion = match conversion {
ConversionFlag::None => bytecode::ConversionFlag::None,
ConversionFlag::Str => bytecode::ConversionFlag::Str,
ConversionFlag::Ascii => bytecode::ConversionFlag::Ascii,
ConversionFlag::Repr => bytecode::ConversionFlag::Repr,
};
emit!(self, Instruction::FormatValue { conversion });
// concatenate formatted string and debug text (if present)
if debug_text_count > 0 {
-2
View File
@@ -10,8 +10,6 @@ license.workspace = true
[dependencies]
# rustpython-parser-core = { workspace = true, features=["location"] }
ruff_python_ast = { workspace = true }
ruff_python_parser = { workspace = true }
ruff_source_file = { workspace = true }
rustpython-wtf8 = { workspace = true }
+13 -3
View File
@@ -5,14 +5,24 @@ use bitflags::bitflags;
use itertools::Itertools;
use malachite_bigint::BigInt;
use num_complex::Complex64;
pub use ruff_python_ast::ConversionFlag;
// use rustpython_parser_core::source_code::{OneIndexed, SourceLocation};
use ruff_source_file::{OneIndexed, SourceLocation};
use rustpython_wtf8::{Wtf8, Wtf8Buf};
use std::marker::PhantomData;
use std::{collections::BTreeSet, fmt, hash, mem};
// pub use rustpython_parser_core::ConversionFlag;
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[repr(i8)]
#[allow(clippy::cast_possible_wrap)]
pub enum ConversionFlag {
/// No conversion
None = -1, // CPython uses -1
/// Converts by calling `str(<value>)`.
Str = b's' as i8,
/// Converts by calling `ascii(<value>)`.
Ascii = b'a' as i8,
/// Converts by calling `repr(<value>)`.
Repr = b'r' as i8,
}
pub trait Constant: Sized {
type Name: AsRef<str>;
+8 -11
View File
@@ -1,10 +1,9 @@
pub use ruff_python_parser::ModeParseError;
#[derive(Clone, Copy)]
pub enum Mode {
Exec,
Eval,
Single,
/// Returns the value of the last statement in the statement list.
BlockExpr,
}
@@ -22,14 +21,12 @@ impl std::str::FromStr for Mode {
}
}
impl From<Mode> for ruff_python_parser::Mode {
fn from(mode: Mode) -> Self {
match mode {
Mode::Exec => Self::Module,
Mode::Eval => Self::Expression,
// TODO: Improve ruff API
// ruff does not have an interactive mode
Mode::Single | Mode::BlockExpr => Self::Ipython,
}
/// Returned when a given mode is not valid.
#[derive(Debug)]
pub struct ModeParseError;
impl std::fmt::Display for ModeParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, r#"mode must be "exec", "eval", or "single""#)
}
}
+8 -1
View File
@@ -119,7 +119,14 @@ fn _compile(
mode: Mode,
opts: CompileOpts,
) -> Result<CodeObject, CompileError> {
let parsed = parser::parse(source_code.text, parser::Mode::from(mode).into())
let parser_mode = match mode {
Mode::Exec => parser::Mode::Module,
Mode::Eval => parser::Mode::Expression,
// ruff does not have an interactive mode, which is fine,
// since these are only different in terms of compilation
Mode::Single | Mode::BlockExpr => parser::Mode::Module,
};
let parsed = parser::parse(source_code.text, parser_mode.into())
.map_err(|err| CompileError::from_ruff_parse_error(err, &source_code))?;
let ast = parsed.into_syntax();
compile::compile_top(ast, source_code, mode, opts).map_err(|e| e.into())
+8 -1
View File
@@ -1,5 +1,6 @@
use super::*;
use num_traits::ToPrimitive;
use rustpython_compiler_core::bytecode;
impl Node for ruff::ConversionFlag {
fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef {
@@ -13,7 +14,13 @@ impl Node for ruff::ConversionFlag {
) -> PyResult<Self> {
i32::try_from_object(vm, object)?
.to_u32()
.and_then(ruff::ConversionFlag::from_op_arg)
.and_then(bytecode::ConversionFlag::from_op_arg)
.map(|flag| match flag {
bytecode::ConversionFlag::None => Self::None,
bytecode::ConversionFlag::Str => Self::Str,
bytecode::ConversionFlag::Ascii => Self::Ascii,
bytecode::ConversionFlag::Repr => Self::Repr,
})
.ok_or_else(|| vm.new_value_error("invalid conversion flag".to_owned()))
}
}