Skip to content

Commit 2c6949d

Browse files
committed
Fix parsing of functions in a block.
1 parent fec39f2 commit 2c6949d

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/functions.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ use ast::*;
1313

1414
// decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
1515
named_args!(decorator(indent: usize) <StrSpan, Decorator>,
16-
preceded!(count!(char!(' '), indent),
17-
ws2!(do_parse!(
18-
char!('@') >>
19-
name: call!(ImportParser::<NewlinesAreNotSpaces>::dotted_name) >>
20-
args: opt!(ws2!(delimited!(char!('('), ws4!(call!(ExpressionParser::<NewlinesAreSpaces>::arglist)), char!(')')))) >>
21-
newline >> (
22-
Decorator { name, args }
23-
)
24-
))
16+
do_parse!(
17+
count!(char!(' '), indent) >>
18+
char!('@') >>
19+
name: ws2!(call!(ImportParser::<NewlinesAreNotSpaces>::dotted_name)) >>
20+
args: opt!(ws2!(delimited!(char!('('), ws4!(call!(ExpressionParser::<NewlinesAreSpaces>::arglist)), char!(')')))) >>
21+
newline >> (
22+
Decorator { name, args }
23+
)
2524
)
2625
);
2726

src/helpers.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ named!(pub name<StrSpan, String>,
126126

127127
named!(pub newline<StrSpan, ()>,
128128
map!(
129-
tuple!(
130-
space,
131-
opt!(preceded!(char!('#'), many0!(none_of!("\n")))),
132-
char!('\n')
129+
many1!(
130+
tuple!(
131+
space,
132+
opt!(preceded!(char!('#'), many0!(none_of!("\n")))),
133+
char!('\n')
134+
)
133135
),
134136
|_| ()
135137
)

src/statements.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro_rules! call_test {
1818
named_args!(pub statement(indent: usize) <StrSpan, Vec<Statement>>,
1919
alt!(
2020
call!(compound_stmt, indent) => { |stmt| vec![Statement::Compound(Box::new(stmt))] }
21-
| call!(simple_stmt)
21+
| preceded!(count!(char!(' '), indent), call!(simple_stmt))
2222
)
2323
);
2424

@@ -324,7 +324,6 @@ named_args!(pub block(indent: usize) <StrSpan, Vec<Statement>>,
324324
stmts: fold_many1!(
325325
do_parse!(
326326
newline >>
327-
count!(char!(' '), new_indent) >>
328327
r: call!(statement, new_indent) >>
329328
(r)
330329
),
@@ -350,7 +349,7 @@ named_args!(cond_and_block(indent: usize) <StrSpan, (Expression, Vec<Statement>)
350349

351350
// compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
352351
named_args!(compound_stmt(indent: usize) <StrSpan, CompoundStatement>,
353-
switch!(peek!(first_word),
352+
switch!(peek!(ws2!(first_word)),
354353
"if" => call!(if_stmt, indent)
355354
| "for" => call!(for_stmt, indent)
356355
| "while" => call!(while_stmt, indent)
@@ -381,6 +380,7 @@ named_args!(else_block(indent: usize) <StrSpan, Option<Vec<Statement>>>,
381380
// if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
382381
named_args!(if_stmt(indent: usize) <StrSpan, CompoundStatement>,
383382
do_parse!(
383+
count!(char!(' '), indent) >>
384384
tag!("if") >>
385385
if_block: call!(cond_and_block, indent) >>
386386
elif_blocks: many0!(
@@ -400,6 +400,7 @@ named_args!(if_stmt(indent: usize) <StrSpan, CompoundStatement>,
400400
// while_stmt: 'while' test ':' suite ['else' ':' suite]
401401
named_args!(while_stmt(indent: usize) <StrSpan, CompoundStatement>,
402402
do_parse!(
403+
count!(char!(' '), indent) >>
403404
tag!("while") >>
404405
while_block: call!(cond_and_block, indent) >>
405406
else_block: call!(else_block, indent) >> ({
@@ -412,6 +413,7 @@ named_args!(while_stmt(indent: usize) <StrSpan, CompoundStatement>,
412413
// for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
413414
named_args!(for_stmt(indent: usize) <StrSpan, CompoundStatement>,
414415
do_parse!(
416+
count!(char!(' '), indent) >>
415417
async: opt!(tuple!(tag!("async"), space_sep2)) >>
416418
tag!("for") >>
417419
space_sep2 >>
@@ -439,6 +441,7 @@ named_args!(for_stmt(indent: usize) <StrSpan, CompoundStatement>,
439441
// except_clause: 'except' [test ['as' NAME]]
440442
named_args!(try_stmt(indent: usize) <StrSpan, CompoundStatement>,
441443
do_parse!(
444+
count!(char!(' '), indent) >>
442445
tag!("try") >>
443446
ws2!(char!(':')) >>
444447
try_block: call!(block, indent) >>
@@ -492,6 +495,7 @@ named_args!(try_stmt(indent: usize) <StrSpan, CompoundStatement>,
492495
// with_item: test ['as' expr]
493496
named_args!(with_stmt(indent: usize) <StrSpan, CompoundStatement>,
494497
do_parse!(
498+
count!(char!(' '), indent) >>
495499
tag!("with") >>
496500
space_sep2 >>
497501
contexts: separated_nonempty_list!(ws2!(char!(',')), do_parse!(
@@ -526,8 +530,10 @@ mod tests {
526530
#[test]
527531
fn test_statement_indent() {
528532
assert_parse_eq(statement(make_strspan("del foo"), 0), Ok((make_strspan(""), vec![Statement::Del(vec!["foo".to_string()])])));
533+
assert_parse_eq(statement(make_strspan(" del foo"), 1), Ok((make_strspan(""), vec![Statement::Del(vec!["foo".to_string()])])));
529534
assert!(statement(make_strspan(" del foo"), 0).is_err());
530-
assert!(statement(make_strspan(" del foo"), 1).is_err());
535+
assert!(statement(make_strspan(" del foo"), 1).is_err());
536+
assert!(statement(make_strspan("del foo"), 1).is_err());
531537
}
532538

533539
#[test]

0 commit comments

Comments
 (0)