Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

chore: allow attributes on enum cases #196

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/parser/ast/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use crate::parser::ast::functions::Method;
use crate::parser::ast::identifiers::SimpleIdentifier;
use crate::parser::ast::Expression;

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[derive(Debug, Clone, PartialEq)]
pub struct UnitEnumCase {
pub start: Span,
pub end: Span,
pub attributes: Vec<AttributeGroup>,
pub name: SimpleIdentifier,
}

Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct BackedEnumCase {
pub start: Span,
pub end: Span,
pub name: SimpleIdentifier,
pub attributes: Vec<AttributeGroup>,
pub value: Expression,
}

Expand Down
20 changes: 15 additions & 5 deletions src/parser/internal/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ pub fn parse(state: &mut State) -> ParseResult<Statement> {
}

fn unit_member(state: &mut State, enum_name: String) -> ParseResult<UnitEnumMember> {
let has_attributes = attributes::gather_attributes(state)?;
attributes::gather_attributes(state)?;

if state.current.kind == TokenKind::Case {
let attributes = state.get_attributes();

if !has_attributes && state.current.kind == TokenKind::Case {
let start = state.current.span;
state.next();

Expand All @@ -121,7 +123,12 @@ fn unit_member(state: &mut State, enum_name: String) -> ParseResult<UnitEnumMemb

let end = utils::skip_semicolon(state)?;

return Ok(UnitEnumMember::Case(UnitEnumCase { start, end, name }));
return Ok(UnitEnumMember::Case(UnitEnumCase {
start,
end,
name,
attributes,
}));
}

let modifiers = modifiers::collect(state)?;
Expand All @@ -135,9 +142,11 @@ fn unit_member(state: &mut State, enum_name: String) -> ParseResult<UnitEnumMemb
}

fn backed_member(state: &mut State, enum_name: String) -> ParseResult<BackedEnumMember> {
let has_attributes = attributes::gather_attributes(state)?;
attributes::gather_attributes(state)?;

if state.current.kind == TokenKind::Case {
let attributes = state.get_attributes();

if !has_attributes && state.current.kind == TokenKind::Case {
let start = state.current.span;
state.next();

Expand All @@ -162,6 +171,7 @@ fn backed_member(state: &mut State, enum_name: String) -> ParseResult<BackedEnum
end,
name,
value,
attributes,
}));
}

Expand Down