(
env: &mut Environment,
tokens: &[Token],
position: &mut usize,
)
| 119 | } |
| 120 | |
| 121 | fn parse_set_query( |
| 122 | env: &mut Environment, |
| 123 | tokens: &[Token], |
| 124 | position: &mut usize, |
| 125 | ) -> Result<Query, Box<Diagnostic>> { |
| 126 | let len = tokens.len(); |
| 127 | let mut context = ParserContext::default(); |
| 128 | |
| 129 | // Consume Set keyword |
| 130 | *position += 1; |
| 131 | |
| 132 | if !is_current_token_with_condition(tokens, position, |token| { |
| 133 | matches!(token.kind, TokenKind::GlobalVariable(_)) |
| 134 | }) { |
| 135 | return Err(Diagnostic::error( |
| 136 | "Expect Global variable name start with `@` after `SET` keyword", |
| 137 | ) |
| 138 | .with_location(calculate_safe_location(tokens, *position - 1)) |
| 139 | .as_boxed()); |
| 140 | } |
| 141 | |
| 142 | let name = &tokens[*position].to_string(); |
| 143 | |
| 144 | // Consume variable name |
| 145 | *position += 1; |
| 146 | |
| 147 | if *position >= len || !is_assignment_operator(&tokens[*position]) { |
| 148 | return Err( |
| 149 | Diagnostic::error("Expect `=` or `:=` and Value after Variable name") |
| 150 | .with_location(calculate_safe_location(tokens, *position - 1)) |
| 151 | .as_boxed(), |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | // Consume `=` or `:=` token |
| 156 | *position += 1; |
| 157 | |
| 158 | let aggregations_count_before = context.aggregations.len(); |
| 159 | let value = parse_expression(&mut context, env, tokens, position)?; |
| 160 | let has_aggregations = context.aggregations.len() != aggregations_count_before; |
| 161 | |
| 162 | // Until supports sub queries, aggregation value can't be stored in variables |
| 163 | if has_aggregations { |
| 164 | return Err( |
| 165 | Diagnostic::error("Aggregation value can't be assigned to global variable") |
| 166 | .with_location(calculate_safe_location(tokens, *position - 1)) |
| 167 | .as_boxed(), |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | env.define_global(name.to_string(), value.expr_type()); |
| 172 | |
| 173 | Ok(Query::GlobalVariableDecl(GlobalVariableDeclQuery { |
| 174 | name: name.to_string(), |
| 175 | value, |
| 176 | })) |
| 177 | } |
| 178 |
no test coverage detected
searching dependent graphs…