Parser - Disallow static initializers
This commit is contained in:
parent
b77d69c989
commit
0dbc631c96
1 changed files with 17 additions and 6 deletions
|
@ -226,13 +226,24 @@ impl Parser {
|
||||||
self.consume(&TokenType::LeftBrace, "'{' expected")?;
|
self.consume(&TokenType::LeftBrace, "'{' expected")?;
|
||||||
let mut members = Vec::new();
|
let mut members = Vec::new();
|
||||||
while !self.check(&TokenType::RightBrace) && !self.is_at_end() {
|
while !self.check(&TokenType::RightBrace) && !self.is_at_end() {
|
||||||
let is_static = self.expect(&[TokenType::Static]).is_some();
|
let static_token = self.expect(&[TokenType::Static]);
|
||||||
match self.parse_function(FunctionKind::Method)? {
|
match self.parse_function(FunctionKind::Method)? {
|
||||||
StmtNode::FunDecl(d) => members.push(if is_static {
|
StmtNode::FunDecl(d) => {
|
||||||
|
if let Some(tok) = static_token {
|
||||||
|
if d.name.lexeme == "init" {
|
||||||
|
return Err(SloxError::with_token(
|
||||||
|
ErrorKind::Parse,
|
||||||
|
&tok,
|
||||||
|
"initializer cannot be declared static".to_owned(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
members.push(if static_token.is_some() {
|
||||||
ClassMemberDecl::StaticMethod(d)
|
ClassMemberDecl::StaticMethod(d)
|
||||||
} else {
|
} else {
|
||||||
ClassMemberDecl::Method(d)
|
ClassMemberDecl::Method(d)
|
||||||
}),
|
})
|
||||||
|
}
|
||||||
_ => panic!("Function declaration expected"),
|
_ => panic!("Function declaration expected"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue