~starkingdoms/starkingdoms

ref: 4ea4c4fa871a7dec830b43197333b6f8899f6af5 starkingdoms/kabel/src/macros.rs -rw-r--r-- 2.8 KiB
4ea4c4fa — ghostly_zsh debug printing 1 year, 4 months ago
                                                                                
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
#[macro_export]
macro_rules! token {
    ($self:expr, $token:expr) => {
        crate::lexer::Token {
            line: $self.line,
            line_start: $self.line_start,
            column: $self.column,
            start: $self.start,
            end: $self.current,
            token_type: $token,
        }
    };
}

#[macro_export]
macro_rules! token_display {
    ( $match_to:expr, $f:expr, $( $name:tt),*) => {
        match $match_to {
            $( $name => { $f.write_str(stringify!($name))?; } ),*
            _ => ()
        }
    }
}

#[macro_export]
macro_rules! lit {
    ($type:ident, $data:expr, $token:expr) => {
        $crate::parser::AST {
            ast_type: $crate::parser::ASTType::Lit($crate::parser::Lit::$type($data)),
            line_start: $token.line_start,
            start: $token.start,
            end: $token.end,
            line: $token.line,
            column: $token.column,
        }
    };
}

#[macro_export]
macro_rules! ast {
    ($ast_type:expr, $start:expr, $end:expr) => {
        AST {
            ast_type: $ast_type,
            line_start: $start.line_start,
            start: $start.start,
            end: $end.end,
            line: $start.line,
            column: $start.column,
        }
    };
}

#[macro_export]
macro_rules! name {
    ($name:expr, $token:expr) => {
        Name {
            name: $name,
            line_start: $token.line_start,
            end: $token.end,
            line: $token.line,
            column: $token.column,
        }
    };
}

#[macro_export]
macro_rules! push_output {
    ( $match_to:expr, $output:expr, $( $name:tt),*) => {
        match $match_to {
            $( $name => { $output += stringify!($name); } ),*
        }
    }
}

#[macro_export]
macro_rules! unexpected_token {
    ($self:expr, $message:expr, $token:expr) => {
        $crate::error::KabelError::new(
            $crate::error::ErrorKind::UnexpectedToken,
            format!($message, $self.text[$token.start..$token.end].to_string()),
            $token.line,
            $token.column,
            $self.text[$token.line_start..$token.end].to_string(),
        )
    };
}

#[macro_export]
macro_rules! out_of_scope {
    ($self:expr, $message:expr, $name:expr, $expr:expr) => {
        $crate::error::KabelError::new(
            $crate::error::ErrorKind::OutOfScope,
            format!($message, $name),
            $expr.line,
            $expr.column,
            $self.text[$expr.line_start..$expr.end].to_string(),
        )
    };
}
#[macro_export]
macro_rules! out_of_scope_var {
    ($self:expr, $message:expr, $name:expr, $expr:expr) => {
        $crate::error::KabelError::new(
            $crate::error::ErrorKind::OutOfScope,
            format!($message, $name.name),
            $expr.line,
            $name.column,
            $self.text[$expr.line_start..$expr.end].to_string(),
        )
    };
}