oxabl_parser 0.3.1

Parser for Progress ABL source code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Expression parsing for the Oxabl parser.
//!
//! Precedence levels (lowest to highest):
//! ternary (IF/THEN/ELSE) > OR > AND > comparison > additive > multiplicative
//! > unary > postfix (member access, method calls, array/field access) > primary.

use oxabl_ast::{Expression, Identifier, Span};
use oxabl_lexer::{Kind, is_callable_kind};

use super::{ParseError, ParseResult, Parser};
use crate::literal::token_to_literal;

impl Parser<'_> {
    pub fn parse_expression(&mut self) -> ParseResult<Expression> {
        self.parse_ternary()
    }

    pub fn parse_ternary(&mut self) -> ParseResult<Expression> {
        if !self.check(Kind::KwIf) {
            return self.parse_or();
        }

        self.advance(); // consume IF
        let condition = self.parse_or()?; // condition can use OR/AND/comparison

        self.expect_kind(Kind::Then, "Expected 'THEN' after IF condition")?;

        let then_expr = self.parse_ternary()?; // recursive for nested ternary in then branch

        self.expect_kind(Kind::KwElse, "Expected 'ELSE' in IF expression")?;

        let else_expr = self.parse_ternary()?; // recursive for nested ternary in else branch

        Ok(Expression::IfThenElse(
            Box::new(condition),
            Box::new(then_expr),
            Box::new(else_expr),
        ))
    }

    pub fn parse_or(&mut self) -> ParseResult<Expression> {
        let mut expr = self.parse_and()?;
        while self.check(Kind::Or) {
            self.advance();
            let right = self.parse_and()?;
            expr = Expression::Or(Box::new(expr), Box::new(right));
        }
        Ok(expr)
    }

    pub fn parse_and(&mut self) -> ParseResult<Expression> {
        let mut expr = self.parse_comparison()?;
        while self.check(Kind::And) {
            self.advance();
            let right = self.parse_comparison()?;
            expr = Expression::And(Box::new(expr), Box::new(right));
        }
        Ok(expr)
    }

    pub(super) fn is_comparison_operator(&self) -> bool {
        matches!(
            self.peek().kind,
            Kind::Equals
                | Kind::NotEqual
                | Kind::LessThan
                | Kind::LessThanOrEqual
                | Kind::GreaterThan
                | Kind::GreaterThanOrEqual
                | Kind::Eq
                | Kind::Ne
                | Kind::Lt
                | Kind::Le
                | Kind::Gt
                | Kind::Ge
                | Kind::Begins
                | Kind::Matches
                | Kind::Contains
        )
    }

    pub fn parse_comparison(&mut self) -> ParseResult<Expression> {
        let left = self.parse_additive()?;

        if !self.is_comparison_operator() {
            return Ok(left);
        }

        let op_kind = self.advance().kind;
        let right = self.parse_additive()?;

        let expr = match op_kind {
            Kind::Equals | Kind::Eq => Expression::Equal(Box::new(left), Box::new(right)),
            Kind::NotEqual | Kind::Ne => Expression::NotEqual(Box::new(left), Box::new(right)),
            Kind::LessThan | Kind::Lt => Expression::LessThan(Box::new(left), Box::new(right)),
            Kind::LessThanOrEqual | Kind::Le => {
                Expression::LessThanOrEqual(Box::new(left), Box::new(right))
            }
            Kind::GreaterThan | Kind::Gt => {
                Expression::GreaterThan(Box::new(left), Box::new(right))
            }
            Kind::GreaterThanOrEqual | Kind::Ge => {
                Expression::GreaterThanOrEqual(Box::new(left), Box::new(right))
            }
            Kind::Begins => Expression::Begins(Box::new(left), Box::new(right)),
            Kind::Matches => Expression::Matches(Box::new(left), Box::new(right)),
            Kind::Contains => Expression::Contains(Box::new(left), Box::new(right)),
            _ => unreachable!(),
        };

        Ok(expr)
    }

    pub fn parse_additive(&mut self) -> ParseResult<Expression> {
        let mut expr = self.parse_multiplicative()?;
        while self.check(Kind::Add) || self.check(Kind::Minus) {
            let operator = self.advance();
            match operator.kind {
                Kind::Add => {
                    let right_exp = self.parse_multiplicative()?;
                    expr = Expression::Add(Box::new(expr), Box::new(right_exp));
                }
                Kind::Minus => {
                    let right_exp = self.parse_multiplicative()?;
                    expr = Expression::Minus(Box::new(expr), Box::new(right_exp));
                }
                _ => unreachable!(),
            }
        }
        Ok(expr)
    }

    pub fn parse_multiplicative(&mut self) -> ParseResult<Expression> {
        let mut expr = self.parse_unary()?;
        while self.check(Kind::Star) || self.check(Kind::Slash) || self.check(Kind::Modulo) {
            let operator = self.advance();
            match operator.kind {
                Kind::Star => {
                    let right_exp = self.parse_unary()?;
                    expr = Expression::Multiply(Box::new(expr), Box::new(right_exp));
                }
                Kind::Slash => {
                    let right_exp = self.parse_unary()?;
                    expr = Expression::Divide(Box::new(expr), Box::new(right_exp));
                }
                Kind::Modulo => {
                    let right_exp = self.parse_unary()?;
                    expr = Expression::Modulo(Box::new(expr), Box::new(right_exp));
                }
                _ => unreachable!(),
            }
        }
        Ok(expr)
    }

    pub fn parse_unary(&mut self) -> ParseResult<Expression> {
        if self.check(Kind::Minus) {
            self.advance();
            let expr = self.parse_unary()?;
            return Ok(Expression::Negate(Box::new(expr)));
        }
        if self.check(Kind::Not) {
            self.advance();
            let expr = self.parse_unary()?;
            return Ok(Expression::Not(Box::new(expr)));
        }
        self.parse_postfix()
    }

    pub fn parse_postfix(&mut self) -> ParseResult<Expression> {
        let mut expr = self.parse_primary()?;

        // Literals can't have postfix operations (member access, method calls, etc.)
        // Return early to avoid incorrectly parsing following tokens like ':' in "do i = 1 to 10:"
        if matches!(expr, Expression::Literal(_)) {
            return Ok(expr);
        }

        loop {
            if self.check(Kind::Colon) {
                // Only parse as member/method access if the next token is a valid member name
                // This avoids consuming ':' in block delimiters like "CASE x:" or "DO:"
                let next_is_member = self
                    .tokens
                    .get(self.current + 1)
                    .is_some_and(|t| is_callable_kind(t.kind));
                if !next_is_member {
                    break;
                }
                expr = self.parse_member_or_method(expr)?;
            } else if self.check(Kind::LeftBracket) {
                expr = self.parse_array_access(expr)?;
            } else if self.check(Kind::Period)
                && self.is_field_access_ahead()
                && Self::can_have_field_access(&expr)
            {
                expr = self.parse_field_access(expr)?;
            } else {
                break;
            }
        }

        Ok(expr)
    }

    pub fn parse_member_or_method(&mut self, object: Expression) -> ParseResult<Expression> {
        self.advance(); // consumes ':'

        // Expect identifier after ':'
        if !is_callable_kind(self.peek().kind) {
            return Err(ParseError {
                message: format!(
                    "Expected identifier after ':', found {:?}",
                    self.peek().kind
                ),
                span: Span {
                    start: self.peek().start as u32,
                    end: self.peek().end as u32,
                },
            });
        }

        let token = self.advance().clone();
        let member = Identifier {
            span: Span {
                start: token.start as u32,
                end: token.end as u32,
            },
            name: self.source[token.start..token.end].to_string(),
        };

        // Check for method call: member followed by (
        if self.check(Kind::LeftParen) {
            self.advance(); // Consume '('

            // consume and store all arguments
            let mut arguments = Vec::new();
            if !self.check(Kind::RightParen) {
                arguments.push(self.parse_expression()?);

                while self.check(Kind::Comma) {
                    self.advance(); // Consume ','
                    arguments.push(self.parse_expression()?);
                }
            }

            // if after parsing all arguments we don't find the
            // closing ), throw error
            self.expect_kind(Kind::RightParen, "Expected ')' after method arguments")?;

            return Ok(Expression::MethodCall {
                object: Box::new(object),
                method: member,
                arguments,
            });
        }

        Ok(Expression::MemberAccess {
            object: Box::new(object),
            member,
        })
    }

    pub fn parse_array_access(&mut self, array: Expression) -> ParseResult<Expression> {
        self.advance(); // consume the '['

        let index = self.parse_expression()?;

        self.expect_kind(Kind::RightBracket, "Expected ']' after array index")?;

        Ok(Expression::ArrayAccess {
            array: Box::new(array),
            index: Box::new(index),
        })
    }

    /// Check if we're looking at field access, rather that a statement terminator
    pub fn is_field_access_ahead(&mut self) -> bool {
        // skip if it's not a period
        if !self.check(Kind::Period) {
            return false;
        }

        // return true if there is an identifer after the period
        self.tokens
            .get(self.current + 1)
            .is_some_and(|t| t.kind == Kind::Identifier)
    }

    /// Check if an expression can be the base of field access (Table.Field)
    fn can_have_field_access(expr: &Expression) -> bool {
        matches!(
            expr,
            Expression::Identifier(_) | Expression::FieldAccess { .. }
        )
    }

    pub fn parse_field_access(&mut self, qualifier: Expression) -> ParseResult<Expression> {
        self.advance(); // consume '.'

        // Expect identifier after '.'
        if !self.check(Kind::Identifier) {
            return Err(ParseError {
                message: "Expected field name after '.'".to_string(),
                span: Span {
                    start: self.peek().start as u32,
                    end: self.peek().end as u32,
                },
            });
        }

        let token = self.advance().clone();
        let field = Identifier {
            span: Span {
                start: token.start as u32,
                end: token.end as u32,
            },
            name: self.source[token.start..token.end].to_string(),
        };

        Ok(Expression::FieldAccess {
            qualifier: Box::new(qualifier),
            field,
        })
    }

    pub fn parse_primary(&mut self) -> ParseResult<Expression> {
        // Parenthesized expression
        if self.check(Kind::LeftParen) {
            self.advance();
            let expr = self.parse_expression()?;
            self.expect_kind(Kind::RightParen, "Expected ')' after expression")?;
            return Ok(expr);
        }

        // Literals
        if self.check(Kind::IntegerLiteral)
            || self.check(Kind::DecimalLiteral)
            || self.check(Kind::StringLiteral)
            || self.check(Kind::KwTrue)
            || self.check(Kind::KwFalse)
            || self.check(Kind::Question)
        {
            let token = self.advance();
            let literal = token_to_literal(token).ok_or_else(|| ParseError {
                message: "Failed to convert token to literal".to_string(),
                span: Span {
                    start: token.start as u32,
                    end: token.end as u32,
                },
            })?;
            return Ok(Expression::Literal(literal));
        }

        // Identifiers and callable keywords (built-in functions like NOW, TRIM, etc.)
        if is_callable_kind(self.peek().kind) {
            let token = self.advance();
            let start = token.start;
            let end = token.end;
            let name = self.source[start..end].to_string();
            let identifier = Identifier {
                span: Span {
                    start: start as u32,
                    end: end as u32,
                },
                name,
            };

            // Check for function call: identifier/callable followed by (
            if self.check(Kind::LeftParen) {
                return self.parse_function_call(identifier);
            }

            return Ok(Expression::Identifier(identifier));
        }

        Err(ParseError {
            message: format!("Unexpected token {:?}", self.peek().kind),
            span: Span {
                start: self.peek().start as u32,
                end: self.peek().end as u32,
            },
        })
    }

    pub fn parse_function_call(&mut self, name: Identifier) -> ParseResult<Expression> {
        self.advance(); // consume the left parenthesis

        let mut arguments = Vec::new();

        // Empty argument list
        if !self.check(Kind::RightParen) {
            // parse first argument
            arguments.push(self.parse_expression()?);

            // parse remaining
            while self.check(Kind::Comma) {
                self.advance(); // consume ','
                arguments.push(self.parse_expression()?);
            }
        }

        self.expect_kind(Kind::RightParen, "Expected ')' after function arguments")?;

        Ok(Expression::FunctionCall { name, arguments })
    }
}