Skip to content

Commit bf3f3ec

Browse files
committed
Flatten rustpython_parser interface
1 parent a8d4de2 commit bf3f3ec

File tree

9 files changed

+174
-184
lines changed

9 files changed

+174
-184
lines changed

codegen/src/compile.rs

Lines changed: 2 additions & 2 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,
@@ -2846,7 +2846,7 @@ mod tests {
28462846
use super::{CompileOpts, Compiler};
28472847
use crate::symboltable::SymbolTable;
28482848
use rustpython_compiler_core::CodeObject;
2849-
use rustpython_parser::parser;
2849+
use rustpython_parser as parser;
28502850

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

parser/python.lalrpop

Lines changed: 98 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use crate::{
77
ast,
88
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
}

parser/src/function.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Contains functions that perform validation and parsing of arguments and parameters.
22
// Checks apply both to functions and to lambdas.
3-
use crate::ast;
4-
use crate::lexer::{LexicalError, LexicalErrorType};
3+
use crate::{
4+
ast,
5+
lexer::{LexicalError, LexicalErrorType},
6+
};
57
use rustc_hash::FxHashSet;
68

79
pub(crate) struct ArgumentList {

parser/src/lexer.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
//! # Example
1313
//!
1414
//! ```
15-
//! use rustpython_parser::lexer::{lex, Tok};
16-
//! use rustpython_parser::mode::Mode;
17-
//! use rustpython_parser::token::StringKind;
15+
//! use rustpython_parser::{lexer::lex, Tok, Mode, StringKind};
1816
//!
1917
//! let source = "x = 'RustPython'";
2018
//! let tokens = lex(source, Mode::Module)
@@ -33,19 +31,16 @@
3331
//! ```
3432
//!
3533
//! [Lexical analysis]: https://docs.python.org/3/reference/lexical_analysis.html
36-
pub use super::token::{StringKind, Tok};
37-
use crate::ast::Location;
38-
use crate::mode::Mode;
39-
use crate::soft_keywords::SoftKeywordTransformer;
40-
use crate::string::FStringErrorType;
34+
use crate::{
35+
ast::Location,
36+
mode::Mode,
37+
soft_keywords::SoftKeywordTransformer,
38+
string::FStringErrorType,
39+
token::{StringKind, Tok},
40+
};
4141
use num_bigint::BigInt;
42-
use num_traits::identities::Zero;
43-
use num_traits::Num;
44-
use std::char;
45-
use std::cmp::Ordering;
46-
use std::ops::Index;
47-
use std::slice::SliceIndex;
48-
use std::str::FromStr;
42+
use num_traits::{Num, Zero};
43+
use std::{char, cmp::Ordering, ops::Index, slice::SliceIndex, str::FromStr};
4944
use unic_emoji_char::is_emoji_presentation;
5045
use unic_ucd_ident::{is_xid_continue, is_xid_start};
5146

@@ -200,8 +195,7 @@ pub type LexResult = Result<Spanned, LexicalError>;
200195
/// # Examples
201196
///
202197
/// ```
203-
/// use rustpython_parser::mode::Mode;
204-
/// use rustpython_parser::lexer::{lex};
198+
/// use rustpython_parser::{Mode, lexer::lex};
205199
///
206200
/// let source = "def hello(): return 'world'";
207201
/// let lexer = lex(source, Mode::Module);
@@ -1320,8 +1314,7 @@ impl std::fmt::Display for LexicalErrorType {
13201314

13211315
#[cfg(test)]
13221316
mod tests {
1323-
use super::{lex, StringKind, Tok};
1324-
use crate::mode::Mode;
1317+
use super::*;
13251318
use num_bigint::BigInt;
13261319

13271320
const WINDOWS_EOL: &str = "\r\n";

parser/src/lib.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,18 @@
5454
//!
5555
//! The functionality of this crate is split into several modules:
5656
//!
57-
//! - [token]: This module contains the definition of the tokens that are generated by the lexer.
57+
//! - token: This module contains the definition of the tokens that are generated by the lexer.
5858
//! - [lexer]: This module contains the lexer and is responsible for generating the tokens.
59-
//! - [parser]: This module contains an interface to the parser and is responsible for generating the AST.
59+
//! - parser: This module contains an interface to the parser and is responsible for generating the AST.
6060
//! - Functions and strings have special parsing requirements that are handled in additional files.
61-
//! - [mode]: This module contains the definition of the different modes that the parser can be in.
62-
//! - [error]: This module contains the definition of the errors that can be returned by the parser.
61+
//! - mode: This module contains the definition of the different modes that the parser can be in.
6362
//!
6463
//! # Examples
6564
//!
6665
//! For example, to get a stream of tokens from a given string, one could do this:
6766
//!
6867
//! ```
69-
//! use rustpython_parser::mode::Mode;
70-
//! use rustpython_parser::lexer::lex;
68+
//! use rustpython_parser::{lexer::lex, Mode};
7169
//!
7270
//! let python_source = r#"
7371
//! def is_odd(i):
@@ -80,9 +78,7 @@
8078
//! These tokens can be directly fed into the parser to generate an AST:
8179
//!
8280
//! ```
83-
//! use rustpython_parser::lexer::lex;
84-
//! use rustpython_parser::mode::Mode;
85-
//! use rustpython_parser::parser::parse_tokens;
81+
//! use rustpython_parser::{lexer::lex, Mode, parse_tokens};
8682
//!
8783
//! let python_source = r#"
8884
//! def is_odd(i):
@@ -98,7 +94,7 @@
9894
//! mode or tokenizing the source beforehand:
9995
//!
10096
//! ```
101-
//! use rustpython_parser::parser::parse_program;
97+
//! use rustpython_parser::parse_program;
10298
//!
10399
//! let python_source = r#"
104100
//! def is_odd(i):
@@ -111,11 +107,7 @@
111107
//!
112108
//! [lexical analysis]: https://en.wikipedia.org/wiki/Lexical_analysis
113109
//! [parsing]: https://en.wikipedia.org/wiki/Parsing
114-
//! [token]: crate::token
115110
//! [lexer]: crate::lexer
116-
//! [parser]: crate::parser
117-
//! [mode]: crate::mode
118-
//! [error]: crate::error
119111
120112
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
121113
#![doc(html_root_url = "https://docs.rs/rustpython-parser/")]
@@ -125,12 +117,21 @@ extern crate log;
125117
pub use rustpython_ast as ast;
126118

127119
mod function;
120+
// Skip flattening lexer to distinguish from full parser
128121
pub mod lexer;
129-
pub mod mode;
130-
pub mod parser;
122+
mod mode;
123+
mod parser;
131124
mod string;
132125
#[rustfmt::skip]
133126
mod python;
134127
mod context;
135128
mod soft_keywords;
136-
pub mod token;
129+
mod token;
130+
131+
pub use mode::Mode;
132+
pub use parser::{
133+
parse, parse_expression, parse_expression_located, parse_located, parse_program, parse_tokens,
134+
ParseError, ParseErrorType,
135+
};
136+
pub use string::FStringErrorType;
137+
pub use token::{StringKind, Tok};

0 commit comments

Comments
 (0)