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
use crate::lexer::token::TokenKind;
use crate::parser::ast::constant::ClassishConstant;
use crate::parser::ast::constant::Constant;
use crate::parser::ast::constant::ConstantEntry;
use crate::parser::ast::modifiers::ConstantModifierGroup;
use crate::parser::error::ParseResult;
use crate::parser::expressions;
use crate::parser::internal::identifiers;
use crate::parser::internal::utils;
use crate::parser::state::State;
pub fn parse(state: &mut State) -> ParseResult<Constant> {
let start = utils::skip(state, TokenKind::Const)?;
let mut entries = vec![];
loop {
let name = identifiers::constant_identifier(state)?;
utils::skip(state, TokenKind::Equals)?;
let value = expressions::lowest_precedence(state)?;
entries.push(ConstantEntry { name, value });
if state.stream.current().kind == TokenKind::Comma {
state.stream.next();
} else {
break;
}
}
let end = utils::skip_semicolon(state)?;
Ok(Constant {
start,
end,
entries,
})
}
pub fn classish(
state: &mut State,
modifiers: ConstantModifierGroup,
) -> ParseResult<ClassishConstant> {
let attributes = state.get_attributes();
let start = utils::skip(state, TokenKind::Const)?;
let mut entries = vec![];
loop {
let name = identifiers::identifier_maybe_reserved(state)?;
utils::skip(state, TokenKind::Equals)?;
let value = expressions::lowest_precedence(state)?;
entries.push(ConstantEntry { name, value });
if state.stream.current().kind == TokenKind::Comma {
state.stream.next();
} else {
break;
}
}
let end = utils::skip_semicolon(state)?;
Ok(ClassishConstant {
start,
end,
attributes,
modifiers,
entries,
})
}