parser: start supporting reserved non modifiers as method names

This commit is contained in:
Ryan Chandler 2022-08-08 23:53:32 +01:00
parent 56310906a8
commit 06cbbeff40
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
3 changed files with 17 additions and 4 deletions

View File

@ -215,6 +215,7 @@ impl Display for TokenKind {
Self::Finally => "finally",
Self::Float(_) => "float",
Self::Fn => "fn",
Self::For => "for",
Self::FullyQualifiedIdentifier(id) => &id[..],
Self::Function => "function",
Self::GreaterThan => ">",

View File

@ -21,4 +21,17 @@ impl Parser {
pub(crate) fn var(&mut self) -> ParseResult<String> {
Ok(expect!(self, TokenKind::Variable(v), v, "expected variable name"))
}
pub(crate) fn ident_maybe_reserved(&mut self) -> ParseResult<String> {
match self.current.kind {
TokenKind::Static | TokenKind::Abstract | TokenKind::Final | TokenKind::For |
TokenKind::Private | TokenKind::Protected | TokenKind::Public | TokenKind::Require |
TokenKind::RequireOnce | TokenKind::New | TokenKind::Clone | TokenKind::If | TokenKind::Else | TokenKind::ElseIf => {
let string = self.current.kind.to_string();
self.next();
Ok(string)
},
_ => self.ident()
}
}
}

View File

@ -1361,13 +1361,13 @@ impl Parser {
Expression::StaticPropertyFetch { target: Box::new(lhs), property: Box::new(var) }
},
TokenKind::Class | TokenKind::Identifier(_) => {
_ => {
let ident = if self.current.kind == TokenKind::Class {
self.next();
String::from("class")
} else {
self.ident()?
self.ident_maybe_reserved()?
};
if self.current.kind == TokenKind::LeftParen {
@ -1389,12 +1389,11 @@ impl Parser {
Expression::ConstFetch { target: Box::new(lhs), constant: ident.into() }
}
},
_ => return Err(ParseError::UnexpectedToken(self.current.kind.to_string(), self.current.span))
}
},
TokenKind::Arrow => {
// TODO: Add support for dynamic property fetch or method call here.
let property = self.ident()?;
let property = self.ident_maybe_reserved()?;
if self.current.kind == TokenKind::LeftParen {
self.next();