Skip to content

Commit 4de0cb1

Browse files
authored
Parse Trait (#71)
1 parent 5493c9f commit 4de0cb1

File tree

7 files changed

+278
-97
lines changed

7 files changed

+278
-97
lines changed

parser/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ Execution mode check. Allowed modes are `exec`, `eval` or `single`.
6060

6161
For example, one could do this:
6262
```
63-
use rustpython_parser::{parser, ast};
63+
use rustpython_parser::{Parse, ast};
6464
let python_source = "print('Hello world')";
65-
let python_ast = parser::parse_expression(python_source).unwrap();
65+
let python_statements = ast::Suite::parse(python_source).unwrap(); // statements
66+
let python_expr = ast::Expr::parse(python_source).unwrap(); // or expr
6667
```

parser/src/context.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -49,135 +49,135 @@ pub(crate) fn set_context(expr: Expr, ctx: ExprContext) -> Expr {
4949

5050
#[cfg(test)]
5151
mod tests {
52-
use crate::parser::parse_program;
52+
use crate::{ast, Parse};
5353

5454
#[test]
5555
fn test_assign_name() {
5656
let source = "x = (1, 2, 3)";
57-
let parse_ast = parse_program(source, "<test>").unwrap();
57+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
5858
insta::assert_debug_snapshot!(parse_ast);
5959
}
6060

6161
#[test]
6262
fn test_assign_tuple() {
6363
let source = "(x, y) = (1, 2, 3)";
64-
let parse_ast = parse_program(source, "<test>").unwrap();
64+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
6565
insta::assert_debug_snapshot!(parse_ast);
6666
}
6767

6868
#[test]
6969
#[cfg(not(feature = "all-nodes-with-ranges"))]
7070
fn test_assign_list() {
7171
let source = "[x, y] = (1, 2, 3)";
72-
let parse_ast = parse_program(source, "<test>").unwrap();
72+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
7373
insta::assert_debug_snapshot!(parse_ast);
7474
}
7575

7676
#[test]
7777
fn test_assign_attribute() {
7878
let source = "x.y = (1, 2, 3)";
79-
let parse_ast = parse_program(source, "<test>").unwrap();
79+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
8080
insta::assert_debug_snapshot!(parse_ast);
8181
}
8282

8383
#[test]
8484
fn test_assign_subscript() {
8585
let source = "x[y] = (1, 2, 3)";
86-
let parse_ast = parse_program(source, "<test>").unwrap();
86+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
8787
insta::assert_debug_snapshot!(parse_ast);
8888
}
8989

9090
#[test]
9191
fn test_assign_starred() {
9292
let source = "(x, *y) = (1, 2, 3)";
93-
let parse_ast = parse_program(source, "<test>").unwrap();
93+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
9494
insta::assert_debug_snapshot!(parse_ast);
9595
}
9696

9797
#[test]
9898
fn test_assign_for() {
9999
let source = "for x in (1, 2, 3): pass";
100-
let parse_ast = parse_program(source, "<test>").unwrap();
100+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
101101
insta::assert_debug_snapshot!(parse_ast);
102102
}
103103

104104
#[test]
105105
#[cfg(not(feature = "all-nodes-with-ranges"))]
106106
fn test_assign_list_comp() {
107107
let source = "x = [y for y in (1, 2, 3)]";
108-
let parse_ast = parse_program(source, "<test>").unwrap();
108+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
109109
insta::assert_debug_snapshot!(parse_ast);
110110
}
111111

112112
#[test]
113113
#[cfg(not(feature = "all-nodes-with-ranges"))]
114114
fn test_assign_set_comp() {
115115
let source = "x = {y for y in (1, 2, 3)}";
116-
let parse_ast = parse_program(source, "<test>").unwrap();
116+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
117117
insta::assert_debug_snapshot!(parse_ast);
118118
}
119119

120120
#[test]
121121
#[cfg(not(feature = "all-nodes-with-ranges"))]
122122
fn test_assign_with() {
123123
let source = "with 1 as x: pass";
124-
let parse_ast = parse_program(source, "<test>").unwrap();
124+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
125125
insta::assert_debug_snapshot!(parse_ast);
126126
}
127127

128128
#[test]
129129
fn test_assign_named_expr() {
130130
let source = "if x:= 1: pass";
131-
let parse_ast = parse_program(source, "<test>").unwrap();
131+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
132132
insta::assert_debug_snapshot!(parse_ast);
133133
}
134134

135135
#[test]
136136
fn test_ann_assign_name() {
137137
let source = "x: int = 1";
138-
let parse_ast = parse_program(source, "<test>").unwrap();
138+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
139139
insta::assert_debug_snapshot!(parse_ast);
140140
}
141141

142142
#[test]
143143
fn test_aug_assign_name() {
144144
let source = "x += 1";
145-
let parse_ast = parse_program(source, "<test>").unwrap();
145+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
146146
insta::assert_debug_snapshot!(parse_ast);
147147
}
148148

149149
#[test]
150150
fn test_aug_assign_attribute() {
151151
let source = "x.y += (1, 2, 3)";
152-
let parse_ast = parse_program(source, "<test>").unwrap();
152+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
153153
insta::assert_debug_snapshot!(parse_ast);
154154
}
155155

156156
#[test]
157157
fn test_aug_assign_subscript() {
158158
let source = "x[y] += (1, 2, 3)";
159-
let parse_ast = parse_program(source, "<test>").unwrap();
159+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
160160
insta::assert_debug_snapshot!(parse_ast);
161161
}
162162

163163
#[test]
164164
fn test_del_name() {
165165
let source = "del x";
166-
let parse_ast = parse_program(source, "<test>").unwrap();
166+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
167167
insta::assert_debug_snapshot!(parse_ast);
168168
}
169169

170170
#[test]
171171
fn test_del_attribute() {
172172
let source = "del x.y";
173-
let parse_ast = parse_program(source, "<test>").unwrap();
173+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
174174
insta::assert_debug_snapshot!(parse_ast);
175175
}
176176

177177
#[test]
178178
fn test_del_subscript() {
179179
let source = "del x[y]";
180-
let parse_ast = parse_program(source, "<test>").unwrap();
180+
let parse_ast = ast::Suite::parse(source, "<test>").unwrap();
181181
insta::assert_debug_snapshot!(parse_ast);
182182
}
183183
}

parser/src/function.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ const fn is_starred(exp: &ast::Expr) -> bool {
155155
#[cfg(test)]
156156
mod tests {
157157
use super::*;
158-
use crate::parser::{parse_program, ParseErrorType};
158+
use crate::{ast, parser::ParseErrorType, Parse};
159159

160160
#[cfg(not(feature = "all-nodes-with-ranges"))]
161161
macro_rules! function_and_lambda {
162162
($($name:ident: $code:expr,)*) => {
163163
$(
164164
#[test]
165165
fn $name() {
166-
let parse_ast = parse_program($code, "<test>");
166+
let parse_ast = ast::Suite::parse($code, "<test>");
167167
insta::assert_debug_snapshot!(parse_ast);
168168
}
169169
)*
@@ -190,7 +190,7 @@ mod tests {
190190
}
191191

192192
fn function_parse_error(src: &str) -> LexicalErrorType {
193-
let parse_ast = parse_program(src, "<test>");
193+
let parse_ast = ast::Suite::parse(src, "<test>");
194194
parse_ast
195195
.map_err(|e| match e.error {
196196
ParseErrorType::Lexical(e) => e,

parser/src/lexer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub fn lex_starts_at(
212212
source: &str,
213213
mode: Mode,
214214
start_offset: TextSize,
215-
) -> impl Iterator<Item = LexResult> + '_ {
215+
) -> SoftKeywordTransformer<Lexer<std::str::Chars<'_>>> {
216216
SoftKeywordTransformer::new(Lexer::new(source.chars(), start_offset), mode)
217217
}
218218

parser/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@
9494
//! mode or tokenizing the source beforehand:
9595
//!
9696
//! ```
97-
//! use rustpython_parser::parse_program;
97+
//! use rustpython_parser::{Parse, ast};
9898
//!
9999
//! let python_source = r#"
100100
//! def is_odd(i):
101101
//! return bool(i & 1)
102102
//! "#;
103-
//! let ast = parse_program(python_source, "<embedded>");
103+
//! let ast = ast::Suite::parse(python_source, "<embedded>");
104104
//!
105105
//! assert!(ast.is_ok());
106106
//! ```
@@ -126,13 +126,13 @@ mod soft_keywords;
126126
mod string;
127127
mod token;
128128

129-
pub use parser::{
130-
parse, parse_expression, parse_expression_starts_at, parse_program, parse_starts_at,
131-
parse_tokens, ParseError, ParseErrorType,
132-
};
129+
pub use parser::{parse, parse_starts_at, parse_tokens, Parse, ParseError, ParseErrorType};
133130
pub use string::FStringErrorType;
134131
pub use token::{StringKind, Tok};
135132

133+
#[allow(deprecated)]
134+
pub use parser::{parse_expression, parse_expression_starts_at, parse_program};
135+
136136
#[rustfmt::skip]
137137
mod python {
138138
#![allow(clippy::all)]

0 commit comments

Comments
 (0)