Search Results for

    Show / Hide Table of Contents

    Getting started with Loretta

    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

    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.

    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.RootScope to get the global scope;
    • Call Script.GetScope(SyntaxNode) or Script.FindScope(SyntaxNode, ScopeKind) to get an IScope;
    • Call Script.GetVariable(SyntaxNode) to get an IVariable;
    • Call Script.GetLabel(SyntaxNode) on a GotoStatementSyntax or a GotoLabelStatementSyntax to get an IGotoLabel;

    Using Variables

    There are 4 kinds of variables:

    • VariableKind.Local a variable declared in one of the following nodes:
      • LocalVariableDeclarationStatementSyntax;
      • LocalFunctionDeclarationStatementSyntax.
    • VariableKind.Global a variable used without a previous declaration;
    • VariableKind.Parameter a function parameter.
    • VariableKind.Iteration a variable that is an iteration variable from a NumericForStatementSyntax or GenericForStatementSyntax;

    The interface for variables is IVariable which exposes the following information:

    • IVariable.Kind- The VariableKind;
    • IVariable.ContainingScope - The containing scope;
    • IVariable.Name - The variable name (might be ... for varargs);
    • IVariable.Declaration - The place where the variable was declared (null for the implcit arg and ... 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, the Script.RootScope. It implements IScope and only contains globals;
    • ScopeKind.File - These implement IFileScope and are the scope for CompilationUnitSyntaxes which are a file's root node;
    • ScopeKind.Function - These implement IFunctionScope and are generated for these nodes:
      • AnonymousFunctionExpressionSyntax;
      • LocalFunctionDeclarationStatementSyntax;
      • FunctionDeclarationStatementSyntax.
    • ScopeKind.Block - These implement only IScope and are generated for normal blocks from these nodes:
      • NumericForStatementSyntax;
      • GenericForStatementSyntax;
      • WhileStatementSyntax;
      • RepeatUntilStatementSyntax;
      • IfStatementSyntax;
      • ElseIfClauseSyntax;
      • ElseClauseSyntax;
      • DoStatementSyntax.
    • Improve this Doc
    ☀
    ☾
    In This Article
    Back to top

    Copyright (c) LorettaDevs

    Generated by DocFX

    ☀
    ☾