lua-users home
lua-l archive

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


On Fri, May 29, 2015 at 9:47 AM, Parke <parke.nexus@gmail.com> wrote:
> I don't know a simple way to do what you want.  It might be possible
> by using function captures, and/or match time function captures, but I
> consider the chucks to be simpler.

How about this, which uses a substitution capture, two function
captures, and two closures?

pattern  =  [==[

  {~  (

  '[['
  !':'
  ''->'Name::'
         (  !']]'  !'|'  .  )+  ->  item
  ( '|'  (  !']]'        .  )*  )?
  ']]'

  /  ( [,;*#]  %s* ) -> separator

  /  .

  )*  ~}

]==]


s  =  'Perhaps, [[Peter|Simon]], or [[Paul]], so they say (see [[:Apocrypha]])'
items = {}
separators = {}
function item      ( s )  table.insert ( items,      s )  return s end
function separator ( s )  table.insert ( separators, s )  return s end
defs = { item = item, separator = separator }
parser  =  require ( 're' ).compile ( pattern, defs )
full  =  parser : match ( s )

print ( s )
print ( full )
for n,v in ipairs ( items      ) do   print ( n, v )  end
for n,v in ipairs ( separators ) do   print ( n, v )  end

----

The above will print:

Perhaps, [[Peter|Simon]], or [[Paul]], so they say (see [[:Apocrypha]])
Perhaps, [[Name::Peter|Simon]], or [[Name::Paul]], so they say (see
[[:Apocrypha]])
1       Peter
2       Paul
1       ,
2       ,
3       ,