lua-users home
lua-l archive

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


On Fri, Jul 7, 2017 at 9:09 PM, Matthias Dörfelt <lists@mokafolio.de> wrote:
> Awesome, thanks that is super helpful!
> Haven’t looked at the “re” portion of lpeg, yet. I was initially gonna stick with pure LPeg, but let’s see!
>
> Is it pretty straight forward to express your re code in pure lua & LPeg?
>
> Thanks!

Sure, I think this should be the equivalent:


--
local hierarchy = lpeg.P {

  'hierarchy';

  hierarchy = initial_indent
    *
    lpeg.Ct(
      lpeg.Cg(
        lpeg.Ct(
          (
            lpeg.V('first_root')
            *
            lpeg.V('next_root')^0
          )^-1
        ),
        'children'
      )
    )
    *
    lpeg.P(-1);

  first_root = lpeg.V('entry');

  next_root = '\n' * lpeg.V('entry');

  entry = lpeg.Ct(
    lpeg.Cg(lpeg.V('name'), 'name')
    *
    lpeg.Cg(
      lpeg.Ct(
        (
          lpeg.V('first_child')
          *
          lpeg.V('next_child')^0
        )^-1
      ),
      'children'
    )
  );

  first_child = '\n' * capture_deeper_indent * lpeg.V('entry');
  next_child = '\n' * match_same_indent * lpeg.V('entry');

  name = (1 - lpeg.P('\n'))^0;

}
--


-Duncan