1mod r#impl;
4
5use lsp_types::Position;
6use smol_str::SmolStr;
7
8use crate::prelude::{ParseError, PositionExt};
9
10crate_reexport!(literal, keyword, symbol, operator, comment);
11
12#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
15#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
16pub struct Token {
17 pub start: Position,
19
20 pub leading_trivia: Vec<Trivia>,
22
23 pub token_type: TokenType,
25
26 pub trailing_trivia: Vec<Trivia>,
28
29 pub end: Position,
31}
32
33#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
35#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
36pub enum Trivia {
37 Spaces(SmolStr),
39
40 Comment(Comment),
42}
43
44impl Token {
45 #[inline]
48 pub const fn empty(token_type: TokenType) -> Self {
49 Self {
50 start: Position::MAX,
51 leading_trivia: Vec::new(),
52 token_type,
53 trailing_trivia: Vec::new(),
54 end: Position::MAX,
55 }
56 }
57}
58
59impl PartialEq<TokenType> for Token {
60 fn eq(&self, other: &TokenType) -> bool {
61 &self.token_type == other
62 }
63}
64
65#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
67#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
68pub enum TokenType {
69 Error(ParseError),
71
72 Literal(Literal),
74
75 Identifier(SmolStr),
77
78 Comment(Comment),
80
81 Keyword(Keyword),
83
84 PartialKeyword(PartialKeyword),
86
87 Symbol(Symbol),
89
90 Operator(Operator),
92
93 CompoundOperator(CompoundOperator),
95
96 EndOfFile,
98}
99
100impl TokenType {
101 pub fn into_token(
103 self,
104 start: Position,
105 end: Position,
106 leading_trivia: Vec<Trivia>,
107 trailing_trivia: Vec<Trivia>,
108 ) -> Token {
109 Token {
110 start,
111 leading_trivia,
112 token_type: self,
113 trailing_trivia,
114 end,
115 }
116 }
117}
118
119impl TokenType {
120 pub fn try_as_string(&self) -> Option<String> {
122 match self {
123 TokenType::Literal(literal) => match literal {
124 Literal::Number(luau_number) => match luau_number {
125 LuauNumber::Plain(smol_str)
126 | LuauNumber::Binary(smol_str)
127 | LuauNumber::Hex(smol_str) => Some(smol_str.to_string()),
128 },
129 Literal::String(luau_string) => match luau_string {
130 LuauString::SingleQuotes(smol_str)
131 | LuauString::DoubleQuotes(smol_str)
132 | LuauString::Backticks(smol_str)
133 | LuauString::MultiLine(smol_str) => Some(smol_str.to_string()),
134 },
135 Literal::Boolean(true) => Some("true".to_string()),
136 Literal::Boolean(false) => Some("false".to_string()),
137 },
138 TokenType::Identifier(smol_str) => Some(smol_str.to_string()),
139 TokenType::Comment(comment) => match comment {
140 Comment::MultiLine(smol_str) | Comment::SingleLine(smol_str) => {
141 Some(smol_str.to_string())
142 }
143 },
144 TokenType::Keyword(keyword) => Some(keyword.to_string()),
145 TokenType::PartialKeyword(partial_keyword) => Some(partial_keyword.to_string()),
146 TokenType::Symbol(symbol) => Some(symbol.to_string()),
147 TokenType::Operator(operator) => Some(operator.to_string()),
148 TokenType::CompoundOperator(compound_operator) => Some(compound_operator.to_string()),
149 _ => None,
150 }
151 }
152}
153
154impl_from!(TokenType <= {
155 Error(ParseError),
156 Literal(Literal),
157 Keyword(Keyword),
158 PartialKeyword(PartialKeyword),
159 Symbol(Symbol),
160 Operator(Operator),
161 CompoundOperator(CompoundOperator),
162});