Advertisement
Anaristos

lua speedwalk parser

Jan 26th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.62 KB | None | 0 0
  1. function parse_cmud_sws( _, _, wildcards )
  2. --
  3.     lpeg = require( "lpeg" )
  4.  
  5.     world.SetVariable ( "sws", "" )
  6.  
  7.     local sw = wildcards[1] or ""
  8.  
  9.     local C, Cc, Cf, P, V, match = lpeg.C, lpeg.Cc, lpeg.Cf, lpeg.P, lpeg.V, lpeg.match
  10.  
  11.     local directions, portal, sw_e, sw_s, walk = V( 'directions' ), V( 'portal' ), V( 'sw_e' ), V( 'sw_s' ), V( 'walk' )
  12.  
  13.     local sep = ";"
  14.  
  15.     local run = function( sw ) if sw:len() > 1 then sw = "run "..sw end return sw end
  16.  
  17.     local fold = function( walk, sw )
  18.         if walk:len() == 0 then return sw end
  19.         if sw:len() == 0 then return walk end
  20.         return walk..sep..sw
  21.     end
  22.  
  23.     local speedwalk = P {                                                                   -- grammar for path decoding.
  24.                           'speedwalk',                                                      -- starting symbol name.
  25.                           sw_e       = P(sep),                                              -- end-of-speedwalk   marker.
  26.                           sw_s       = P('.'),                                              -- start-of-speedwalk marker.        
  27.                           directions = C((1 - sw_e)^0),                                     -- speedwalk string capture.
  28.                           portal     = P('(' * C((1 - P(')'))^1) * ')')^0,                  -- custom exit command capture.
  29.                           walk       = sw_s * portal * (directions / run) * sw_e^-1,        -- speedwalk iteration element.
  30.                           speedwalk  = Cf(Cc("") * walk^0, fold)                            -- complete speedwalk search pattern.
  31.                         } * -1                                                              -- end-of-input marker.
  32. -- 
  33.     world.SetVariable ( "sws", match( speedwalk, sw ) )
  34.  
  35.     return
  36. --
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement