regular expressions - compile time check

classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

regular expressions - compile time check

avoro
Hello!

My question may sound strange but it is very important for me.

Configuration that I use for my program essentially is very simple

if(var1 == "string1") then
   if(match("some_pattern", var2)) then return 1 end
   if(match("some_pattern1", var2)) then return 2 end
   if(match("some_pattern1", var2)) then return 3 end
end

...

BUT. I have to check ALL patterns before the execution starts. In other words
I want to reject configuration file if there is some error in one of the
RE patterns. Ideally there would be some hook that gets called when "match" 
and all parameters for it are identified, giving chance to check which
parameters would be passed at run-time.

I was thinking about additional passing  thru regular expression to find all
'match' and then compiling all regular expression found, but decided to ask
first if there is some "cleaner" solution already :)

Andrei

Reply | Threaded
Open this post in threaded view
|

Re: regular expressions - compile time check

David Jones-2
In message <20030630095616.GB14133@...>, [hidden email] writes:
> Hello!
> 
> My question may sound strange but it is very important for me.
> 
> Configuration that I use for my program essentially is very simple
> 
> if(var1 == "string1") then
>    if(match("some_pattern", var2)) then return 1 end
>    if(match("some_pattern1", var2)) then return 2 end
>    if(match("some_pattern1", var2)) then return 3 end
> end
> 
> ...
> 
> BUT. I have to check ALL patterns before the execution starts. In other words
> I want to reject configuration file if there is some error in one of the
> RE patterns. Ideally there would be some hook that gets called when "match" 
> and all parameters for it are identified, giving chance to check which
> parameters would be passed at run-time.
> 
> I was thinking about additional passing  thru regular expression to find all
> 'match' and then compiling all regular expression found, but decided to ask
> first if there is some "cleaner" solution already :)

Put all your patterns in a table.  This will simplify the lookup
procedure and allow you to check the patterns.  It won't be "compile
time" but will be "ahead of time":

t = {
  "some_pattern",
  "some_pattern2",
  "some_pattern3"
}

function check(t)
  for pattern,v in t do
    -- perform check on pattern
  end
end

function config(var1, var2)
  if var1 == "string1" then
    for i,pattern in ipairs(t) do
      if match(pattern, var2) then
	return i
      end
    end
  end
end

Now call the function check just after loading the script (or include a
call to it at the end of the script).

HTH

Cheers,
 drj

Reply | Threaded
Open this post in threaded view
|

writting GUI is possible with LUA?

Carlos Lorenz
Hello,

I am new to Lua and yet had no time to look
in its documentation so forgive me for my
question but I want to know if it is possible also
to write GUI with LUA

Thanks

Carlos 	

Reply | Threaded
Open this post in threaded view
|

Re: regular expressions - compile time check

avoro
In reply to this post by David Jones-2
On Mon, Jun 30, 2003 at 12:47:42PM +0000, David Jones wrote:
....
> Put all your patterns in a table.  This will simplify the lookup
> procedure and allow you to check the patterns.  It won't be "compile
> time" but will be "ahead of time":
> 
> t = {
>   "some_pattern",
>   "some_pattern2",
>   "some_pattern3"
> }
> 
....

I thought about this as well. But this is not very practical solution. Imagine
100 patterns that have to be taken out of context and put into table. And then
in the code maintainer would have to point to correct slot in the table. That
is why I decided to go with extra run thru regular expressions, kind of
"before compilation check". It just looks kind of dirty: parsing before
parsing.

Andrei

Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

benjamin sunshine-hill
In reply to this post by Carlos Lorenz
Yes. Check out the wiki at http://lua-users.org for a few lua distributions that
interface with GUI toolkits.

Ben

> Hello,
> 
> I am new to Lua and yet had no time to look
> in its documentation so forgive me for my
> question but I want to know if it is possible also
> to write GUI with LUA



Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

