forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.rs
More file actions
189 lines (174 loc) · 5.99 KB
/
parser.rs
File metadata and controls
189 lines (174 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::error::Error;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use super::ast;
use super::lexer;
use super::python;
pub fn read_file(filename: &Path) -> Result<String, String> {
match File::open(&filename) {
Ok(mut file) => {
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => Err(String::from("Reading file failed: ") + why.description()),
Ok(_) => Ok(s),
}
}
Err(why) => Err(String::from("Opening file failed: ") + why.description()),
}
}
/*
* Parse python code.
* Grammar may be inspired by antlr grammar for python:
* https://github.com/antlr/grammars-v4/tree/master/python3
*/
pub fn parse(filename: &Path) -> Result<ast::Program, String> {
info!("Parsing: {}", filename.display());
match read_file(filename) {
Ok(txt) => {
debug!("Read contents of file: {}", txt);
parse_program(&txt)
}
Err(msg) => Err(msg),
}
}
pub fn parse_program(source: &String) -> Result<ast::Program, String> {
let lxr = lexer::Lexer::new(&source);
match python::ProgramParser::new().parse(lxr) {
Err(why) => Err(String::from(format!("{:?}", why))),
Ok(p) => Ok(p),
}
}
pub fn parse_statement(source: &String) -> Result<ast::Statement, String> {
let lxr = lexer::Lexer::new(&source);
match python::StatementParser::new().parse(lxr) {
Err(why) => Err(String::from(format!("{:?}", why))),
Ok(p) => Ok(p),
}
}
pub fn parse_expression(source: &String) -> Result<ast::Expression, String> {
let lxr = lexer::Lexer::new(&source);
match python::ExpressionParser::new().parse(lxr) {
Err(why) => Err(String::from(format!("{:?}", why))),
Ok(p) => Ok(p),
}
}
#[cfg(test)]
mod tests {
use super::ast;
use super::parse_program;
use super::parse_statement;
#[test]
fn test_parse_empty() {
let parse_ast = parse_program(&String::from("\n"));
assert_eq!(
parse_ast,
Ok(ast::Program {
statements: vec![]
})
)
}
#[test]
fn test_parse_print_hello() {
let source = String::from("print('Hello world')\n");
let parse_ast = parse_program(&source).unwrap();
assert_eq!(
parse_ast,
ast::Program {
statements: vec![
ast::Statement::Expression {
expression: ast::Expression::Call {
function: Box::new(ast::Expression::Identifier {
name: String::from("print"),
}),
args: vec![
ast::Expression::String {
value: String::from("Hello world"),
},
],
},
},
],
}
);
}
#[test]
fn test_parse_print_2() {
let source = String::from("print('Hello world', 2)\n");
let parse_ast = parse_program(&source).unwrap();
assert_eq!(
parse_ast,
ast::Program {
statements: vec![
ast::Statement::Expression {
expression: ast::Expression::Call {
function: Box::new(ast::Expression::Identifier {
name: String::from("print"),
}),
args: vec![
ast::Expression::String {
value: String::from("Hello world"),
},
ast::Expression::Number { value: 2 },
],
},
},
],
}
);
}
#[test]
fn test_parse_if_elif_else() {
let source = String::from("if 1: 10\nelif 2: 20\nelse: 30\n");
let parse_ast = parse_statement(&source).unwrap();
assert_eq!(
parse_ast,
ast::Statement::If {
test: ast::Expression::Number { value: 1 },
body: vec![
ast::Statement::Expression {
expression: ast::Expression::Number { value: 10 },
},
],
orelse: Some(vec![
ast::Statement::If {
test: ast::Expression::Number { value: 2 },
body: vec![
ast::Statement::Expression {
expression: ast::Expression::Number { value: 20 },
},
],
orelse: Some(vec![
ast::Statement::Expression {
expression: ast::Expression::Number { value: 30 },
},
]),
},
]),
}
);
}
#[test]
fn test_parse_lambda() {
let source = String::from("lambda x, y: x * y\n"); // lambda(x, y): x * y");
let parse_ast = parse_statement(&source);
assert_eq!(
parse_ast,
Ok(ast::Statement::Expression {
expression: ast::Expression::Lambda {
args: vec![String::from("x"), String::from("y")],
body:
Box::new(ast::Expression::Binop {
a: Box::new(ast::Expression::Identifier {
name: String::from("x"),
}),
op: ast::Operator::Mult,
b: Box::new(ast::Expression::Identifier {
name: String::from("y"),
})
})
}
})
)
}
}