(
context: &mut ParserContext,
env: &mut Environment,
tokens: &[Token],
position: &mut usize,
)
| 92 | } |
| 93 | |
| 94 | pub(crate) fn parse_glob_expression( |
| 95 | context: &mut ParserContext, |
| 96 | env: &mut Environment, |
| 97 | tokens: &[Token], |
| 98 | position: &mut usize, |
| 99 | ) -> Result<Box<dyn Expr>, Box<Diagnostic>> { |
| 100 | let lhs = parse_cast_operator_expression(context, env, tokens, position)?; |
| 101 | |
| 102 | if is_current_token(tokens, position, TokenKind::Glob) { |
| 103 | let glob_location = tokens[*position].location; |
| 104 | |
| 105 | // Consume `GLOB` Token |
| 106 | *position += 1; |
| 107 | |
| 108 | let pattern = parse_cast_operator_expression(context, env, tokens, position)?; |
| 109 | |
| 110 | let lhs_type = lhs.expr_type(); |
| 111 | let rhs_type = pattern.expr_type(); |
| 112 | |
| 113 | // Can perform this operator between LHS and RHS |
| 114 | let expected_rhs_types = lhs_type.can_perform_glob_op_with(); |
| 115 | if expected_rhs_types.contains(&rhs_type) { |
| 116 | return Ok(Box::new(GlobExpr { |
| 117 | input: lhs, |
| 118 | pattern, |
| 119 | })); |
| 120 | } |
| 121 | |
| 122 | // Check if RHS expr can be implicit casted to Expected LHS type to make this |
| 123 | // Expression valid |
| 124 | for expected_type in expected_rhs_types.iter() { |
| 125 | if !expected_type.has_implicit_cast_from(&pattern) { |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | let casting = Box::new(CastExpr { |
| 130 | value: pattern, |
| 131 | result_type: expected_type.clone(), |
| 132 | }); |
| 133 | |
| 134 | return Ok(Box::new(GlobExpr { |
| 135 | input: lhs, |
| 136 | pattern: casting, |
| 137 | })); |
| 138 | } |
| 139 | |
| 140 | // Return error if this operator can't be performed even with implicit cast |
| 141 | return Err(Diagnostic::error(&format!( |
| 142 | "Operator `GLOB` can't be performed between types `{lhs_type}` and `{rhs_type}`" |
| 143 | )) |
| 144 | .with_location(glob_location) |
| 145 | .as_boxed()); |
| 146 | } |
| 147 | |
| 148 | Ok(lhs) |
| 149 | } |
| 150 | |
| 151 | pub(crate) fn parse_regex_expression( |
no test coverage detected
searching dependent graphs…