Skip to content

Commit

Permalink
Various fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed Jun 16, 2018
1 parent ffa8b20 commit 7c0354b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 37 deletions.
20 changes: 19 additions & 1 deletion src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ bop!(and_test, Self::not_test, alt!(
// not_test: 'not' not_test | comparison
named!(not_test<StrSpan, Box<Expression>>,
alt!(
preceded!(tuple!(keyword!("not"), spaces!()), call!(Self::comparison)) => { |e| Box::new(Expression::Uop(Uop::Not, e)) }
preceded!(tuple!(keyword!("not"), spaces!()), call!(Self::not_test)) => { |e| Box::new(Expression::Uop(Uop::Not, e)) }
| call!(Self::comparison)
)
);
Expand Down Expand Up @@ -819,6 +819,24 @@ mod tests {
)));
}

#[test]
fn test_not() {
let test = ExpressionParser::<NewlinesAreNotSpaces>::test;
assert_parse_eq(test(make_strspan("not foo")), Ok((make_strspan(""),
Box::new(Expression::Uop(Uop::Not,
Box::new(Expression::Name("foo".to_string())),
))
)));

assert_parse_eq(test(make_strspan("not not foo")), Ok((make_strspan(""),
Box::new(Expression::Uop(Uop::Not,
Box::new(Expression::Uop(Uop::Not,
Box::new(Expression::Name("foo".to_string())),
))
))
)));
}

#[test]
fn test_parentheses1() {
let test = ExpressionParser::<NewlinesAreNotSpaces>::test;
Expand Down
10 changes: 5 additions & 5 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ named!(escaped_newline<StrSpan, ()>,
);

named!(pub spaces<StrSpan, ()>,
map!(many0!(alt!(one_of!(" \t") => { |_|() } | escaped_newline | newline)), |_| ())
map!(many0!(alt!(one_of!(" \t\x0c") => { |_|() } | escaped_newline | newline)), |_| ())
);

named!(pub spaces2<StrSpan, ()>,
map!(many0!(alt!(one_of!(" \t") => { |_| () }|escaped_newline)), |_| ())
map!(many0!(alt!(one_of!(" \t\x0c") => { |_| () }|escaped_newline)), |_| ())
);

named!(pub space_sep<StrSpan, ()>,
map!(many1!(alt!(one_of!(" \t") => { |_|() } | escaped_newline | newline)), |_| ())
map!(many1!(alt!(one_of!(" \t\x0c") => { |_|() } | escaped_newline | newline)), |_| ())
);

named!(pub space_sep2<StrSpan, ()>,
map!(many1!(alt!(one_of!(" \t") => { |_| () } | escaped_newline)), |_| ())
map!(many1!(alt!(one_of!(" \t\x0c") => { |_| () } | escaped_newline)), |_| ())
);

// Let me explain this ugliness.
Expand Down Expand Up @@ -117,7 +117,7 @@ macro_rules! space_sep {
}
}

