GithubHelp home page GithubHelp logo

lorettadevs / loretta Goto Github PK

View Code? Open in Web Editor NEW
115.0 115.0 11.0 9.04 MB

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

Home Page: https://loretta-oss.dev/

License: MIT License

C# 99.78% Batchfile 0.01% PowerShell 0.15% Shell 0.06% Dockerfile 0.01%
ast-transformations c-sharp code-analysis code-generator garrysmodlua glua lexer lua lua-lexer luau parser roblox-lua tokenizer

loretta's People

Contributors

bmcq-0 avatar fiusen avatar ggg-killer avatar thegreatsageequaltoheaven avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

loretta's Issues

Add semantic validation

Add validation for language semantics.
Including but not limited to:

  • Duplicate goto label detection;
  • Missing goto label detection;
  • const variable assignment;
  • close variable assignment.

Enumerating children of a FunctionDefinitionStatement fails with vararg

Reported by: Slappy826#0001

Error:

FunctionDefinitionStatement.Children enumerator throws an exception when there is a vararg because a vararg is not an IdentifierExpression.

C# Code:

var tree = Parse(@"function a(...) end");
var func = tree.Body.Single();

var children = func.Children.ToArray();
// Throws exception

Add Lua 5.3 bitwise operators

Feature:
Add Lua 5.3 bitwise operators: & (BAND), | (BOR), ~ (BXOR and BNOT), << (left shift), >> (right shift).

Blocked by: #14

Slot is freed twice when a variable has a write with a read inside it

Reported by @Fiusen.

When an IVariable has a read with a write inside it (writes for assignments and local var decls span the entire node instead of only the name), the last use location wasn't being obtained properly leading to the outer node being marked as the last use instead of the read inside the write.

Example:

   local x = 0
   x = x + 1
-- ^^^^^^^^^ write
--     ^     read

Here, the write node was being considered the last use while the read one should've been as it's the one furthest in the code.

Numeric for loop scope is spilled out

Reported by: bmcq#0001

Error:

When parsing a NumericForLoopStatement, the for loop scope is spilled.

Lua Code:

local a = 1
for a = 2, 4, 2 do print(a) end
a = 2 -- Refers to the `a` in the `for` loop instead of `a` in `local a`

Add assume no metatable overrides option to the constant folder

Suggested by: bmcq#0001

Feature:
An AssumeNoMetatableOverrides option to enable more aggressive constant folding for cases like Roblox where debug.getmetatable and debug.setmetatable aren't available therefore the behavior of operators can't be modified for base types.

Constant folder does not handle the <boolean> <op> <boolean> case

Reported by: bmcq#0001
The constant folder can handle the <number> <op> <number>, <nil> <op> <any>, <bool-like> <op> <nil> cases but not <bool> <op> <bool> nor <bool-like> <op> <bool-like> cases.

Current behavior:

-- Transforms:
print(true == true)
-- Into:
print(true == true)

Expected behavior:

-- Transforms:
print(true == true)
-- Into:
print(true)

Add support for ULL and LL suffixes

LuaJIT (with FFI support enabled) has the ULL and LL suffixes for numbers.

ULL results in an unsigned 64-bit integer.
LL results in an signed 64-bit integer.

Requested by @bmcq-0.

LuaJIT numeric suffixes aren't being parsed in a case-insensitive manner

Describe the bug
LuaJIT has 3 number suffixes when FFI is enabled:

  • LL for 64-bit signed integers
  • ULL for 64-bit unsigned integers
  • and i for imaginary numbers

To Reproduce
Steps to reproduce the behavior:

  1. Type in 1I or 1ull or 1ll
  2. Error is generated

Expected behavior
No error is generated.

Additional context
Loretta Version: master

Loretta incorrectly generates IScope.ContainedScopes for certain statements.

Certain statements will not have their scopes in their parent scope's ContainedScopes list.

See the following Lua code:

(function(Variable) end)()

