From 067bac959aa7bdb2149d7fa8aa3beb503ee54321 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Tue, 13 Sep 2022 12:15:29 +0100 Subject: [PATCH] parser: support backed enums with cases that have no backing value --- trunk_lexer/src/lexer.rs | 3 ++- trunk_parser/src/parser/mod.rs | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/trunk_lexer/src/lexer.rs b/trunk_lexer/src/lexer.rs index 9479849..68e9817 100644 --- a/trunk_lexer/src/lexer.rs +++ b/trunk_lexer/src/lexer.rs @@ -531,7 +531,8 @@ impl Lexer { if qualified { TokenKind::QualifiedIdentifier(buffer.into()) } else { - identifier_to_keyword(&buffer).unwrap_or_else(|| TokenKind::Identifier(buffer.into())) + identifier_to_keyword(&buffer) + .unwrap_or_else(|| TokenKind::Identifier(buffer.into())) } } b'/' | b'#' => { diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index df244a1..147f0bc 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -622,7 +622,7 @@ impl Parser { let name = self.ident()?; let mut value = None; - if is_backed { + if self.current.kind == TokenKind::Equals { expect!(self, TokenKind::Equals, "expected ="); value = Some(self.expression(0)?); @@ -2212,8 +2212,8 @@ mod tests { use super::Parser; use crate::{ ast::{ - Arg, ArrayItem, Case, ClassFlag, Constant, DeclareItem, ElseIf, IncludeKind, InfixOp, - MethodFlag, PropertyFlag, + Arg, ArrayItem, BackedEnumType, Case, ClassFlag, Constant, DeclareItem, ElseIf, + IncludeKind, InfixOp, MethodFlag, PropertyFlag, }, Catch, Expression, Identifier, Param, Statement, Type, }; @@ -3567,6 +3567,33 @@ mod tests { ); } + #[test] + fn backed_enum_without_values() { + assert_ast( + "