Skip to content

Commit 7c0354b

Browse files
committed
Various fixes.
1 parent ffa8b20 commit 7c0354b

4 files changed

Lines changed: 91 additions & 37 deletions

File tree

src/expressions.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ bop!(and_test, Self::not_test, alt!(
128128
// not_test: 'not' not_test | comparison
129129
named!(not_test<StrSpan, Box<Expression>>,
130130
alt!(
131-
preceded!(tuple!(keyword!("not"), spaces!()), call!(Self::comparison)) => { |e| Box::new(Expression::Uop(Uop::Not, e)) }
131+
preceded!(tuple!(keyword!("not"), spaces!()), call!(Self::not_test)) => { |e| Box::new(Expression::Uop(Uop::Not, e)) }
132132
| call!(Self::comparison)
133133
)
134134
);
@@ -819,6 +819,24 @@ mod tests {
819819
)));
820820
}
821821

822+
#[test]
823+
fn test_not() {
824+
let test = ExpressionParser::<NewlinesAreNotSpaces>::test;
825+
assert_parse_eq(test(make_strspan("not foo")), Ok((make_strspan(""),
826+
Box::new(Expression::Uop(Uop::Not,
827+
Box::new(Expression::Name("foo".to_string())),
828+
))
829+
)));
830+
831+
assert_parse_eq(test(make_strspan("not not foo")), Ok((make_strspan(""),
832+
Box::new(Expression::Uop(Uop::Not,
833+
Box::new(Expression::Uop(Uop::Not,
834+
Box::new(Expression::Name("foo".to_string())),
835+
))
836+
))
837+
)));
838+
}
839+
822840
#[test]
823841
fn test_parentheses1() {
824842
let test = ExpressionParser::<NewlinesAreNotSpaces>::test;

src/helpers.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ named!(escaped_newline<StrSpan, ()>,
5555
);
5656

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

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

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

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

7373
// Let me explain this ugliness.
@@ -117,7 +117,7 @@ macro_rules! space_sep {
117117
}
118118
}
119119

