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
16 changes: 0 additions & 16 deletions src/parser/internal/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ impl Parser {
};

scoped!(state, Scope::AnonymousFunction(is_static), {
self.lparen(state)?;

let params = self.param_list(state)?;

self.rparen(state)?;

let mut uses = vec![];
if state.current.kind == TokenKind::Use {
state.next();
Expand Down Expand Up @@ -118,12 +114,8 @@ impl Parser {
};

scoped!(state, Scope::ArrowFunction(is_static), {
self.lparen(state)?;

let params = self.param_list(state)?;

self.rparen(state)?;

let mut return_type = None;
if state.current.kind == TokenKind::Colon {
self.colon(state)?;
Expand Down Expand Up @@ -158,12 +150,8 @@ impl Parser {
let name = self.ident(state)?;

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

let params = self.param_list(state)?;

self.rparen(state)?;

let mut return_type = None;

if state.current.kind == TokenKind::Colon {
Expand Down Expand Up @@ -230,12 +218,8 @@ impl Parser {
], state);

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

let params = self.param_list(state)?;

self.rparen(state)?;

let mut return_type = None;

if state.current.kind == TokenKind::Colon {
Expand Down
12 changes: 9 additions & 3 deletions src/parser/internal/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl Parser {
_ => unreachable!(),
};

self.lparen(state)?;

while !state.is_eof() && state.current.kind != TokenKind::RightParen {
let flags: Vec<PropertyFlag> = self
.promoted_property_flags(state)?
Expand Down Expand Up @@ -136,11 +138,15 @@ impl Parser {
by_ref,
});

// TODO: bug! this allows `function foo(string $a ...$b &$c) {}`
// TODO: if `,` is found, look for next param, otherwise break out of the loop.
self.optional_comma(state)?;
if state.current.kind == TokenKind::Comma {
self.comma(state)?;
} else {
break;
}
}

self.rparen(state)?;

Ok(params)
}

Expand Down
26 changes: 11 additions & 15 deletions src/parser/internal/punc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@ use crate::expect_token;

impl Parser {
pub(in crate::parser) fn semi(&self, state: &mut State) -> ParseResult<()> {
expect_token!([TokenKind::SemiColon], state, "`;`");
Ok(())
expect_token!([TokenKind::SemiColon => Ok(())], state, "`;`")
}

pub(in crate::parser) fn lbrace(&self, state: &mut State) -> ParseResult<()> {
expect_token!([TokenKind::LeftBrace], state, "`{`");
Ok(())
expect_token!([TokenKind::LeftBrace => Ok(())], state, "`{`")
}

pub(in crate::parser) fn rbrace(&self, state: &mut State) -> ParseResult<()> {
expect_token!([TokenKind::RightBrace], state, "`}`");
Ok(())
expect_token!([TokenKind::RightBrace => Ok(())], state, "`}`")
}

pub(in crate::parser) fn lparen(&self, state: &mut State) -> ParseResult<()> {
expect_token!([TokenKind::LeftParen], state, "`(`");
Ok(())
expect_token!([TokenKind::LeftParen => Ok(())], state, "`(`")
}

pub(in crate::parser) fn rparen(&self, state: &mut State) -> ParseResult<()> {
expect_token!([TokenKind::RightParen], state, "`)`");
Ok(())
expect_token!([TokenKind::RightParen => Ok(())], state, "`)`")
}

pub(in crate::parser) fn rbracket(&self, state: &mut State) -> ParseResult<()> {
expect_token!([TokenKind::RightBracket], state, "`]`");
Ok(())
expect_token!([TokenKind::RightBracket => Ok(())], state, "`]`")
}

pub(in crate::parser) fn comma(&self, state: &mut State) -> ParseResult<()> {
expect_token!([TokenKind::Comma => Ok(())], state, "`,`")
}

pub(in crate::parser) fn optional_comma(&self, state: &mut State) -> ParseResult<()> {
Expand All @@ -45,8 +43,6 @@ impl Parser {
}

pub(in crate::parser) fn colon(&self, state: &mut State) -> ParseResult<()> {
expect_token!([TokenKind::Colon], state, "`:`");

Ok(())
expect_token!([TokenKind::Colon => Ok(())], state, "`:`")
}
}
6 changes: 6 additions & 0 deletions tests/0174/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

function foo(
string $a
...$bar
) {}
1 change: 1 addition & 0 deletions tests/0174/parser-error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExpectedToken(["`)`"], Some("..."), (5, 5)) -> Parse Error: unexpected token `...`, expecting `)` on line 5 column 5
89 changes: 89 additions & 0 deletions tests/0174/tokens.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[
Token {
kind: OpenTag(
Full,
),
span: (
1,
1,
),
},
Token {
kind: Function,
span: (
3,
1,
),
},
Token {
kind: Identifier(
"foo",
),
span: (
3,
10,
),
},
Token {
kind: LeftParen,
span: (
3,
13,
),
},
Token {
kind: Identifier(
"string",
),
span: (
4,
5,
),
},
Token {
kind: Variable(
"a",
),
span: (
4,
12,
),
},
Token {
kind: Ellipsis,
span: (
5,
5,
),
},
Token {
kind: Variable(
"bar",
),
span: (
5,
8,
),
},
Token {
kind: RightParen,
span: (
6,
1,
),
},
Token {
kind: LeftBrace,
span: (
6,
3,
),
},
Token {
kind: RightBrace,
span: (
6,
4,
),
},
]
7 changes: 7 additions & 0 deletions tests/0175/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

function foo(
string $a,
int $bar
float $baz
) {}
1 change: 1 addition & 0 deletions tests/0175/parser-error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExpectedToken(["`)`"], Some("float"), (6, 5)) -> Parse Error: unexpected token `float`, expecting `)` on line 6 column 5
116 changes: 116 additions & 0 deletions tests/0175/tokens.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
[
Token {
kind: OpenTag(
Full,
),
span: (
1,
1,
),
},
Token {
kind: Function,
span: (
3,
1,
),
},
Token {
kind: Identifier(
"foo",
),
span: (
3,
10,
),
},
Token {
kind: LeftParen,
span: (
3,
13,
),
},
Token {
kind: Identifier(
"string",
),
span: (
4,
5,
),
},
Token {
kind: Variable(
"a",
),
span: (
4,
12,
),
},
Token {
kind: Comma,
span: (
4,
14,
),
},
Token {
kind: Identifier(
"int",
),
span: (
5,
5,
),
},
Token {
kind: Variable(
"bar",
),
span: (
5,
9,
),
},
Token {
kind: Identifier(
"float",
),
span: (
6,
5,
),
},
Token {
kind: Variable(
"baz",
),
span: (
6,
11,
),
},
Token {
kind: RightParen,
span: (
7,
1,
),
},
Token {
kind: LeftBrace,
span: (
7,
3,
),
},
Token {
kind: RightBrace,
span: (
7,
4,
),
},
]
Loading