/Loretta

A C# Lua, GLua and Luau parser, code analysis, transformation and generation library.

Primary LanguageC#MIT LicenseMIT

Loretta

A C# (G)Lua lexer, parser, code analysis, transformation and code generation toolkit. For getting started, see the tutorial.

Discord Loretta.CodeAnalysis.Lua Loretta.CodeAnalysis.Lua GitHub Workflow Status

This is (another) rewrite from scratch based on Roslyn and The Complete Syntax of Lua with a few extensions:

  1. Operators introduced in Garry's Mod Lua (glua):
    • && for and;
    • || for or;
    • != for ~=;
    • ! for not;
  2. Comment types introduced in Garry's Mod Lua (glua):
    • C style single line comment: // ...;
    • C style multi line comment: /* */;
  3. Characters accepted as part of identifiers by LuaJIT (emojis, non-rendering characters, or basically any byte above 127/0x7F);
  4. Luau (Roblox Lua) syntax:
    • Compound assignment: +=, -=, *=, /=, ^=, %=, ..=;
    • If expressions: if a then b else c and if a then b elseif c then d else e;
    • Typed lua syntax.
  5. FiveM's hash string syntax;
  6. Continue support. The following options are available:
    • No continue at all;
    • Luau's continue which is a contextual keyword;
    • Garry's Mod's continue which is a full fledged keyword.

TL;DR: This supports Lua 5.1, Lua 5.2, Lua 5.3, Lua 5.4, LuaJIT 2.0, LuaJIT 2.1, FiveM, GLua and Luau (Roblox Lua).

Installing Loretta v0.2

We have two NuGet packages:

Package Stable Latest
Main Loretta.CodeAnalysis.Lua Loretta.CodeAnalysis.Lua
Experimental Loretta.CodeAnalysis.Lua.Experimental Loretta.CodeAnalysis.Lua.Experimental

Using Loretta v0.2

The best source for getting started is the tutorial.

Parsing text

  1. (Optional) Pick a LuaSyntaxOptions preset and then create a LuaParseOptions from it. If no preset is picked, LuaSyntaxOptions.All is used by default;
  2. (Optional) Create a SourceText from your code (using one of the SourceText.From overloads);
  3. Call LuaSyntaxTree.ParseText with your SourceText/string, (optional) LuaParseOptions, (optional) path and (optional) CancellationToken;
  4. Do whatever you want with the returned LuaSyntaxTree.