sap-parser 1.0.1

The parser crate for the SAP programming language
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
//! # Parser Module
//!
//! The parser module contains the implementation of the SAP language parser. The parser
//! is responsible for taking a sequence of tokens and converting them into an abstract
//! syntax tree (AST). The AST is then used by the interpreter to execute the program.
//!
//! The parser is implemented as a recursive descent parser, which is a top-down parser
//! that starts from the root of the syntax tree and works its way down to the leaves.
//!
//! It is also responsible for reporting syntax errors in the input program. When
//! a syntax error is encountered, the parser returns an error containing a message
//! describing the error.
use std::num::IntErrorKind;

use ast::expression::{
    Array, Binary, Expression, FunctionCall, Identifier, Index, Selection, Unary,
};
use ast::literal::Literal;
use ast::statement::{
    Display, FunctionDeclaration, RepeatForever, RepeatUntil, Return, Set, Statement,
};
use ast::{Program, StatementList};
use lexer::token::{Token, TokenKind};
use lexer::Lexer;
use shared::error::{Error, ErrorKind};
use shared::span::{GetSpan, Span};

// Attempt to obtain the current version of the parser module.
pub const VERSION: Option<&str> = std::option_env!("CARGO_PKG_VERSION");

#[cfg(test)]
mod test;

/// The `Parser` struct is responsible for parsing a sequence of tokens into an abstract
/// syntax tree (AST). The parser is implemented as a recursive descent parser, which is
/// a top-down parser that starts from the root of the syntax tree and works its way down
/// to the leaves.
///
/// The `Parser` struct contains a reference to a `Lexer` instance, which is used to
/// obtain the tokens that make up the input program. The parser is also responsible for
/// reporting syntax errors in the input program. When a syntax error is encountered, the
/// parser returns an error containing a message describing the error.
pub struct Parser<'lexer> {
    lexer: Lexer<'lexer>,
    cur_token: Token,
}

/// This macro is an expansion for creating a new `Error` instance with a syntax error
/// kind. It's intended for use within the parser to simplify the creation of syntax
/// errors.
///
/// Arguments are identical to the `format!` macro.
macro_rules! parse_err {
    ($($arg:tt)*) => {{
        Err(shared::error::Error::new(&format!($($arg)*), ErrorKind::SyntaxError))
    }}
}

/// This macro is an expansion for parsing binary expressions. It's intended for use
/// within the parser to simplify the repetitive pattern of parsing binary operations.
///
/// # Arguments
///
/// * `self` - The parser instance, typically the `self` keyword in a method.
/// * `downstream_parse_fn` - A function that parses the individual expressions on both
///   sides of the binary operation. This function is called repeatedly to handle the left
///   and right-hand sides of the binary operation.
/// * `pattern` - A pattern matching the current token to the relevant operator(s).
macro_rules! parse_binary_expr {
    ($self:ident, $downstream_parse_fn:ident, $pattern:pat) => {
        let start = $self.cur_token.span.start;
        // Parse the left hand side
        let mut node = $self.$downstream_parse_fn()?;

        // While the current token matches the expected operator, repeatedly parse the right hand
        // side and construct a new binary expression.
        while matches!(&$self.cur_token.kind, $pattern) {
            let operator = $self.cur_token.kind.clone();
            // Move onto next token
            $self.next_token();
            // Parse the right hand side
            let right = $self.$downstream_parse_fn()?;
            let span = Span::new(start, $self.cur_token.span.end);
            // Construct a new binary expression, placing the old `node` inside the new one.
            node = Expression::Binary(Binary {
                operator,
                left: Box::new(node),
                right: Box::new(right),
                span,
            });
        }

        let result: Result<Expression, Error> = Ok(node);
        return result;
    };
}

impl<'lexer> Parser<'lexer> {
    pub fn new(mut lexer: Lexer<'lexer>) -> Self {
        // Load the first token from the lexer to be used as the current token.
        let cur = lexer.next_token();
        Parser {
            lexer,
            cur_token: cur,
        }
    }

