From 239bd69b862d23e21b1328cc0eba6098e801f8f6 Mon Sep 17 00:00:00 2001 From: Saif Eddin Gmati <29315886+azjezz@users.noreply.github.com> Date: Sat, 10 Dec 2022 16:36:24 +0100 Subject: [PATCH] chore: allow static, self, and parent in exprs (#195) Signed-off-by: azjezz --- src/parser/expressions.rs | 42 -------- tests/fixtures/0270/parser-error.txt | 2 +- tests/fixtures/0271/ast.txt | 33 +++++++ tests/fixtures/0271/parser-error.txt | 1 - tests/fixtures/0284/ast.txt | 138 +++++++++++++++++++++++++++ tests/fixtures/0284/code.php | 9 ++ 6 files changed, 181 insertions(+), 44 deletions(-) create mode 100644 tests/fixtures/0271/ast.txt delete mode 100644 tests/fixtures/0271/parser-error.txt create mode 100644 tests/fixtures/0284/ast.txt create mode 100644 tests/fixtures/0284/code.php diff --git a/src/parser/expressions.rs b/src/parser/expressions.rs index 11be6a1..7ee8be3 100644 --- a/src/parser/expressions.rs +++ b/src/parser/expressions.rs @@ -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 diff --git a/tests/fixtures/0270/parser-error.txt b/tests/fixtures/0270/parser-error.txt index 8cd37ce..7c8dc34 100644 --- a/tests/fixtures/0270/parser-error.txt +++ b/tests/fixtures/0270/parser-error.txt @@ -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 diff --git a/tests/fixtures/0271/ast.txt b/tests/fixtures/0271/ast.txt new file mode 100644 index 0000000..eb153b0 --- /dev/null +++ b/tests/fixtures/0271/ast.txt @@ -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", + }, + }, + }, + ), + }, +] diff --git a/tests/fixtures/0271/parser-error.txt b/tests/fixtures/0271/parser-error.txt deleted file mode 100644 index 8cd37ce..0000000 --- a/tests/fixtures/0271/parser-error.txt +++ /dev/null @@ -1 +0,0 @@ -CannotFindTypeInCurrentScope("static", (3, 6)) -> Parse Error: Cannot find type `static` in this scope on line 3 on column 6 diff --git a/tests/fixtures/0284/ast.txt b/tests/fixtures/0284/ast.txt new file mode 100644 index 0000000..f3f2caa --- /dev/null +++ b/tests/fixtures/0284/ast.txt @@ -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: [], + }, + ), +] diff --git a/tests/fixtures/0284/code.php b/tests/fixtures/0284/code.php new file mode 100644 index 0000000..ee1ef75 --- /dev/null +++ b/tests/fixtures/0284/code.php @@ -0,0 +1,9 @@ +