Skip to content

Commit 2a90df8

Browse files
committed
Run cargo fmt to format the entire project
1 parent a965393 commit 2a90df8

File tree

16 files changed

+276
-233
lines changed

16 files changed

+276
-233
lines changed

parser/src/lexer.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,8 @@ impl<'input> Lexer<'input> {
119119
Some('\r') => {
120120
return;
121121
}
122-
Some(_) => {
123-
}
124-
None => {
125-
return
126-
}
122+
Some(_) => {}
123+
None => return,
127124
}
128125
}
129126
}
@@ -150,12 +147,8 @@ impl<'input> Lexer<'input> {
150147
Some('\\') => {
151148
string_content.push('\\');
152149
}
153-
Some('\'') => {
154-
string_content.push('\'')
155-
}
156-
Some('\"') => {
157-
string_content.push('\"')
158-
}
150+
Some('\'') => string_content.push('\''),
151+
Some('\"') => string_content.push('\"'),
159152
Some('\n') => {
160153
// Ignore Unix EOL character
161154
}
@@ -170,27 +163,17 @@ impl<'input> Lexer<'input> {
170163
}
171164
}
172165
}
173-
Some('a') => {
174-
string_content.push('\x07')
175-
}
176-
Some('b') => {
177-
string_content.push('\x08')
178-
}
179-
Some('f') => {
180-
string_content.push('\x0c')
181-
}
166+
Some('a') => string_content.push('\x07'),
167+
Some('b') => string_content.push('\x08'),
168+
Some('f') => string_content.push('\x0c'),
182169
Some('n') => {
183170
string_content.push('\n');
184171
}
185-
Some('r') => {
186-
string_content.push('\r')
187-
},
172+
Some('r') => string_content.push('\r'),
188173
Some('t') => {
189174
string_content.push('\t');
190175
}
191-
Some('v') => {
192-
string_content.push('\x0b')
193-
}
176+
Some('v') => string_content.push('\x0b'),
194177
Some(c) => {
195178
string_content.push('\\');
196179
string_content.push(c);

parser/src/parser.rs

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ pub fn parse(filename: &Path) -> Result<ast::Program, String> {
4343
pub fn parse_program(source: &String) -> Result<ast::Program, String> {
4444
let lxr = lexer::Lexer::new(&source);
4545
match python::ProgramParser::new().parse(lxr) {
46-
Err(lalrpop_util::ParseError::UnrecognizedToken{token: None, expected: _}) =>
47-
Err(String::from("Unexpected end of input.")),
46+
Err(lalrpop_util::ParseError::UnrecognizedToken {
47+
token: None,
48+
expected: _,
49+
}) => Err(String::from("Unexpected end of input.")),
4850
Err(why) => Err(String::from(format!("{:?}", why))),
4951
Ok(p) => Ok(p),
5052
}
@@ -76,12 +78,7 @@ mod tests {
7678
fn test_parse_empty() {
7779
let parse_ast = parse_program(&String::from("\n"));
7880

79-
assert_eq!(
80-
parse_ast,
81-
Ok(ast::Program {
82-
statements: vec![]
83-
})
84-
)
81+
assert_eq!(parse_ast, Ok(ast::Program { statements: vec![] }))
8582
}
8683

8784
#[test]
@@ -91,20 +88,16 @@ mod tests {
9188
assert_eq!(
9289
parse_ast,
9390
ast::Program {
94-
statements: vec![
95-
ast::Statement::Expression {
96-
expression: ast::Expression::Call {
97-
function: Box::new(ast::Expression::Identifier {
98-
name: String::from("print"),
99-
}),
100-
args: vec![
101-
ast::Expression::String {
102-
value: String::from("Hello world"),
103-
},
104-
],
105-
},
91+
statements: vec![ast::Statement::Expression {
92+
expression: ast::Expression::Call {
93+
function: Box::new(ast::Expression::Identifier {
94+
name: String::from("print"),
95+
}),
96+
args: vec![ast::Expression::String {
97+
value: String::from("Hello world"),
98+
},],
10699
},
107-
],
100+
},],
108101
}
109102
);
110103
}
@@ -116,21 +109,19 @@ mod tests {
116109
assert_eq!(
117110
parse_ast,
118111
ast::Program {
119-
statements: vec![
120-
ast::Statement::Expression {
121-
expression: ast::Expression::Call {
122-
function: Box::new(ast::Expression::Identifier {
123-
name: String::from("print"),
124-
}),
125-
args: vec![
126-
ast::Expression::String {
127-
value: String::from("Hello world"),
128-
},
129-
ast::Expression::Number { value: 2 },
130-
],
131-
},
112+
statements: vec![ast::Statement::Expression {
113+
expression: ast::Expression::Call {
114+
function: Box::new(ast::Expression::Identifier {
115+
name: String::from("print"),
116+
}),
117+
args: vec![
118+
ast::Expression::String {
119+
value: String::from("Hello world"),
120+
},
121+
ast::Expression::Number { value: 2 },
122+
],
132123
},
133-
],
124+
},],
134125
}
135126
);
136127
}
@@ -143,26 +134,18 @@ mod tests {
143134
parse_ast,
144135
ast::Statement::If {
145136
test: ast::Expression::Number { value: 1 },
146-
body: vec![
147-
ast::Statement::Expression {
148-
expression: ast::Expression::Number { value: 10 },
149-
},
150-
],
151-
orelse: Some(vec![
152-
ast::Statement::If {
153-
test: ast::Expression::Number { value: 2 },
154-
body: vec![
155-
ast::Statement::Expression {
156-
expression: ast::Expression::Number { value: 20 },
157-
},
158-
],
159-
orelse: Some(vec![
160-
ast::Statement::Expression {
161-
expression: ast::Expression::Number { value: 30 },
162-
},
163-
]),
164-
},
165-
]),
137+
body: vec![ast::Statement::Expression {
138+
expression: ast::Expression::Number { value: 10 },
139+
},],
140+
orelse: Some(vec![ast::Statement::If {
141+
test: ast::Expression::Number { value: 2 },
142+
body: vec![ast::Statement::Expression {
143+
expression: ast::Expression::Number { value: 20 },
144+
},],
145+
orelse: Some(vec![ast::Statement::Expression {
146+
expression: ast::Expression::Number { value: 30 },
147+
},]),
148+
},]),
166149
}
167150
);
168151
}
@@ -176,17 +159,16 @@ mod tests {
176159
Ok(ast::Statement::Expression {
177160
expression: ast::Expression::Lambda {
178161
args: vec![String::from("x"), String::from("y")],
179-
body:
180-
Box::new(ast::Expression::Binop {
181-
a: Box::new(ast::Expression::Identifier {
182-
name: String::from("x"),
183-
}),
184-
op: ast::Operator::Mult,
185-
b: Box::new(ast::Expression::Identifier {
186-
name: String::from("y"),
187-
})
162+
body: Box::new(ast::Expression::Binop {
163+
a: Box::new(ast::Expression::Identifier {
164+
name: String::from("x"),
165+
}),
166+
op: ast::Operator::Mult,
167+
b: Box::new(ast::Expression::Identifier {
168+
name: String::from("y"),
188169
})
189-
}
170+
})
171+
}
190172
})
191173
)
192174
}
@@ -198,13 +180,11 @@ mod tests {
198180
parse_statement(&source),
199181
Ok(ast::Statement::ClassDef {
200182
name: String::from("Foo"),
201-
body: vec![
202-
ast::Statement::FunctionDef {
203-
name: String::from("__init__"),
204-
args: vec![String::from("self")],
205-
body: vec![ast::Statement::Pass],
206-
}
207-
],
183+
body: vec![ast::Statement::FunctionDef {
184+
name: String::from("__init__"),
185+
args: vec![String::from("self")],
186+
body: vec![ast::Statement::Pass],
187+
}],
208188
})
209189
)
210190
}

parser/src/token.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
21
// Loosely based on token.h from CPython source:
3-
#[derive(Debug)]
4-
#[derive(PartialEq)]
2+
#[derive(Debug, PartialEq)]
53
pub enum Tok {
64
Name { name: String },
75
Number { value: i32 },
@@ -21,7 +19,7 @@ pub enum Tok {
2119
Star,
2220
Slash,
2321
Vbar, // '|'
24-
Amper, // '&'
22+
Amper, // '&'
2523
Less,
2624
Greater,
2725
Equal,
@@ -38,26 +36,25 @@ pub enum Tok {
3836
LeftShift,
3937
RightShift,
4038
DoubleStar,
41-
DoubleStarEqual, // '**='
39+
DoubleStarEqual, // '**='
4240
PlusEqual,
4341
MinusEqual,
4442
StarEqual,
4543
SlashEqual,
4644
PercentEqual,
47-
AmperEqual, // '&='
45+
AmperEqual, // '&='
4846
VbarEqual,
49-
CircumflexEqual, // '^='
47+
CircumflexEqual, // '^='
5048
LeftShiftEqual,
5149
RightShiftEqual,
52-
DoubleSlash, // '//'
50+
DoubleSlash, // '//'
5351
DoubleSlashEqual,
5452
At,
5553
AtEqual,
5654
Rarrow,
5755
Ellipsis,
5856

5957
// Keywords (alphabetically):
60-
6158
False,
6259
None,
6360
True,

src/main.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ extern crate rustpython_vm;
99

1010
use clap::{App, Arg};
1111
use rustpython_parser::parser;
12-
use rustpython_vm::VirtualMachine;
1312
use rustpython_vm::compile;
13+
use rustpython_vm::VirtualMachine;
1414
use std::io;
1515
use std::io::prelude::*;
1616
use std::path::Path;
@@ -29,14 +29,12 @@ fn main() {
2929
.short("v")
3030
.multiple(true)
3131
.help("Give the verbosity"),
32-
)
33-
.arg(
32+
).arg(
3433
Arg::with_name("c")
3534
.short("c")
3635
.takes_value(true)
3736
.help("run the given string as a program"),
38-
)
39-
.get_matches();
37+
).get_matches();
4038

4139
// Figure out if a -c option was given:
4240
if let Some(command) = matches.value_of("c") {
@@ -95,7 +93,7 @@ fn shell_exec(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> b
9593
}
9694
Err(msg) => {
9795
println!("Error: {:?}", msg);
98-
},
96+
}
9997
}
10098
}
10199
Err(msg) => {
@@ -140,6 +138,7 @@ fn run_shell() {
140138
let mut vm = VirtualMachine::new();
141139
let builtins = vm.get_builtin_scope();
142140
let vars = vm.context().new_scope(Some(builtins)); // Keep track of local variables
141+
143142
// Read a single line:
144143
let mut input = String::new();
145144
loop {
@@ -162,15 +161,11 @@ fn run_shell() {
162161
Ok(_) => {
163162
shell_exec(&mut vm, &input, vars.clone());
164163
}
165-
Err(msg) => {
166-
panic!("Error: {:?}", msg)
167-
}
164+
Err(msg) => panic!("Error: {:?}", msg),
168165
}
169166
}
170167
}
171-
Err(msg) => {
172-
panic!("Error: {:?}", msg)
173-
}
168+
Err(msg) => panic!("Error: {:?}", msg),
174169
};
175170
}
176171
}

0 commit comments

Comments
 (0)