Mark Stroetzel Glasberg
In reply to this post by Carlos Lorenz
Carlos Lorenz wrote:
> I am new to Lua and yet had no time to look
> in its documentation so forgive me for my
> question but I want to know if it is possible also
> to write GUI with LUA

Yes it is. Take a look at:

http://www.tecgraf.puc-rio.br/iup

There is a binding of IUP to Lua.

Regards,
Mark

-- 
|\/|ark Stroetzel Glasberg
[hidden email]

System Analyst
Tecgraf/PUC-Rio
URL:  http://www.tecgraf.puc-rio.br/~mark
Tel:  55 21 2512-5984 ext. 116
Fax:  55 21 2259-2232

Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

Carlos Lorenz
In reply to this post by benjamin sunshine-hill
Thanks Ben

Em Seg 30 Jun 2003 11:44, benjamin sunshine-hill escreveu:
> Yes. Check out the wiki at http://lua-users.org for a few lua distributions
> that interface with GUI toolkits.
>
> Ben
>
> > Hello,
> >
> > I am new to Lua and yet had no time to look
> > in its documentation so forgive me for my
> > question but I want to know if it is possible also
> > to write GUI with LUA

Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

Rafael Jannone
In reply to this post by Mark Stroetzel Glasberg
On Mon, 30 Jun 2003 11:46:57 -0300
Mark Stroetzel Glasberg <[hidden email]> wrote:

> Carlos Lorenz wrote:
> > I am new to Lua and yet had no time to look
> > in its documentation so forgive me for my
> > question but I want to know if it is possible also
> > to write GUI with LUA
> 
> Yes it is. Take a look at:
> 
> http://www.tecgraf.puc-rio.br/iup
> 
> There is a binding of IUP to Lua.

did anyone come up with a native lua toolkit instead of a binding?
i mean, lua can handle garbage collection, signaling and other
"gui patterns" quite easily.
would it be too slow?

-- 
Rafael Jannone           Just remember: wherever you go,    __O
ICQ # 10115284                   there you are            _`\<,_
Computacao @ UFRGS      .................................(*)/ (*)
Porto Alegre/RS Brasil   Debian GNU/Linux www.debian.org

Reply | Threaded
Open this post in threaded view
|

is "__metatable" an event similar to "__gc" and "__tostring" or is it a convention?

Vijay Aswadhati-2
I have been studying some of the examples such as
"UserDataWithPointerExample"
on the Wiki as well as some real code like the gdbm binding by LHF. To
support
object:method() syntax, LHF adds a "metatable" key to the "gdbm" table in
"C"
and then in "gdbm.lua" he has a statement like this:
  gdbm.metatable.__index=gdbm
which is what is allowing the object:method() syntax to work; I am guessing
since this whole metatable business is still very unclear to me.

Whereas in the case of "UserDataWithPointerExample", the "IMAGE" table's
metatable gets an "__index" as well as "__metatable" keys both of them set
to the value of the "IMAGE" methods in "C".

The question finally. Could someone clarify in more elaborate terms all
this metatable magic and which of the two approaches are better. In terms
of understanding, I can atleast follow blindly "UserDataWithPointerExample"
and in fact got my own bindings working.

I tried LHF's approach and found out obviously it works as well, however
I am unable to figure out what exactly is the magic behind the statement
"gdbm.metatable.__index=gdbm"

Vijay Aswadhati



Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

Asko Kauppi-3
In reply to this post by Rafael Jannone

What do you mean by 'native'? There's got to be some non-Lua graphics stuff underneath such formation. You could use SDL, it's certainly fast enough, and many Lua-only games exist on it.

-ak

Rafael Jannone kirjoittaa sunnuntaina, 6. heinäkuuta 2003, kello 05:22:

On Mon, 30 Jun 2003 11:46:57 -0300
Mark Stroetzel Glasberg <[hidden email]> wrote:

Carlos Lorenz wrote:
I am new to Lua and yet had no time to look
in its documentation so forgive me for my
question but I want to know if it is possible also
to write GUI with LUA

Yes it is. Take a look at:

http://www.tecgraf.puc-rio.br/iup

There is a binding of IUP to Lua.

did anyone come up with a native lua toolkit instead of a binding?
i mean, lua can handle garbage collection, signaling and other
"gui patterns" quite easily.
would it be too slow?

--
Rafael Jannone           Just remember: wherever you go,    __O
ICQ # 10115284                   there you are            _`\<,_
Computacao @ UFRGS      .................................(*)/ (*)
Porto Alegre/RS Brasil   Debian GNU/Linux www.debian.org



Reply | Threaded
Open this post in threaded view
|

Re: is "__metatable" an event similar to "__gc" and "__tostring" or is it a convention?

Adam D. Moss
In reply to this post by Vijay Aswadhati-2
The subject: is a good question.  I don't believe it's an event
handler similar to __gc etc.  After revisiting the subject in the
manual it's not clear to me what __metatable is for either, except
that it appears to be some kind of user-settable shadow-metatable.

The manual suggests that getmetatable() will return this shadow
metatable in preference to the real one, and that it's an error
to setmetatable() on a table whose existing metatable contains
a __metatable field (whew!) but the actual purpose and semantics
(with regards to metamethods) of __metatable aren't defined at all.

(I think it's safe to not worry about it, until someone wants
to make the purpose of __metatable clear -- I'm doing lots
of fun and wacky things with metatables and I've not needed
to care about __metatable so far.)

--Adam
--
Adam D. Moss   . ,,^^   [hidden email]   http://www.foxbox.org/   co:3
"When I was young, I would break down and cry whenever mom made me
throw an empty toilet paper tube in the trash can. I just imagined
it sitting at the dump, all cardboard and sad." -- T.Farnon


Reply | Threaded
Open this post in threaded view
|

Re: is "__metatable" an event similar to "__gc" and "__tostring" or is it a convention?

Peter Shook-5
In reply to this post by Vijay Aswadhati-2

Vijay Aswadhati wrote:

I have been studying some of the examples such as
"UserDataWithPointerExample"
on the Wiki as well as some real code like the gdbm binding by LHF.

I tried LHF's approach and found out obviously it works as well, however
I am unable to figure out what exactly is the magic behind the statement
"gdbm.metatable.__index=gdbm"

Regarding __metatable, Adam has explained it very well. My example will work without it. I only added it so that one cannot alter the userdata's real metatable from a Lua script. The Lua function getmetatable will return the table of methods instead of the real metatable.

All Lua function are written in the Lua C API, and they are fairly easy to understand. If you look at lua-5.0/src/lib/lbaselib.c you'll see:

static int luaB_getmetatable (lua_State *L) {
  luaL_checkany(L, 1);
  if (!lua_getmetatable(L, 1)) {
    lua_pushnil(L);
    return 1;  /* no metatable */
  }
  luaL_getmetafield(L, 1, "__metatable");
return 1; /* returns either __metatable field (if present) or metatable */
}

static int luaB_setmetatable (lua_State *L) {
  int t = lua_type(L, 2);
  luaL_checktype(L, 1, LUA_TTABLE);
  luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
                    "nil or table expected");
  if (luaL_getmetafield(L, 1, "__metatable"))
    luaL_error(L, "cannot change a protected metatable");
  lua_settop(L, 2);
  lua_setmetatable(L, 1);
  return 1;
}


I don't have a current copy of lgdm.tar.gz, and the site is down, so I cannot explain LHF's code right now. But it appears that he puts the real metatable in the table of methods, and the index event uses the table of methods. Something like:

gdbm = {}  -- table of methods
gdbm.metatable = { __index = gdbm }  -- real metatable

It's hard to explain without diagrams.


The metatable magic is the same for Lua tables as it is for userdata, so it's best to experiment with Lua tables first.

Play around with this example first.

$ cat x.lua

local methods = {
  show = function(t, ...) return t, t.x, t.y, unpack(arg) end,
  dot  = function(t1, t2) return t1.x * t2.x + t1.y * t2.y end,
  common = 'this is common',
}