    fn next_token(&mut self) {
        self.cur_token = self.lexer.next_token();
    }

    /// Returns true if the current token is equal to the expected token.
    #[inline]
    fn cur_token_is(&self, token_kind: &TokenKind) -> bool {
        self.cur_token.kind == *token_kind
    }

    /// Consumes the current token if it matches the expected token, otherwise returns
    /// a syntax error.
    ///
    /// # Arguments
    ///
    /// * `expected_kind` - The expected token kind.
    ///
    /// # Returns
    ///
    /// An `Ok` containing `()` if the current token matches the expected token, otherwise
    /// an `Err` containing a syntax error.
    fn eat(&mut self, expected_kind: &TokenKind) -> Result<(), Error> {
        if self.cur_token_is(expected_kind) {
            self.next_token();
            Ok(())
        } else {
            // This is essentially the default syntax error, any error which doesn't have a specific
            // handler be caught by this.
            parse_err!("expected {}, got {}", expected_kind, self.cur_token.kind)
        }
    }

    /// The main entry point for the parser. This method is responsible for parsing and
    /// lexing the entire input program, and returning the resulting AST or a syntax
    /// error.
    ///
    /// # Returns
    ///
    /// An `Ok` containing the AST (starting with the root `Program` node) if the input
    /// program is successfully parsed, otherwise an `Err` containing a syntax error.
    pub fn parse_program(&mut self) -> Result<Program, Error> {
        let mut program = Program::new();

        if self.cur_token_is(&TokenKind::Eof) {
            return Ok(program);
        } else {
            program.statements = self.parse_statement_list()?.statements;
            program.span.end = self.cur_token.span.end;
            self.eat(&TokenKind::Eof)?;
            return Ok(program);
        }
    }

    /// Parses a list of statements without consuming the termination token
    /// (`End` | `Eof` | `Otherwise`)
    ///
    /// Note: Does not handle empty statements lists, the caller should handle this case.
    fn parse_statement_list(&mut self) -> Result<StatementList, Error> {
        let mut list = StatementList::new();

        // Optionally consume leading NewLines
        while matches!(self.cur_token.kind, TokenKind::NewLine) {
            self.next_token();
        }

        loop {
            // Parse a statement and add it to the list
            list.statements.push(self.parse_statement()?);

            // If the next token is 'End' or 'Eof' or 'Otherwise', this represents the end of the
            // statement list, so we break out of the loop.
            if matches!(
                self.cur_token.kind,
                TokenKind::End | TokenKind::Eof | TokenKind::Otherwise
            ) {
                break; // No separator needed before 'End' or 'Eof'
            }

            // Each statement must be separated by a `NewLine` or `Semicolon`, which is enforced
            // here. The exception is when the next token is 'End' or 'Eof' or 'Otherwise', which is
            // handled by the previous if statement.
            if !matches!(
                self.cur_token.kind,
                TokenKind::Semicolon | TokenKind::NewLine
            ) {
                return parse_err!("expected ';' or newline between statements");
            }
            // Multiple separators are allowed, so we consume all of them.
            while matches!(
                self.cur_token.kind,
                TokenKind::Semicolon | TokenKind::NewLine
            ) {
                self.next_token();
            }

            // If we encounter 'End' or 'Eof' after consuming separators, break out of the loop.
            if matches!(
                self.cur_token.kind,
                TokenKind::End | TokenKind::Eof | TokenKind::Otherwise
            ) {
                break;
            }
        }

        Ok(list)
    }

    /// Parses a single statement.
    ///
    /// The EBNF for a statement is:
    /// ```ebnf
    ///     statement = set_stmt
    ///                 | return_stmt
    ///                 | expression
    ///                 | fn_decl_stmt
    ///                 | repeat_stmt
    ///                 | display_stmt;
    /// ```
    fn parse_statement(&mut self) -> Result<Statement, Error> {
        match self.cur_token.kind {
            TokenKind::Set => self.parse_set_stmt(),
            TokenKind::Return => self.parse_return_stmt(),
            TokenKind::DefineFunction => self.parse_fn_decl_stmt(),
            TokenKind::Repeat => self.parse_repeat_stmt(),
            TokenKind::Display => self.parse_display_stmt(),
            _ => self.parse_expr_stmt(),
        }
    }

    /// Parses a `Set` (i.e. `set x = 4`) statement.
    ///
    /// The EBNF for a `Set` statement is:
    /// ```ebnf
    ///     set_stmt = "set" , ident , "=" , expression;
    /// ```
    fn parse_set_stmt(&mut self) -> Result<Statement, Error> {
        let start = self.cur_token.span.start;
        self.eat(&TokenKind::Set)?;
        let ident = self.parse_identifier()?;
        self.eat(&TokenKind::Assign)?;
        let expr = self.parse_expression()?;
        let span = Span::new(start, self.cur_token.span.end);

        return Ok(Statement::Set(Set { ident, expr, span }));
    }

    /// Parses a `Return` statement.
    ///
    /// The EBNF for a `Return` statement is:
    /// ```ebnf
    ///    return_stmt = "return" , expression;
    /// ```
    fn parse_return_stmt(&mut self) -> Result<Statement, Error> {
        let start = self.cur_token.span.start;
        self.eat(&TokenKind::Return)?;
        let return_value = self.parse_expression()?;
        let span = Span::new(start, self.cur_token.span.end);
        return Ok(Statement::Return(Return {
            value: return_value,
            span,
        }));
    }

    /// Parses a function declaration statement.
    ///
    /// The EBNF for a function declaration statement is:
    /// ```ebnf
    ///     fn_decl_stmt = "defineFunction" , "(" , [params] , ")" , [statements]
    ///                    , "end";
    ///     params = ident , {"," , ident};
    /// ```
    fn parse_fn_decl_stmt(&mut self) -> Result<Statement, Error> {
        let start = self.cur_token.span.start;

        self.eat(&TokenKind::DefineFunction)?;

        // Parse the function name
        let name = self.parse_identifier()?;

        // Parse the function parameters.
        self.eat(&TokenKind::LParen)?;
        let parameters = self.parse_fn_decl_parameters()?;
        self.eat(&TokenKind::RParen)?;

        // Parse the function body.
        // If the function body is empty, return an empty statement list. Otherwise, parse the
        // function body.
        let body = if self.cur_token_is(&TokenKind::End) {
            self.create_empty_statement_list()
        } else {
            self.parse_statement_list()?
        };

        let end = self.cur_token.span.end;
        self.eat(&TokenKind::End)?;
        let span = Span::new(start, end);

        return Ok(Statement::FunctionDeclaration(FunctionDeclaration {
            name,
            parameters,
            body,
            span,
        }));
    }

    /// Parses the parameters of a function declaration.
    ///
    /// The EBNF grammar for this function is:
    /// ```ebnf
    ///     params = [ident , {"," , ident}]
    /// ```
    fn parse_fn_decl_parameters(&mut self) -> Result<Vec<Identifier>, Error> {
        if self.cur_token_is(&TokenKind::RParen) {
            // Return an empty array if there are no parameters.
            return Ok(vec![]);
        } else {
            let mut parameters: Vec<Identifier> = Vec::new();
            // Parse the first parameter
            let param = self.parse_fn_decl_parameter()?;
            parameters.push(param);

            // Parse all subsequent parameters seperated by commas.
            while self.cur_token_is(&TokenKind::Comma) {
                self.eat(&TokenKind::Comma)?;
                let param = self.parse_fn_decl_parameter()?;
                parameters.push(param);
            }
            return Ok(parameters);
        }
    }

