luau_parser/types/value/table.rs
1//! Holding all needed information for tables.
2
3use luau_lexer::prelude::Token;
4use luau_parser_derive::{Print, Range};
5use smol_str::SmolStr;
6
7use crate::types::{Bracketed, BracketedList, Expression, Pointer, TypeValue};
8
9/// A possible key entry in a table. The key is usually a string, but it can be a value
10/// (from an expression) in tables or a type in type definitions.
11///
12/// ```lua
13/// local t = {
14/// -- The normal string key.
15/// foo = "foo",
16///
17/// -- A Value key, it's still a string, but what's inside [] is
18/// -- always counted as an expression, even if simple.
19/// ["bar"] = "bar",
20/// }
21///
22/// type T = {
23/// -- The normal string key.
24/// foo: "foo",
25///
26/// -- A Type key, it indicates that the key can be any string, not the word "string".
27/// [string]: number,
28/// }
29/// ```
30#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
31#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
32pub enum TableKey {
33 /// This [`TableKey`] had a syntax error.
34 #[default]
35 ERROR,
36
37 /// Cases in which a key in a table expression wasn't provided,
38 /// it's guessed as a number in that case.
39 UndefinedNumber(u32),
40
41 /// Cases in which a key in a type expression wasn't provided,
42 /// it's guessed as `number`. It has no other possible values
43 /// than `number`.
44 UndefinedString(SmolStr),
45
46 /// Simple key
47 Simple(Token),
48
49 /// An expression, can only be used in definitions and not in types.
50 Expression(Bracketed<Pointer<Expression>>),
51
52 /// A type definition, can only be used in other types and not definitions.
53 Type(Bracketed<Pointer<TypeValue>>),
54}
55
56/// A struct representing one table field. It'll always have a [`key`](TableKey) and a
57/// value that's either a [`type`](TypeDefinition) or an [`expression`](Expression). See
58/// [`table field values`](TableFieldValue).
59#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
60#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
61pub struct TableField {
62 /// The [`key`](TableKey) used to index field.
63 pub key: Pointer<TableKey>,
64
65 /// The `=` or `:` tokens, it's `=` in variables and `:` in types.
66 pub equal_or_colon: Option<Token>,
67
68 /// The value of this field. An expression in variables and a type in type
69 /// definitions.
70 pub value: Pointer<TableFieldValue>,
71}
72
73/// A possible value for a [`table field`](TableField).
74#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
75#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
76pub enum TableFieldValue {
77 /// This [`TableFieldValue`] had a syntax error.
78 #[default]
79 ERROR,
80
81 /// An [`expression`](Expression), can be found in declarations of tables as variables
82 /// only.
83 Expression(Expression),
84
85 /// A [`type`](TypeValue), can be found in type definitions only.
86 Type(TypeValue),
87
88 ///```lua
89 /// {...}
90 /// ```
91 // only in expressions
92 VariadicValues(Token),
93}
94
95/// Struct representing a luau table.
96#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
97#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
98pub struct Table(pub BracketedList<TableField>);