chore: allow static, self, and parent in exprs (#195)

Signed-off-by: azjezz <azjezz@protonmail.com>
This commit is contained in:
Saif Eddin Gmati 2022-12-10 16:36:24 +01:00 committed by GitHub
parent 5264269078
commit 239bd69b86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 181 additions and 44 deletions

View File

@ -618,13 +618,6 @@ expressions! {
#[before(static_postfix), current(TokenKind::Self_)]
self_postfix(|state: &mut State| {
if !state.has_class_scope {
return Err(ParseError::CannotFindTypeInCurrentScope(
state.current.kind.to_string(),
state.current.span,
));
}
state.next();
postfix(state, Expression::Self_, &TokenKind::DoubleColon)
@ -632,13 +625,6 @@ expressions! {
#[before(parent_postfix), current(TokenKind::Static)]
static_postfix(|state: &mut State| {
if !state.has_class_scope {
return Err(ParseError::CannotFindTypeInCurrentScope(
state.current.kind.to_string(),
state.current.span,
));
}
state.next();
postfix(state, Expression::Static, &TokenKind::DoubleColon)
@ -646,13 +632,6 @@ expressions! {
#[before(left_parenthesis), current(TokenKind::Parent)]
parent_postfix(|state: &mut State| {
if !state.has_class_scope {
return Err(ParseError::CannotFindTypeInCurrentScope(
state.current.kind.to_string(),
state.current.span,
));
}
state.next();
postfix(state, Expression::Parent, &TokenKind::DoubleColon)
@ -693,37 +672,16 @@ expressions! {
let target = match state.current.kind {
TokenKind::Self_ => {
if !state.has_class_scope {
return Err(ParseError::CannotFindTypeInCurrentScope(
state.current.kind.to_string(),
state.current.span,
));
}
state.next();
Expression::Self_
}
TokenKind::Static => {
if !state.has_class_scope {
return Err(ParseError::CannotFindTypeInCurrentScope(
state.current.kind.to_string(),
state.current.span,
));
}
state.next();
Expression::Static
}
TokenKind::Parent => {
if !state.has_class_scope {
return Err(ParseError::CannotFindTypeInCurrentScope(
state.current.kind.to_string(),
state.current.span,
));
}
state.next();
Expression::Parent

View File

@ -1 +1 @@
CannotFindTypeInCurrentScope("static", (3, 6)) -> Parse Error: Cannot find type `static` in this scope on line 3 on column 6
ExpectedToken(["`::`"], Some(";"), (3, 12)) -> Parse Error: unexpected token `;`, expecting `::` on line 3 column 12

33
tests/fixtures/0271/ast.txt vendored Normal file
View File

@ -0,0 +1,33 @@
[
Expression {
expr: AssignmentOperation(
Assign {
left: Variable(
SimpleVariable(
SimpleVariable {
span: (
3,
1,
),
name: "a",
},
),
),
span: (
3,
4,
),
right: ConstFetch {
target: Static,
constant: SimpleIdentifier {
span: (
3,
14,
),
name: "foo",
},
},
},
),
},
]

View File

@ -1 +0,0 @@
CannotFindTypeInCurrentScope("static", (3, 6)) -> Parse Error: Cannot find type `static` in this scope on line 3 on column 6

138
tests/fixtures/0284/ast.txt vendored Normal file
View File

@ -0,0 +1,138 @@
[
Class(
Class {
start: (
7,
1,
),
end: (
9,
1,
),
name: SimpleIdentifier {
span: (
7,
7,
),
name: "a",
},
extends: None,
implements: None,
attributes: [
AttributeGroup {
start: (
6,
1,
),
end: (
6,
64,
),
members: [
Attribute {
start: (
6,
3,
),
end: (
6,
19,
),
expression: Call {
target: Identifier(
SimpleIdentifier(
SimpleIdentifier {
span: (
6,
3,
),
name: "foo",
},
),
),
args: [
Arg {
name: None,
value: ConstFetch {
target: Self_,
constant: SimpleIdentifier {
span: (
6,
13,
),
name: "class",
},
},
unpack: false,
},
],
},
},
Attribute {
start: (
6,
21,
),
end: (
6,
64,
),
expression: Call {
target: Identifier(
SimpleIdentifier(
SimpleIdentifier {
span: (
6,
21,
),
name: "bar",
},
),
),
args: [
Arg {
name: None,
value: New {
target: Self_,
span: (
6,
25,
),
args: [],
},
unpack: false,
},
Arg {
name: None,
value: New {
target: Parent,
span: (
6,
37,
),
args: [],
},
unpack: false,
},
Arg {
name: None,
value: New {
target: Static,
span: (
6,
51,
),
args: [],
},
unpack: false,
},
],
},
},
],
},
],
members: [],
},
),
]

9
tests/fixtures/0284/code.php vendored Normal file
View File

@ -0,0 +1,9 @@
<?php
// |- TODO: static in constant expression is not allowed.
// |
// v
#[foo(self::class), bar(new self(), new parent(), new static())]
class a {
}