Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum ParseError {
PromotedPropertyOnAbstractConstructor(Span),
AbstractModifierOnNonAbstractClassMethod(Span),
ConstructorInEnum(String, Span),
MissingCaseValueForBackedEnum(String, String, Span),
CaseValueForUnitEnum(String, String, Span),
StaticModifierOnConstant(Span),
ReadonlyModifierOnConstant(Span),
FinalModifierOnAbstractClassMember(Span),
Expand Down Expand Up @@ -61,6 +63,8 @@ impl Display for ParseError {
Self::AbstractModifierOnNonAbstractClassMethod(span) => write!(f, "Parse Error: Cannot declare abstract methods on a non-abstract class on line {} column {}", span.0, span.1),
Self::FinalModifierOnAbstractClass(span) => write!(f, "Parse Error: Cannot use the final modifier on an abstract class on line {} column {}", span.0, span.1),
Self::ConstructorInEnum(name, span) => write!(f, "Parse Error: Enum '{}' cannot have a constructor on line {} column {}", name, span.0, span.1),
Self::MissingCaseValueForBackedEnum(case, name, span) => write!(f, "Parse Error: Case `{}` of backed enum `{}` must have a value on line {} column {}", case, name, span.0, span.1),
Self::CaseValueForUnitEnum(case, name, span) => write!(f, "Parse Error: Case `{}` of unit enum `{}` must not have a value on line {} column {}", case, name, span.0, span.1),
Self::UnpredictableState(span) => write!(f, "Parse Error: Reached an unpredictable state on line {} column {}", span.0, span.1)
}
}
Expand Down
42 changes: 18 additions & 24 deletions src/parser/internal/classish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::parser::state::State;
use crate::parser::Parser;

use crate::expect_token;
use crate::expected_token_err;
use crate::scoped;