120-
const KEYWORDS: [&'static str; 1] = ["yield"];
120+
const KEYWORDS: [&'static str; 2] = ["yield", "import"];
121121
named!(pub name<StrSpan, String>,
122122
do_parse!(
123123
name: map!(

src/statements.rs

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,23 @@ named!(import_from<StrSpan, Import>,
242242
keyword!("from") >>
243243
spaces2 >>
244244
import_from: alt!(
245-
preceded!(char!('.'), do_parse!(
246-
leading_dots: ws2!(map!(many0!(char!('.')), |dots| dots.len()+1)) >>
245+
do_parse!(
246+
leading_dots: ws2!(map!(many0!(char!('.')), |dots| dots.len())) >>
247247
from_name: opt!(call!(ImportParser::<NewlinesAreNotSpaces>::dotted_name)) >> (
248248
(leading_dots, from_name.unwrap_or(Vec::new()))
249249
)
250-
))
250+
)
251251
| call!(ImportParser::<NewlinesAreNotSpaces>::dotted_name) => { |n| (0, n) }
252252
) >>
253-
space_sep2 >>
254-
keyword!("import") >>
253+
spaces2 >>
254+
dbg_dmp!(keyword!("import")) >>
255255
spaces2 >>
256256
names: alt!(
257257
char!('*') => { |_| Vec::new() }
258258
| ws2!(delimited!(char!('('), call!(ImportParser::<NewlinesAreSpaces>::import_as_names), char!(')')))
259259
| call!(ImportParser::<NewlinesAreNotSpaces>::import_as_names)
260-
) >> ({
260+
) >>
261+
({
261262
let (leading_dots, path) = import_from;
262263
if names.len() > 0 {
263264
Import::ImportFrom { leading_dots, path, names }
@@ -1108,6 +1109,57 @@ mod tests {
11081109
)));
11091110
}
11101111

1112+
#[test]
1113+
fn test_import_from () {
1114+
assert_parse_eq(statement(make_strspan("from . import foo"), 0), Ok((make_strspan(""),
1115+
vec![Statement::Import(Import::ImportFrom {
1116+
leading_dots: 1,
1117+
path: vec![],
1118+
names: vec![("foo".to_string(), None)],
1119+
})]
1120+
)));
1121+
1122+
assert_parse_eq(statement(make_strspan("from . import foo as bar"), 0), Ok((make_strspan(""),
1123+
vec![Statement::Import(Import::ImportFrom {
1124+
leading_dots: 1,
1125+
path: vec![],
1126+
names: vec![("foo".to_string(), Some("bar".to_string()))],
1127+
})]
1128+
)));
1129+
1130+
assert_parse_eq(statement(make_strspan("from qux import foo"), 0), Ok((make_strspan(""),
1131+
vec![Statement::Import(Import::ImportFrom {
1132+
leading_dots: 0,
1133+
path: vec!["qux".to_string()],
1134+
names: vec![("foo".to_string(), None)],
1135+
})]
1136+
)));
1137+
1138+
assert_parse_eq(statement(make_strspan("from qux import foo as bar"), 0), Ok((make_strspan(""),
1139+
vec![Statement::Import(Import::ImportFrom {
1140+
leading_dots: 0,
1141+
path: vec!["qux".to_string()],
1142+
names: vec![("foo".to_string(), Some("bar".to_string()))],
1143+
})]
1144+
)));
1145+
1146+
assert_parse_eq(statement(make_strspan("from .qux import foo"), 0), Ok((make_strspan(""),
1147+
vec![Statement::Import(Import::ImportFrom {
1148+
leading_dots: 1,
1149+
path: vec!["qux".to_string()],
1150+
names: vec![("foo".to_string(), None)],
1151+
})]
1152+
)));
1153+
1154+
assert_parse_eq(statement(make_strspan("from .qux import foo as bar"), 0), Ok((make_strspan(""),
1155+
vec![Statement::Import(Import::ImportFrom {
1156+
leading_dots: 1,
1157+
path: vec!["qux".to_string()],
1158+
names: vec![("foo".to_string(), Some("bar".to_string()))],
1159+
})]
1160+
)));
1161+
}
1162+
11111163
#[test]
11121164
fn test_unpack() {
11131165
assert_parse_eq(testlist_star_expr(make_strspan("foo,")), Ok((make_strspan(""),

src/visitors/printer.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -383,34 +383,26 @@ fn format_typed_param(param: &(Name, Option<Expression>, Option<Expression>)) ->
383383

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

388-
s.push_str(&comma_join(positional_args.iter().map(format_untyped_param)));
389-
if positional_args.len() > 0 {
390-
s.push_str(", ");
391-
}
387+
let mut chunks = Vec::new();
388+
389+
chunks.extend(positional_args.iter().map(format_untyped_param));
392390

393391
match star_args {
394392
StarParams::No => (),
395-
StarParams::Anonymous => s.push_str("*, "),
393+
StarParams::Anonymous => chunks.push("*".to_string()),
396394
StarParams::Named(ref name) => {
397-
s.push_str("*");
398-
s.push_str(name);
395+
chunks.push(format!("*{}", name))
399396
},
400397
}
401398

402-
s.push_str(&comma_join(keyword_args.iter().map(format_untyped_param)));
403-
if keyword_args.len() > 0 {
404-
s.push_str(", ");
405-
}
399+
chunks.extend(keyword_args.iter().map(format_untyped_param));
406400

407401
if let Some(name) = star_kwargs {
408-
s.push_str("**");
409-
s.push_str(name);
410-
s.push_str(", ");
402+
chunks.push(format!("**{}", name));
411403
}
412404

413-
s
405+
comma_join(&chunks)
414406
}
415407

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

523515
Expression::Call(e, ref args) => {
524516
match **e {
525-
Expression::Ellipsis | Expression::None | Expression::True |
526-
Expression::False | Expression::Int(_) |
527-
Expression::ImaginaryInt(_) | Expression::ImaginaryFloat(_) |
528-
Expression::Float(_) | Expression::String(_) | Expression::Bytes(_) |
529517
Expression::Name(_) | Expression::DictComp(_, _) | Expression::SetComp(_, _) |
530518
Expression::ListComp(_, _) | Expression::Generator(_, _) |
531519
Expression::DictLiteral(_) | Expression::SetLiteral(_) |
@@ -536,13 +524,9 @@ fn format_expr(e: &Expression) -> String {
536524
}
537525
},
538526
Expression::Subscript(e, ref sub) =>
539-
format!("{}[{}]", format_expr(e), comma_join(sub.iter().map(format_subscript))),
527+
format!("({})[{}]", format_expr(e), comma_join(sub.iter().map(format_subscript))),
540528
Expression::Attribute(e, ref n) => {
541529
match **e {
542-
Expression::Ellipsis | Expression::None | Expression::True |
543-
Expression::False | Expression::Int(_) |
544-
Expression::ImaginaryInt(_) | Expression::ImaginaryFloat(_) |
545-
Expression::Float(_) | Expression::String(_) | Expression::Bytes(_) |
546530
Expression::Name(_) | Expression::DictComp(_, _) | Expression::SetComp(_, _) |
547531
Expression::ListComp(_, _) | Expression::Generator(_, _) |
548532
Expression::DictLiteral(_) | Expression::SetLiteral(_) |

0 commit comments

Comments
 (0)