lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2012/1/7 Patrick Donnelly <batrick@batbytes.com>:
> On Fri, Jan 6, 2012 at 10:51 AM, Gavin Wraith <gavin@wra1th.plus.com> wrote:
>> Purely as an exercise I have been writing a small Lua 5.2
>> script using lpeg 0.10 to remove comments from scripts. This
>> has started me thinking about aspects of syntax, and I suspect
>> that on the list there may be those who can point me toward
>> useful material.
>
> Take a look at the Lua Parser on [1]. Using that, it should be trivial
> to modify to remove comments/unnecessary whitespace. [Someone looking
> to modify it should probably change most (all?) of the instances of
> calls to lpeg.P to some local function so it can be easily changed to,
> say, lpeg.C. That would allow for changing the subject string code to
> something else more easily.]
>
> Some other changes you can do to make the code as compact as possible:
>
> o Change local names to one or two characters.
> o Remove redundant parenthesis. e.g. if (foo) then <block> end  OR
> foo("bar") --> foo"bar"
> o Change numbers to a more compact format. e.g. 0x00000 --> 0
> o Change strings to a more compact format. e.g. "\000" --> "\0" (where
> permissible)
>
> I'm sure there are many more...
>
> [1] http://lua-users.org/wiki/LpegRecipes
>
> --
> - Patrick Donnelly
>

I have upload a module using lpeg and Lua Lexer[1] to strip comments
and spaces, but remaining newline for debug information:
https://gist.github.com/1572004

it contains three functions, lexer is a lua lexer, strip strips
comments and spaces in code, and tocstring converts string to C
multiline string used in C header/sources.

regards.

[1]: http://lua-users.org/wiki/LpegRecipes