const KEYWORDS: [&'static str; 1] = ["yield"];
const KEYWORDS: [&'static str; 2] = ["yield", "import"];
named!(pub name<StrSpan, String>,
do_parse!(
name: map!(
Expand Down
64 changes: 58 additions & 6 deletions src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,22 +242,23 @@ named!(import_from<StrSpan, Import>,
keyword!("from") >>
spaces2 >>
import_from: alt!(
preceded!(char!('.'), do_parse!(
leading_dots: ws2!(map!(many0!(char!('.')), |dots| dots.len()+1)) >>
do_parse!(
leading_dots: ws2!(map!(many0!(char!('.')), |dots| dots.len())) >>
from_name: opt!(call!(ImportParser::<NewlinesAreNotSpaces>::dotted_name)) >> (
(leading_dots, from_name.unwrap_or(Vec::new()))
)
))
)
| call!(ImportParser::<NewlinesAreNotSpaces>::dotted_name) => { |n| (0, n) }
) >>
space_sep2 >>
keyword!("import") >>
spaces2 >>
dbg_dmp!(keyword!("import")) >>
spaces2 >>
names: alt!(
char!('*') => { |_| Vec::new() }
| ws2!(delimited!(char!('('), call!(ImportParser::<NewlinesAreSpaces>::import_as_names), char!(')')))
| call!(ImportParser::<NewlinesAreNotSpaces>::import_as_names)
) >> ({
) >>
({
let (leading_dots, path) = import_from;
if names.len() > 0 {
Import::ImportFrom { leading_dots, path, names }
Expand Down Expand Up @@ -1108,6 +1109,57 @@ mod tests {
)));
}

#[test]
fn test_import_from () {
assert_parse_eq(statement(make_strspan("from . import foo"), 0), Ok((make_strspan(""),
vec![Statement::Import(Import::ImportFrom {
leading_dots: 1,
path: vec![],
names: vec![("foo".to_string(), None)],
})]
)));

assert_parse_eq(statement(make_strspan("from . import foo as bar"), 0), Ok((make_strspan(""),
vec![Statement::Import(Import::ImportFrom {
leading_dots: 1,
path: vec![],
names: vec![("foo".to_string(), Some("bar".to_string()))],
})]
)));

assert_parse_eq(statement(make_strspan("from qux import foo"), 0), Ok((make_strspan(""),
vec![Statement::Import(Import::ImportFrom {
leading_dots: 0,
path: vec!["qux".to_string()],
names: vec![("foo".to_string(), None)],
})]
)));

assert_parse_eq(statement(make_strspan("from qux import foo as bar"), 0), Ok((make_strspan(""),
vec![Statement::Import(Import::ImportFrom {
leading_dots: 0,
path: vec!["qux".to_string()],
names: vec![("foo".to_string(), Some("bar".to_string()))],
})]
)));

assert_parse_eq(statement(make_strspan("from .qux import foo"), 0), Ok((make_strspan(""),
vec![Statement::Import(Import::ImportFrom {
leading_dots: 1,
path: vec!["qux".to_string()],
names: vec![("foo".to_string(), None)],
})]
)));

assert_parse_eq(statement(make_strspan("from .qux import foo as bar"), 0), Ok((make_strspan(""),
vec![Statement::Import(Import::ImportFrom {
leading_dots: 1,
path: vec!["qux".to_string()],
names: vec![("foo".to_string(), Some("bar".to_string()))],
})]
)));
}

#[test]
fn test_unpack() {
assert_parse_eq(testlist_star_expr(make_strspan("foo,")), Ok((make_strspan(""),
Expand Down
34 changes: 9 additions & 25 deletions src/visitors/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,34 +383,26 @@ fn format_typed_param(param: &(Name, Option<Expression>, Option<Expression>)) ->

fn format_untyped_params(param: &UntypedArgsList) -> String {
let UntypedArgsList { ref positional_args, ref star_args, ref keyword_args, ref star_kwargs } = *param;
let mut s = String::new();

s.push_str(&comma_join(positional_args.iter().map(format_untyped_param)));
if positional_args.len() > 0 {
s.push_str(", ");
}
let mut chunks = Vec::new();

chunks.extend(positional_args.iter().map(format_untyped_param));

match star_args {
StarParams::No => (),
StarParams::Anonymous => s.push_str("*, "),
StarParams::Anonymous => chunks.push("*".to_string()),
StarParams::Named(ref name) => {
s.push_str("*");
s.push_str(name);
chunks.push(format!("*{}", name))
},
}

s.push_str(&comma_join(keyword_args.iter().map(format_untyped_param)));
if keyword_args.len() > 0 {
s.push_str(", ");
}
chunks.extend(keyword_args.iter().map(format_untyped_param));

if let Some(name) = star_kwargs {
s.push_str("**");
s.push_str(name);
s.push_str(", ");
chunks.push(format!("**{}", name));
}

s
comma_join(&chunks)
}

fn format_untyped_param(param: &(Name, Option<Expression>)) -> String {
Expand Down Expand Up @@ -522,10 +514,6 @@ fn format_expr(e: &Expression) -> String {

Expression::Call(e, ref args) => {
match **e {
Expression::Ellipsis | Expression::None | Expression::True |
Expression::False | Expression::Int(_) |
Expression::ImaginaryInt(_) | Expression::ImaginaryFloat(_) |
Expression::Float(_) | Expression::String(_) | Expression::Bytes(_) |
Expression::Name(_) | Expression::DictComp(_, _) | Expression::SetComp(_, _) |
Expression::ListComp(_, _) | Expression::Generator(_, _) |
Expression::DictLiteral(_) | Expression::SetLiteral(_) |
Expand All @@ -536,13 +524,9 @@ fn format_expr(e: &Expression) -> String {
}
},
Expression::Subscript(e, ref sub) =>
format!("{}[{}]", format_expr(e), comma_join(sub.iter().map(format_subscript))),
format!("({})[{}]", format_expr(e), comma_join(sub.iter().map(format_subscript))),
Expression::Attribute(e, ref n) => {
match **e {
Expression::Ellipsis | Expression::None | Expression::True |
Expression::False | Expression::Int(_) |
Expression::ImaginaryInt(_) | Expression::ImaginaryFloat(_) |
Expression::Float(_) | Expression::String(_) | Expression::Bytes(_) |
Expression::Name(_) | Expression::DictComp(_, _) | Expression::SetComp(_, _) |
Expression::ListComp(_, _) | Expression::Generator(_, _) |
Expression::DictLiteral(_) | Expression::SetLiteral(_) |
Expand Down

0 comments on commit 7c0354b

Please sign in to comment.