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
8 changes: 7 additions & 1 deletion src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ pub enum Expression {
},
Match {
condition: Box<Self>,
default: Option<Box<DefaultMatchArm>>,
arms: Vec<MatchArm>,
},
Throw {
Expand Down Expand Up @@ -755,9 +756,14 @@ pub struct ClosureUse {
pub by_ref: bool,
}

#[derive(Debug, PartialEq, Clone)]
pub struct DefaultMatchArm {
pub body: Expression,
}

#[derive(Debug, PartialEq, Clone)]
pub struct MatchArm {
pub conditions: Option<Vec<Expression>>,
pub conditions: Vec<Expression>,
pub body: Expression,
}

Expand Down
2 changes: 2 additions & 0 deletions src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum ParseError {
MixingBracedAndUnBracedNamespaceDeclarations(Span),
NestedNamespaceDeclarations(Span),
ForbiddenTypeUsedInProperty(String, String, Type, Span),
MatchExpressionWithMultipleDefaultArms(Span),
}

impl Display for ParseError {
Expand Down Expand Up @@ -78,6 +79,7 @@ impl Display for ParseError {
Self::NestedNamespaceDeclarations(span) => write!(f, "Parse Error: Namespace declarations cannot be mixed on line {} column {}", span.0, span.1),
Self::UnpredictableState(span) => write!(f, "Parse Error: Reached an unpredictable state on line {} column {}", span.0, span.1),
Self::ForbiddenTypeUsedInProperty(class, prop, ty, span) => write!(f, "Parse Error: Property {}::${} cannot have type `{}` on line {} column {}", class, prop, ty, span.0, span.1),
Self::MatchExpressionWithMultipleDefaultArms(span) => write!(f, "Parse Error: Match expressions may only contain one default arm on line {} column {}", span.0, span.1),
}
}
}
36 changes: 27 additions & 9 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::parser::error::ParseResult;
use crate::parser::internal::ident::is_reserved_ident;
use crate::parser::internal::precedence::{Associativity, Precedence};
use crate::parser::state::State;
use crate::prelude::DefaultMatchArm;

pub mod ast;
pub mod error;
Expand Down Expand Up @@ -998,19 +999,30 @@ impl Parser {
self.rparen(state)?;
self.lbrace(state)?;

let mut default = None;
let mut arms = Vec::new();
while state.current.kind != TokenKind::RightBrace {
state.skip_comments();

let conditions = if state.current.kind == TokenKind::Default {
if state.current.kind == TokenKind::Default {
if default.is_some() {
return Err(ParseError::MatchExpressionWithMultipleDefaultArms(
state.current.span,
));
}

state.next();

// match conditions can have an extra comma at the end, including `default`.
if state.current.kind == TokenKind::Comma {
state.next();
}

None
expect_token!([TokenKind::DoubleArrow], state, "`=>`");

let body = self.expression(state, Precedence::Lowest)?;

default = Some(Box::new(DefaultMatchArm { body }));
} else {
let mut conditions = Vec::new();
while state.current.kind != TokenKind::DoubleArrow {
Expand All @@ -1023,14 +1035,16 @@ impl Parser {
}
}

Some(conditions)
};

expect_token!([TokenKind::DoubleArrow], state, "`=>`");
if !conditions.is_empty() {
expect_token!([TokenKind::DoubleArrow], state, "`=>`");
} else {
break;
}

let body = self.expression(state, Precedence::Lowest)?;
let body = self.expression(state, Precedence::Lowest)?;

arms.push(MatchArm { conditions, body });
arms.push(MatchArm { conditions, body });
}

if state.current.kind == TokenKind::Comma {
state.next();
Expand All @@ -1041,7 +1055,11 @@ impl Parser {

self.rbrace(state)?;

Expression::Match { condition, arms }
Expression::Match {
condition,
default,
arms,
}
}
TokenKind::Array => {
let mut items = vec![];
Expand Down
1 change: 1 addition & 0 deletions tests/0193/ast.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
condition: Variable {
name: "a",
},
default: None,
arms: [],
},
},
Expand Down
31 changes: 15 additions & 16 deletions tests/0194/ast.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@
condition: Variable {
name: "a",
},
default: None,
arms: [
MatchArm {
conditions: Some(
[
LiteralInteger {
i: 1,
},
LiteralInteger {
i: 2,
},
LiteralInteger {
i: 3,
},
LiteralInteger {
i: 4,
},
],
),
conditions: [
LiteralInteger {
i: 1,
},
LiteralInteger {
i: 2,
},
LiteralInteger {
i: 3,
},
LiteralInteger {
i: 4,
},
],
body: Null,
},
],
Expand Down
31 changes: 15 additions & 16 deletions tests/0195/ast.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@
condition: Variable {
name: "a",
},
default: None,
arms: [
MatchArm {
conditions: Some(
[
LiteralInteger {
i: 1,
},
LiteralInteger {
i: 2,
},
LiteralInteger {
i: 3,
},
LiteralInteger {
i: 4,
},
],
),
conditions: [
LiteralInteger {
i: 1,
},
LiteralInteger {
i: 2,
},
LiteralInteger {
i: 3,
},
LiteralInteger {
i: 4,
},
],
body: Null,
},
],
Expand Down
31 changes: 15 additions & 16 deletions tests/0196/ast.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@
condition: Variable {
name: "a",
},
default: None,
arms: [
MatchArm {
conditions: Some(
[
LiteralInteger {
i: 1,
},
LiteralInteger {
i: 2,
},
LiteralInteger {
i: 3,
},
LiteralInteger {
i: 4,
},
],
),
conditions: [
LiteralInteger {
i: 1,
},
LiteralInteger {
i: 2,
},
LiteralInteger {
i: 3,
},
LiteralInteger {
i: 4,
},
],
body: Null,
},
],
Expand Down
37 changes: 18 additions & 19 deletions tests/0197/ast.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,27 @@
condition: Variable {
name: "a",
},
arms: [
MatchArm {
conditions: Some(
[
LiteralInteger {
i: 1,
},
LiteralInteger {
i: 2,
},
LiteralInteger {
i: 3,
},
LiteralInteger {
i: 4,
},
],
),
default: Some(
DefaultMatchArm {
body: Null,
},
),
arms: [
MatchArm {
conditions: None,
conditions: [
LiteralInteger {
i: 1,
},
LiteralInteger {
i: 2,
},
LiteralInteger {
i: 3,
},
LiteralInteger {
i: 4,
},
],
body: Null,
},
],
Expand Down
7 changes: 7 additions & 0 deletions tests/0210/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php


match ($s) {
default => 43,
default => 34,
};
1 change: 1 addition & 0 deletions tests/0210/parser-error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MatchExpressionWithMultipleDefaultArms((6, 5)) -> Parse Error: Match expressions may only contain one default arm on line 6 column 5
Loading