    /// This function parses a single function parameter, the same as parsing an
    /// identifier, except with a specialised error message.
    fn parse_fn_decl_parameter(&mut self) -> Result<Identifier, Error> {
        let prev_token_kind = self.cur_token.kind.clone();
        self.parse_identifier().map_err(|mut err| {
            err.message = format!("expected function parameter, got '{}'", prev_token_kind);
            return err;
        })
    }

    /// Parses a `Display` statement.
    ///
    /// The EBNF for a `Display` statement is:
    /// ```ebnf
    ///     display_stmt = "display" , expression , {"," , expression};
    /// ```
    fn parse_display_stmt(&mut self) -> Result<Statement, Error> {
        let start = self.cur_token.span.start;
        self.eat(&TokenKind::Display)?;
        let expressions = match self.cur_token.kind {
            TokenKind::NewLine | TokenKind::Semicolon => {
                return parse_err!("empty display statement");
            }
            _ => self.parse_expr_list()?,
        };
        let span = Span::new(start, self.cur_token.span.end);
        return Ok(Statement::Display(Display { expressions, span }));
    }

    /// Parses a `Repeat` statement.
    ///
    /// The EBNF for a `Repeat` statement is:
    /// ```ebnf
    ///     repeat_stmt = "repeat" , (repeat_n_times | repeat_until | repeat_forever);
    /// ```
    fn parse_repeat_stmt(&mut self) -> Result<Statement, Error> {
        let start = self.cur_token.span.start;

        self.eat(&TokenKind::Repeat)?;

        let statement = match &self.cur_token.kind {
            TokenKind::Until => self.parse_repeat_until(start)?,
            TokenKind::Forever => self.parse_repeat_forever(start)?,
            _ => self.parse_repeat_n_times(start)?,
        };

        return Ok(statement);
    }

    /// Parses a `RepeatUntil` statement.
    ///
    /// The EBNF for a `RepeatUntil` statement is:
    /// ```ebnf
    ///    repeat_until = "until" , expression , [statements] , "end";
    /// ```
    fn parse_repeat_until(&mut self, start: usize) -> Result<Statement, Error> {
        self.eat(&TokenKind::Until)?;

        let expression = self.parse_expression()?;

        let statements = match self.cur_token.kind {
            TokenKind::End => self.create_empty_statement_list(),
            _ => self.parse_statement_list()?,
        };

        let span = Span::new(start, self.cur_token.span.end);

        self.eat(&TokenKind::End)?;

        return Ok(Statement::RepeatUntil(RepeatUntil {
            condition: expression,
            body: statements,
            span,
        }));
    }

    /// Parses a `RepeatForever` statement.
    ///
    /// The EBNF for a `RepeatForever` statement is:
    /// ```ebnf
    ///   repeat_forever = "forever" , [statements] , "end";
    /// ```
    fn parse_repeat_forever(&mut self, start: usize) -> Result<Statement, Error> {
        self.eat(&TokenKind::Forever)?;

        let statements = match self.cur_token.kind {
            TokenKind::End => self.create_empty_statement_list(),
            _ => self.parse_statement_list()?,
        };

        let span = Span::new(start, self.cur_token.span.end);

        self.eat(&TokenKind::End)?;

        return Ok(Statement::RepeatForever(RepeatForever {
            body: statements,
            span,
        }));
    }

    /// Parses a `RepeatNTimes` statement.
    ///
    /// The EBNF for a `RepeatNTimes` statement is:
    /// ```ebnf
    ///    repeat_n_times = expression , "times" , [statements] , "end";
    /// ```
    fn parse_repeat_n_times(&mut self, start: usize) -> Result<Statement, Error> {
        let n = self.parse_expression()?;

        self.eat(&TokenKind::Times)?;

        let statements = match self.cur_token.kind {
            TokenKind::End => self.create_empty_statement_list(),
            _ => self.parse_statement_list()?,
        };

        let span = Span::new(start, self.cur_token.span.end);

        self.eat(&TokenKind::End)?;

        return Ok(Statement::RepeatNTimes(ast::statement::RepeatNTimes {
            n,
            body: statements,
            span,
        }));
    }