local mt = {
  __index = methods,
  __tostring = function(t) return 'this object is '.. t.name end,
}

obj1 = { name = 'bob', x=0, y=2 }
setmetatable(obj1, mt)

obj2 = { name = 'bill', x=4, y=3 }
setmetatable(obj2, mt)

$ lua -lx -i
> = obj1, obj1.common
this object is bob      this is common
>
> = obj2, obj2.common
this object is bill     this is common
>
> = obj1:show('a','b')
this object is bob      0       2       a       b
>
> = obj2:show'cow'
this object is bill     4       3       cow
>
> = obj1:dot(obj2)
6
>

Then read:
http://lua-users.org/lists/lua-l/2003-05/msg00287.html

I hope this helps.  Let us know if you have more questions.

There isn't a lot of magic to it. Once you have a mental image of how it works, it's pretty straight forward. I started to make some diagrams but then I got lazy. I'll try to finish them.

- Peter


Reply | Threaded
Open this post in threaded view
|

Re: is "__metatable" an event similar to "__gc" and "__tostring" or is it a convention?

Roberto Ierusalimschy
In reply to this post by Adam D. Moss
__metatable is for protecting metatables. If you do not want a program
to change the contents of a metatable, you set its __metatable field.
With that, the program cannot access the metatable (and therefore cannot
change it).

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

Rafael Jannone
In reply to this post by Asko Kauppi-3
On Sun, 6 Jul 2003 10:10:50 +0300
Asko Kauppi <[hidden email]> wrote:

> What do you mean by 'native'?  There's got to be some non-Lua graphics > stuff underneath such formation. You could use SDL, it's certainly
> fast enough, and many Lua-only games exist on it.

I agree, let me rephrase my thought:

What I am really talking about is having some kind of standard set
of lua data structures for GUI development, but more in a sense
of "let's program a GTK-like toolkit using lua instead of C".
Then you could have a number of appropriate graphical backends.

Why is that?  A GUI toolkit like GTK does many things (in a somewhat
complex way) that a Lua VM can handle much naturally.  Hence, it feels
like overkill to have Lua + bindings to a toolkit that replicates
part of this same functionality (with signalling, reference counting,
blablabla). 

Note that I am talking about standard code, which would be activelly
developed and widely deployed (in the same way STL that is standard to
C++).

It could be an interesting project... lua stuff is interesting by
default :o)

-- 
Rafael Jannone           Just remember: wherever you go,    __O
ICQ # 10115284                   there you are            _`\<,_
Computacao @ UFRGS      .................................(*)/ (*)
Porto Alegre/RS Brasil   Debian GNU/Linux www.debian.org

Reply | Threaded
Open this post in threaded view
|

RE: is "__metatable" an event similar to "__gc" and "__tostring" oris it a convention?

