Skip to main content

luaur_ast/methods/
parser_expect_match_and_consume_fail.rs

1use crate::records::lexeme::Lexeme;
2use crate::records::lexeme::Type;
3use crate::records::location::Location;
4use crate::records::match_lexeme::MatchLexeme;
5use crate::records::parser::Parser;
6use crate::records::position::Position;
7use luaur_common::macros::luau_noinline::LUAU_NOINLINE;
8
9impl Parser {
10    LUAU_NOINLINE! {
11        pub fn expect_match_and_consume_fail(
12            &mut self,
13            type_: Type,
14            begin: &MatchLexeme,
15            extra: Option<&str>,
16        ) {
17            let type_string = Lexeme::new(Location::new(Position::new(0, 0), Position::new(0, 0)), type_).to_string();
18            let match_string = Lexeme::new(Location::new(Position::new(0, 0), Position::new(0, 0)), begin.type_).to_string();
19
20            let current_lexeme = self.lexer.current();
21            let extra_str = extra.unwrap_or("");
22
23            if current_lexeme.location.begin.line == begin.position.line {
24                self.report(
25                    current_lexeme.location,
26                    format_args!(
27                        "Expected {} (to close {} at column {}), got {}{}",
28                        type_string,
29                        match_string,
30                        begin.position.column + 1,
31                        current_lexeme.to_string(),
32                        extra_str
33                    ),
34                );
35            } else {
36                self.report(
37                    current_lexeme.location,
38                    format_args!(
39                        "Expected {} (to close {} at line {}), got {}{}",
40                        type_string,
41                        match_string,
42                        begin.position.line + 1,
43                        current_lexeme.to_string(),
44                        extra_str
45                    ),
46                );
47            }
48        }
49    }
50}