emmylua_parser/kind/
mod.rs

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
pub use lua_operator_kind::{BinaryOperator, UnaryOperator, UNARY_PRIORITY};
pub use lua_syntax_kind::LuaSyntaxKind;
pub use lua_token_kind::LuaTokenKind;
pub use lua_language_level::LuaLanguageLevel;
pub use lua_type_operator_kind::{LuaTypeBinaryOperator, LuaTypeTernaryOperator, LuaTypeUnaryOperator};
pub use lua_visibility_kind::VisibilityKind;
pub use lua_version::LuaVersionNumber;

mod lua_operator_kind;
mod lua_syntax_kind;
mod lua_token_kind;
mod lua_visibility_kind;
mod lua_language_level;
mod lua_type_operator_kind;
mod lua_version;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u16)]
pub enum LuaKind {
    Syntax(LuaSyntaxKind),
    Token(LuaTokenKind),
}

impl From<LuaSyntaxKind> for LuaKind {
    fn from(kind: LuaSyntaxKind) -> Self {
        LuaKind::Syntax(kind)
    }
}

impl From<LuaTokenKind> for LuaKind {
    fn from(kind: LuaTokenKind) -> Self {
        LuaKind::Token(kind)
    }
}

impl Into<LuaSyntaxKind> for LuaKind {
    fn into(self) -> LuaSyntaxKind {
        match self {
            LuaKind::Syntax(kind) => kind,
            _ => LuaSyntaxKind::None,
        }
    }
}

impl Into<LuaTokenKind> for LuaKind {
    fn into(self) -> LuaTokenKind {
        match self {
            LuaKind::Token(kind) => kind,
            _ => LuaTokenKind::None,
        }
    }
}

impl LuaKind {
    pub fn is_syntax(self) -> bool {
        matches!(self, LuaKind::Syntax(_))
    }

    pub fn is_token(self) -> bool {
        matches!(self, LuaKind::Token(_))
    }

    pub fn get_raw(self) -> u16 {
        match self {
            LuaKind::Syntax(kind) => kind as u16 | 0x8000,
            LuaKind::Token(kind) => kind as u16,
        }
    }

    pub fn from_raw(raw: u16) -> LuaKind {
        if raw & 0x8000 != 0 {
            LuaKind::Syntax(unsafe { std::mem::transmute(raw & 0x7FFF) })
        } else {
            LuaKind::Token(unsafe { std::mem::transmute(raw) })
        }
    }
}

#[derive(Debug)]
pub struct PriorityTable {
    pub left: i32,
    pub right: i32,
}

#[derive(Debug, PartialEq)]
pub enum LuaOpKind {
    None,
    Unary(UnaryOperator),
    Binary(BinaryOperator),
    TypeUnary(LuaTypeUnaryOperator),
    TypeBinary(LuaTypeBinaryOperator),
    TypeTernary(LuaTypeTernaryOperator),
}

impl From<UnaryOperator> for LuaOpKind {
    fn from(op: UnaryOperator) -> Self {
        LuaOpKind::Unary(op)
    }
}

impl From<BinaryOperator> for LuaOpKind {
    fn from(op: BinaryOperator) -> Self {
        LuaOpKind::Binary(op)
    }
}

impl From<LuaTypeUnaryOperator> for LuaOpKind {
    fn from(op: LuaTypeUnaryOperator) -> Self {
        LuaOpKind::TypeUnary(op)
    }
}

impl From<LuaTypeBinaryOperator> for LuaOpKind {
    fn from(op: LuaTypeBinaryOperator) -> Self {
        LuaOpKind::TypeBinary(op)
    }
}

impl From<LuaTypeTernaryOperator> for LuaOpKind {
    fn from(op: LuaTypeTernaryOperator) -> Self {
        LuaOpKind::TypeTernary(op)
    }
}

impl LuaOpKind {
    pub fn to_unary_operator(kind: LuaTokenKind) -> UnaryOperator {
        match kind {
            LuaTokenKind::TkNot => UnaryOperator::OpNot,
            LuaTokenKind::TkLen => UnaryOperator::OpLen,
            LuaTokenKind::TkMinus => UnaryOperator::OpUnm,
            LuaTokenKind::TkBitXor => UnaryOperator::OpBNot,
            _ => UnaryOperator::OpNop,
        }
    }

    pub fn to_binary_operator(kind: LuaTokenKind) -> BinaryOperator {
        match kind {
            LuaTokenKind::TkPlus => BinaryOperator::OpAdd,
            LuaTokenKind::TkMinus => BinaryOperator::OpSub,
            LuaTokenKind::TkMul => BinaryOperator::OpMul,
            LuaTokenKind::TkMod => BinaryOperator::OpMod,
            LuaTokenKind::TkPow => BinaryOperator::OpPow,
            LuaTokenKind::TkDiv => BinaryOperator::OpDiv,
            LuaTokenKind::TkIDiv => BinaryOperator::OpIDiv,
            LuaTokenKind::TkBitAnd => BinaryOperator::OpBAnd,
            LuaTokenKind::TkBitOr => BinaryOperator::OpBOr,
            LuaTokenKind::TkBitXor => BinaryOperator::OpBXor,
            LuaTokenKind::TkShl => BinaryOperator::OpShl,
            LuaTokenKind::TkShr => BinaryOperator::OpShr,
            LuaTokenKind::TkConcat => BinaryOperator::OpConcat,
            LuaTokenKind::TkLt => BinaryOperator::OpLt,
            LuaTokenKind::TkLe => BinaryOperator::OpLe,
            LuaTokenKind::TkGt => BinaryOperator::OpGt,
            LuaTokenKind::TkGe => BinaryOperator::OpGe,
            LuaTokenKind::TkEq => BinaryOperator::OpEq,
            LuaTokenKind::TkNe => BinaryOperator::OpNe,
            LuaTokenKind::TkAnd => BinaryOperator::OpAnd,
            LuaTokenKind::TkOr => BinaryOperator::OpOr,
            _ => BinaryOperator::OpNop,
        }
    }

    pub fn to_type_unary_operator(kind: LuaTokenKind) -> LuaTypeUnaryOperator {
        match kind {
            LuaTokenKind::TkDocKeyOf => LuaTypeUnaryOperator::Keyof,
            _ => LuaTypeUnaryOperator::None,
        }
    }

    pub fn to_type_binary_operator(kind: LuaTokenKind) -> LuaTypeBinaryOperator {
        match kind {
            LuaTokenKind::TkDocOr => LuaTypeBinaryOperator::Union,
            LuaTokenKind::TkDocAnd => LuaTypeBinaryOperator::Intersection,
            LuaTokenKind::TkIn => LuaTypeBinaryOperator::In,
            LuaTokenKind::TkDocExtends => LuaTypeBinaryOperator::Extends,
            _ => LuaTypeBinaryOperator::None,
        }
    }
}