Skip to content

Commit

Permalink
improvements to unfinished comments inside fns
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Aug 18, 2023
1 parent 9f5d54a commit d106e62
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
25 changes: 14 additions & 11 deletions sway-parse/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,29 @@ impl ParseToEnd for CodeBlockContents {
mut parser: Parser<'a, '_>,
) -> ParseResult<(CodeBlockContents, ParserConsumed<'a>)> {
let mut statements = Vec::new();

let (final_expr_opt, consumed) = loop {
if let Some(consumed) = parser.check_empty() {
break (None, consumed);
}
match parse_stmt(&mut parser)? {
StmtOrTail::Stmt(s) => statements.push(s),
StmtOrTail::Tail(e, c) => break (Some(e), c),

match parser.parse_fn_with_recovery(parse_stmt) {
Ok(StmtOrTail::Stmt(s)) => statements.push(s),
Ok(StmtOrTail::Tail(e, c)) => break (Some(e), c),
Err(r) => {
let (spans, error) = r
.recover_at_next_line_with_fallback_error(ParseErrorKind::InvalidStatement);
statements.push(Statement::Error(spans, error));
}
}
};

let code_block_contents = CodeBlockContents {
statements,
final_expr_opt,
span: parser.full_span().clone(),
};

Ok((code_block_contents, consumed))
}
}
Expand Down Expand Up @@ -187,14 +196,8 @@ fn parse_stmt<'a>(parser: &mut Parser<'a, '_>) -> ParseResult<StmtOrTail<'a>> {
}

// Try a `let` statement.
match parser.guarded_parse_with_recovery::<LetToken, StatementLet>() {
Ok(None) => {}
Ok(Some(item)) => return stmt(Statement::Let(item)),
Err(r) => {
let (spans, error) =
r.recover_at_next_line_with_fallback_error(ParseErrorKind::InvalidStatement);
return stmt(Statement::Error(spans, error));
}
if let Some(item) = parser.guarded_parse::<LetToken, StatementLet>()? {
return stmt(Statement::Let(item));
}

// Try an `expr;` statement.
Expand Down
38 changes: 38 additions & 0 deletions sway-parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,44 @@ impl<'a, 'e> Parser<'a, 'e> {
Peeker::with(self.token_trees).map(|(v, _)| v)
}

pub fn parse_fn_with_recovery<
'original,
T,
F: FnOnce(&mut Parser<'a, '_>) -> ParseResult<T>,
>(
&'original mut self,
f: F,
) -> Result<T, Recoverer<'original, 'a, 'e>> {
let handler = Handler::default();
let mut fork = Parser {
token_trees: self.token_trees,
full_span: self.full_span.clone(),
handler: &handler,
};

match f(&mut fork) {
Ok(result) => {
self.token_trees = fork.token_trees;
self.handler.append(handler);
Ok(result)
}
Err(error) => {
let Parser {
token_trees,
full_span,
..
} = fork;
Err(Recoverer {
original: RefCell::new(self),
handler,
fork_token_trees: token_trees,
fork_full_span: full_span,
error,
})
}
}
}

pub fn parse_with_recovery<'original, T: Parse>(
&'original mut self,
) -> Result<T, Recoverer<'original, 'a, 'e>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ mod foo;
mod bar;
mod baz;

fn f() {
/
f2();
}

fn a() -> bool { 0 } // recovery witness

/*
Expand Down

0 comments on commit d106e62

Please sign in to comment.