@exercism/codemirror-lang-lua
v1.0.2
Published
Lua language support for the CodeMirror code editor
Readme
@exercism/codemirror-lang-lua
[ CHANGELOG ]
This package implements Lua language support for the CodeMirror code editor, using a Lezer grammar written for this package. Exercism uses it in the Lua editor on exercism.org.
CodeMirror ships Lua as a CodeMirror 5 stream mode. A stream mode colours tokens and stops there. Without a tree there is no structural folding, no indentation that knows what a block is, and no node-aware selection.
This code is released under an MIT license.
Install
npm install @exercism/codemirror-lang-luaUsage
import {EditorView, basicSetup} from "codemirror"
import {lua} from "@exercism/codemirror-lang-lua"
const view = new EditorView({
parent: document.body,
doc: `print("Hello World!")`,
extensions: [basicSetup, lua()]
})Completion
lua() includes completion for keywords, snippets, the names declared in the
file, the members of the standard library tables, and the fields declared on a
table. The API reference below describes each completion source.
Coverage
Lua 5.4, which accepts everything 5.1 through 5.3 do, so older code parses too.
That includes integer division // and the bitwise operators with their own
precedence levels, goto and ::labels::, the <const> and <close>
attributes, \z line continuations, calls written without parentheses as
f"string" and f{table}, and hex floats like 0xA23p-4.
Long brackets carry a level. [[...]] through [=====[...]=====] are read for
both strings and comments, including the half-typed case where the closer is
not there yet.
Two departures from the reference manual, both because this parses code in an editor instead of compiling it.
Identifiers may hold characters above U+007F. Lua's own lexer asks the C
library whether a byte is a letter, so whether local α = 1 compiles depends
on the locale the interpreter was built against, and code in the wild uses it.
Painting working code as broken is the worse failure.
A newline never ends a statement, so f\n(x) is one call, which is what Lua
does. Lua 5.1 called this ambiguous syntax and 5.2 dropped the complaint.
Luau's extensions, meaning type annotations, compound assignment and
continue, are not supported.
Accuracy
Measured against 1072 Lua programs, the Lua half of the Rosetta Code corpus behind CodinCod's language-guessing game. A file counts as clean only when the parse leaves no error node anywhere.
files 1072
clean parses 1048 (97.76%)
error nodes 370All 24 files that are not clean were read, and none is a gap in the grammar.
| | |
| --: | --- |
| 14 | are not Lua: C source, HTML output, shell command lines, REPL transcripts and program output, filed under Lua on the wiki |
| 5 | are Lua that does not compile: a missing do, a missing then, an unclosed print( |
| 4 | are fragments instead of chunks, like a bare 3.14159 or an anonymous function header |
| 3 | were damaged before the parser saw them, by a corpus pipeline that masks the language's own name and decodes HTML entities inside string literals |
The measurement paid for itself on its first run by finding a bug in that
pipeline instead of in the grammar. The comment stripper read --[==[ as an
ordinary -- line comment, so a levelled long comment left its body and its
closer behind as code, and one snippet had been reduced to nothing but
]====]. These numbers are from the corpus rebuilt after that was fixed.
No corpus ships here. Rosetta Code is under the GNU Free Documentation License 1.2 and cannot be redistributed under this one.
Building and testing
npm install # installs the dependencies and builds dist/
npm run build # regenerates the parser and rebuilds dist/
npm test # builds, then runs the testsAPI Reference
lua() → LanguageSupport
Lua language support, with completion for keywords, snippets, the standard library and the names declared in the file.
luaLanguage: LRLanguage
A language provider based on the Lezer Lua parser in this package, extended with highlighting, folding and indentation information. Blocks fold from the line where they open.
luaCompletion: CompletionSource
Autocompletion for Lua's keywords, snippets, globals and standard library tables. It offers nothing inside strings and comments.
localCompletionSource: CompletionSource
Completes the names declared in the file. A local is offered from the end of
its declaration to the end of its block, a parameter inside its function and a
loop variable inside its loop. Globals and global functions are offered
everywhere. After name. or name: it offers the fields declared on that
table, or the standard library's members, and after self. inside a method it
offers the fields of the method's table.
snippets: readonly Completion[]
Snippets for functions, conditionals, loops and a metatable class.
parser: LRParser
The raw Lezer parser, for tooling that wants the tree without an editor.
Contributing
Please read about how to contribute to Exercism. Be sure to read the Exercism Code of Conduct.
