lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


What is the idiomatic way to create error messages in LPEG ?

To be specific I would like to create error messages for incomplete bracket completion.

Example :

> [ 1 2 3     -- ERROR ! MISSING ] AT LINE 20

> ( 1 2 3     -- ERROR ! MISSING ) AT LINE 35

> { 1 2 3     -- ERROR ! MISSING } AT LINE 12

>  1 2 3 ]    -- ERROR ! EXTRA   ] AT LINE 42

> [ ( 1 2 3 ] -- ERROR ! MISSING ) AT LINE 71

I have figured out that I need to use lpeg.Carg to pass a table to log line number but how can a pattern fail in a certain way as to let its error be know ?

```lua

local show = function(...)

  {tab,cap} = arg

  print (cap)

end


local inner = C(1 - P(']'))^0

local patt = P('[') * inner * P(']')

local test = Cmt((patt * (Carg(1))), show)

local out = test:match('[hello', 1, { })

return log(out) -- nil ??

```

The problem is the function `show` does not run if the pattern fails.

It also does not not run if I use `Cmt`, but I think I am missing the larger picture.

I also created a markdown gist to make the code easier to read

https://gist.github.com/sourcevault/0fbfaf94cb7d691500ce74f626d91feb


best wishes,
Joy