    /// Parses an `Expression` statement.
    fn parse_expr_stmt(&mut self) -> Result<Statement, Error> {
        let expr = self.parse_expression()?;
        return Ok(Statement::Expression(expr));
    }

    /// Parses an expression (e.g. `1+1`).
    ///
    /// The EBNF for an expression is:
    /// ```ebnf
    ///     expression = or_expr;
    /// ```
    fn parse_expression(&mut self) -> Result<Expression, Error> {
        self.parse_or_expr()
    }

    /// Parses an or expression (e.g. `true or false`).
    ///
    /// The EBNF for an or expression is:
    /// ```ebnf
    ///     or_expr = and_expr {"or" , and_expr};
    /// ```
    fn parse_or_expr(&mut self) -> Result<Expression, Error> {
        // <or_expr> -> <and_expr> (`Or` <and_expr>)*
        parse_binary_expr!(self, parse_and_expr, TokenKind::Or);
    }

    /// Parses an and expression (e.g. `x > 1 and x < 10`)
    ///
    /// The EBNF for an and expression is:
    /// ```ebnf
    ///     and_expr = eq_expr {"and" , eq_expr};
    /// ```
    fn parse_and_expr(&mut self) -> Result<Expression, Error> {
        parse_binary_expr!(self, parse_eq_expr, TokenKind::And);
    }

    /// Parses an equality expression (e.g. `a == b != c`)
    ///
    /// The EBNF for an equality expression is:
    /// ```ebnf
    ///     eq_expr = comp_expr {("==" | "!=") , comp_expr};
    /// ```
    fn parse_eq_expr(&mut self) -> Result<Expression, Error> {
        parse_binary_expr!(self, parse_comp_expr, TokenKind::Eq | TokenKind::NotEq);
    }

    /// Parses a comparison expression (e.g. `x > 4`)
    ///
    /// The EBNF for a comparison expression is:
    /// ```ebnf
    ///     comp_expr = sum_expr {("<" | "<=" | ">" | ">=") , sum_expr};
    /// ```
    fn parse_comp_expr(&mut self) -> Result<Expression, Error> {
        parse_binary_expr!(
            self,
            parse_sum_expr,
            TokenKind::Lt | TokenKind::LtEq | TokenKind::Gt | TokenKind::GtEq
        );
    }

    /// Parses a sum expression (e.g. `1+2-3`)
    ///
    /// The EBNF for a sum expression is:
    /// ```ebnf
    ///     sum_expr = product_expr {("+" | "-") , product_expr}
    /// ```
    fn parse_sum_expr(&mut self) -> Result<Expression, Error> {
        parse_binary_expr!(self, parse_product_expr, TokenKind::Plus | TokenKind::Minus);
    }

    /// Parses a product expression (e.g. `1 * 2 / 3 % 4`)
    ///
    /// The EBNF for a product expression is:
    /// ```ebnf
    ///     product_expr = postfix_expr {("*" | "/" | "%") , postfix_expr};
    /// ```
    fn parse_product_expr(&mut self) -> Result<Expression, Error> {
        // <product_expr> -> <postfix_expr> ((`Mult` | `Div` | `Mod`) <postfix_expr>)*
        parse_binary_expr!(
            self,
            parse_postfix_expr,
            TokenKind::Mult | TokenKind::Div | TokenKind::Mod
        );
    }

