Skip to content

Instantly share code, notes, and snippets.

@sourcevault
Created April 2, 2019 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sourcevault/0fbfaf94cb7d691500ce74f626d91feb to your computer and use it in GitHub Desktop.
Save sourcevault/0fbfaf94cb7d691500ce74f626d91feb to your computer and use it in GitHub Desktop.

What is the idiomic 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 ?

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 doen't not run if I use Cmt, but I think I am missing the larger picture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment