@@ -56,7 +56,11 @@ local TermOp = Space * C(S"*/%^")
5656
5757local Shebang = P " #!" * P (1 - Stop )^ 0
5858
59- local function wrap (fn )
59+ -- can't have P(false) because it causes preceding patterns not to run
60+ local Cut = P (function () return false end )
61+
62+ -- auto declare Proper variables with lpeg.V
63+ local function wrap_env (fn )
6064 local env = getfenv (fn )
6165
6266 return setfenv (fn , setmetatable ({}, {
@@ -121,9 +125,96 @@ local function flatten_or_mark(name)
121125 end
122126end
123127
124- local build_grammar = wrap (function ()
125- local err_msg = " Failed to parse:\n [%d] >> %s (%d)"
128+ -- makes sure the last item in a chain is an index
129+ local _assignable = { index = true , dot = true , slice = true }
130+ local function check_assignable (str , pos , value )
131+ if ntype (value ) == " chain" and _assignable [ntype (value [# value ])]
132+ or type (value ) == " string"
133+ then
134+ return true , value
135+ end
136+ return false
137+ end
138+
139+ local function sym (chars )
140+ return Space * chars
141+ end
142+
143+ local function symx (chars )
144+ return chars
145+ end
146+
147+ local function simple_string (delim , x )
148+ return C (symx (delim )) * C ((P (' \\ ' .. delim ) +
149+ " \\\\ " +
150+ (1 - S (' \r\n ' .. delim )))^ 0 ) * sym (delim ) / mark " string"
151+ end
152+
153+ local function wrap_func_arg (value )
154+ return {" call" , {value }}
155+ end
156+
157+ -- DOCME
158+ local function flatten_func (callee , args )
159+ if # args == 0 then return callee end
160+
161+ args = {" call" , args }
162+ if ntype (callee ) == " chain" then
163+ -- check for colon stub that needs arguments
164+ if ntype (callee [# callee ]) == " colon_stub" then
165+ local stub = callee [# callee ]
166+ stub [1 ] = " colon"
167+ table.insert (stub , args )
168+ else
169+ table.insert (callee , args )
170+ end
171+
172+ return callee
173+ end
174+
175+ return {" chain" , callee , args }
176+ end
177+
178+ -- wraps a statement that has a line decorator
179+ local function wrap_decorator (stm , dec )
180+ if not dec then return stm end
181+
182+ local arg = {stm , dec }
183+
184+ if dec [1 ] == " if" then
185+ local _ , cond , fail = unpack (dec )
186+ if fail then fail = {" else" , {fail }} end
187+ stm = {" if" , cond , {stm }, fail }
188+ elseif dec [1 ] == " comprehension" then
189+ local _ , clauses = unpack (dec )
190+ stm = {" comprehension" , stm , clauses }
191+ end
192+
193+ return stm
194+ end
195+
196+ -- wrap if statement if there is a conditional decorator
197+ local function wrap_if (stm , cond )
198+ if cond then
199+ local pass , fail = unpack (cond )
200+ if fail then fail = {" else" , {fail }} end
201+ return {" if" , cond [2 ], {stm }, fail }
202+ end
203+ return stm
204+ end
205+
206+ local function check_lua_string (str , pos , right , left )
207+ return # left == # right
208+ end
209+
210+ -- :name in table literal
211+ local function self_assign (name )
212+ return {name , name }
213+ end
214+
215+ local err_msg = " Failed to parse:\n [%d] >> %s (%d)"
126216
217+ local build_grammar = wrap_env (function ()
127218 local _indent = Stack (0 ) -- current indent
128219
129220 local last_pos = 0 -- used to know where to report error
@@ -164,49 +255,6 @@ local build_grammar = wrap(function()
164255 return patt
165256 end
166257
167- local function sym (chars )
168- return Space * chars
169- end
170-
171- local function symx (chars )
172- return chars
173- end
174-
175- local function flatten_func (callee , args )
176- if # args == 0 then return callee end
177-
178- args = {" call" , args }
179- if ntype (callee ) == " chain" then
180- -- check for colon stub that needs arguments
181- if ntype (callee [# callee ]) == " colon_stub" then
182- local stub = callee [# callee ]
183- stub [1 ] = " colon"
184- table.insert (stub , args )
185- else
186- table.insert (callee , args )
187- end
188-
189- return callee
190- end
191-
192- return {" chain" , callee , args }
193- end
194-
195- local function wrap_func_arg (value )
196- return {" call" , {value }}
197- end
198-
199- -- makes sure the last item in a chain is an index
200- local _assignable = { index = true , dot = true , slice = true }
201- local function check_assignable (str , pos , value )
202- if ntype (value ) == " chain" and _assignable [ntype (value [# value ])]
203- or type (value ) == " string"
204- then
205- return true , value
206- end
207- return false
208- end
209-
210258 local SimpleName = Name -- for table key
211259
212260 -- make sure name is not a keyword
@@ -217,51 +265,6 @@ local build_grammar = wrap(function()
217265
218266 local Name = sym " @" * Name / mark " self" + Name + Space * " ..." / trim
219267
220- local function simple_string (delim , x )
221- return C (symx (delim )) * C ((P (' \\ ' .. delim ) +
222- " \\\\ " +
223- (1 - S (' \r\n ' .. delim )))^ 0 ) * sym (delim ) / mark " string"
224- end
225-
226- -- wrap if statement if there is a conditional decorator
227- local function wrap_if (stm , cond )
228- if cond then
229- local pass , fail = unpack (cond )
230- if fail then fail = {" else" , {fail }} end
231- return {" if" , cond [2 ], {stm }, fail }
232- end
233- return stm
234- end
235-
236- local function wrap_decorator (stm , dec )
237- if not dec then return stm end
238-
239- local arg = {stm , dec }
240-
241- if dec [1 ] == " if" then
242- local _ , cond , fail = unpack (dec )
243- if fail then fail = {" else" , {fail }} end
244- stm = {" if" , cond , {stm }, fail }
245- elseif dec [1 ] == " comprehension" then
246- local _ , clauses = unpack (dec )
247- stm = {" comprehension" , stm , clauses }
248- end
249-
250- return stm
251- end
252-
253- local function check_lua_string (str , pos , right , left )
254- return # left == # right
255- end
256-
257- -- :name in table literal
258- local function self_assign (name )
259- return {name , name }
260- end
261-
262- -- can't have P(false) because it causes preceding patterns not to run
263- local Cut = P (function () return false end )
264-
265268 local g = lpeg .P {
266269 File ,
267270 File = Shebang ^- 1 * (Block + Ct " " ),
0 commit comments