From 4178947be8e3cb2ee5569a6b7ad30106691365bb Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Wed, 30 Nov 2022 00:48:34 +0000 Subject: [PATCH] parser: allow reserved idents in static method calls --- src/parser/ident.rs | 158 +++++++++++++++++++++++--------------------- src/parser/mod.rs | 5 ++ 2 files changed, 86 insertions(+), 77 deletions(-) diff --git a/src/parser/ident.rs b/src/parser/ident.rs index c859100..efa7f71 100644 --- a/src/parser/ident.rs +++ b/src/parser/ident.rs @@ -58,83 +58,7 @@ impl Parser { pub(crate) fn ident_maybe_reserved(&mut self) -> ParseResult { match self.current.kind { - TokenKind::Static - | TokenKind::Abstract - | TokenKind::Final - | TokenKind::For - | TokenKind::Private - | TokenKind::Protected - | TokenKind::Public - | TokenKind::Include - | TokenKind::IncludeOnce - | TokenKind::Eval - | TokenKind::Require - | TokenKind::RequireOnce - | TokenKind::LogicalOr - | TokenKind::LogicalXor - | TokenKind::LogicalAnd - | TokenKind::Instanceof - | TokenKind::New - | TokenKind::Clone - | TokenKind::Exit - | TokenKind::If - | TokenKind::ElseIf - | TokenKind::Else - | TokenKind::EndIf - | TokenKind::Echo - | TokenKind::Do - | TokenKind::While - | TokenKind::EndWhile - | TokenKind::EndFor - | TokenKind::Foreach - | TokenKind::EndForeach - | TokenKind::Declare - | TokenKind::EndDeclare - | TokenKind::As - | TokenKind::Try - | TokenKind::Catch - | TokenKind::Finally - | TokenKind::Throw - | TokenKind::Use - | TokenKind::Insteadof - | TokenKind::Global - | TokenKind::Var - | TokenKind::Unset - | TokenKind::Isset - | TokenKind::Empty - | TokenKind::Continue - | TokenKind::Goto - | TokenKind::Function - | TokenKind::Const - | TokenKind::Return - | TokenKind::Print - | TokenKind::Yield - | TokenKind::List - | TokenKind::Switch - | TokenKind::EndSwitch - | TokenKind::Case - | TokenKind::Default - | TokenKind::Break - | TokenKind::Array - | TokenKind::Callable - | TokenKind::Extends - | TokenKind::Implements - | TokenKind::Namespace - | TokenKind::Trait - | TokenKind::Interface - | TokenKind::Class - | TokenKind::ClassConstant - | TokenKind::TraitConstant - | TokenKind::FunctionConstant - | TokenKind::MethodConstant - | TokenKind::LineConstant - | TokenKind::FileConstant - | TokenKind::DirConstant - | TokenKind::NamespaceConstant - | TokenKind::HaltCompiler - | TokenKind::Fn - | TokenKind::Match - | TokenKind::Enum => { + _ if is_reserved_ident(&self.current.kind) => { let string = self.current.kind.to_string().into(); self.next(); Ok(string) @@ -143,3 +67,83 @@ impl Parser { } } } + +pub(crate) fn is_reserved_ident(kind: &TokenKind) -> bool { + matches!(kind, TokenKind::Static + | TokenKind::Abstract + | TokenKind::Final + | TokenKind::For + | TokenKind::Private + | TokenKind::Protected + | TokenKind::Public + | TokenKind::Include + | TokenKind::IncludeOnce + | TokenKind::Eval + | TokenKind::Require + | TokenKind::RequireOnce + | TokenKind::LogicalOr + | TokenKind::LogicalXor + | TokenKind::LogicalAnd + | TokenKind::Instanceof + | TokenKind::New + | TokenKind::Clone + | TokenKind::Exit + | TokenKind::If + | TokenKind::ElseIf + | TokenKind::Else + | TokenKind::EndIf + | TokenKind::Echo + | TokenKind::Do + | TokenKind::While + | TokenKind::EndWhile + | TokenKind::EndFor + | TokenKind::Foreach + | TokenKind::EndForeach + | TokenKind::Declare + | TokenKind::EndDeclare + | TokenKind::As + | TokenKind::Try + | TokenKind::Catch + | TokenKind::Finally + | TokenKind::Throw + | TokenKind::Use + | TokenKind::Insteadof + | TokenKind::Global + | TokenKind::Var + | TokenKind::Unset + | TokenKind::Isset + | TokenKind::Empty + | TokenKind::Continue + | TokenKind::Goto + | TokenKind::Function + | TokenKind::Const + | TokenKind::Return + | TokenKind::Print + | TokenKind::Yield + | TokenKind::List + | TokenKind::Switch + | TokenKind::EndSwitch + | TokenKind::Case + | TokenKind::Default + | TokenKind::Break + | TokenKind::Array + | TokenKind::Callable + | TokenKind::Extends + | TokenKind::Implements + | TokenKind::Namespace + | TokenKind::Trait + | TokenKind::Interface + | TokenKind::Class + | TokenKind::ClassConstant + | TokenKind::TraitConstant + | TokenKind::FunctionConstant + | TokenKind::MethodConstant + | TokenKind::LineConstant + | TokenKind::FileConstant + | TokenKind::DirConstant + | TokenKind::NamespaceConstant + | TokenKind::HaltCompiler + | TokenKind::Fn + | TokenKind::Match + | TokenKind::Enum) +} \ No newline at end of file diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9c284c2..a9dbc2b 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -16,6 +16,8 @@ use crate::{ }; use crate::{ByteString, TraitAdaptation, TryBlockCaughtType}; +use self::ident::is_reserved_ident; + pub mod error; mod block; @@ -2361,6 +2363,9 @@ impl Parser { // FIXME: Can this be represented in a nicer way? Kind of hacky. Expression::Identifier { name: "class".into() } }, + _ if is_reserved_ident(&self.current.kind) => { + Expression::Identifier { name: self.ident_maybe_reserved()?.into() } + }, _ => { return expected_token_err!(["`{`", "`$`", "an identifier"], self); }