Skip to main content

luna_core/
version.rs

1//! Lua dialect selection.
2//!
3//! Grammar and semantics switches are expressed as capability predicates, not
4//! version comparisons at use sites, so further dialects can be added by
5//! extending this enum only.
6
7/// Lua dialect the VM emulates. Drives lexer, parser, and runtime feature
8/// gating. `Lua55` is the primary; the others are compat modes.
9#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
10pub enum LuaVersion {
11    /// Lua 5.1 — no integer subtype, `arg` table, no `goto`.
12    Lua51,
13    /// Lua 5.2 — adds `goto` / `bit32` / `\xXX` escapes, retires `setfenv`.
14    Lua52,
15    /// Lua 5.3 — adds native 64-bit integers, bitwise ops, `string.pack`.
16    Lua53,
17    /// Lua 5.4 — adds `<const>` / `<close>` attributes and integer-for spec.
18    Lua54,
19    /// Lua 5.5 — adds `global` declarations and named vararg parameters.
20    Lua55,
21}
22
23impl LuaVersion {
24    /// Integer subtype and integer literals (5.3+).
25    pub fn has_integers(self) -> bool {
26        self >= LuaVersion::Lua53
27    }
28
29    /// `goto` / `::label::` (5.2+); `goto` is a reserved word.
30    pub fn has_goto(self) -> bool {
31        self >= LuaVersion::Lua52
32    }
33
34    /// `& | ~ << >>` operators (5.3+).
35    pub fn has_bitwise_ops(self) -> bool {
36        self >= LuaVersion::Lua53
37    }
38
39    /// `//` floor division (5.3+).
40    pub fn has_idiv(self) -> bool {
41        self >= LuaVersion::Lua53
42    }
43
44    /// `<const>` / `<close>` attributes on local declarations (5.4+).
45    pub fn has_attribs(self) -> bool {
46        self >= LuaVersion::Lua54
47    }
48
49    /// Hexadecimal float literals `0x1p4` (5.2+).
50    pub fn has_hex_float(self) -> bool {
51        self >= LuaVersion::Lua52
52    }
53
54    /// String escapes `\z`, `\xXX` (5.2+) and `\u{XXX}` (5.3+).
55    pub fn has_extended_escapes(self) -> bool {
56        self >= LuaVersion::Lua52
57    }
58
59    /// `\u{XXX}` unicode escape specifically (5.3+).
60    pub fn has_unicode_escape(self) -> bool {
61        self >= LuaVersion::Lua53
62    }
63
64    /// Empty statement `;` (5.2+).
65    pub fn has_empty_statement(self) -> bool {
66        self >= LuaVersion::Lua52
67    }
68
69    /// `global` declarations; `global` is a reserved word (5.5+).
70    pub fn has_global_decl(self) -> bool {
71        self >= LuaVersion::Lua55
72    }
73
74    /// Named vararg parameter `function f(...name)` (5.5+).
75    pub fn has_named_vararg(self) -> bool {
76        self >= LuaVersion::Lua55
77    }
78
79    /// Leading collective attribute in declarations: `local <const> a, b` (5.5+).
80    pub fn has_collective_attrib(self) -> bool {
81        self >= LuaVersion::Lua55
82    }
83
84    /// In 5.1 `break` is a "last statement" like `return`; later versions allow
85    /// it anywhere in a block.
86    pub fn break_is_last_statement(self) -> bool {
87        self == LuaVersion::Lua51
88    }
89
90    /// In 5.1, `[[` inside a level-0 long string is an error
91    /// ("nesting of [[...]] is deprecated").
92    pub fn rejects_nested_long_string(self) -> bool {
93        self == LuaVersion::Lua51
94    }
95}