Skip to content

Commit 822f093

Browse files
authored
Merge pull request #4543 from youknowone/flatten-parser
Flatten parser interface
2 parents ba9e7c9 + 30f8578 commit 822f093

File tree

14 files changed

+528
-552
lines changed

14 files changed

+528
-552
lines changed

ast/src/constant.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::fmt::Error;
2-
31
use num_bigint::BigInt;
42
pub use rustpython_compiler_core::ConversionFlag;
53

@@ -44,7 +42,9 @@ impl std::fmt::Display for Constant {
4442
Constant::None => f.pad("None"),
4543
Constant::Bool(b) => f.pad(if *b { "True" } else { "False" }),
4644
Constant::Str(s) => rustpython_common::str::repr(s).fmt(f),
47-
Constant::Bytes(b) => f.pad(&rustpython_common::bytes::repr(b).map_err(|_err| Error)?),
45+
Constant::Bytes(b) => {
46+
f.pad(&rustpython_common::bytes::repr(b).map_err(|_err| std::fmt::Error)?)
47+
}
4848
Constant::Int(i) => i.fmt(f),
4949
Constant::Tuple(tup) => {
5050
if let [elt] = &**tup {
@@ -133,12 +133,12 @@ impl<U> crate::fold::Fold<U> for ConstantOptimizer {
133133

134134
#[cfg(test)]
135135
mod tests {
136+
use super::*;
137+
136138
#[cfg(feature = "constant-optimization")]
137139
#[test]
138140
fn test_constant_opt() {
139-
use super::*;
140-
use crate::fold::Fold;
141-
use crate::*;
141+
use crate::{fold::Fold, *};
142142

143143
let start = Default::default();
144144
let end = None;

codegen/src/compile.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl CompileContext {
8686
}
8787
}
8888

89-
/// Compile an ast::Mod produced from rustpython_parser::parser::parse()
89+
/// Compile an ast::Mod produced from rustpython_parser::parse()
9090
pub fn compile_top(
9191
ast: &ast::Mod,
9292
source_path: String,
@@ -2843,10 +2843,8 @@ fn compile_constant(value: &ast::Constant) -> ConstantData {
28432843

28442844
#[cfg(test)]
28452845
mod tests {
2846-
use super::{CompileOpts, Compiler};
2847-
use crate::symboltable::SymbolTable;
2848-
use rustpython_compiler_core::CodeObject;
2849-
use rustpython_parser::parser;
2846+
use super::*;
2847+
use rustpython_parser as parser;
28502848

28512849
fn compile_exec(source: &str) -> CodeObject {
28522850
let mut compiler: Compiler = Compiler::new(

core/src/location.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl Location {
9696

9797
#[cfg(test)]
9898
mod tests {
99-
use crate::Location;
99+
use super::*;
100100

101101
#[test]
102102
fn test_gt() {

core/src/mode.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ impl std::str::FromStr for Mode {
1515
"exec" => Ok(Mode::Exec),
1616
"eval" => Ok(Mode::Eval),
1717
"single" => Ok(Mode::Single),
18-
_ => Err(ModeParseError { _priv: () }),
18+
_ => Err(ModeParseError(())),
1919
}
2020
}
2121
}
2222

2323
#[derive(Debug)]
24-
pub struct ModeParseError {
25-
_priv: (),
26-
}
24+
pub struct ModeParseError(());
2725

2826
impl std::fmt::Display for ModeParseError {
2927
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30-
write!(f, r#"mode should be "exec", "eval", or "single""#)
28+
write!(f, r#"mode must be "exec", "eval", or "single""#)
3129
}
3230
}

parser/python.lalrpop

Lines changed: 99 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
use crate::{
77
ast,
8-
error::{LexicalError, LexicalErrorType},
8+
lexer::{LexicalError, LexicalErrorType},
99
function::{ArgumentList, parse_args, parse_params, validate_arguments},
10-
lexer,
1110
context::set_context,
1211
string::parse_strings,
13-
token::StringKind,
12+
token::{self, StringKind},
1413
};
1514
use num_bigint::BigInt;
1615

@@ -1937,106 +1936,106 @@ extern {
19371936
type Location = ast::Location;
19381937
type Error = LexicalError;
19391938

1940-
enum lexer::Tok {
1941-
Indent => lexer::Tok::Indent,
1942-
Dedent => lexer::Tok::Dedent,
1943-
StartModule => lexer::Tok::StartModule,
1944-
StartInteractive => lexer::Tok::StartInteractive,
1945-
StartExpression => lexer::Tok::StartExpression,
1946-
"+" => lexer::Tok::Plus,
1947-
"-" => lexer::Tok::Minus,
1948-
"~" => lexer::Tok::Tilde,
1949-
":" => lexer::Tok::Colon,
1950-
"." => lexer::Tok::Dot,
1951-
"..." => lexer::Tok::Ellipsis,
1952-
"," => lexer::Tok::Comma,
1953-
"*" => lexer::Tok::Star,
1954-
"**" => lexer::Tok::DoubleStar,
1955-
"&" => lexer::Tok::Amper,
1956-
"@" => lexer::Tok::At,
1957-
"%" => lexer::Tok::Percent,
1958-
"//" => lexer::Tok::DoubleSlash,
1959-
"^" => lexer::Tok::CircumFlex,
1960-
"|" => lexer::Tok::Vbar,
1961-
"<<" => lexer::Tok::LeftShift,
1962-
">>" => lexer::Tok::RightShift,
1963-
"/" => lexer::Tok::Slash,
1964-
"(" => lexer::Tok::Lpar,
1965-
")" => lexer::Tok::Rpar,
1966-
"[" => lexer::Tok::Lsqb,
1967-
"]" => lexer::Tok::Rsqb,
1968-
"{" => lexer::Tok::Lbrace,
1969-
"}" => lexer::Tok::Rbrace,
1970-
"=" => lexer::Tok::Equal,
1971-
"+=" => lexer::Tok::PlusEqual,
1972-
"-=" => lexer::Tok::MinusEqual,
1973-
"*=" => lexer::Tok::StarEqual,
1974-
"@=" => lexer::Tok::AtEqual,
1975-
"/=" => lexer::Tok::SlashEqual,
1976-
"%=" => lexer::Tok::PercentEqual,
1977-
"&=" => lexer::Tok::AmperEqual,
1978-
"|=" => lexer::Tok::VbarEqual,
1979-
"^=" => lexer::Tok::CircumflexEqual,
1980-
"<<=" => lexer::Tok::LeftShiftEqual,
1981-
">>=" => lexer::Tok::RightShiftEqual,
1982-
"**=" => lexer::Tok::DoubleStarEqual,
1983-
"//=" => lexer::Tok::DoubleSlashEqual,
1984-
":=" => lexer::Tok::ColonEqual,
1985-
"==" => lexer::Tok::EqEqual,
1986-
"!=" => lexer::Tok::NotEqual,
1987-
"<" => lexer::Tok::Less,
1988-
"<=" => lexer::Tok::LessEqual,
1989-
">" => lexer::Tok::Greater,
1990-
">=" => lexer::Tok::GreaterEqual,
1991-
"->" => lexer::Tok::Rarrow,
1992-
"and" => lexer::Tok::And,
1993-
"as" => lexer::Tok::As,
1994-
"assert" => lexer::Tok::Assert,
1995-
"async" => lexer::Tok::Async,
1996-
"await" => lexer::Tok::Await,
1997-
"break" => lexer::Tok::Break,
1998-
"class" => lexer::Tok::Class,
1999-
"continue" => lexer::Tok::Continue,
2000-
"def" => lexer::Tok::Def,
2001-
"del" => lexer::Tok::Del,
2002-
"elif" => lexer::Tok::Elif,
2003-
"else" => lexer::Tok::Else,
2004-
"except" => lexer::Tok::Except,
2005-
"finally" => lexer::Tok::Finally,
2006-
"for" => lexer::Tok::For,
2007-
"from" => lexer::Tok::From,
2008-
"global" => lexer::Tok::Global,
2009-
"if" => lexer::Tok::If,
2010-
"import" => lexer::Tok::Import,
2011-
"in" => lexer::Tok::In,
2012-
"is" => lexer::Tok::Is,
2013-
"lambda" => lexer::Tok::Lambda,
2014-
"nonlocal" => lexer::Tok::Nonlocal,
2015-
"not" => lexer::Tok::Not,
2016-
"or" => lexer::Tok::Or,
2017-
"pass" => lexer::Tok::Pass,
2018-
"raise" => lexer::Tok::Raise,
2019-
"return" => lexer::Tok::Return,
2020-
"try" => lexer::Tok::Try,
2021-
"while" => lexer::Tok::While,
2022-
"match" => lexer::Tok::Match,
2023-
"case" => lexer::Tok::Case,
2024-
"with" => lexer::Tok::With,
2025-
"yield" => lexer::Tok::Yield,
2026-
"True" => lexer::Tok::True,
2027-
"False" => lexer::Tok::False,
2028-
"None" => lexer::Tok::None,
2029-
int => lexer::Tok::Int { value: <BigInt> },
2030-
float => lexer::Tok::Float { value: <f64> },
2031-
complex => lexer::Tok::Complex { real: <f64>, imag: <f64> },
2032-
string => lexer::Tok::String {
1939+
enum token::Tok {
1940+
Indent => token::Tok::Indent,
1941+
Dedent => token::Tok::Dedent,
1942+
StartModule => token::Tok::StartModule,
1943+
StartInteractive => token::Tok::StartInteractive,
1944+
StartExpression => token::Tok::StartExpression,
1945+
"+" => token::Tok::Plus,
1946+
"-" => token::Tok::Minus,
1947+
"~" => token::Tok::Tilde,
1948+
":" => token::Tok::Colon,
1949+
"." => token::Tok::Dot,
1950+
"..." => token::Tok::Ellipsis,
1951+
"," => token::Tok::Comma,
1952+
"*" => token::Tok::Star,
1953+
"**" => token::Tok::DoubleStar,
1954+
"&" => token::Tok::Amper,
1955+
"@" => token::Tok::At,
1956+
"%" => token::Tok::Percent,
1957+
"//" => token::Tok::DoubleSlash,
1958+
"^" => token::Tok::CircumFlex,
1959+
"|" => token::Tok::Vbar,
1960+
"<<" => token::Tok::LeftShift,
1961+
">>" => token::Tok::RightShift,
1962+
"/" => token::Tok::Slash,
1963+
"(" => token::Tok::Lpar,
1964+
")" => token::Tok::Rpar,
1965+
"[" => token::Tok::Lsqb,
1966+
"]" => token::Tok::Rsqb,
1967+
"{" => token::Tok::Lbrace,
1968+
"}" => token::Tok::Rbrace,
1969+
"=" => token::Tok::Equal,
1970+
"+=" => token::Tok::PlusEqual,
1971+
"-=" => token::Tok::MinusEqual,
1972+
"*=" => token::Tok::StarEqual,
1973+
"@=" => token::Tok::AtEqual,
1974+
"/=" => token::Tok::SlashEqual,
1975+
"%=" => token::Tok::PercentEqual,
1976+
"&=" => token::Tok::AmperEqual,
1977+
"|=" => token::Tok::VbarEqual,
1978+
"^=" => token::Tok::CircumflexEqual,
1979+
"<<=" => token::Tok::LeftShiftEqual,
1980+
">>=" => token::Tok::RightShiftEqual,
1981+
"**=" => token::Tok::DoubleStarEqual,
1982+
"//=" => token::Tok::DoubleSlashEqual,
1983+
":=" => token::Tok::ColonEqual,
1984+
"==" => token::Tok::EqEqual,
1985+
"!=" => token::Tok::NotEqual,
1986+
"<" => token::Tok::Less,
1987+
"<=" => token::Tok::LessEqual,
1988+
">" => token::Tok::Greater,
1989+
">=" => token::Tok::GreaterEqual,
1990+
"->" => token::Tok::Rarrow,
1991+
"and" => token::Tok::And,
1992+
"as" => token::Tok::As,
1993+
"assert" => token::Tok::Assert,
1994+
"async" => token::Tok::Async,
1995+
"await" => token::Tok::Await,
1996+
"break" => token::Tok::Break,
1997+
"class" => token::Tok::Class,
1998+
"continue" => token::Tok::Continue,
1999+
"def" => token::Tok::Def,
2000+
"del" => token::Tok::Del,
2001+
"elif" => token::Tok::Elif,
2002+
"else" => token::Tok::Else,
2003+
"except" => token::Tok::Except,
2004+
"finally" => token::Tok::Finally,
2005+
"for" => token::Tok::For,
2006+
"from" => token::Tok::From,
2007+
"global" => token::Tok::Global,
2008+
"if" => token::Tok::If,
2009+
"import" => token::Tok::Import,
2010+
"in" => token::Tok::In,
2011+
"is" => token::Tok::Is,
2012+
"lambda" => token::Tok::Lambda,
2013+
"nonlocal" => token::Tok::Nonlocal,
2014+
"not" => token::Tok::Not,
2015+
"or" => token::Tok::Or,
2016+
"pass" => token::Tok::Pass,
2017+
"raise" => token::Tok::Raise,
2018+
"return" => token::Tok::Return,
2019+
"try" => token::Tok::Try,
2020+
"while" => token::Tok::While,
2021+
"match" => token::Tok::Match,
2022+
"case" => token::Tok::Case,
2023+
"with" => token::Tok::With,
2024+
"yield" => token::Tok::Yield,
2025+
"True" => token::Tok::True,
2026+
"False" => token::Tok::False,
2027+
"None" => token::Tok::None,
2028+
int => token::Tok::Int { value: <BigInt> },
2029+
float => token::Tok::Float { value: <f64> },
2030+
complex => token::Tok::Complex { real: <f64>, imag: <f64> },
2031+
string => token::Tok::String {
20332032
value: <String>,
20342033
kind: <StringKind>,
20352034
triple_quoted: <bool>
20362035
},
2037-
name => lexer::Tok::Name { name: <String> },
2038-
"\n" => lexer::Tok::Newline,
2039-
";" => lexer::Tok::Semi,
2040-
"#" => lexer::Tok::Comment(_),
2036+
name => token::Tok::Name { name: <String> },
2037+
"\n" => token::Tok::Newline,
2038+
";" => token::Tok::Semi,
2039+
"#" => token::Tok::Comment(_),
20412040
}
20422041
}

0 commit comments

Comments
 (0)