Skip to content

Commit 570b284

Browse files
committed
Fix tests
1 parent 995fac1 commit 570b284

File tree

7 files changed

+72
-69
lines changed

7 files changed

+72
-69
lines changed

parser/src/error.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
extern crate lalrpop_util;
44
use self::lalrpop_util::ParseError as InnerError;
55

6-
use lexer::{Location, LexicalError};
6+
use lexer::{LexicalError, Location};
77
use token::Tok;
88

99
use std::error::Error;
@@ -13,7 +13,7 @@ use std::fmt;
1313
type TokSpan = (Location, Tok, Location);
1414

1515
/// Represents an error during parsing
16-
#[derive(Debug)]
16+
#[derive(Debug, PartialEq)]
1717
pub enum ParseError {
1818
/// Parser encountered an unexpected end of input
1919
EOF(Option<Location>),
@@ -35,7 +35,7 @@ impl From<InnerError<Location, Tok, LexicalError>> for ParseError {
3535
InnerError::InvalidToken { location } => ParseError::EOF(Some(location)),
3636
InnerError::ExtraToken { token } => ParseError::ExtraToken(token),
3737
// Inner field is a unit-like enum `LexicalError::StringError` with no useful info
38-
InnerError::User { ..} => ParseError::Other,
38+
InnerError::User { .. } => ParseError::Other,
3939
InnerError::UnrecognizedToken { token, expected } => {
4040
match token {
4141
Some(tok) => ParseError::UnrecognizedToken(tok, expected),
@@ -59,8 +59,10 @@ impl fmt::Display for ParseError {
5959
}
6060
ParseError::ExtraToken(ref t_span) => {
6161
write!(f, "Got extraneous token: {:?} at: {:?}", t_span.1, t_span.0)
62-
},
63-
ParseError::InvalidToken(ref location) => write!(f, "Got invalid token at: {:?}", location),
62+
}
63+
ParseError::InvalidToken(ref location) => {
64+
write!(f, "Got invalid token at: {:?}", location)
65+
}
6466
ParseError::UnrecognizedToken(ref t_span, _) => {
6567
write!(f, "Got unexpected token: {:?} at {:?}", t_span.1, t_span.0)
6668
}

parser/src/parser.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ mod tests {
7878
#[test]
7979
fn test_parse_empty() {
8080
let parse_ast = parse_program(&String::from("\n"));
81-
8281
assert_eq!(parse_ast, Ok(ast::Program { statements: vec![] }))
8382
}
8483

src/main.rs

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,36 @@ fn run_script(vm: &mut VirtualMachine, script_file: &str) -> PyResult {
123123
}
124124
}
125125

126-
fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool {
127-
match compile::compile(source, &compile::Mode::Single, "<stdin>".to_string(), vm.ctx.code_type()) {
126+
fn shell_exec(
127+
vm: &mut VirtualMachine,
128+
source: &str,
129+
scope: PyObjectRef,
130+
) -> Result<(), CompileError> {
131+
match compile::compile(
132+
source,
133+
&compile::Mode::Single,
134+
"<stdin>".to_string(),
135+
vm.ctx.code_type(),
136+
) {
128137
Ok(code) => {
129138
if let Err(err) = vm.run_code_obj(code, scope) {
130139
print_exception(vm, &err);
131140
}
141+
Ok(())
132142
}
133143
// Don't inject syntax errors for line continuation
134-
Err(CompileError::Parse(ParseError::EOF(_))) => {
135-
return false;
136-
}
144+
Err(err @ CompileError::Parse(ParseError::EOF(_))) => Err(err),
137145
Err(err) => {
138146
let syntax_error = vm.context().exceptions.syntax_error.clone();
139147
let exc = vm.new_exception(syntax_error, format!("{}", err));
140148
print_exception(vm, &exc);
149+
Err(err)
141150
}
142-
};
143-
true
151+
}
144152
}
145153

146154
#[cfg(not(target_family = "unix"))]
147155
fn get_history_path() -> PathBuf {
148-
//Path buffer
149156
PathBuf::from(".repl_history.txt")
150157
}
151158

@@ -169,57 +176,34 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
169176

170177
// Read a single line:
171178
let mut input = String::new();
172-
let mut rl = Editor::<()>::new();
179+
let mut repl = Editor::<()>::new();
173180

174-
//retrieve a history_path_str dependent to the os
181+
// Retrieve a `history_path_str` dependent on the OS
175182
let repl_history_path_str = &get_history_path();
176-
if rl.load_history(repl_history_path_str).is_err() {
183+
if repl.load_history(repl_history_path_str).is_err() {
177184
println!("No previous history.");
178185
}
179186

180-
loop {
181-
// TODO: modules don't support getattr / setattr yet
182-
//let prompt = match vm.get_attribute(vm.sys_module.clone(), "ps1") {
183-
// Ok(value) => objstr::get_value(&value),
184-
// Err(_) => ">>>>> ".to_string(),
185-
//};
186-
187-
// We can customize the prompt:
188-
let ps1 = objstr::get_value(&vm.sys_module.get_attr("ps1").unwrap());
189-
let ps2 = objstr::get_value(&vm.sys_module.get_attr("ps2").unwrap());
187+
let ps1 = &objstr::get_value(&vm.sys_module.get_attr("ps1").unwrap());
188+
let ps2 = &objstr::get_value(&vm.sys_module.get_attr("ps2").unwrap());
189+
let mut prompt = ps1;
190190

191-
match rl.readline(&ps1) {
191+
loop {
192+
match repl.readline(prompt) {
192193
Ok(line) => {
194+
debug!("You entered {:?}", line);
193195
input.push_str(&line);
194196
input.push_str("\n");
197+
repl.add_history_entry(line.trim_end().as_ref());
195198

196-
debug!("You entered {:?}", input);
197-
if shell_exec(vm, &input, vars.clone()) {
198-
// Line was complete.
199-
rl.add_history_entry(input.trim_end());
200-
input = String::new();
201-
} else {
202-
loop {
203-
// until an empty line is pressed AND the code is complete
204-
//let prompt = match vm.get_attribute(vm.sys_module.clone(), "ps2") {
205-
// Ok(value) => objstr::get_value(&value),
206-
// Err(_) => "..... ".to_string(),
207-
//};
208-
match rl.readline(&ps2) {
209-
Ok(line) => {
210-
if line.is_empty() {
211-
if shell_exec(vm, &input, vars.clone()) {
212-
rl.add_history_entry(input.trim_end());
213-
input = String::new();
214-
break;
215-
}
216-
} else {
217-
input.push_str(&line);
218-
input.push_str("\n");
219-
}
220-
}
221-
Err(msg) => panic!("Error: {:?}", msg),
222-
}
199+
match shell_exec(vm, &input, vars.clone()) {
200+
Err(CompileError::Parse(ParseError::EOF(_))) => {
201+
prompt = ps2;
202+
continue;
203+
}
204+
_ => {
205+
prompt = ps1;
206+
input = String::new();
223207
}
224208
}
225209
}
@@ -237,7 +221,7 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
237221
}
238222
};
239223
}
240-
rl.save_history(repl_history_path_str).unwrap();
224+
repl.save_history(repl_history_path_str).unwrap();
241225

242226
Ok(vm.get_none())
243227
}

vm/src/builtins.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
203203
let source = objstr::get_value(source);
204204
// TODO: fix this newline bug:
205205
let source = format!("{}\n", source);
206-
compile::compile(&source, &mode, "<string>".to_string(), vm.ctx.code_type()).map_err(|err| {
207-
let syntax_error = vm.context().exceptions.syntax_error.clone();
208-
vm.new_exception(syntax_error, err.description().to_string())
209-
})?
206+
compile::compile(&source, &mode, "<string>".to_string(), vm.ctx.code_type()).map_err(
207+
|err| {
208+
let syntax_error = vm.context().exceptions.syntax_error.clone();
209+
vm.new_exception(syntax_error, err.description().to_string())
210+
},
211+
)?
210212
} else {
211213
return Err(vm.new_type_error("code argument must be str or code object".to_string()));
212214
};
@@ -252,10 +254,12 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
252254
let source = objstr::get_value(source);
253255
// TODO: fix this newline bug:
254256
let source = format!("{}\n", source);
255-
compile::compile(&source, &mode, "<string>".to_string(), vm.ctx.code_type()).map_err(|err| {
256-
let syntax_error = vm.context().exceptions.syntax_error.clone();
257-
vm.new_exception(syntax_error, err.description().to_string())
258-
})?
257+
compile::compile(&source, &mode, "<string>".to_string(), vm.ctx.code_type()).map_err(
258+
|err| {
259+
let syntax_error = vm.context().exceptions.syntax_error.clone();
260+
vm.new_exception(syntax_error, err.description().to_string())
261+
},
262+
)?
259263
} else if objtype::isinstance(source, &vm.ctx.code_type()) {
260264
source.clone()
261265
} else {

vm/src/eval.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ pub fn eval(
1212
scope: PyObjectRef,
1313
source_path: &str,
1414
) -> PyResult {
15-
match compile::compile(source, &compile::Mode::Eval, source_path.to_string(), vm.ctx.code_type()) {
15+
match compile::compile(
16+
source,
17+
&compile::Mode::Eval,
18+
source_path.to_string(),
19+
vm.ctx.code_type(),
20+
) {
1621
Ok(bytecode) => {
1722
debug!("Code object: {:?}", bytecode);
1823
vm.run_code_obj(bytecode, scope)

vm/src/stdlib/ast.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,8 @@ fn ast_parse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
571571
arg_check!(vm, args, required = [(source, Some(vm.ctx.str_type()))]);
572572

573573
let source_string = objstr::get_value(source);
574-
let internal_ast = parser::parse_program(&source_string).map_err(|err| {
575-
vm.new_value_error(format!("{}", err))
576-
})?;
574+
let internal_ast = parser::parse_program(&source_string)
575+
.map_err(|err| vm.new_value_error(format!("{}", err)))?;
577576
// source.clone();
578577
let ast_node = program_to_ast(&vm.ctx, &internal_ast);
579578
Ok(ast_node)

wasm/lib/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use js_sys::{Array, Object, Reflect, TypeError};
99
use rustpython_vm::compile;
1010
use rustpython_vm::pyobject::{self, PyFuncArgs, PyObjectRef, PyResult};
1111
use rustpython_vm::VirtualMachine;
12+
use std::error::Error;
1213
use wasm_bindgen::{prelude::*, JsCast};
1314

1415
// Hack to comment out wasm-bindgen's typescript definitions
@@ -123,7 +124,16 @@ fn eval(vm: &mut VirtualMachine, source: &str, vars: PyObjectRef) -> PyResult {
123124
source.push('\n');
124125
}
125126

126-
let code_obj = compile::compile(&source, &compile::Mode::Exec, "<string>".to_string(), vm.ctx.code_type())?;
127+
let code_obj = compile::compile(
128+
&source,
129+
&compile::Mode::Exec,
130+
"<string>".to_string(),
131+
vm.ctx.code_type(),
132+
)
133+
.map_err(|err| {
134+
let syntax_error = vm.context().exceptions.syntax_error.clone();
135+
vm.new_exception(syntax_error, err.description().to_string())
136+
})?;
127137

128138
vm.run_code_obj(code_obj, vars)
129139
}

0 commit comments

Comments
 (0)