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:
I have done it like so, but it is not very robust or properly working:
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:
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:
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 )

Regards,
LeDarkLua ( Laurynas ).
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 )

Regards,
LeDarkLua ( Laurynas ).