Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/tolua/lua/clean.lua @ 2664

Last change on this file since 2664 was 2087, checked in by landauf, 16 years ago

merged objecthierarchy branch back to trunk

  • Property svn:eol-style set to native
File size: 1.3 KB
Line 
1-- mark up comments and strings
2STR1 = "\001"
3STR2 = "\002"
4STR3 = "\003"
5STR4 = "\004"
6REM  = "\005"
7ANY  = "([\001-\005])"
8ESC1 = "\006"
9ESC2 = "\007"
10
11MASK = { -- the substitution order is important
12 {ESC1, "\\'"},
13 {ESC2, '\\"'},
14 {STR1, "'"},
15 {STR2, '"'},
16 {STR3, "%[%["},
17 {STR4, "%]%]"},
18 {REM , "%-%-"},
19}
20
21function mask (s)
22 for i = 1,getn(MASK)  do
23  s = gsub(s,MASK[i][2],MASK[i][1])
24 end
25 return s
26end
27
28function unmask (s)
29 for i = 1,getn(MASK)  do
30  s = gsub(s,MASK[i][1],MASK[i][2])
31 end
32 return s
33end
34
35function 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
78end
79
Note: See TracBrowser for help on using the repository browser.