1use core::fmt;
2use oak_core::{Source, Token, TokenType, UniversalElementRole, UniversalTokenRole};
3
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7pub type GoToken = Token<GoTokenType>;
8
9#[derive(Clone, Copy, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub enum GoTokenType {
12 SourceFile,
14 PackageClause,
15 ImportDeclaration,
16 ImportSpec,
17 FunctionDeclaration,
18 ParameterList,
19 ParameterDecl,
20 Block,
21 VariableDeclaration,
22 VariableSpec,
23 ConstDeclaration,
24 ConstSpec,
25 TypeDeclaration,
26 TypeSpec,
27 StructType,
28 FieldDeclList,
29 FieldDecl,
30 InterfaceType,
31 MethodSpecList,
32 MethodSpec,
33 ExpressionList,
34 AssignmentStatement,
35 ShortVarDecl,
36 ReturnStatement,
37 IfStatement,
38 ForStatement,
39 SwitchStatement,
40 ExprCaseClause,
41 TypeSwitchStatement,
42 TypeCaseClause,
43 SelectStatement,
44 CommClause,
45 GoStatement,
46 DeferStatement,
47 CallExpression,
48 IndexExpression,
49 SelectorExpression,
50 SliceExpression,
51 TypeAssertion,
52 UnaryExpression,
53 BinaryExpression,
54 LiteralValue,
55 ElementList,
56 KeyedElement,
57
58 IntLiteral,
60 FloatLiteral,
61 StringLiteral,
62 RuneLiteral,
63 BoolLiteral,
64
65 Identifier,
67
68 Break,
70 Case,
71 Chan,
72 Const,
73 Continue,
74 Default,
75 Defer,
76 Else,
77 Fallthrough,
78 For,
79 Func,
80 Go,
81 Goto,
82 If,
83 Import,
84 Interface,
85 Map,
86 Package,
87 Range,
88 Return,
89 Select,
90 Struct,
91 Switch,
92 Type,
93 Var,
94
95 Bool,
97 Byte,
98 Complex64,
99 Complex128,
100 ErrorType,
101 Float32,
102 Float64,
103 Int,
104 Int8,
105 Int16,
106 Int32,
107 Int64,
108 Rune,
109 String,
110 Uint,
111 Uint8,
112 Uint16,
113 Uint32,
114 Uint64,
115 Uintptr,
116
117 NilLiteral,
119 NumberLiteral,
120 CharLiteral,
121
122 Plus, Minus, Star, Slash, Percent, Ampersand, Pipe, Caret, LeftShift, RightShift, AmpersandCaret, PlusAssign, MinusAssign, StarAssign, SlashAssign, PercentAssign, AmpersandAssign, PipeAssign, CaretAssign, XorAssign, LeftShiftAssign, RightShiftAssign, AmpersandCaretAssign, AndAssign, OrAssign, AndNotAssign, AndNot, LogicalAnd, LogicalOr, And, Or, Arrow, LeftArrow, Increment, Decrement, Equal, Less, Greater, Assign, LogicalNot, Not, NotEqual, LessEqual, GreaterEqual, ColonAssign, Define, Ellipsis, LeftParen, RightParen, LeftBracket, RightBracket, LeftBrace, RightBrace, Comma, Period, Dot, Semicolon, Colon, Whitespace,
190 Comment,
191
192 Eof,
194 Error,
195}
196
197impl GoTokenType {
198 pub fn is_ignored(&self) -> bool {
199 matches!(self, Self::Whitespace | Self::Comment)
200 }
201
202 pub fn is_keyword(&self) -> bool {
203 matches!(
204 self,
205 Self::Break
206 | Self::Case
207 | Self::Chan
208 | Self::Const
209 | Self::Continue
210 | Self::Default
211 | Self::Defer
212 | Self::Else
213 | Self::Fallthrough
214 | Self::For
215 | Self::Func
216 | Self::Go
217 | Self::Goto
218 | Self::If
219 | Self::Import
220 | Self::Interface
221 | Self::Map
222 | Self::Package
223 | Self::Range
224 | Self::Return
225 | Self::Select
226 | Self::Struct
227 | Self::Switch
228 | Self::Type
229 | Self::Var
230 )
231 }
232}
233
234impl fmt::Debug for GoTokenType {
235 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
236 let name = match self {
237 Self::SourceFile => "SourceFile",
238 Self::PackageClause => "PackageClause",
239 Self::ImportDeclaration => "ImportDeclaration",
240 Self::ImportSpec => "ImportSpec",
241 Self::FunctionDeclaration => "FunctionDeclaration",
242 Self::ParameterList => "ParameterList",
243 Self::ParameterDecl => "ParameterDecl",
244 Self::Block => "Block",
245 Self::VariableDeclaration => "VariableDeclaration",
246 Self::VariableSpec => "VariableSpec",
247 Self::ConstDeclaration => "ConstDeclaration",
248 Self::ConstSpec => "ConstSpec",
249 Self::TypeDeclaration => "TypeDeclaration",
250 Self::TypeSpec => "TypeSpec",
251 Self::StructType => "StructType",
252 Self::FieldDeclList => "FieldDeclList",
253 Self::FieldDecl => "FieldDecl",
254 Self::InterfaceType => "InterfaceType",
255 Self::MethodSpecList => "MethodSpecList",
256 Self::MethodSpec => "MethodSpec",
257 Self::ExpressionList => "ExpressionList",
258 Self::AssignmentStatement => "AssignmentStatement",
259 Self::ShortVarDecl => "ShortVarDecl",
260 Self::ReturnStatement => "ReturnStatement",
261 Self::IfStatement => "IfStatement",
262 Self::ForStatement => "ForStatement",
263 Self::SwitchStatement => "SwitchStatement",
264 Self::ExprCaseClause => "ExprCaseClause",
265 Self::TypeSwitchStatement => "TypeSwitchStatement",
266 Self::TypeCaseClause => "TypeCaseClause",
267 Self::SelectStatement => "SelectStatement",
268 Self::CommClause => "CommClause",
269 Self::GoStatement => "GoStatement",
270 Self::DeferStatement => "DeferStatement",
271 Self::CallExpression => "CallExpression",
272 Self::IndexExpression => "IndexExpression",
273 Self::SelectorExpression => "SelectorExpression",
274 Self::SliceExpression => "SliceExpression",
275 Self::TypeAssertion => "TypeAssertion",
276 Self::UnaryExpression => "UnaryExpression",
277 Self::BinaryExpression => "BinaryExpression",
278 Self::LiteralValue => "LiteralValue",
279 Self::ElementList => "ElementList",
280 Self::KeyedElement => "KeyedElement",
281 Self::IntLiteral => "IntLiteral",
282 Self::FloatLiteral => "FloatLiteral",
283 Self::StringLiteral => "StringLiteral",
284 Self::RuneLiteral => "RuneLiteral",
285 Self::BoolLiteral => "BoolLiteral",
286 Self::NilLiteral => "NilLiteral",
287 Self::Identifier => "Identifier",
288 Self::Package => "Package",
289 Self::Import => "Import",
290 Self::Func => "Func",
291 Self::Var => "Var",
292 Self::Const => "Const",
293 Self::Type => "Type",
294 Self::Struct => "Struct",
295 Self::Interface => "Interface",
296 Self::Map => "Map",
297 Self::Chan => "Chan",
298 Self::If => "If",
299 Self::Else => "Else",
300 Self::For => "For",
301 Self::Range => "Range",
302 Self::Switch => "Switch",
303 Self::Case => "Case",
304 Self::Default => "Default",
305 Self::Break => "Break",
306 Self::Continue => "Continue",
307 Self::Return => "Return",
308 Self::Go => "Go",
309 Self::Defer => "Defer",
310 Self::Select => "Select",
311 Self::Fallthrough => "Fallthrough",
312 Self::Goto => "Goto",
313 Self::LeftParen => "LeftParen",
314 Self::RightParen => "RightParen",
315 Self::LeftBrace => "LeftBrace",
316 Self::RightBrace => "RightBrace",
317 Self::LeftBracket => "LeftBracket",
318 Self::RightBracket => "RightBracket",
319 Self::Plus => "Plus",
320 Self::Minus => "Minus",
321 Self::Star => "Star",
322 Self::Slash => "Slash",
323 Self::Percent => "Percent",
324 Self::Ampersand => "Ampersand",
325 Self::Pipe => "Pipe",
326 Self::Caret => "Caret",
327 Self::LeftShift => "LeftShift",
328 Self::RightShift => "RightShift",
329 Self::AndNot => "AndNot",
330 Self::PlusAssign => "PlusAssign",
331 Self::MinusAssign => "MinusAssign",
332 Self::StarAssign => "StarAssign",
333 Self::SlashAssign => "SlashAssign",
334 Self::PercentAssign => "PercentAssign",
335 Self::AmpersandAssign => "AmpersandAssign",
336 Self::PipeAssign => "PipeAssign",
337 Self::CaretAssign => "CaretAssign",
338 Self::LeftShiftAssign => "LeftShiftAssign",
339 Self::RightShiftAssign => "RightShiftAssign",
340 Self::XorAssign => "XorAssign",
341 Self::AndAssign => "AndAssign",
342 Self::OrAssign => "OrAssign",
343 Self::AndNotAssign => "AndNotAssign",
344 Self::LogicalAnd => "LogicalAnd",
345 Self::LogicalOr => "LogicalOr",
346 Self::And => "And",
347 Self::Or => "Or",
348 Self::Arrow => "Arrow",
349 Self::LeftArrow => "LeftArrow",
350 Self::Increment => "Increment",
351 Self::Decrement => "Decrement",
352 Self::Equal => "Equal",
353 Self::Less => "Less",
354 Self::Greater => "Greater",
355 Self::Assign => "Assign",
356 Self::LogicalNot => "LogicalNot",
357 Self::Not => "Not",
358 Self::NotEqual => "NotEqual",
359 Self::LessEqual => "LessEqual",
360 Self::GreaterEqual => "GreaterEqual",
361 Self::ColonAssign => "ColonAssign",
362 Self::Define => "Define",
363 Self::Comma => "Comma",
364 Self::Period => "Period",
365 Self::Dot => "Dot",
366 Self::Semicolon => "Semicolon",
367 Self::Colon => "Colon",
368 Self::Ellipsis => "Ellipsis",
369 Self::AmpersandCaret => "AmpersandCaret",
370 Self::AmpersandCaretAssign => "AmpersandCaretAssign",
371 Self::Bool => "Bool",
372 Self::Byte => "Byte",
373 Self::Complex64 => "Complex64",
374 Self::Complex128 => "Complex128",
375 Self::ErrorType => "ErrorType",
376 Self::Float32 => "Float32",
377 Self::Float64 => "Float64",
378 Self::Int => "Int",
379 Self::Int8 => "Int8",
380 Self::Int16 => "Int16",
381 Self::Int32 => "Int32",
382 Self::Int64 => "Int64",
383 Self::Rune => "Rune",
384 Self::String => "String",
385 Self::Uint => "Uint",
386 Self::Uint8 => "Uint8",
387 Self::Uint16 => "Uint16",
388 Self::Uint32 => "Uint32",
389 Self::Uint64 => "Uint64",
390 Self::Uintptr => "Uintptr",
391 Self::NumberLiteral => "NumberLiteral",
392 Self::CharLiteral => "CharLiteral",
393 Self::Whitespace => "Whitespace",
394 Self::Comment => "Comment",
395 Self::Eof => "Eof",
396 Self::Error => "Error",
397 };
398 write!(f, "{}", name)
399 }
400}
401
402impl TokenType for GoTokenType {
403 type Role = UniversalTokenRole;
404 const END_OF_STREAM: Self = Self::Error;
405
406 fn is_ignored(&self) -> bool {
407 false
408 }
409
410 fn role(&self) -> Self::Role {
411 match self {
412 _ => UniversalTokenRole::None,
413 }
414 }
415}