This file is indexed.

/usr/share/lua/5.1/leg/grammar.lua is in lua-leg 0.1.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
--[=[
<%
  project.title = "grammar"
  project.description = "LPeg grammar manipulation"
  project.version = "0.1.2"
  project.date = _G.os.date'%B %d, %Y'
  project.modules = { 'grammar', 'parser', 'scanner' }
%>

# Description

This module defines a handful of operations which can be applied to 
[http://www.inf.puc-rio.br/~roberto/lpeg.html LPeg] patterns and grammars in
general.

# Dependencies

* [http://www.inf.puc-rio.br/~roberto/lpeg.html LPeg].

# Operations

## Piping

Pattern matching dissociates the notion of *matching* from the notion of 
*capturing*: matching checks if a given string follows a certain pattern, 
and capturing generates values according to the match made. This division 
allows interesting possibilities:

* different problems can be solved by applying different captures to the same grammar;
* captures may be defined separately;
* captures may be done on top of other captures.

Accounting for the first and second bullets, the grammar given in 
[parser.html parser] has no captures, enabling the user to reuse it to solve any 
problems that require a Lua grammar. One good example is documentation 
generation, described in a little more detail [#section_Example below].

The third bullet depicts a more interesting idea: a capture might take the 
result of another capture as input, doing a further transformation of the 
original data. This capture chaining, with the latter ones using the former's 
output as its input, is very similar to [http://en.wikipedia.org/wiki/Pipeline_%28Unix%29 Unix pipelines], 
so this mechanism was named **piping**.

## Completing

With piping, several levels of captures can be chained together up to the 
most appropriate for the task at hand. Yet some levels might require extra rules, and modifications to existing ones, to ensure proper matching. 

To avoid manual copying, the new grammar should redefine only the necessary 
rules, copying the rest from the older grammar. This action is dubbed 
**completing**.

## Applying

Once a new rule set is created and [#section_Completing completed], and 
all captures are correctly [#section_Piping piped], all that's left is 
to put them together, a process called **applying**. The result is a grammar ready for [http://www.inf.puc-rio.br/~roberto/lpeg.html#lpeg lpeg.P] 
consumption, whose pattern will return the intended result when a match is made.

## Example

Let's consider the problem of documenting a Lua module. In this case, comments 
must be captured before every function declaration when in the outermost scope:

``
-- -- the code to parse
subject = %[%[
--  -- Calculates the sum a+b. 
--  -- An extra line.
  function sum (a, b)
--  -- code
  end

--  -- f1: assume a variable assignment is not a proper declaration for an 
--  -- exported function
  f1 = function ()
--  -- code
  end

  while true do
--    -- this function is not in the outermost scope
    function aux() end
  end
  
  function something:other(a, ...)
--    -- a global function without comments
  end
%]%]
``

In the code above only `sum` and `something:other` should be documented, as `f1` isn't properly (by our standards) declared and `aux` is not in the outermost scope. 

By combining [http://www.inf.puc-rio.br/~roberto/lpeg.html LPeg] and the modules [scanner.html scanner], [parser.html parser] and [grammar.html grammar], this specific problem can be solved as follows:

``
-- -- change only the initial rule and make no captures
patt = grammar.apply(parser.rules, scanner.COMMENT^-1 %* lpeg.V'GlobalFunction', nil)

-- -- transform the new grammar into a LPeg pattern
patt = lpeg.P(patt)

-- -- making a pattern that matches any Lua statement, also without captures
Stat = lpeg.P( grammar.apply(parser.rules, lpeg.V'Stat', nil) )

-- -- a pattern which matches function declarations and skips statements in
-- -- inner scopes or undesired tokens
patt = (patt + Stat + scanner.ANY)^0

-- -- matching a string
patt:match(subject)
``

These are the relevant rules in [parser.html#section_The_Grammar the grammar]:

``
GlobalFunction = 'function' %* FuncName %* FuncBody
FuncName     = ID %* ('.' %* ID)^0 %* (':' %* ID)^-1
FuncBody     = '(' %* (ParList + EPSILON) %* ')' %* Block %* 'end'
ParList      = NameList %* (',' %* '...')^-1
NameList     = ID %* (',' %* ID)^0
ID           = scanner.ID
EPSILON      = lpeg.P(true)
``

It may seem that `ParList + EPSILON` could be substituted for `ParList^-1` (optionally match `ParList`), but then no captures would be made for empty parameter lists, and `GlobalFunction` would get all strings matched by `FuncBody`. The `EPSILON` rule acts in this manner as a placeholder in the argument list, avoiding any argument list processing in the capture function.

Since no captures are being made, [http://www.inf.puc-rio.br/~roberto/lpeg.html#basic lpeg.match] doesn't return anything interesting. Here are some possible captures:

``
-- -- some interesting captures bundled up in a table. Note that the table keys
-- -- match the grammar rules we want to add captures to. Whatever rules aren't in
-- -- the rules table below will come from parser.rules .
captures = {
  %[1%] = function (...) -- the initial rule
    return '&lt;function&gt;'..table.concat{...}..'&lt;/function&gt;' 
  end,
  
  GlobalFunction = function (name, parlist)
    return '&lt;name&gt;'..name..'&lt;/name&gt;&lt;parlist&gt;'..(parlist or '')..'&lt;/parlist&gt;' 
  end,
  
  FuncName = grammar.C, -- capture the raw text
  ParList  = grammar.C, -- capture the raw text
  COMMENT  = scanner.comment2text, -- remove the comment trappings
}

-- -- spacing rule
local S = scanner.SPACE ^ 0

-- -- rules table
rules = {
  %[1%]     = ((lpeg.V'COMMENT' %*S) ^ 0) %*S%* lpeg.V'GlobalFunction',
  COMMENT = scanner.COMMENT,
}

-- -- building the new grammar and adding the captures
patt = lpeg.P( grammar.apply(parser.rules, rules, captures) )

-- -- a pattern that matches a sequence of patts and concatenates the results
patt = (patt + Stat + scanner.ANY)^0 / function(...) 
  return table.concat({...}, '\n\n') -- some line breaks for easier reading
end

-- -- finally, matching a string
print(patt:match(subject))
``

`FuncBody` needs no captures, as `Block` and all its non-terminals have none; it 
just needs to pass along any captures made by `ParList`. `NameList` and `ID` also have no captures, and the whole subject string is passed further.

The printed result is:
<pre class="example">
&lt;function&gt;Calculates the sum a+b. An extra line.&lt;name&gt;sum&lt;/name&gt;&lt;parlist&gt;a, b&lt;/parlist&gt;&lt;/function&gt;
<br/>
&lt;function&gt;&lt;name&gt;something:other&lt;/name&gt;&lt;parlist&gt;a, ...&lt;/parlist&gt;&lt;/function&gt;
</pre>
--]=]


-- $Id: grammar.lua,v 1.3 2007/11/26 18:41:51 hanjos Exp $

-- basic functions
local assert  = assert
local pairs   = pairs
local type    = type

-- imported modules
local lpeg = require 'lpeg'

-- imported functions
local P, V = lpeg.P, lpeg.V

-- module declaration
module 'leg.grammar'

--[[ 
Returns a pattern which matches any of the patterns received.

**Example:**
``
local g, s, m = require 'leg.grammar', require 'leg.scanner', require 'lpeg'

-- -- match numbers or operators, capture the numbers
print( (g.anyOf { '+', '-', '%*', '/', m.C(s.NUMBER) }):match '34.5@23 %* 56 / 45 - 45' )
-- --> prints 34.5
``

**Parameters:**
* `list`: a list of zero or more LPeg patterns or values which can be fed to [http://www.inf.puc-rio.br/~roberto/lpeg.html#lpeg lpeg.P].

**Returns:**
* a pattern which matches any of the patterns received.
--]]
function anyOf(list)
  local patt = P(false)
  
  for i = 1, #list, 1 do
    patt = P(list[i]) + patt
  end
  
  return patt
end

--[=[
Returns a pattern which matches a list of `patt`s, separated by `sep`.

**Example:** matching comma-separated values:
``
local g, m = require 'leg.grammar', require 'lpeg'

-- -- separator
local sep = m.P',' + m.P'\n'

-- -- element: anything but sep, capture it
local elem = m.C((1 - sep)^0)

-- -- pattern
local patt = g.listOf(elem, sep)

-- -- matching
print( patt:match %[%[a, b, 'christmas eve'
  d, evening; mate!
  f%]%])
-- --> prints out "a        b       'christmas eve'  d        evening; mate! f"
``

**Parameters:**
* `patt`: a LPeg pattern.
* `sep`: a LPeg pattern.

**Returns:**
* the following pattern: ``patt %* (sep %* patt)^0``
--]=]
function listOf(patt, sep)
  patt, sep = P(patt), P(sep)
  
  return patt * (sep * patt)^0
end


--[[ 
A capture function, made so that `patt / C` is equivalent to `m.C(patt)`. It's intended to be used in capture tables, such as those required by [#function_pipe pipe] and [#function_apply apply].
--]]
function C(...) return ... end

--[[ 
A capture function, made so that `patt / Ct` is equivalent to `m.Ct(patt)`. It's intended to be used in capture tables, such as those required by [#function_pipe pipe] and [#function_apply apply].
--]]
function Ct(...) return { ... } end

--[[
Creates a shallow copy of `grammar`.

**Parameters:**
* `grammar`: a regular table.

**Returns:**
* a newly created table, with `grammar`'s keys and values.
--]]
function copy(grammar)
	local newt = {}
  
	for k, v in pairs(grammar) do
		newt[k] = v
	end
  
	return newt
end

--[[
[#section_Completing Completes] `dest` with `orig`.

**Parameters:**
* `dest`: the new grammar. Must be a table.
* `orig`: the original grammar. Must be a table.

**Returns:**
* `dest`, with new rules inherited from `orig`.
--]]
function complete (dest, orig)
	for rule, patt in pairs(orig) do
		if not dest[rule] then
			dest[rule] = patt
		end
	end
  
	return dest
end

--[[
[#section_Piping Pipes] the captures in `orig` to the ones in `dest`.

`dest` and `orig` should be tables, with each key storing a capture function. Each capture in `dest` will be altered to use the results for the matching one in `orig` as input, using function composition. Should `orig` possess keys not in `dest`, `dest` will copy them.

**Parameters:**
* `dest`: a capture table.
* `orig`: a capture table.

**Returns:**
* `dest`, suitably modified.
--]]
function pipe (dest, orig)
	for k, vorig in pairs(orig) do
		local vdest = dest[k]
		if vdest then
			dest[k] = function(...) return vdest(vorig(...)) end
		else
			dest[k] = vorig
		end
	end
	
	return dest
end

--[[
[#section_Completing Completes] `rules` with `grammar` and then [#Applying applies] `captures`.     

`rules` can either be:
* a single pattern, which is taken to be the new initial rule, 
* a possibly incomplete LPeg grammar, as per [#function_complete complete], or 
* `nil`, which means no new rules are added.

`captures` can either be:
* a capture table, as per [#function_pipe pipe], or
* `nil`, which means no captures are applied.

**Parameters:**
* `grammar`: the old grammar. It stays unmodified.
* `rules`: optional, the new rules. 
* `captures`: optional, the final capture table.

**Returns:**
* `rules`, suitably augmented by `grammar` and `captures`.
--]]
function apply (grammar, rules, captures)
  if rules == nil then
    rules = {}
  elseif type(rules) ~= 'table' then
    rules = { rules }
  end
  
  complete(rules, grammar)
  
  if type(grammar[1]) == 'string' then
    rules[1] = lpeg.V(grammar[1])
  end
	
	if captures ~= nil then
		assert(type(captures) == 'table', 'captures must be a table')
    
		for rule, cap in pairs(captures) do
			rules[rule] = rules[rule] / cap
		end
	end
  
	return rules
end