Vijay Aswadhati-2
In reply to this post by Roberto Ierusalimschy
Thank you all for the answers. It took me about 6 months
of lurking around and Lua 5.0, to finally sit down and write
a Lua binding for austin (http://users.footprints.net/~kaz/austin.html)
in three hours. The results are well worth the effort. Now I can move
on to the real stuff. 

Some diagrams of the Lua VM and how it manages the metatables, registry
and the stack in general would help the curious to get hooked to Lua
faster.

-- Vijay Aswadhati

PS: I wrote the binding for austin just to get a feel of writing
    bindings in "C" for Lua. I do not have any other use for it
    other than to serve as an example. Let me know if it would be
    useful to post the code on http://lua-users.org/wiki/

-----Original Message-----
From: [hidden email]
[[hidden email] Behalf Of Roberto
Ierusalimschy
Sent: Monday, July 07, 2003 6:14 AM
To: Lua list
Subject: Re: is "__metatable" an event similar to "__gc" and
"__tostring" oris it a convention? 


__metatable is for protecting metatables. If you do not want a program
to change the contents of a metatable, you set its __metatable field.
With that, the program cannot access the metatable (and therefore cannot
change it).

-- Roberto


Reply | Threaded
Open this post in threaded view
|

Re: is "__metatable" an event similar to "__gc" and "__tostring" oris it a convention?

Peter Shook-5

Vijay Aswadhati wrote:
Thank you all for the answers.

You're welcome.

PS: I wrote the binding for austin just to get a feel of writing
    bindings in "C" for Lua. I do not have any other use for it
    other than to serve as an example. Let me know if it would be
    useful to post the code on http://lua-users.org/wiki/

Sure, when you have some time, add it to the wiki. All my examples were contrived to answer a particular question or request, so it would be good to have a more life size example documented on the wiki.

You could even add it to:  http://lua-users.org/wiki/LibrariesAndBindings

Cheers,

- Peter


Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

David Cuny
In reply to this post by Rafael Jannone
Rafael Jannone wrote:

> What I am really talking about is having some kind of standard set
> of lua data structures for GUI development, but more in a sense
> of "let's program a GTK-like toolkit using lua instead of C".
> Then you could have a number of appropriate graphical backends.

Well, you have two options: write a toolkit that uses native widgets (like 
wxWindows or Zinc), or emulate everything yourself (just about everyone 
else).

Using native widgets is more complex, but ultimately provides a better 
solution. People expect their platform to look and feel like their platform, 
be it Windows, Linux or a Mac.

One problem you run into is that certain controls are not availble 
cross-platform, so you end up having to leave them out, or having to emulate 
them somehow.

Emulated widgets is a much cleaner approach. Typically, you can use something 
like OpenGL since it's widely available. You can choose to emulate a 
platform's look and feel (like Swing does) or just choose your own look and 
feel (Swing Metal, for example).

The nice thing about emulated controls is that you generally only have one 
codebase to maintain.

That said, I think that writing a cross-platform toolkit in Lua just for the 
sake of it being in Lua isn't a very compelling project.

I say this having spent a good chunk of time writing a Win32 toolkit, and then 
sunk another chunk of time into trying to get a cross-platform toolkit 
working. Cross-platform libraries are complex, and they are never done. If 
you are wrapping native controls, you are always trying to keep up with the 
latest developments. Multiply this by several platforms, and you've got a 
huge task, even if you've got a lot of volunteers.

Tlhere are a number of excellent cross-platform toolkits available. I 
personally think it would be more useful to concentrate on using one of them. 
I'm a huge fan of wxWindows, and I think wxLua does a great job wrapping it. 

If you don't like that, write your own set of wrappers that hide the klunky C 
interface and give it a clean Lua interface. But I'd advise against starting 
a project like this from scratch.

Just my two cents.

-- David Cuny

Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

Basile STARYNKEVITCH
>>>>> "David" == David  <[hidden email]> writes:

    David> Rafael Jannone wrote:

    Rafael>> What I am really talking about is having some kind of
    Rafael>> standard set of lua data structures for GUI development,
    Rafael>> but more in a sense of "let's program a GTK-like toolkit
    Rafael>> using lua instead of C".  Then you could have a number of
    Rafael>> appropriate graphical backends.

    David> Well, you have two options: write a toolkit that uses
    David> native widgets (like wxWindows or Zinc), or emulate
    David> everything yourself (just about everyone else).

    David> Using native widgets is more complex, but ultimately
    David> provides a better solution. People expect their platform to
    David> look and feel like their platform, be it Windows, Linux or
    David> a Mac. [...]

An alternative would be to make a widget server containing a Lua
interpreter. The idea (reminiscent of the dead NeWS system made by Sun
around 1990) would be to have a GUI "widget server" program listening
for script on a socket or pipe, which communicate with an application
program. The application program send lua commands to the GUI server,
and the callbacks are Lua script functions, some of which may send
back a message to the application program.

I actually made such a GUI server (called guis) for Lua4 and GTK2. It
is opensource free software, available from
http://freshmeat.net/projects/guis/ - I probably don't have much time
to work on it, but your comments are welcome!

The difficulty is to interface all (or at least most) of GTK2 to
Lua. This is a big boring task, because GTK API is rather big. I tried
to use the semi formal lispy description of GTK2 API available fot
GtkPython, and I coded a quick&dirty CommonLisp (Clisp) program to
generate the glue code between GTK2 and Lua4.

A practical problem with such stuff is the widget & memory management
issue. Most toolkits today seems to incorporate some (crude) kind of
"garbage collection" - a reference counting mechanism provided by Glib
in GTK2- and the link between it and Lua GC might be not very robust.

Perhaps Trolltech new QSA (see
http://www.trolltech.com/products/qsa/index.html for details) could be
used to easily embed Javascript to make a Javascript driven QT3 widget
server. This should require some work (I actually tried a bit to make
a QT3 program listening for commands on a pipe, and did not tried
enough to succeed on this). But I did not investigate into QSA and may
have misunderstood it.

A major but interesting Lua project would be to make a LUA based GUI
server, which would mean to develop (or adapt) a widget library for
LUA conventions and GC.

More generally, I really am missing a good scripting capabilities in
today widgets' sets, and even a standard way of doing it. (Web
interfaces are not enough, even by using Mozilla's XPCOM etc..). I
still think that NeWS idea of programming a GUI server is a good one,
and I don't understand why the Java team (which was earlier the NeWS
development team) did not propose a standard JVM embedding into X11.


Regards.

-- 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France

Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

David Cuny
Basile Starynkevitch wrote:

> An alternative would be to make a widget server containing a Lua
> interpreter. The idea (reminiscent of the dead NeWS system made by Sun
> around 1990) would be to have a GUI "widget server" program listening
> for script on a socket or pipe, which communicate with an application
> program. The application program send lua commands to the GUI server,
> and the callbacks are Lua script functions, some of which may send
> back a message to the application program.

The big problem with NeWS (besides alienating the XWindows folk) was that you 
ended up having to code applications in a hacked version of PostScript that 
could respond to events. I don't see how James Gosling could be suprised to 
discover that Unix geeks didn't want to code in FORTH.

Erm... but back on topic. :-)

A while back, I got an email a while back from an Erlang coder who was 
interested in implementing exactly that. 

One nice thing about Erlang is that it's really easy to create client/server 
applications in a network transparent way. It's just as easy to communicate 
with a process that's running on your machine as it is to communicate with 
one that's running remotely across the network.

Because of this network transparency, you could (in theory) run applications 
remotely, where the widget server would be local on your machine, but the 
actual application could be anywhere - local on your machine, or hosted 
remotely across the network, transparent to the user.

So (again, in theory) you get the client/server goodness of XWindows, but with 
a native look and feel on the client machine. 

It would be interesting to see something like that implemented in Lua.

-- David Cuny

Reply | Threaded
Open this post in threaded view
|

Re: writting GUI is possible with LUA?

Eero Pajarre-3
David wrote:

So (again, in theory) you get the client/server goodness of XWindows, but with a native look and feel on the client machine.
It would be interesting to see something like that implemented in Lua.


I needed a "control GUI" for my simulation application. I ended
implementing it with wxLua. I run a Lua interpreter embedded in
our simulator, and the wxLua based GUI in a separate (notebook)
computer. These are at the moment connected using a RS232 serial
cable and communicate by sending Lua code pieces to each other.

What is sort of cool is that the GUI-program is a really dummy one,
by itself it only opens a basically empty window, with a status line.
(And even this happens mostly for debugging reasons). First
thing it then does is send a boot request message to the simulator.

The reply for the boot-message then contains all the Lua code which
is needed for creating the real GUI.

After the bootstrap phase the GUI process continues listening
incomming communications in the wxLua idle callback. When user
press buttons etc on the GUI it sends corresponding command strings
to the Lua embedded in the simulator.

This took me only couple of days to implement, and most of the time
went in implementing the serial port communication part. Using something
like a TCP/IP protocol would have been much easier.

		Eero