• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

"Fast" way of Parsing in Lua (1 Viewer)

LeDarkLua

Newcomer
Joined
Feb 1, 2021
Messages
1
Reaction score
0
Hello guys,

I'm new to this forum, but I have a question in mind with parsing, and I will state this that I know there are other possibilities to parse a snipped of code for my desired effect and I'm doing that also, and have succeeded, but as I'm moving to another platform "Love2D" I'd like to see if there is a faster approach of doing this...

So the question in mind is, how do I parse this block of text on the fly, for example:
Lua:
class Person
    function Person( arg1, arg2, arg3, ... )
        --do stuff here
        self.name = arg1
    end
    
    function printName()
        print( self.name )
    end
end

local person = new Person( "Random Name like Harry" )

I have done it like so, but it is not very robust or properly working:
Lua:
function mysplit (inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={}
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        table.insert(t, str)
    end
    return t
end

local program = [[class Person
    function Person()
        if ok then end
    end
end

local x = new Person()]]

local function pass( p ) return p end

program = program:gsub( "(class %w+%s-(%s-.+%s-)%s-end)", function( p )
    local className = mysplit( p:gsub( "class %w+", pass ), " \t" )[ 2 ]
    className = className:sub( 1, #className-1 )
    local arguments = ""
    print( p:gsub( "function "..className.."%s-%((%s-.+%s-%))%s-(%s-.+%s-)%s-end", pass ) )
    p = p:gsub( "class %w+", className.."=class(function(self"..arguments..")" )
    return p
end )

program = program:gsub( "(%w+%s-=%s-new %w+%s-%()", function( p )
    p = p:gsub( "new ", "" )
    return p
end )

--print( program )

As you can see I'm clearly stuck. The function with a name Person is a constructor, and I'd like to parse it so that:
Code:
Person = class( function( self, arg1, arg2, arg3, arg4, ... )
    --do stuff here
    self.name = arg1
end )

On the other hand, I'd like other functions in the block to be parsed and stored so that I could put them outside of the block:

Lua:
Person = class( function() ... end )

function Person:printName()
    print( self.name )
end

Why am I asking this question? I'm not experienced with REGEX that is why I'm here.
Why you ask? Because I don't want garbage looking code, I wan't to make myself some readable, working code around classes. For quick prototyping, like a game for example, or a virtual machine, or a proper parser for this exact syntax system.

I know it is possible with REGEX to do this. I just wan't a guide on how to make this. I saw that there are Lua-built parsers for JSON, but... of course this is not JSON and it is harder. I'm aware that this has nested functions with an "end" keyword that would give trouble.

Anyways... anyone has an Idea of how to approach this? Without a complex parser system, that redefines the Lua programming language just to implement a basic class system... ( yes, I tried it, but I gave up because redefining Lua is a bit tricky with my approach at least )
1612193821816.png

Regards,
LeDarkLua ( Laurynas ).
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
68
Reaction score
27
Location
Victoria BC
Website
winlua.net
Hi @LeDarkLua, welcome to Lua Form.

I agree with you that regex can turn into unreadable gibberish. There is an alternative in Lua called LPeg. I know next to nothing about LPeg other than it is an API to build parsing languages. That is, LPeg expressions can be created and combined and used like a language to describe the thing that you are parsing.


There is lots of good stuff on LPeg on the internet (lots of stack overflows!). The Lua mailing list is a good place for help with lpeg as well. Sorry I can't help you with it. There are some experts lurking on luaform if you can catch their attention? If you do agree that LPeg is going to suite your needs, you could re-post a message with LPeg in the title to get the attention of others on the forum; maybe something like LPeg for parsing . (I'm not sure if you have permission to change your title).

It's a little hard to follow what you are trying to achieve through your code. Perhaps if you could describe your intent without code we could suggest something? There are many, many, many object oriented libraries out there for Lua if that is what your attempting to create. Even if you do decide to write your own they could be useful? (I think someone posted about on here on LuaForm if you search?)
 
Top