Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: add is_writable() and is_readable() to Expression #25

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 4 deletions src/parser/internal/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ expressions! {
), peek(TokenKind::LeftParen | TokenKind::Generic)]
reserved_identifier_function_call({
let ident = identifier::identifier_maybe_soft_reserved(state)?;
let lhs = Expression::Identifier(ident);
let lhs = Expression::Constant(ident);
let op = &state.iterator.current().kind;

postfix::postfix(state, lhs, op)
Expand All @@ -243,7 +243,7 @@ expressions! {
), peek(TokenKind::DoubleColon)]
reserved_identifier_static_call({
let ident = identifier::classname_identifier(state)?;
let lhs = Expression::Identifier(ident);
let lhs = Expression::Constant(ident);

postfix::postfix(state, lhs, &TokenKind::DoubleColon)
})
Expand Down Expand Up @@ -479,7 +479,7 @@ expressions! {

#[before(reserved_identifier), current(TokenKind::Identifier | TokenKind::QualifiedIdentifier | TokenKind::FullyQualifiedIdentifier)]
identifier({
Ok(Expression::Identifier(identifier::fully_qualified_type_identifier(state)?))
Ok(Expression::Constant(identifier::fully_qualified_type_identifier(state)?))
})

#[before(left_parenthesis), current(
Expand All @@ -495,7 +495,7 @@ expressions! {

state.iterator.next();

Ok(Expression::Identifier(Identifier {
Ok(Expression::Constant(Identifier {
position: current.position,
value: current.value.clone(),
}))
Expand Down
45 changes: 40 additions & 5 deletions src/tree/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum Expression {
StringOperation(StringOperationExpression),
TypeOperation(TypeOperationExpression),
TernaryOperation(TernaryOperationExpression),
Identifier(Identifier),
Constant(Identifier),
KennedyTedesco marked this conversation as resolved.
Show resolved Hide resolved
Variable(Variable),
Match(MatchExpression),
AnonymousFunction(AnonymousFunctionExpression),
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Expression {
pub fn is_constant(&self, initilization: bool) -> bool {
match self {
Expression::Literal(_) => true,
Expression::Identifier(_) => true,
Expression::Constant(_) => true,
Expression::MagicConstant(_) => true,
Expression::Parenthesized(expression) => {
expression.expression.is_constant(initilization)
Expand Down Expand Up @@ -244,6 +244,41 @@ impl Expression {
_ => false,
}
}

/// Return true if the expression is writable
pub fn is_writable(&self) -> bool {
match self {
Expression::Variable(_)
| Expression::ArrayOperation(ArrayOperationExpression::Push { .. })
| Expression::ArrayOperation(ArrayOperationExpression::Access { .. })
| Expression::ObjectOperation(ObjectOperationExpression::PropertyFetch { .. })
| Expression::ClassOperation(ClassOperationExpression::StaticPropertyFetch {
..
}) => true,
Expression::Tuple(TupleExpression { elements, .. }) => {
elements.inner.iter().all(|element| element.is_writable())
}
_ => false,
}
}

/// Return true if the expression is readable
pub fn is_readable(&self) -> bool {
match self {
Expression::AssignmentOperation(..)
| Expression::ExitConstruct(..)
| Expression::ExceptionOperation(ExceptionOperationExpression::Throw { .. })
| Expression::ArrayOperation(ArrayOperationExpression::Push { .. }) => false,
Expression::AsyncOperation(AsyncOperationExpression::Concurrently {
expressions,
..
}) => expressions
.inner
.iter()
.all(|expression| expression.is_readable()),
_ => true,
}
}
}

impl Node for Expression {
Expand Down Expand Up @@ -273,7 +308,7 @@ impl Node for Expression {
Expression::StringOperation(expression) => expression.initial_position(),
Expression::TypeOperation(expression) => expression.initial_position(),
Expression::TernaryOperation(expression) => expression.initial_position(),
Expression::Identifier(expression) => expression.initial_position(),
Expression::Constant(expression) => expression.initial_position(),
Expression::Variable(expression) => expression.initial_position(),
Expression::Match(expression) => expression.initial_position(),
Expression::AnonymousFunction(expression) => expression.initial_position(),
Expand Down Expand Up @@ -307,7 +342,7 @@ impl Node for Expression {
Expression::StringOperation(expression) => expression.final_position(),
Expression::TypeOperation(expression) => expression.final_position(),
Expression::TernaryOperation(expression) => expression.final_position(),
Expression::Identifier(expression) => expression.final_position(),
Expression::Constant(expression) => expression.final_position(),
Expression::Variable(expression) => expression.final_position(),
Expression::Match(expression) => expression.final_position(),
Expression::AnonymousFunction(expression) => expression.final_position(),
Expand Down Expand Up @@ -341,7 +376,7 @@ impl Node for Expression {
Expression::StringOperation(expression) => vec![expression],
Expression::TypeOperation(expression) => vec![expression],
Expression::TernaryOperation(expression) => vec![expression],
Expression::Identifier(expression) => vec![expression],
Expression::Constant(expression) => vec![expression],
Expression::Variable(expression) => vec![expression],
Expression::Match(expression) => vec![expression],
Expression::AnonymousFunction(expression) => vec![expression],
Expand Down
18 changes: 9 additions & 9 deletions tests/samples/0001/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
expression: Identifier(
expression: Constant(
Identifier {
position: 27,
value: "a",
Expand Down Expand Up @@ -1281,7 +1281,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 497,
value: "a",
Expand Down Expand Up @@ -1314,7 +1314,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 506,
value: "a",
Expand Down Expand Up @@ -1359,7 +1359,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 517,
value: "a",
Expand Down Expand Up @@ -1389,7 +1389,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 529,
value: "a",
Expand Down Expand Up @@ -1435,7 +1435,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 544,
value: "a",
Expand Down Expand Up @@ -1484,7 +1484,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 560,
value: "a",
Expand Down Expand Up @@ -1545,7 +1545,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 578,
value: "a",
Expand Down Expand Up @@ -1591,7 +1591,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 597,
value: "a",
Expand Down
4 changes: 2 additions & 2 deletions tests/samples/0015/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 83,
value: "fib",
Expand Down Expand Up @@ -213,7 +213,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 97,
value: "fib",
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/0042/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 72,
value: "foo",
Expand Down
4 changes: 2 additions & 2 deletions tests/samples/0055/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
class: Identifier(
class: Constant(
Identifier {
position: 86,
value: "static",
Expand Down Expand Up @@ -164,7 +164,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
class: Identifier(
class: Constant(
Identifier {
position: 108,
value: "static",
Expand Down
16 changes: 8 additions & 8 deletions tests/samples/0056/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 236,
value: "true",
Expand Down Expand Up @@ -444,7 +444,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 248,
value: "false",
Expand Down Expand Up @@ -477,7 +477,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 261,
value: "null",
Expand Down Expand Up @@ -510,7 +510,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 273,
value: "readonly",
Expand Down Expand Up @@ -543,7 +543,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 289,
value: "self",
Expand Down Expand Up @@ -576,7 +576,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 301,
value: "parent",
Expand Down Expand Up @@ -609,7 +609,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 315,
value: "enum",
Expand Down Expand Up @@ -642,7 +642,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
function: Identifier(
function: Constant(
Identifier {
position: 327,
value: "from",
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/0058/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ DefinitionTree {
comments: CommentGroup {
comments: [],
},
class: Identifier(
class: Constant(
Identifier {
position: 6,
value: "self",
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/0059/tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DefinitionTree {
value: "a",
},
equals: 8,
value: Identifier(
value: Constant(
Identifier {
position: 10,
value: "\null",
Expand Down
Loading