Skip to main content

luaparse_rs/
marker.rs

1//! Lua version markers that control which syntax features the parser accepts.
2//!
3//! Pass one of these as the type parameter to [`Parser`](crate::Parser) to
4//! select which grammar rules apply.
5
6use sealed::sealed;
7
8/// Describes which syntax features a Lua version supports.
9///
10/// This is a sealed trait; you cannot implement it for your own types.
11/// Pick one of the provided versions: [`Luau`], [`Lua51`], [`Lua52`],
12/// [`Lua53`], or [`Lua54`].
13#[sealed]
14pub trait LuaVersion: 'static {
15    /// A display name for this version (e.g. `"Lua 5.4"`, `"Luau"`).
16    const NAME: &'static str;
17    /// `//` integer division operator.
18    const HAS_FLOOR_DIV: bool;
19    /// Compound assignment operators (`+=`, `-=`, etc.).
20    const HAS_COMPOUND_ASSIGN: bool;
21    /// The `continue` keyword inside loops.
22    const HAS_CONTINUE: bool;
23    /// Inline `if cond then a else b` expressions.
24    const HAS_IF_EXPR: bool;
25    /// Backtick interpolated strings (`` `hello {name}` ``).
26    const HAS_STRING_INTERP: bool;
27    /// Type annotations (`: Type`, `-> ReturnType`).
28    const HAS_TYPE_ANNOTATIONS: bool;
29    /// `@native` and similar function attributes.
30    const HAS_ATTRIBUTES: bool;
31    /// Generic type parameters (`<T>`).
32    const HAS_GENERICS: bool;
33    /// `export type` declarations.
34    const HAS_EXPORT: bool;
35    /// `goto` and `::label::` statements.
36    const HAS_GOTO: bool;
37    /// Bitwise operators (`&`, `|`, `~`, `<<`, `>>`).
38    const HAS_BITWISE_OPS: bool;
39    /// `<const>` and `<close>` variable attributes (Lua 5.4).
40    const HAS_VARIABLE_ATTRIBUTES: bool;
41    /// `local const x = 5` immutable bindings (Luau).
42    const HAS_CONST: bool;
43}
44
45/// Roblox's Luau dialect.
46///
47/// Supports type annotations, if expressions, string interpolation,
48/// compound assignment, `continue`, `@native` attributes, generics,
49/// `export type`, and `const` bindings. Does not have `goto` or bitwise operators.
50#[derive(Debug, Clone, Copy)]
51pub struct Luau;
52
53// #[cfg(feature = "luau")]
54#[sealed]
55impl LuaVersion for Luau {
56    const NAME: &'static str = "Luau";
57    const HAS_FLOOR_DIV: bool = true;
58    const HAS_COMPOUND_ASSIGN: bool = true;
59    const HAS_CONTINUE: bool = true;
60    const HAS_IF_EXPR: bool = true;
61    const HAS_STRING_INTERP: bool = true;
62    const HAS_TYPE_ANNOTATIONS: bool = true;
63    const HAS_ATTRIBUTES: bool = true;
64    const HAS_GENERICS: bool = true;
65    const HAS_EXPORT: bool = true;
66    const HAS_GOTO: bool = false;
67    const HAS_BITWISE_OPS: bool = false;
68    const HAS_VARIABLE_ATTRIBUTES: bool = false;
69    const HAS_CONST: bool = true;
70}
71
72/// Standard Lua 5.1.
73///
74/// The baseline version. No floor division, no compound assignment,
75/// no `goto`, no bitwise operators, no type annotations.
76#[derive(Debug, Clone, Copy)]
77pub struct Lua51;
78
79// #[cfg(feature = "lua51")]
80#[sealed]
81impl LuaVersion for Lua51 {
82    const NAME: &'static str = "Lua 5.1";
83    const HAS_FLOOR_DIV: bool = false;
84    const HAS_COMPOUND_ASSIGN: bool = false;
85    const HAS_CONTINUE: bool = false;
86    const HAS_IF_EXPR: bool = false;
87    const HAS_STRING_INTERP: bool = false;
88    const HAS_TYPE_ANNOTATIONS: bool = false;
89    const HAS_ATTRIBUTES: bool = false;
90    const HAS_GENERICS: bool = false;
91    const HAS_EXPORT: bool = false;
92    const HAS_GOTO: bool = false;
93    const HAS_BITWISE_OPS: bool = false;
94    const HAS_VARIABLE_ATTRIBUTES: bool = false;
95    const HAS_CONST: bool = false;
96}
97
98/// Standard Lua 5.2.
99///
100/// Adds `goto` and `::label::` over Lua 5.1. No floor division,
101/// no compound assignment, no bitwise operators.
102#[derive(Debug, Clone, Copy)]
103pub struct Lua52;
104
105// #[cfg(feature = "lua52")]
106#[sealed]
107impl LuaVersion for Lua52 {
108    const NAME: &'static str = "Lua 5.2";
109    const HAS_FLOOR_DIV: bool = false;
110    const HAS_COMPOUND_ASSIGN: bool = false;
111    const HAS_CONTINUE: bool = false;
112    const HAS_IF_EXPR: bool = false;
113    const HAS_STRING_INTERP: bool = false;
114    const HAS_TYPE_ANNOTATIONS: bool = false;
115    const HAS_ATTRIBUTES: bool = false;
116    const HAS_GENERICS: bool = false;
117    const HAS_EXPORT: bool = false;
118    const HAS_GOTO: bool = true;
119    const HAS_BITWISE_OPS: bool = false;
120    const HAS_VARIABLE_ATTRIBUTES: bool = false;
121    const HAS_CONST: bool = false;
122}
123
124/// Standard Lua 5.3.
125///
126/// Adds `//` floor division, bitwise operators (`&`, `|`, `~`, `<<`, `>>`),
127/// and `goto` over Lua 5.2.
128#[derive(Debug, Clone, Copy)]
129pub struct Lua53;
130
131// #[cfg(feature = "lua53")]
132#[sealed]
133impl LuaVersion for Lua53 {
134    const NAME: &'static str = "Lua 5.3"; // gross
135    const HAS_FLOOR_DIV: bool = true;
136    const HAS_COMPOUND_ASSIGN: bool = false;
137    const HAS_CONTINUE: bool = false;
138    const HAS_IF_EXPR: bool = false;
139    const HAS_STRING_INTERP: bool = false;
140    const HAS_TYPE_ANNOTATIONS: bool = false;
141    const HAS_ATTRIBUTES: bool = false;
142    const HAS_GENERICS: bool = false;
143    const HAS_EXPORT: bool = false;
144    const HAS_GOTO: bool = true;
145    const HAS_BITWISE_OPS: bool = true;
146    const HAS_VARIABLE_ATTRIBUTES: bool = false;
147    const HAS_CONST: bool = false;
148}
149
150/// Standard Lua 5.4.
151///
152/// Adds `<const>` and `<close>` variable attributes and `@` function
153/// attributes over Lua 5.3. Keeps floor division, bitwise operators, and `goto`.
154#[derive(Debug, Clone, Copy)]
155pub struct Lua54;
156
157#[sealed]
158impl LuaVersion for Lua54 {
159    const NAME: &'static str = "Lua 5.4";
160    const HAS_FLOOR_DIV: bool = true;
161    const HAS_COMPOUND_ASSIGN: bool = false;
162    const HAS_CONTINUE: bool = false;
163    const HAS_IF_EXPR: bool = false;
164    const HAS_STRING_INTERP: bool = false;
165    const HAS_TYPE_ANNOTATIONS: bool = false;
166    const HAS_ATTRIBUTES: bool = true;
167    const HAS_GENERICS: bool = false;
168    const HAS_EXPORT: bool = false;
169    const HAS_GOTO: bool = true;
170    const HAS_BITWISE_OPS: bool = true;
171    const HAS_VARIABLE_ATTRIBUTES: bool = true;
172    const HAS_CONST: bool = false;
173}