Last change
on this file since 2248 was
2229,
checked in by rgrieder, 16 years ago
|
Cleaned up indentation chaos in tolua. There were tabs and 1 space indentations, even mixed in a single block.
It should now be possible to actually pimp these files.
|
File size:
1.6 KB
|
Rev | Line | |
---|
[1650] | 1 | -- mark up comments and strings |
---|
| 2 | STR1 = "\001" |
---|
| 3 | STR2 = "\002" |
---|
| 4 | STR3 = "\003" |
---|
| 5 | STR4 = "\004" |
---|
| 6 | REM = "\005" |
---|
| 7 | ANY = "([\001-\005])" |
---|
| 8 | ESC1 = "\006" |
---|
| 9 | ESC2 = "\007" |
---|
| 10 | |
---|
| 11 | MASK = { -- the substitution order is important |
---|
[2229] | 12 | {ESC1, "\\'"}, |
---|
| 13 | {ESC2, '\\"'}, |
---|
| 14 | {STR1, "'"}, |
---|
| 15 | {STR2, '"'}, |
---|
| 16 | {STR3, "%[%["}, |
---|
| 17 | {STR4, "%]%]"}, |
---|
| 18 | {REM , "%-%-"}, |
---|
[1650] | 19 | } |
---|
| 20 | |
---|
| 21 | function mask (s) |
---|
[2229] | 22 | for i = 1,getn(MASK) do |
---|
| 23 | s = gsub(s,MASK[i][2],MASK[i][1]) |
---|
| 24 | end |
---|
| 25 | return s |
---|
[1650] | 26 | end |
---|
| 27 | |
---|
| 28 | function unmask (s) |
---|
[2229] | 29 | for i = 1,getn(MASK) do |
---|
| 30 | s = gsub(s,MASK[i][1],MASK[i][2]) |
---|
| 31 | end |
---|
| 32 | return s |
---|
[1650] | 33 | end |
---|
| 34 | |
---|
| 35 | function clean (s) |
---|
[2229] | 36 | -- check for compilation error |
---|
| 37 | local code = "return function ()\n" .. s .. "\n end" |
---|
| 38 | if not dostring(code) then |
---|
| 39 | return nil |
---|
| 40 | end |
---|
[1650] | 41 | |
---|
[2229] | 42 | if flags['C'] then |
---|
| 43 | return s |
---|
| 44 | end |
---|
[1650] | 45 | |
---|
[2229] | 46 | local S = "" -- saved string |
---|
[1650] | 47 | |
---|
[2229] | 48 | s = mask(s) |
---|
[1650] | 49 | |
---|
[2229] | 50 | -- remove blanks and comments |
---|
| 51 | while 1 do |
---|
| 52 | local b,e,d = strfind(s,ANY) |
---|
| 53 | if b then |
---|
| 54 | S = S..strsub(s,1,b-1) |
---|
| 55 | s = strsub(s,b+1) |
---|
| 56 | if d==STR1 or d==STR2 then |
---|
| 57 | e = strfind(s,d) |
---|
| 58 | S = S ..d..strsub(s,1,e) |
---|
| 59 | s = strsub(s,e+1) |
---|
| 60 | elseif d==STR3 then |
---|
| 61 | e = strfind(s,STR4) |
---|
| 62 | S = S..d..strsub(s,1,e) |
---|
| 63 | s = strsub(s,e+1) |
---|
| 64 | elseif d==REM then |
---|
| 65 | s = gsub(s,"[^\n]*(\n?)","%1",1) |
---|
| 66 | end |
---|
| 67 | else |
---|
| 68 | S = S..s |
---|
| 69 | break |
---|
| 70 | end |
---|
| 71 | end |
---|
| 72 | -- eliminate unecessary spaces |
---|
| 73 | S = gsub(S,"[ \t]+"," ") |
---|
| 74 | S = gsub(S,"[ \t]*\n[ \t]*","\n") |
---|
| 75 | S = gsub(S,"\n+","\n") |
---|
| 76 | S = unmask(S) |
---|
| 77 | return S |
---|
[1650] | 78 | end |
---|
| 79 | |
---|
Note: See
TracBrowser
for help on using the repository browser.