Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat: pass error kind via parameter (#1788)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Nov 23, 2021
1 parent eebed7d commit bba1add
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 12 deletions.
27 changes: 24 additions & 3 deletions crates/rslint_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,20 @@ impl<'t> Parser<'t> {
}

/// Recover from an error with a recovery set or by using a `{` or `}`.
///
/// # Arguments
///
/// * `error` - the [Diagnostic] to emit
/// * `recovery` - it recovers the parser position is inside a set [tokens](TokenSet)
/// * `include_braces` - it recovers the parser if the current token is a curly brace
/// * `unknown_node` - The kind of the unknown node the parser inserts if it isn't able to recover because
/// the current token is neither in the recovery set nor any of `{` or `}`.
pub fn err_recover(
&mut self,
error: impl Into<ParserError>,
recovery: TokenSet,
include_braces: bool,
unknown_node: SyntaxKind,
) -> Option<()> {
if self.state.no_recovery {
return None;
Expand All @@ -217,12 +226,24 @@ impl<'t> Parser<'t> {
let m = self.start();
self.error(error);
self.bump_any();
m.complete(self, SyntaxKind::ERROR);
m.complete(self, unknown_node);
Some(())
}

/// Recover from an error but don't add an error to the events
pub fn err_recover_no_err(&mut self, recovery: TokenSet, include_braces: bool) {
///
/// # Arguments
///
/// * `recovery` - it recovers the parser position is inside a set [tokens](TokenSet)
/// * `include_braces` - it recovers the parser if the current token is a curly brace
/// * `unknown_node` - The kind of the unknown node the parser inserts if it isn't able to recover because
/// the current token is neither in the recovery set nor any of `{` or `}`.
pub fn err_recover_no_err(
&mut self,
recovery: TokenSet,
include_braces: bool,
unknown_node: SyntaxKind,
) {
match self.cur() {
T!['{'] | T!['}'] if include_braces => {
return;
Expand All @@ -239,7 +260,7 @@ impl<'t> Parser<'t> {

let m = self.start();
self.bump_any();
m.complete(self, SyntaxKind::ERROR);
m.complete(self, unknown_node);
}

/// Starts a new node in the syntax tree. All nodes and tokens
Expand Down
1 change: 1 addition & 0 deletions crates/rslint_parser/src/syntax/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub(super) fn parameters_list(
T![')'],
],
true,
ERROR,
);
None
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rslint_parser/src/syntax/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ pub fn paren_or_arrow_expr(p: &mut Parser, can_be_arrow: bool) -> CompletedMarke
let err = temp.err_builder(&format!("expect a closing parenthesis after a spread element, but instead found `{}`", temp.cur_src()))
.primary(temp.cur_tok().range, "");

temp.err_recover(err, EXPR_RECOVERY_SET, false);
temp.err_recover(err, EXPR_RECOVERY_SET, false, ERROR);
}
}
break;
Expand Down Expand Up @@ -1085,7 +1085,7 @@ pub fn primary_expr(p: &mut Parser) -> Option<CompletedMarker> {
let err = p
.err_builder("Expected an expression, but found none")
.primary(p.cur_tok().range, "Expected an expression here");
p.err_recover(err, p.state.expr_recovery_set, true);
p.err_recover(err, p.state.expr_recovery_set, true, ERROR);
return None;
}
};
Expand All @@ -1105,7 +1105,7 @@ pub fn reference_identifier_expression(p: &mut Parser) -> Option<CompletedMarker
.err_builder("Expected an identifier, but found none")
.primary(p.cur_tok().range, "");

p.err_recover(err, p.state.expr_recovery_set, true);
p.err_recover(err, p.state.expr_recovery_set, true, ERROR);
None
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rslint_parser/src/syntax/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn object_member(p: &mut Parser) -> Option<CompletedMarker> {
// test_err object_expr_non_ident_literal_prop
// let b = {5}

p.err_recover_no_err(token_set![T![:], T![,]], false);
p.err_recover_no_err(token_set![T![:], T![,]], false, ERROR);

if p.eat(T![:]) {
assign_expr(p);
Expand Down Expand Up @@ -298,7 +298,7 @@ fn method_object_member_body(p: &mut Parser) -> Result<(), ()> {
.err_builder("expected a method definition, but found none")
.primary(p.cur_tok().range, "");

p.err_recover(err, BASE_METHOD_RECOVERY_SET, false);
p.err_recover(err, BASE_METHOD_RECOVERY_SET, false, ERROR);
Err(())
};

Expand Down
4 changes: 3 additions & 1 deletion crates/rslint_parser/src/syntax/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn pattern(p: &mut Parser, parameters: bool, assignment: bool) -> Option<Com
if p.state.allow_object_expr {
ts = ts.union(token_set![T!['{']]);
}
p.err_recover(err, ts, false);
p.err_recover(err, ts, false, ERROR);
return None;
}
})
Expand Down Expand Up @@ -178,6 +178,7 @@ pub fn array_binding_pattern(
p.err_recover_no_err(
token_set![T![await], T![ident], T![yield], T![:], T![=], T![']']],
false,
ERROR,
);
}
if !p.at(T![']']) {
Expand Down Expand Up @@ -247,6 +248,7 @@ fn object_binding_prop(p: &mut Parser, parameters: bool) -> Option<CompletedMark
p.err_recover_no_err(
token_set![T![await], T![ident], T![yield], T![:], T![=], T!['}']],
false,
ERROR,
);
return None;
};
Expand Down
9 changes: 7 additions & 2 deletions crates/rslint_parser/src/syntax/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ pub fn stmt(p: &mut Parser, recovery_set: impl Into<Option<TokenSet>>) -> Option
return None;
}

p.err_recover(err, recovery_set.into().unwrap_or(STMT_RECOVERY_SET), false);
p.err_recover(
err,
recovery_set.into().unwrap_or(STMT_RECOVERY_SET),
false,
ERROR,
);
return None;
}
};
Expand Down Expand Up @@ -984,7 +989,7 @@ fn switch_clause(p: &mut Parser) -> Option<Range<usize>> {
"Expected the start to a case or default clause here",
);

p.err_recover(err, STMT_RECOVERY_SET, true);
p.err_recover(err, STMT_RECOVERY_SET, true, ERROR);
}
}
None
Expand Down
5 changes: 4 additions & 1 deletion crates/rslint_parser/src/syntax/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ pub fn ts_enum(p: &mut Parser) -> CompletedMarker {
err,
token_set![T!['}'], T![ident], T![yield], T![await], T![=], T![,]],
false,
ERROR,
);
true
} else {
Expand Down Expand Up @@ -1054,6 +1055,7 @@ pub fn ts_non_array_type(p: &mut Parser) -> Option<CompletedMarker> {
T![|]
]),
false,
ERROR,
);
None
}
Expand Down Expand Up @@ -1166,6 +1168,7 @@ fn type_param(p: &mut Parser) -> Option<CompletedMarker> {
err,
token_set![T![ident], T![yield], T![await], T![>], T![=]],
false,
ERROR,
);
None
}
Expand Down Expand Up @@ -1402,6 +1405,6 @@ pub fn ts_type_name(
))
.primary(p.cur_tok().range, "");

p.err_recover(err, set, false)?;
p.err_recover(err, set, false, ERROR)?;
None
}

0 comments on commit bba1add

Please sign in to comment.