    /// Parses a post-fix expression (e.g. `x[10]` or `print(x)`)
    ///
    /// The EBNF for a post-fix expression is:
    /// ```ebnf
    ///     postfix_expr = prefix_expr {fn_call | array_index};
    ///         array_index = "[" , expression , "]"
    ///         fn_call = "(" , [args] , ")";
    ///         args = expression , {"," , expression};
    /// ```
    fn parse_postfix_expr(&mut self) -> Result<Expression, Error> {
        let start = self.cur_token.span.start;
        let mut node = self.parse_prefix_expr()?;

        loop {
            match &self.cur_token.kind {
                // Parse array index
                TokenKind::LBracket => {
                    self.eat(&TokenKind::LBracket)?;
                    let index_expr = self.parse_expression()?;
                    self.eat(&TokenKind::RBracket)?;
                    let span = Span::new(start, self.cur_token.span.end);
                    node = Expression::Index(Index {
                        object: Box::new(node),
                        index: Box::new(index_expr),
                        span,
                    })
                }
                // Parse function call
                TokenKind::LParen => {
                    self.eat(&TokenKind::LParen)?;
                    let arguments = self
                        .parse_expr_list_maybe_empty(&TokenKind::RParen)
                        .map_err(|mut err| {
                            err.message =
                                format!("expected function parameter, got {}", self.cur_token.kind);
                            err
                        })?;
                    self.eat(&TokenKind::RParen)?;

                    let span = Span::new(start, self.cur_token.span.end);
                    node = Expression::FunctionCall(FunctionCall {
                        callee: Box::new(node),
                        arguments,
                        span,
                    })
                }
                _ => break,
            };
        }

        return Ok(node);
    }

    /// Parse a prefix expression (e.g. `not true` or `---1`)
    ///
    /// The EBNF for a prefix expression is:
    /// ```ebnf
    ///     prefix_expr = {"not" | "-"} , ident_expr;
    /// ```
    fn parse_prefix_expr(&mut self) -> Result<Expression, Error> {
        // Add each prefix operation into an array. For example, if the expression was `not -1`,
        // the array would be `[TokenKind::Not, TokenKind::Minus]`.
        let mut prefix_operations = Vec::new();
        while matches!(&self.cur_token.kind, TokenKind::Not | TokenKind::Minus) {
            prefix_operations.push(self.cur_token.clone());
            self.next_token();
        }

        // Parse the expression.
        let mut node: Expression = self.parse_ident_expr()?;

        // Now, add each operation to the syntax tree IN REVERSE, since the tree starts from the
        // root node (the expression), and then the prefix operation closest to the expression is
        // added to the tree first.
        for operation in prefix_operations.into_iter().rev() {
            let operator = operation.kind.clone();
            let span = Span::new(operation.span.start, node.span().end);
            node = Expression::Unary(Unary {
                operator,
                operand: Box::new(node),
                span,
            })
        }

        return Ok(node);
    }

    /// Parse an identifier expression (e.g. `my_var`).
    ///
    /// The EBNF for an identfier expression is:
    /// ```ebnf
    ///     ident_expr = ident | group_expr;
    /// ```
    fn parse_ident_expr(&mut self) -> Result<Expression, Error> {
        match &self.cur_token.kind.clone() {
            TokenKind::Identifier { .. } => Ok(Expression::Identifier(self.parse_identifier()?)),
            _ => self.parse_group_expr(),
        }
    }

    /// Parse a group expression (e.g. `(1+1)*2`).
    ///
    /// The EBNF for a group expression is:
    /// ```ebnf
    ///     group_expr = ("(" , expression , ")") | entity_expr;
    /// ```
    fn parse_group_expr(&mut self) -> Result<Expression, Error> {
        if self.cur_token_is(&TokenKind::LParen) {
            self.eat(&TokenKind::LParen)?;
            let expr = self.parse_expression()?;
            self.eat(&TokenKind::RParen)?;
            return Ok(expr);
        } else {
            return self.parse_entity_expr();
        }
    }

    /// Parse an entity expression. An 'entity' is a single building block of an
    /// expression, it cannot be sliced up into smaller expressions.
    ///
    /// The EBNF for an entity expression is:
    /// ```ebnf
    ///     entity_expr = selection_expr | array_expr | literal;
    /// ```
    fn parse_entity_expr(&mut self) -> Result<Expression, Error> {
        match &self.cur_token.kind {
            TokenKind::If => return self.parse_selection_expr(),
            TokenKind::LBracket => return self.parse_array_expr(),
            _ => {
                let literal = self.parse_literal()?;
                return Ok(Expression::Literal(literal));
            }
        }
    }

    /// Parse an array expression (e.g. `[1, 2, x, 16/4]`).
    ///
    /// The EBNF for an array expression is:
    /// ```ebnf
    ///     array_expr = "[" , [elements] , "]";
    ///         elements = expression , {"," , expression};
    /// ```
    fn parse_array_expr(&mut self) -> Result<Expression, Error> {
        let start = self.cur_token.span.start;
        self.eat(&TokenKind::LBracket)?;
        let elements = self.parse_expr_list_maybe_empty(&TokenKind::RBracket)?;
        self.eat(&TokenKind::RBracket)?;
        let span = Span::new(start, self.cur_token.span.end);

        return Ok(Expression::Array(Array { elements, span }));
    }

    /// Parse a selection expression (e.g. `if x > 4 then return x end`)
    ///
    /// The EBNF for an array expression is:
    /// ```ebnf
    ///     selection_expr = "if" , expression , "then" , [statements]
    ///                      , ["otherwise" [statements]] "end";
    /// ```
    fn parse_selection_expr(&mut self) -> Result<Expression, Error> {
        let start = self.cur_token.span.start;

        self.eat(&TokenKind::If)?;
        let condition_expr = self.parse_expression()?;
        self.eat(&TokenKind::Then)?;
        let conditional_statements = self.parse_selection_if_body()?;
        let else_conditional_block = self.parse_selection_else_body()?;

        let end = self.cur_token.span.end;
        self.eat(&TokenKind::End)?;
        let span = Span::new(start, end);

        return Ok(Expression::Selection(Selection {
            condition: Box::new(condition_expr),
            conditional: conditional_statements,
            else_conditional: else_conditional_block,
            span,
        }));
    }

    /// Parse the statements within the 'if' branch of a selection statement.
    fn parse_selection_if_body(&mut self) -> Result<StatementList, Error> {
        match matches!(self.cur_token.kind, TokenKind::End | TokenKind::Otherwise) {
            false => self.parse_statement_list(),
            true => Ok(self.create_empty_statement_list()),
        }
    }

    /// Parse the statements within the 'else' branch of a selection statement (if it
    /// exists).
    fn parse_selection_else_body(&mut self) -> Result<Option<StatementList>, Error> {
        if self.cur_token_is(&TokenKind::Otherwise) {
            self.eat(&TokenKind::Otherwise)?;
            let else_conditional_statements = match matches!(self.cur_token.kind, TokenKind::End) {
                false => self.parse_statement_list()?,
                true => self.create_empty_statement_list(),
            };
            Ok(Some(else_conditional_statements))
        } else {
            Ok(None)
        }
    }

    /// Parse a literal. A literal is a hard-coded `string`, `int`, `float` or `bool`.
    ///
    /// The EBNF grammar for a literal is:
    /// ```ebnf
    ///     literal = int | float | bool | string;
    ///         bool = "true" | "false";
    ///         float = int , "." , int;
    ///         int = digit , {digit};
    ///             digit = "0" | "1" | ... | "9";
    ///         string = '"', {char} , '"';
    ///             char = ? any character ? -'"';
    /// ```
    fn parse_literal(&mut self) -> Result<Literal, Error> {
        let span = self.cur_token.span.clone();
        match &self.cur_token.kind.clone() {
            TokenKind::Int(n) => {
                return self.parse_integer_literal(n);
            }
            TokenKind::Float(n) => {
                return self.parse_float_literal(n);
            }
            TokenKind::True => {
                self.next_token();
                return Ok(Literal::Boolean { value: true, span });
            }
            TokenKind::False => {
                self.next_token();
                return Ok(Literal::Boolean { value: false, span });
            }
            TokenKind::String(string) => {
                self.next_token();
                return Ok(Literal::String {
                    value: string.to_owned(),
                    span,
                });
            }
            _ => parse_err!("expected Literal, got {}", self.cur_token.kind),
        }
    }