It's expected that the anonymous function's scope should be in the ContainedScopes list of the file's scope, but it is not.

var source = "(function(Variable) end)()";
var syntaxTree = LuaSyntaxTree.ParseText(source, LuaParseOptions.Default);
var script = new Script(ImmutableArray.Create(syntaxTree));
var fileScope = script.GetScope(tree.GetRoot());

Console.WriteLine(fileScope.ContainedScopes.Count()) // expected 1, got 0

Support Decompiling Lua.

I noticed this supports 5.3, but what about 5.1 or 5.2 as well? Cant those be made supported as well for old games that use old lua 5.1 or 5.2?

Also I have a version of unluac that supports 5.1 and 5.2 which I can help provide too you to help you out (would need it to be available to nuget.org so then I could drop my version of an lua decompiler for this sort of project just because it seems far better.

Likewise the compiler I would need to be able to tell it to compile using 5.1 or 5.2 as well and not just 5.3 (for games like Elsword of Grand Chase). For the game versions they used the normal lua 5.1 compiler but for some reason it supported ellipsis which my decompiler either screws it up or the compiler does. Also the files contain Korean words and so must be made to support compiling and decompiling files encoded in UTF-8 WITH BOM. The normal compiler I got forces me to resave without BOM that might be what screws up the korean letters / the '...' characters inside of them when I use the tool to regenerate the KIM file after the raw lua bytecode is generated.

I plan to eventually make an KimLib in c# for my Els_kom program that could then be responsible for regenerating the KIM file / updating it / reading it but it takes time to do.

I probably could theoretically update the lua engine but do not want to chance breaking the entire already fragile setup (even though I personally think it only works due to UB and how dumb their devs were to not care how to properly code to not make it barely operable at all).

A better website

The website lags my entire computer and im unable to actually load anything, the colors are rlly shit consider using gitbook thanks ๐Ÿ˜› ๐Ÿ˜› ๐Ÿ˜› ๐Ÿ˜›

ObjectDisplay outputs escapes instead of original character

Describe the bug
ObjectDisplay outputs unicode escapes instead of original character, especially large issue for Lua 5.1 because it does not have unicode escapes

To Reproduce
Steps to reproduce the behavior:
Call .ToFullString on a node

Expected behavior
E.g issue

print("Hello World")

Will become

print("Hello\u{0020}World")

Support for Luau type annotations

Luau has a concept of 'typed Lua' - i.e TypeScript style type annotations for Lua.

This is a custom syntax extension specific to Luau/Roblox.

See syntax here: https://roblox.github.io/luau/syntax

Some examples:

function foo(x: number, y: string): boolean
    local k: string = y:rep(x)
    return k == "a"
end
type Point = { x: number, y: number }
type Array<T> = { [number]: T }
type Something = typeof(string.gmatch("", "\d"))

Keyword continue is not parsed as a contextual keyword

Reported by: bmcq#0001
In Roblox (but not GLua), continue is parsed as a contextual keyword. Lorreta does not do that.

Code sample:

local continue = 100
for k, v in ipairs(tbl) do
      if v == continue then
        continue
    end
end

Current behavior:
Fatal exception: Expected an Identifier but got a Keyword instead on continue in local continue = 100.

Expected behavior:
continue is correctly parsed as a contextual keyword and no exceptions happen.

Rewrite SyntaxNormalizer

The current SyntaxNormalizer was partially based on VB's but the languages are too different for it to work properly and it requires a lot of extra configuration that could be handled in an easier manner.

Incorrect variable referecing

Reported by: Slappy826#0001

Error:

local variable declaration doesn't create a new variable.

Lua Code:

local a = b[a] -- the `a` in `b[a]` refers to the `a` in `local a`

Compound assignment support.

Not sure if Loretta supports Compound assignment support as I looked over the parser.
Reasons for support:

  • Roblox added support for compound assignments.

For contributors moving from the old repo

These are the set of commands contributors should run on your local repositories to migrate from old repo:

git remote set-url origin "https://github.com/LorettaDevs/Loretta.git"
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a

SyntaxFactory string literals incorrectly output nodes

Describe the bug
When creating a string literal node with the SyntaxFactory the inputted stirng

To Reproduce
Run

        Console.WriteLine(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@"Hi From
        Loretta
        ")).ToFullString());

Expected behavior
The node should output

Hi From
        Loretta
        

but outputs

"Hi\u{0020}From\r\n\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}Loretta\r\n\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}\u{0020}"

Unicode escapes support

Support for unicode escape codes.

Format: \u{XX} where XX is less than (and not equal to) 0x110000.

SyntaxFactory is missing constructors

Describe the bug
When typed-Luau was added all constructors relying on the syntax, which removed a lot of old constructors which was an oversight

To Reproduce
Attempting to use old constructors will throw errors
node.Update(node.FunctionKeyword, node.Name, node.Parameters, node.Body, node.EndKeyword, node.SemicolonToken)

Expected behavior
The code should work alongside its typed-constructors

Integer support

Support for integers.

Integer examples: 3, 345, 0xff, 0xBEBADA, 0b1010, 0o77
Float examples: 3.0, 3.1416, 314.16e-2, 0.31416E1, 34e1, 0x0.1E, 0xA23p-4, 0X1.921FB54442D18P+1

Make all fields in `.Update` optional

Is your feature request related to a problem? Please describe.
Having to provide every single node for .Update is very annoying and could be better as what we have for LuaSyntaxOptions.With

Describe the solution you'd like
The same solution as applied to LuaSyntaxOptions.With.

Describe alternatives you've considered
Providing every single parameter every time or using a combination of multiple .With* methods which would result in a lot of unnecessary allocations.

Additional context
None.

Script class initialization is way too slow

Loretta's Script class initialization of a big script takes way too long:

Time on a 20MB file:

  •   Script init: 9s
    
  •   Parse time: 0.9s
    

How to replicate:

  • Have a random script (like allopcodes-5.1.lua) and copy and paste it until ur file gets to ~20MB
var scriptParsed = new Script(ImmutableArray.Create(LuaSyntaxTree.ParseText(code)));
var root = Parsed[0].GetRoot();

SyntaxNode node = root.ChildNodes().FirstOrDefault(); // whatever

scriptParsed.GetScope(node) // long wait here

4KIgA2zjVv

[Question] Lua<->AST

Does Loretta support both ways of conversion from:
Lua->AST
AST->Lua
Thanks for reading.

FormattedLuaCodeSerializer serializes not expressions incorrectly

Reported by: bmcq#0001
FormattedLuaCodeSerializer serializes not expressions incorrectly by not adding a space after the keyword when serializing.

Current behavior:

-- Transforms:
print(not a)
-- Into:
print(nota)

Expected behavior:

-- Transforms:
print(not a)
-- Into:
print(not a)

SyntaxFactory has overloads where the body is not mandatory

Not adding a body to most nodes is a mistake and is not something we should support in the minimal factory methods.

We should also look into other things that we might want to make mandatory other than function bodies.

This will result in breaking changes, but that is the point of this.

Lua 5.1 warning about nested long strings/comments

Describe the bug
Lua 5.1 has a warning when you nest long comment or long string delimiters.
Further lua versions do not have this, nor do LuaJIT or Luau.

To Reproduce
Parse the following file with Loretta

local longstr = [[Show me this
yes!]]
--[[
local unused_string = [[Line1
Line2]]
local longstr = [[
Do not show me this
Multiline string]]
--]]

print(longstr)

Expected behavior
An error diagnostic is generated from [[Line1 to Line2]].

Additional context
Loretta Version: v0.2.9

Parser accepts any expression as a statement

Reported by: bmcq#0001
Parser accepts any expression as a statement

Code:

a + b

Current behavior:

StatementList
\_ ExpressionStatement
    \_ BinaryOperationExpression

Expected behavior:
Error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.