Skip to content

keyword ​

Helpers for Lua keywords and identifiers.

Usage ​

lua
kw = require "mods.keyword"

kw.iskeyword("local"))         --> true
kw.isidentifier("hello_world") --> true

Functions ​

FunctionDescription
iskeyword(v)Return true when v is a reserved Lua keyword.
isidentifier(v)Return true when v is a valid non-keyword Lua identifier.
kwlist()Return Lua keywords as a mods.List.
kwset()Return Lua keywords as a mods.Set.
normalize_identifier(s)Normalize an input into a safe Lua identifier.

iskeyword(v) ​

Return true when v is a reserved Lua keyword.

Parameters:

  • v (any): Value to validate.

Return:

  • isKeyword (boolean): Whether the check succeeds.

Example:

lua
kw.iskeyword("function") --> true
kw.iskeyword("hello") --> false

isidentifier(v) ​

Return true when v is a valid non-keyword Lua identifier.

Parameters:

  • v (any): Value to validate.

Return:

  • isIdentifier (boolean): Whether the check succeeds.

Example:

lua
kw.isidentifier("hello_world") --> true
kw.isidentifier("local") --> false

kwlist() ​

Return Lua keywords as a mods.List.

Return:

  • words (mods.List<string>): List of Lua keywords.

Example:

lua
kw.kwlist():contains("and") --> true

kwset() ​

Return Lua keywords as a mods.Set.

Return:

  • words (mods.Set<string>): Set of Lua keywords.

Example:

lua
kw.kwlset():contains("and") --> true

normalize_identifier(s) ​

Normalize an input into a safe Lua identifier.

Parameters:

  • s (string): Input string.

Return:

  • identifier (string): Normalized Lua identifier.

Example:

lua
kw.normalize_identifier(" 2 bad-name ") --> "_2_bad_name"