1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Enum parsing — extracted from item_parser.rs
use crate::lexer::Token;
use crate::parser::ast::*;
use crate::parser_impl::Parser;
impl Parser {
pub(crate) fn parse_enum(&mut self) -> Result<EnumDecl, String> {
// Token::Enum already consumed in parse_item
let name = if let Token::Ident(n) = self.current_token() {
let name = n.clone();
self.advance();
name
} else {
return Err("Expected enum name".to_string());
};
// Parse type parameters: enum Option<T>, enum Result<T, E>
let type_params = self.parse_type_params()?;
self.expect(Token::LBrace)?;
let mut variants = Vec::new();
while self.current_token() != &Token::RBrace {
// Collect doc comment for variant (if any)
let doc_comment = if let Token::DocComment(comment) = self.current_token() {
let doc = comment.clone();
self.advance();
Some(doc)
} else {
None
};
let variant_name = match self.current_token() {
Token::Ident(n) => {
let name = n.clone();
self.advance();
name
}
// Allow type-keyword names as variant names (e.g. `string(string)` in EventDataValue)
Token::String => {
self.advance();
"string".to_string()
}
_ => return Err("Expected variant name".to_string()),
};
let data = if self.current_token() == &Token::LParen {
// Tuple-style variant: Variant(Type1, Type2, Type3)
self.advance();
let mut types = Vec::new();
// Parse types separated by commas
if self.current_token() != &Token::RParen {
loop {
types.push(self.parse_type()?);
if self.current_token() == &Token::Comma {
self.advance();
// Allow trailing comma
if self.current_token() == &Token::RParen {
break;
}
} else {
break;
}
}
}
self.expect(Token::RParen)?;
EnumVariantData::Tuple(types)
} else if self.current_token() == &Token::LBrace {
// Struct-style variant: Variant { field1: Type1, field2: Type2 }
self.advance(); // consume {
let mut fields = Vec::new();
// Parse field: type pairs
while self.current_token() != &Token::RBrace && self.current_token() != &Token::Eof
{
// Parse field name
let field_name = if let Token::Ident(name) = self.current_token() {
let n = name.clone();
self.advance();
n
} else {
return Err(format!(
"Expected field name in struct variant (at token position {})",
self.position
));
};
self.expect(Token::Colon)?;
// Parse field type
let field_type = self.parse_type()?;
fields.push((field_name, field_type));
// Check for comma or end
if self.current_token() == &Token::Comma {
self.advance();
// Allow trailing comma
if self.current_token() == &Token::RBrace {
break;
}
} else {
break;
}
}
self.expect(Token::RBrace)?;
EnumVariantData::Struct(fields)
} else {
EnumVariantData::Unit
};
variants.push(EnumVariant {
name: variant_name,
data,
doc_comment,
});
if self.current_token() == &Token::Comma {
self.advance();
}
}
self.expect(Token::RBrace)?;
Ok(EnumDecl {
name,
is_pub: false, // Will be set by parse_item() if pub keyword present
type_params,
variants,
doc_comment: None, // Set by parse_item if doc comments present
})
}
}