Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholuac.lua
More file actions
Latest commit
199 lines (168 loc) · 5.34 KB
/
Copy patholuac.lua
File metadata and controls
199 lines (168 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
-- Objective Lua preprocessor
-- © 2009 David Given
-- This program is licensed under the MIT public license.
--
-- WARNING!
-- This is a TOY!
-- Do not use this program for anything useful!
--
-- This program preprocesses an Objective Lua input file into a standard Lua
-- output file. Use it as follows:
--
-- lua oluac.lua <infile> <outfile>
--
-- You'll need the Leg parser on your Lua path. Error handling and
-- documentation is nonexistent; here be dragons.
locallpeg=require("lpeg")
localparser=require("leg.parser")
localV=lpeg.V
localP=lpeg.P
localS=lpeg.S
localC=lpeg.C
localCs=lpeg.Cs
-- Merge our new rules into the standard parser.
localALL
do
localoldStat=parser.rules.Stat
localoldBlock=parser.rules.Block
localoldExp=parser.rules.Exp
locals=parser.rules.IGNORED-- scanner.IGNORED or V'IGNORED' could be used
localOLUA=P(
parser.apply
-- Rules
{
UnarySelectorElement=V'ID',
MultipleSelectorElement=V'ID' *P':',
MultipleMethodArg=s*C(V'MultipleSelectorElement') *s*V'Exp' *s,
MultipleMethodContinuingArg=s*C(V'MultipleSelectorElement' +P',') *s*V'Exp' *s,
UnaryMethodArgList=C(V'UnarySelectorElement')
/function(selector)
returnselector, ""
end,
MultipleMethodArgList=V'MultipleMethodArg' *s*
(s*V'MultipleMethodContinuingArg')^0,
MethodArgList= (V'MultipleMethodArgList' +
V'UnaryMethodArgList')
/function(...)
localselector= {}
localargs= {}
fori=1, select('#', ...), 2do
localselectorelement=select(i, ...)
if (selectorelement~=',') then
selector[#selector+1] =selectorelement
end
localarg=select(i+1, ...)
if (arg~='') then
args[#args+1] =arg
end
end
returntable.concat(selector), args
end,
MultipleDeclarationArg=s*C(V'MultipleSelectorElement') *s*C(V'ID') *s,
MultipleDeclarationContinuingArg=s*C(V'MultipleSelectorElement' +P',') *s*C(V'ID') *s,
UnaryDeclarationArgList=C(V'UnarySelectorElement')
/function(selector)
returnselector, ""
end,
MultipleDeclarationArgList=V'MultipleDeclarationArg' *s*
(s*V'MultipleDeclarationContinuingArg')^0,
DeclarationArgList= (V'MultipleDeclarationArgList' +
V'UnaryDeclarationArgList')
/function(...)
localselector= {}
localargs= {}
fori=1, select('#', ...), 2do
localselectorelement=select(i, ...)
if (selectorelement~=',') then
selector[#selector+1] =selectorelement
end
localarg=select(i+1, ...)
if (arg~='') then
args[#args+1] =arg
end
end
if (#args>0) then
returntable.concat(selector), ','..table.concat(args, ',')
else
returntable.concat(selector), ''
end
end,
SuperMethodCall=P'[' *s*P'super' *s*V'MethodArgList'
*s*P']'
/function(selector, args)
localcomma=','
if#args==0then
comma=''
end
selector=string.gsub(selector, ':', '_')
return'__olua_superclass_methods.' ..selector..
'(self' ..comma..table.concat(args, ',') ..')'
end,
MethodCall=P'[' *s*V'Exp' *s*V'MethodArgList'
*s*P']'
/function(object, selector, args)
selector=string.gsub(selector, ':', '_')
returnobject..':' ..selector..'(' ..table.concat(args, ',') ..')'
end,
ClassImplementation=P'@implementation' *s*C(V'ID')
*s*V'Block' *s*P'@end'
/function(class, block)
return'do ' ..
'local __olua_current_class = '..class..
' local __olua_superclass_methods = '..class..':superclassMethods()'..
''..block..
' end'
end,
ClassDeclaration=P'@interface' *s*C(V'ID')
*s* (P':' *s*C(V'ID'))^-1*s*P'@end'
/function(class, superclass)
ifnotsuperclassthen
superclass='nil'
end
returnclass..'=olua.declareclass(' ..
'"' ..class..'",' ..
superclass..')' ..
'local ' ..class..'=' ..class
end,
MethodImplementation=C(S'-+') *s*V'DeclarationArgList'
*s*V'Block' *s*P'end'
/function(type, selector, args, block)
localresult= {}
if (type=='-') then
result[#result+1] ='olua.definemethod'
else
result[#result+1] ='olua.defineclassmethod'
end
result[#result+1] ='(__olua_current_class, "'
result[#result+1] =selector:gsub(':', '_')
result[#result+1] ='", function(self'
result[#result+1] =args
result[#result+1] =')'
result[#result+1] =block
result[#result+1] ='end)'
returntable.concat(result)
end,
Exp=Cs(V'SuperMethodCall' +V'MethodCall' +oldExp),
Stat=Cs(V'ClassDeclaration' +
V'ClassImplementation' +
V'MethodImplementation' +
V'SuperMethodCall' +
V'MethodCall' +
oldStat),
Block=Cs(oldBlock)
},
-- Captures
{
}
)
ALL=Cs(OLUA)
end
-- Do the preprocessing.
do
localinfile=io.open(arg[1], "r")
localintext=infile:read("*a")
localouttext=ALL:match(intext)
localoutfile=io.open(arg[2], "w")
outfile:write('require "olua"\n')
outfile:write(outtext)
end