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)
57/// and a value that's either a [`type`](TypeValue) or an
58/// [`expression`](Expression).
59///
60/// See [`table field values`](TableFieldValue).
61#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
62#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
63pub struct TableField {
64 /// The [`key`](TableKey) used to index field.
65 pub key: Pointer<TableKey>,
66
67 /// The `=` or `:` tokens, it's `=` in variables and `:` in types.
68 pub equal_or_colon: Option<Token>,
69
70 /// The value of this field. An expression in variables and a type in type
71 /// definitions.
72 pub value: Pointer<TableFieldValue>,
73}
74
75/// A possible value for a [`table field`](TableField).
76#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
77#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
78pub enum TableFieldValue {
79 /// This [`TableFieldValue`] had a syntax error.
80 #[default]
81 ERROR,
82
83 /// An [`expression`](Expression), can be found in declarations of tables as variables
84 /// only.
85 Expression(Expression),
86
87 /// A [`type`](TypeValue), can be found in type definitions only.
88 Type(TypeValue),
89
90 ///```lua
91 /// {...}
92 /// ```
93 // only in expressions
94 VariadicValues(Token),
95}
96
97/// Struct representing a luau table.
98#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Range, Print)]
99#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
100pub struct Table(pub BracketedList<TableField>);