Skip to content

Commit

Permalink
parser: debug assert in skip_token() the desired token kind
Browse files Browse the repository at this point in the history
  • Loading branch information
feds01 committed Sep 19, 2023
1 parent 5c6dfb6 commit c95e77f
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 124 deletions.
16 changes: 6 additions & 10 deletions compiler/hash-parser/src/parser/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ impl<'s> AstGen<'s> {

/// Parse a `for` loop block.
pub(crate) fn parse_for_loop(&mut self) -> ParseResult<AstNode<Block>> {
debug_assert!(self.current_token().has_kind(TokenKind::Keyword(Keyword::For)));
self.skip_fast(); // `for`
self.skip_fast(TokenKind::Keyword(Keyword::For)); // `for`

let start = self.current_pos();

Expand All @@ -100,9 +99,8 @@ impl<'s> AstGen<'s> {

/// Parse a `while` loop block.
pub(crate) fn parse_while_loop(&mut self) -> ParseResult<AstNode<Block>> {
debug_assert!(self.current_token().has_kind(TokenKind::Keyword(Keyword::While)));
let start = self.current_pos();
self.skip_fast(); // `while`
self.skip_fast(TokenKind::Keyword(Keyword::While)); // `while`

let condition = self.parse_expr_with_precedence(0)?;
let while_body = self.parse_block()?;
Expand All @@ -127,9 +125,8 @@ impl<'s> AstGen<'s> {
/// Parse a match block statement, which is composed of a subject and an
/// arbitrary number of match cases that are surrounded in braces.
pub(crate) fn parse_match_block(&mut self) -> ParseResult<AstNode<Block>> {
debug_assert!(self.current_token().has_kind(TokenKind::Keyword(Keyword::Match)));
let start = self.current_pos();
self.skip_fast(); // `match`
self.skip_fast(TokenKind::Keyword(Keyword::Match)); // `match`

let subject = self.parse_expr_with_precedence(0)?;

Expand All @@ -145,9 +142,8 @@ impl<'s> AstGen<'s> {

/// Parse an `if-block` collection.
pub(crate) fn parse_if_block(&mut self) -> ParseResult<AstNode<Block>> {
debug_assert!(matches!(self.current_token().kind, TokenKind::Keyword(Keyword::If)));
let start = self.current_pos();
self.skip_fast(); // `if`
self.skip_fast(TokenKind::Keyword(Keyword::If)); // `if`

let mut clauses = thin_vec![];
let mut otherwise_clause = None;
Expand All @@ -165,12 +161,12 @@ impl<'s> AstGen<'s> {
// onwards...
match self.peek_kind() {
Some(TokenKind::Keyword(Keyword::Else)) => {
self.skip_fast(); // `else`
self.skip_fast(TokenKind::Keyword(Keyword::Else)); // `else`

// skip trying to convert just an 'else' branch since this is another
// if-branch
if let Some(TokenKind::Keyword(Keyword::If)) = self.peek_kind() {
self.skip_fast(); // `if`
self.skip_fast(TokenKind::Keyword(Keyword::If)); // `if`
continue;
}

Expand Down
21 changes: 8 additions & 13 deletions compiler/hash-parser/src/parser/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ impl<'s> AstGen<'s> {
/// Parse a [StructDef]. The keyword `struct` begins the construct and is
/// followed by parentheses with inner struct fields defined.
pub fn parse_struct_def(&mut self) -> ParseResult<StructDef> {
debug_assert!(self.current_token().has_kind(TokenKind::Keyword(Keyword::Struct)));
self.skip_fast(); // `struct`
self.skip_fast(TokenKind::Keyword(Keyword::Struct)); // `struct`

let def_kind = TyParamOrigin::Struct;
let ty_params = self.parse_optional_ty_params(def_kind)?;
Expand All @@ -33,8 +32,7 @@ impl<'s> AstGen<'s> {
/// Parse an [EnumDef]. The keyword `enum` begins the construct and is
/// followed by parentheses with inner enum fields defined.
pub fn parse_enum_def(&mut self) -> ParseResult<EnumDef> {
debug_assert!(self.current_token().has_kind(TokenKind::Keyword(Keyword::Enum)));
self.skip_fast(); // `enum`
self.skip_fast(TokenKind::Keyword(Keyword::Enum)); // `enum`

let def_kind = TyParamOrigin::Enum;
let ty_params = self.parse_optional_ty_params(def_kind)?;
Expand Down Expand Up @@ -103,7 +101,7 @@ impl<'s> AstGen<'s> {
// Parse an optional type annotation...
let ty = match self.peek_kind() {
Some(TokenKind::Colon) => {
self.skip_fast(); // `:`
self.skip_fast(TokenKind::Colon); // `:`
Some(self.parse_ty()?)
}
_ => None,
Expand All @@ -114,7 +112,7 @@ impl<'s> AstGen<'s> {
match self.peek_second() {
Some(token) if token.has_kind(TokenKind::Colon) => {
let name = Some(self.parse_name()?);
self.skip_fast(); // `:`
self.skip_fast(TokenKind::Colon); // `:`

// Now try and parse a type if the next token permits it...
let ty = match self.peek_kind() {
Expand All @@ -132,7 +130,7 @@ impl<'s> AstGen<'s> {
// for un-named fields.
let default = match self.peek_kind() {
Some(TokenKind::Eq) if name.is_some() => {
self.skip_fast(); // `=`
self.skip_fast(TokenKind::Eq); // `=`
Some(self.parse_expr_with_precedence(0)?)
}
_ => None,
Expand Down Expand Up @@ -165,8 +163,7 @@ impl<'s> AstGen<'s> {
/// Parse a [TraitDef]. A [TraitDef] is essentially a block prefixed with
/// `trait` that contains definitions or attach expressions to a trait.
pub fn parse_trait_def(&mut self) -> ParseResult<TraitDef> {
debug_assert!(self.current_token().has_kind(TokenKind::Keyword(Keyword::Trait)));
self.skip_fast(); // `trait`
self.skip_fast(TokenKind::Keyword(Keyword::Trait)); // `trait`

let ty_params = self.parse_optional_ty_params(TyParamOrigin::Trait)?;

Expand All @@ -175,8 +172,7 @@ impl<'s> AstGen<'s> {

/// Parse a `mod` block, with optional type parameters.
pub(crate) fn parse_mod_def(&mut self) -> ParseResult<ModDef> {
debug_assert!(self.current_token().has_kind(TokenKind::Keyword(Keyword::Mod)));
self.skip_fast(); // `mod`
self.skip_fast(TokenKind::Keyword(Keyword::Mod)); // `mod`

let ty_params = self.parse_optional_ty_params(TyParamOrigin::Mod)?;
let block = self.parse_body_block()?;
Expand All @@ -186,8 +182,7 @@ impl<'s> AstGen<'s> {

/// Parse a `impl` block, with optional type parameters.
pub(crate) fn parse_impl_def(&mut self) -> ParseResult<ImplDef> {
debug_assert!(self.current_token().has_kind(TokenKind::Keyword(Keyword::Impl)));
self.skip_fast(); // `impl`
self.skip_fast(TokenKind::Keyword(Keyword::Impl)); // `impl`

let ty_params = self.parse_optional_ty_params(TyParamOrigin::Impl)?;
let block = self.parse_body_block()?;
Expand Down
70 changes: 33 additions & 37 deletions compiler/hash-parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<'s> AstGen<'s> {
// an expression...
let maybe_eat_semi = |this: &mut Self| -> bool {
if let Some(TokenKind::Semi) = this.peek_kind() {
this.skip_fast(); // `;`
this.skip_fast(TokenKind::Semi); // `;`
true
} else {
false
Expand Down Expand Up @@ -80,7 +80,7 @@ impl<'s> AstGen<'s> {
// We don't skip here because it is handled after the statement has been
// generated.
Some(TokenKind::Eq) if !re_assigned => {
self.skip_fast(); // `=`
self.skip_fast(TokenKind::Eq); // `=`

// Parse the right hand-side of the assignment
let rhs = self.parse_expr_with_precedence(0)?;
Expand All @@ -100,11 +100,10 @@ impl<'s> AstGen<'s> {
/// Function to eat a collection of trailing semi-colons.
pub(crate) fn eat_trailing_semis(&mut self) {
let tok = self.current_token();
debug_assert!(tok.has_kind(TokenKind::Semi));

// Collect any additional trailing semis with the one that was encountered
while let Some(TokenKind::Semi) = self.peek_kind() {
self.skip_fast(); // `;`
self.skip_fast(TokenKind::Semi); // `;`
}

// Emit trailing semis diagnostic
Expand Down Expand Up @@ -146,7 +145,7 @@ impl<'s> AstGen<'s> {
self.node_with_span(Expr::Lit(LitExpr { data }), token.span)
}
TokenKind::Ident(ident) => {
self.skip_fast(); // `ident`
self.skip_fast(token.kind); // `ident`
let name = self.node_with_span(Name { ident }, token.span);
self.node_with_span(Expr::Variable(VariableExpr { name }), token.span)
}
Expand All @@ -167,7 +166,7 @@ impl<'s> AstGen<'s> {
self.node_with_joined_span(Expr::TraitDef(def), token.span)
}
TokenKind::Keyword(Keyword::Type) => {
self.skip_fast(); // `type`
self.skip_fast(token.kind); // `type`
let ty = self.parse_ty()?;
self.node_with_joined_span(Expr::Ty(TyExpr { ty }), token.span)
}
Expand All @@ -182,7 +181,7 @@ impl<'s> AstGen<'s> {
let def = self.parse_impl_def()?;
self.node_with_joined_span(Expr::ImplDef(def), token.span)
} else {
self.skip_fast(); // `impl`
self.skip_fast(token.kind); // `impl`

let ty = self.parse_ty()?;
let trait_body = self.parse_exprs_from_braces()?;
Expand All @@ -207,8 +206,8 @@ impl<'s> AstGen<'s> {
let block = match kind {
TokenKind::Keyword(Keyword::For) => self.parse_for_loop()?,
TokenKind::Keyword(Keyword::While) => self.parse_while_loop()?,
TokenKind::Keyword(Keyword::Loop) => {
self.skip_fast(); // `loop`
kw @ TokenKind::Keyword(Keyword::Loop) => {
self.skip_fast(kw); // `loop`
let block = self.parse_block()?;
self.node_with_joined_span(
Block::Loop(LoopBlock { contents: block }),
Expand Down Expand Up @@ -246,16 +245,16 @@ impl<'s> AstGen<'s> {
false => self.parse_expr_or_tuple()?,
}
}
TokenKind::Keyword(Keyword::Continue) => {
self.skip_fast(); // `continue`
kw @ TokenKind::Keyword(Keyword::Continue) => {
self.skip_fast(kw); // `continue`
self.node_with_span(Expr::Continue(ContinueStatement {}), token.span)
}
TokenKind::Keyword(Keyword::Break) => {
self.skip_fast(); // `break`
kw @ TokenKind::Keyword(Keyword::Break) => {
self.skip_fast(kw); // `break`
self.node_with_span(Expr::Break(BreakStatement {}), token.span)
}
TokenKind::Keyword(Keyword::Return) => {
self.skip_fast(); // `return`
kw @ TokenKind::Keyword(Keyword::Return) => {
self.skip_fast(kw); // `return`
let return_expr = match self.peek().copied() {
Some(tok) if !tok.has_kind(TokenKind::Semi) => Expr::Return(ReturnStatement {
expr: Some(self.parse_expr_with_precedence(0)?),
Expand Down Expand Up @@ -429,8 +428,7 @@ impl<'s> AstGen<'s> {
/// provide is references '.hash' extension file or a directory with a
/// `index.hash` file contained within the directory.
pub(crate) fn parse_import(&mut self) -> ParseResult<AstNode<Expr>> {
debug_assert_eq!(self.peek_kind(), Some(TokenKind::Keyword(Keyword::Import)));
self.skip_fast(); // `import`
self.skip_fast(TokenKind::Keyword(Keyword::Import)); // `import`

let start = self.current_pos();
let (path, span) = self.in_tree(Delimiter::Paren, None, |gen| {
Expand Down Expand Up @@ -474,7 +472,7 @@ impl<'s> AstGen<'s> {
Some(Token { kind: TokenKind::Eq, .. }),
) => {
let name = self.parse_name()?;
self.skip_fast(); // `=`
self.skip_fast(TokenKind::Eq); // `=`

Some(name)
}
Expand Down Expand Up @@ -511,18 +509,18 @@ impl<'s> AstGen<'s> {
pub(crate) fn parse_unary_expr(&mut self, token: Token) -> ParseResult<AstNode<Expr>> {
let start = self.current_pos();

let expr = match &token.kind {
let expr = match token.kind {
TokenKind::Star => {
self.skip_fast(); // `*`
self.skip_fast(TokenKind::Star); // `*`
Expr::Deref(DerefExpr { data: self.parse_expr()? })
}
TokenKind::Amp => {
self.skip_fast(); // `&`
self.skip_fast(TokenKind::Amp); // `&`

// Check if this reference is raw...
match self.peek().copied() {
Some(token) if token.has_kind(TokenKind::Keyword(Keyword::Raw)) => {
self.skip_fast(); // `raw`
self.skip_fast(token.kind); // `raw`

// Parse a mutability modifier if any
let mutability = self
Expand All @@ -535,8 +533,8 @@ impl<'s> AstGen<'s> {
mutability,
})
}
Some(Token { kind: TokenKind::Keyword(Keyword::Mut), span }) => {
self.skip_fast(); // `mut`
Some(Token { kind: kw @ TokenKind::Keyword(Keyword::Mut), span }) => {
self.skip_fast(kw); // `mut`

let inner_expr = self.parse_expr()?;

Expand All @@ -554,7 +552,7 @@ impl<'s> AstGen<'s> {
}
}
TokenKind::Plus => {
self.skip_fast(); // `+`
self.skip_fast(TokenKind::Plus); // `+`
let start = self.current_pos();
let inner_expr = self.parse_expr()?;

Expand All @@ -567,7 +565,7 @@ impl<'s> AstGen<'s> {
return Ok(inner_expr);
}
kind @ (TokenKind::Minus | TokenKind::Exclamation | TokenKind::Tilde) => {
self.skip_fast(); // `-` | `!` | `~`
self.skip_fast(kind); // `-` | `!` | `~`
let expr = self.parse_expr()?;

let operator = self.node_with_span(
Expand All @@ -588,8 +586,8 @@ impl<'s> AstGen<'s> {
Expr::Macro(ExprMacroInvocation { macros, subject })
}
TokenKind::At => todo!(),
TokenKind::Keyword(Keyword::Unsafe) => {
self.skip_fast(); // `unsafe`
kw @ TokenKind::Keyword(Keyword::Unsafe) => {
self.skip_fast(kw); // `unsafe`
let arg = self.parse_expr()?;
Expr::Unsafe(UnsafeExpr { data: arg })
}
Expand Down Expand Up @@ -623,7 +621,7 @@ impl<'s> AstGen<'s> {
// Now parse the value after the assignment
match self.peek_kind() {
Some(TokenKind::Eq) => {
self.skip_fast(); // `=`
self.skip_fast(TokenKind::Eq); // `=`

let value = self.parse_expr_with_precedence(0)?;
Ok(Declaration { pat: pattern, ty, value: Some(value) })
Expand Down Expand Up @@ -672,7 +670,7 @@ impl<'s> AstGen<'s> {

match self.peek_kind() {
Some(TokenKind::Eq) => {
self.skip_fast(); // `=`
self.skip_fast(TokenKind::Eq); // `=`

let operator = self.node_with_joined_span(operator, start);
let rhs = self.parse_expr_with_precedence(0)?;
Expand Down Expand Up @@ -700,8 +698,7 @@ impl<'s> AstGen<'s> {
subject: AstNode<Expr>,
subject_span: ByteRange,
) -> ParseResult<AstNode<Expr>> {
debug_assert!(self.current_token().has_kind(TokenKind::Dot));
self.skip_fast(); // `.`
self.skip_fast(TokenKind::Dot); // `.`

if let Some(token) = self.peek() && token.kind.is_numeric() {
// If the next token kind is a integer with no sign, then we can assume
Expand All @@ -713,7 +710,7 @@ impl<'s> AstGen<'s> {
return self.err_with_location(ParseErrorKind::DisallowedSuffix(ty.into()), ExpectedItem::empty(), None, token.span)?;
}

self.skip_fast(); // `int` literal
self.skip_fast(token.kind); // `int` literal
let value = self.source.hunk(token.span).parse::<usize>().map_err(|_| {
self.make_err(ParseErrorKind::InvalidPropertyAccess, ExpectedItem::empty(), None, Some(token.span))
})?;
Expand Down Expand Up @@ -744,8 +741,7 @@ impl<'s> AstGen<'s> {
subject: AstNode<Expr>,
subject_span: ByteRange,
) -> ParseResult<AstNode<Expr>> {
debug_assert!(self.current_token().has_kind(TokenKind::Access));
self.skip_fast(); // `::`
self.skip_fast(TokenKind::Access); // `::`

let property = self.parse_named_field(ParseErrorKind::ExpectedName)?;
Ok(self.node_with_joined_span(
Expand Down Expand Up @@ -782,7 +778,7 @@ impl<'s> AstGen<'s> {

match gen.peek_kind() {
Some(TokenKind::Comma) => {
gen.skip_fast(); // ','
gen.skip_fast(TokenKind::Comma); // ','

return Ok(tuple);
}
Expand Down Expand Up @@ -819,7 +815,7 @@ impl<'s> AstGen<'s> {
loop {
match gen.peek() {
Some(token) if token.has_kind(TokenKind::Comma) => {
gen.skip_fast(); // ','
gen.skip_fast(TokenKind::Comma); // ','

// Handles the case where this is a trailing comma and no tokens after...
if !gen.has_token() {
Expand Down
Loading

0 comments on commit c95e77f

Please sign in to comment.