Getting started with Loretta
Installing Loretta v0.2
We have two NuGet packages:
| Package | Stable | Latest |
|---|---|---|
| Main | ||
| Experimental |
Using Loretta v0.2
Parsing text
- (Optional) Pick a
LuaSyntaxOptionspreset and then create aLuaParseOptionsfrom it. If no preset is picked,LuaSyntaxOptions.Allis used by default; - (Optional) Create a
SourceTextfrom your code (using one of theSourceText.Fromoverloads); - Call
LuaSyntaxTree.ParseTextwith yourSourceText/string, (optional)LuaParseOptions, (optional)pathand (optional)CancellationToken; - Do whatever you want with the returned
LuaSyntaxTree.
Formatting Code
The NormalizeWhitespace method replaces all whitespace and and end of line trivia by normalized (standard code style) ones.
Accessing scope information
If you'd like to get scoping and variable information, create a new Script from your SyntaxTrees and then do one of the following:
- Access
Script.RootScopeto get the global scope; - Call
Script.GetScope(SyntaxNode)orScript.FindScope(SyntaxNode, ScopeKind)to get anIScope; - Call
Script.GetVariable(SyntaxNode)to get anIVariable; - Call
Script.GetLabel(SyntaxNode)on aGotoStatementSyntaxor aGotoLabelStatementSyntaxto get anIGotoLabel;
Using Variables
There are 4 kinds of variables:
VariableKind.Locala variable declared in one of the following nodes:VariableKind.Globala variable used without a previous declaration;VariableKind.Parametera function parameter.VariableKind.Iterationa variable that is an iteration variable from aNumericForStatementSyntaxorGenericForStatementSyntax;
The interface for variables is IVariable which exposes the following information:
IVariable.Kind- TheVariableKind;IVariable.ContainingScope- The containing scope;IVariable.Name- The variable name (might be...for varargs);IVariable.Declaration- The place where the variable was declared (nullfor the implcitargand...variables available in all files and global variables);IVariable.ReferencingScopes- The scopes that have statements that directly reference this variable;IVariable.CapturingScopes- Scopes that capture this variable as an upvalue;IVariable.ReadLocations- Nodes that read from this variable;IVariable.WriteLocations- Nodes that write to this variable;
Using Scopes
There are 4 kinds of scopes:
ScopeKind.Global- There is only one of these, theScript.RootScope. It implementsIScopeand only contains globals;ScopeKind.File- These implementIFileScopeand are the scope forCompilationUnitSyntaxes which are a file's root node;ScopeKind.Function- These implementIFunctionScopeand are generated for these nodes:ScopeKind.Block- These implement onlyIScopeand are generated for normal blocks from these nodes: