@fazelstudio/codemirror-lang-luau
v0.1.1
Published
Luau language support for the CodeMirror code editor
Downloads
148
Maintainers
Readme
@fazelstudio/codemirror-lang-luau
This package implements Luau (.luau) language support for the
CodeMirror code editor: a full Lezer grammar covering
Luau syntax including all Lua 5.1 features plus Luau-specific additions:
gradual typing with type annotations (: Type), type aliases, generic types
with variadic packs (T...), union/intersection/optional types (|, &,
?), type casts (:: Type), export type, typeof types, compound
assignments (+=, -=, *=, /=, //=, %=, ^=, ..=), continue
statement (context-sensitive), string interpolation (backtick
`...{expr}...`), if expressions (if cond then a else b with
elseif chains), bitwise operators (&, |, ~, <<, >>), extended
string escapes (\x, \u, \z), extended numeric literals (hex 0x,
binary 0b, underscore separators), and function attributes
(@[attribute]).
This code is released under an MIT license.
Features
- Full Lua 5.1 compatibility (all valid Lua 5.1 code works)
- Gradual typing: type annotations,
export type,typeof, generic types with packs (T...), union/intersection/optional types, named function types ((a: number) -> string), type casts (::), variadic packs (...T,T...) - Compound assignment operators (8 variants)
continuestatement (context-sensitive keyword, disambiguated vscontinue = 5/continue())- String interpolation with backticks (
`Hello {name} {expr}`) with escapes - If expressions with
elseifchains (if c then a elseif d then b else c) - Correct operator precedence (
or<and<if<cmp<..right <+ -<* / // %<&<~<|<<< >><^right < unary) - Bitwise operators, extended literals, function attributes,
--!strictdirectives - Syntax highlighting, auto-indent and code folding for all blocks
Usage
import { EditorView, basicSetup } from "codemirror"
import { luau } from "@fazelstudio/codemirror-lang-luau"
new EditorView({
parent: document.body,
doc: `local name: string = "World"\nprint(\`Hello, {name}!\`)`,
extensions: [basicSetup, luau()],
})API
luau() → LanguageSupport
Create a language support extension for Luau.
import {EditorView, basicSetup} from "codemirror"
import {luau} from "@fazelstudio/codemirror-lang-luau"
new EditorView({
parent: document.body,
extensions: [basicSetup, luau()],
doc: `export type User = { name: string }\nlocal x: number | string = 42`
})luauLanguage: LRLanguage
The Luau language object. Can be used for custom configuration:
import {luauLanguage} from "@fazelstudio/codemirror-lang-luau"
luauLanguage.data.of({ autocomplete: myCompletions })Testing
npm run build:grammar # generate parser from src/luau.grammar
npm run build # build ESM + CJS + .d.ts via rollup
npm test # run 104 fixture tests (mocha)
npm pack --dry-run # verify only dist/ is publishedGrammar highlights
- External tokenizer (
src/tokens.js) for block comments (--[=[ ... ]=]), long strings ([=[ ... ]=]), and interpolated strings (backtick,{expr}, escapes) — contextual (stack.canShift) to avoid conflict with normal strings. continuedisambiguation via GLR~contsplit:continueisVariableNamewhen followed by=,,,(,[,.,:andContinueStatementotherwise.type/export/typeoftreated as contextual keywords via~typeGLR, allowinglocal type = 10andtype{}call vstype Foo = number.- String interpolation:
`a {b} c {d+e} f`→InterpolatedStringwithInterpContent+Interpolation{ "{" Expression "}" }. - Types:
UnionType,IntersectionType,OptionalType(?),ParenType((A, B)),TableType,FunctionType((a: number) -> stringwithFuncParam),GenericType(Foo<T>),TypeofType(typeof(x)),TypeParams(<T...>).
Known limitations
- Type analysis/inference is out of scope — this package only provides syntax highlighting and parsing, not semantic type checking.
gotoand labels are not supported (Luau doesn't have them).- Module/package system (
require) is runtime-level, not part of the grammar. - Autocomplete for Roblox APIs is out of scope (use a separate language server).
