source:
code/branches/resource/src/tolua/lua/clean.lua
@
  3953
        
        | Last change on this file since 3953 was 2710, checked in by rgrieder, 17 years ago | |
|---|---|
            
  | 
        |
| File size: 1.6 KB | |
| Line | |
|---|---|
| 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 | 
| 12 | {ESC1, "\\'"}, | 
| 13 | {ESC2, '\\"'}, | 
| 14 | {STR1, "'"}, | 
| 15 | {STR2, '"'}, | 
| 16 | {STR3, "%[%["}, | 
| 17 | {STR4, "%]%]"}, | 
| 18 | {REM , "%-%-"}, | 
| 19 | } | 
| 20 | |
| 21 | function mask (s) | 
| 22 | for i = 1,getn(MASK) do | 
| 23 | s = gsub(s,MASK[i][2],MASK[i][1]) | 
| 24 | end | 
| 25 | return s | 
| 26 | end | 
| 27 | |
| 28 | function unmask (s) | 
| 29 | for i = 1,getn(MASK) do | 
| 30 | s = gsub(s,MASK[i][1],MASK[i][2]) | 
| 31 | end | 
| 32 | return s | 
| 33 | end | 
| 34 | |
| 35 | function clean (s) | 
| 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 | 
| 41 | |
| 42 | if flags['C'] then | 
| 43 | return s | 
| 44 | end | 
| 45 | |
| 46 | local S = "" -- saved string | 
| 47 | |
| 48 | s = mask(s) | 
| 49 | |
| 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 | 
| 78 | end | 
| 79 | 
Note: See TracBrowser
        for help on using the repository browser.
    


            






