aboutsummaryrefslogtreecommitdiff
path: root/examples/listIdCatchRe.lua
blob: 5d38fecaf3142836d9cc2e68d5ded73c64c6de06 (plain)
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
local re = require're'

local terror = {} 

local function newError(l, msg) 
  table.insert(terror, { l = l, msg = msg } )
end

newError("errId", "Error: expecting an identifier")
newError("errComma", "Error: expecting ','")

local labelCode = {}
local labelMsg = {}
for k, v in ipairs(terror) do 
  labelCode[v.l] = k
  labelMsg[v.l] = v.msg
end

re.setlabels(labelCode)

local p = re.compile([[
  S        <- Id List  /{errId}  ErrId  /{errComma}  ErrComma
  List     <- !.  /  Comma Id List
  Id       <- [a-z]+  /  %{errId}
  Comma    <- ','  /  %{errComma}
  ErrId    <- '' -> errId
  ErrComma <- '' ->  errComma
]], labelMsg)

print(p:match("a,b"))
print(p:match("a b"))
print(p:match(",b"))