impl Parser {
Expand Down Expand Up @@ -195,28 +194,23 @@ impl Parser {

let name = self.ident(state)?;

scoped!(state, Scope::Enum(name.clone()), {
let backed_type: Option<BackedEnumType> = if state.current.kind == TokenKind::Colon {
self.colon(state)?;

match state.current.kind.clone() {
TokenKind::Identifier(s) if s == b"string" || s == b"int" => {
state.next();

Some(match &s[..] {
b"string" => BackedEnumType::String,
b"int" => BackedEnumType::Int,
_ => unreachable!(),
})
}
_ => {
return expected_token_err!(["`string`", "`int`"], state);
}
}
} else {
None
};

let backed_type: Option<BackedEnumType> = if state.current.kind == TokenKind::Colon {
self.colon(state)?;

expect_token!([
TokenKind::Identifier(s) if s == b"string" || s == b"int" => {
Some(match &s[..] {
b"string" => BackedEnumType::String,
b"int" => BackedEnumType::Int,
_ => unreachable!(),
})
},
], state, ["`string`", "`int`",])
} else {
None
};

scoped!(state, Scope::Enum(name.clone(), backed_type.is_some()), {
let mut implements = Vec::new();
if state.current.kind == TokenKind::Implements {
state.next();
Expand All @@ -233,7 +227,7 @@ impl Parser {
let mut body = Block::new();
while state.current.kind != TokenKind::RightBrace {
state.skip_comments();
body.push(self.enum_statement(state, backed_type.is_some())?);
body.push(self.enum_statement(state)?);
}

self.rbrace(state)?;
Expand Down
28 changes: 23 additions & 5 deletions src/parser/internal/classish_statement.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::expected_scope;
use crate::lexer::token::TokenKind;
use crate::parser::ast::Identifier;
use crate::parser::ast::MethodFlag;
Expand All @@ -6,6 +7,7 @@ use crate::parser::ast::TraitAdaptation;
use crate::parser::error::ParseError;
use crate::parser::error::ParseResult;
use crate::parser::internal::precedence::Precedence;
use crate::parser::state::Scope;
use crate::parser::state::State;
use crate::parser::Parser;

Expand Down Expand Up @@ -37,17 +39,25 @@ impl Parser {
], state, ["`const`", "`function`"])
}

pub(in crate::parser) fn enum_statement(
&self,
state: &mut State,
backed: bool,
) -> ParseResult<Statement> {
pub(in crate::parser) fn enum_statement(&self, state: &mut State) -> ParseResult<Statement> {
let (enum_name, backed) = expected_scope!([
Scope::Enum(enum_name, backed) => (enum_name, backed),
], state);

if state.current.kind == TokenKind::Case {
state.next();

let name = self.ident(state)?;

if backed {
if state.current.kind == TokenKind::SemiColon {
return Err(ParseError::MissingCaseValueForBackedEnum(
name.to_string(),
state.named(&enum_name),
state.current.span,
));
}

expect_token!([TokenKind::Equals], state, "`=`");

let value = self.expression(state, Precedence::Lowest)?;
Expand All @@ -58,6 +68,14 @@ impl Parser {
value,
});
} else {
if state.current.kind == TokenKind::Equals {
return Err(ParseError::CaseValueForUnitEnum(
name.to_string(),
state.named(&enum_name),
state.current.span,
));
}

self.semi(state)?;

return Ok(Statement::UnitEnumCase { name: name.into() });
Expand Down
45 changes: 23 additions & 22 deletions src/parser/internal/functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::expect_token;
use crate::expected_scope;
use crate::lexer::token::TokenKind;
use crate::parser::ast::ClassFlag;
use crate::parser::ast::ClosureUse;
Expand Down Expand Up @@ -203,32 +204,32 @@ impl Parser {

let name = self.ident_maybe_reserved(state)?;

scoped!(state, Scope::Method(name.clone(), flags.clone()), {
let has_body = match state.parent()? {
Scope::Class(_, cf) => {
if !cf.contains(&ClassFlag::Abstract) && flags.contains(&MethodFlag::Abstract) {
return Err(ParseError::AbstractModifierOnNonAbstractClassMethod(
state.current.span,
));
}

!flags.contains(&MethodFlag::Abstract)
let has_body = expected_scope!([
Scope::Class(_, cf) => {
if !cf.contains(&ClassFlag::Abstract) && flags.contains(&MethodFlag::Abstract) {
return Err(ParseError::AbstractModifierOnNonAbstractClassMethod(
state.current.span,
));
}
Scope::Trait(_) => !flags.contains(&MethodFlag::Abstract),
Scope::Interface(_) => false,
Scope::Enum(enum_name) => {
if name.to_string() == "__construct" {
return Err(ParseError::ConstructorInEnum(
state.named(enum_name),
state.current.span,
));
}

true
!flags.contains(&MethodFlag::Abstract)
},
Scope::Trait(_) => !flags.contains(&MethodFlag::Abstract),
Scope::Interface(_) => false,
Scope::Enum(enum_name, _) => {
if name.to_string() == "__construct" {
return Err(ParseError::ConstructorInEnum(
state.named(&enum_name),
state.current.span,
));
}
_ => true,
};

true
},
Scope::AnonymousClass => true,
], state);

scoped!(state, Scope::Method(name.clone(), flags.clone()), {
self.lparen(state)?;

let params = self.param_list(state)?;
Expand Down
46 changes: 30 additions & 16 deletions src/parser/macros.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
#[macro_export]
macro_rules! peek_token {
([ $($expected:pat => $out:expr),+ $(,)? ], $state:expr, [ $($message:literal),+ $(,)? ]) => {{
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? => $out:expr),+ $(,)? ], $state:expr, [ $($message:literal),+ $(,)? ]) => {{
$state.skip_comments();
match $state.current.kind.clone() {
$(
$expected => $out,
$( $pattern )|+ $( if $guard )? => $out,
)+
_ => {
return $crate::expected_token_err!([ $($message,)+ ], $state);
}
}
}};
([ $($expected:pat),+ $(,)? ], $state:expr, [ $($message:literal),+ $(,)? ]) => {{
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?),+ $(,)? ], $state:expr, [ $($message:literal),+ $(,)? ]) => {{
$state.skip_comments();
if !matches!($state.current.kind, $(| $expected )+) {
if !matches!($state.current.kind, $( $pattern )|+ $( if $guard )?) {
return $crate::expected_token_err!([ $($message,)+ ], $state);
}
}};
([ $($expected:pat => $out:expr),+ $(,)? ], $state:expr, $message:literal) => {
$crate::peek_token!([ $($expected => $out,)+ ], $state, [$message])
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? => $out:expr),+ $(,)? ], $state:expr, $message:literal) => {
$crate::peek_token!([ $($( $pattern )|+ $( if $guard )? => $out,)+ ], $state, [$message])
};
([ $($expected:pat),+ $(,)? ], $state:expr, $message:literal) => {
$crate::peek_token!([ $($expected,)+ ], $state, [$message])
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?),+ $(,)? ], $state:expr, $message:literal) => {
$crate::peek_token!([ $($( $pattern )|+ $( if $guard )?,)+ ], $state, [$message])
};
}

#[macro_export]
macro_rules! expect_token {
([ $($expected:pat => $out:expr),+ $(,)? ], $state:expr, [ $($message:literal),+ $(,)? ]) => {
$crate::peek_token!([ $($expected => { $state.next(); $out },)+ ], $state, [$($message,)+])
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? => $out:expr),+ $(,)? ], $state:expr, [ $($message:literal),+ $(,)? ]) => {
$crate::peek_token!([ $($( $pattern )|+ $( if $guard )? => { $state.next(); $out },)+ ], $state, [$($message,)+])
};
([ $($expected:pat),+ $(,)? ], $state:expr, [ $($message:literal),+ $(,)? ]) => {
$crate::peek_token!([ $($expected => { $state.next(); },)+ ], $state, [$($message,)+])
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?),+ $(,)? ], $state:expr, [ $($message:literal),+ $(,)? ]) => {
$crate::peek_token!([ $($( $pattern )|+ $( if $guard )? => { $state.next(); },)+ ], $state, [$($message,)+])
};
([ $($expected:pat => $out:expr),+ $(,)? ], $state:expr, $message:literal) => {
$crate::peek_token!([ $($expected => { $state.next(); $out },)+ ], $state, [$message])
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? => $out:expr),+ $(,)? ], $state:expr, $message:literal) => {
$crate::peek_token!([ $($( $pattern )|+ $( if $guard )? => { $state.next(); $out },)+ ], $state, [$message])
};
([ $($expected:pat),+ $(,)? ], $state:expr, $message:literal) => {
$crate::peek_token!([ $($expected => { $state.next(); },)+ ], $state, [$message])
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?),+ $(,)? ], $state:expr, $message:literal) => {
$crate::peek_token!([ $($( $pattern )|+ $( if $guard )? => { $state.next(); },)+ ], $state, [$message])
};
}

Expand Down Expand Up @@ -94,6 +94,20 @@ macro_rules! expected_token_err {
};
}

#[macro_export]
macro_rules! expected_scope {
([ $($(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? => $out:expr),+ $(,)? ], $state:expr) => {{
match $state.scope().cloned()? {
$(
$( $pattern )|+ $( if $guard )? => $out,
)+
_ => {
return Err($crate::parser::error::ParseError::UnpredictableState($state.current.span));
}
}
}};
}

#[macro_export]
macro_rules! scoped {
($state:expr, $scope:expr, $block:block) => {{
Expand Down
2 changes: 1 addition & 1 deletion src/parser/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum Scope {
Interface(ByteString),
Class(ByteString, Vec<ClassFlag>),
Trait(ByteString),
Enum(ByteString),
Enum(ByteString, bool),
AnonymousClass,

Function(ByteString),
Expand Down
2 changes: 1 addition & 1 deletion tests/0087/parser-error.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ExpectedToken(["`=`"], Some(";"), (5, 13)) -> Parse Error: unexpected token `;`, expecting `=` on line 5 column 13
MissingCaseValueForBackedEnum("Bar", "Foo", (5, 13)) -> Parse Error: Case `Bar` of backed enum `Foo` must have a value on line 5 column 13
8 changes: 8 additions & 0 deletions tests/0147/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace A\B\C\D\E;

enum Foo: int {
case Bar = 1;
case Baz;
}
1 change: 1 addition & 0 deletions tests/0147/parser-error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MissingCaseValueForBackedEnum("Baz", "A\\B\\C\\D\\E\\Foo", (7, 14)) -> Parse Error: Case `Baz` of backed enum `A\B\C\D\E\Foo` must have a value on line 7 column 14
Loading