    /// Converts a string into an integer literal.
    fn parse_integer_literal(&mut self, integer_to_parse: &String) -> Result<Literal, Error> {
        let span = self.cur_token.span.clone();
        self.next_token();
        match integer_to_parse.parse::<i64>() {
            Ok(n) => Ok(Literal::Integer { value: n, span }),
            Err(err) => match err.kind() {
                IntErrorKind::PosOverflow => Err(Error::new(
                    &format!(
                        "literal to large for type Integer, whose maximum value is `{}`",
                        i64::MAX
                    ),
                    ErrorKind::OverflowError,
                )),
                _ => parse_err!("failed to parse literal into Integer"),
            },
        }
    }

    /// Converts a string into a float literal.
    fn parse_float_literal(&mut self, float_to_parse: &String) -> Result<Literal, Error> {
        let span = self.cur_token.span.clone();
        self.next_token();
        match float_to_parse.parse::<f64>() {
            Ok(n) => Ok(Literal::Float { value: n, span }),
            Err(_) => {
                return parse_err!("failed to parse literal into Float");
            }
        }
    }

    /// Parse an identifier (e.g. `my_var`).
    ///
    /// The EBNF for this function is:
    /// ```ebnf
    ///     ident = letter , {letter | digit};
    ///         digit = "0" | "1" | ... | "9";
    ///         letter = ? any alphabetical character ? | "_";
    /// ```
    fn parse_identifier(&mut self) -> Result<Identifier, Error> {
        let span = self.cur_token.span.clone();
        let name = match &self.cur_token.kind {
            TokenKind::Identifier { name } => name.to_string(),
            _ => return parse_err!("expected Identifier, got {}", self.cur_token.kind),
        };
        // TokenKind has already been checked, so we can safely call next_token()
        self.next_token();

        Ok(Identifier { name, span })
    }

    /// Parses a potentially empty list of (comma seperated) expressions.
    ///
    /// The EBNF for this function is:
    /// ```ebnf
    ///     expressions = [expression , {"," , expression}];
    /// ```
    fn parse_expr_list_maybe_empty(
        &mut self,
        ending_token: &TokenKind,
    ) -> Result<Vec<Expression>, Error> {
        if self.cur_token_is(ending_token) {
            return Ok(vec![]);
        } else {
            return self.parse_expr_list();
        };
    }

    /// Parses a list of (comma seperated) expressions.
    ///
    /// The EBNF for this function is:
    /// ```ebnf
    ///     expressions = expression , {"," , expression};
    /// ```
    fn parse_expr_list(&mut self) -> Result<Vec<Expression>, Error> {
        let mut expressions: Vec<Expression> = Vec::new();
        expressions.push(self.parse_expression()?);
        while self.cur_token_is(&TokenKind::Comma) {
            self.eat(&TokenKind::Comma)?;
            expressions.push(self.parse_expression()?);
        }
        return Ok(expressions);
    }

    /// Helper function to create an empty statement list at the location fo the current
    /// token.
    fn create_empty_statement_list(&self) -> StatementList {
        StatementList {
            statements: vec![],
            span: Span::new(self.cur_token.span.start, self.cur_token.span.start),
        }
    }
}

/// This function takes in a string input, and produces either a valid syntax tree, or a
/// syntax error.
///
/// # Arguments
///
/// * `input` - The input SAP program as a string.
///
/// # Returns
///
/// * If there are no syntax errors, an `Ok(Program)`, which is the root node containing the
/// entire AST (Abstract Syntax Tree) which represents the program.
///
/// * If there are syntax errors, then an `Err(Error)` is returned, containing information
/// about what went wrong.
pub fn parse(input: &str) -> Result<Program, Error> {
    let lexer = Lexer::new(input);
    let mut parser = Parser::new(lexer);

    return parser.parse_program();
}