lua-users home
lua-l archive

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


On Wed, Apr 8, 2015 at 2:38 PM, William Ahern
<william@25thandclement.com> wrote:
> On Wed, Apr 08, 2015 at 12:13:17AM -0400, Rena wrote:
>> On Tue, Apr 7, 2015 at 11:06 PM, Andrew Starks <andrew.starks@trms.com> wrote:
>> >
>> >
>> > On Tuesday, April 7, 2015, Sean Conner <sean@conman.org> wrote:
>> >>
>> >> It was thus said that the Great Daurnimator once stated:
>> >> > On 8 April 2015 at 10:12, Hisham <h@hisham.hm> wrote:
>> >> > > Ah! NO LPEG ALLOWED! :)
>> >> >
>> >> > Why not?
>> >>
>> >>   Way too easy.
>> >>
>> >>         lpeg = require "lpeg"
>> >>         id = lpeg.R("AZ","az","09","__")^1 / variables
>> >>         expand_variables = lpeg.Cs((
>> >>                    (lpeg.P"${") / "" * id * (lpeg.P"}"/"")
>> >>                  + (lpeg.P"$")  / "" * id
>> >>                  + lpeg.C(lpeg.P(1))
>> >>                )^0)
>> >>
>> >>   -spc (Could be made even smaller ... )
>> >
>> > Right. Too easy and too readable. :)
>> >
>> > -Andrew
>>
>> That's your idea of readable? Yikes.
>>
>> Maybe not shortest, but somewhat clear solution:
>> local vars = setmetatable({foo="bar"}, {
>>     __index = function(self, k)
>>         return type(k) == 'string' and rawget(self, k:match('{?([%w_]+)}?'))
>>     end,
>> })
>> function expand_variables(str)
>>     return str:gsub('%$(%S+)', vars)
>> end
>
> Now modify yours to support escaping $, like "Hello \$planet!". Note should
> also permit escaping the escape character, "Hello \\$planet!".
>
>
>
>

That would be interesting, because backslash is also an escape character:
> =#"a\$b"
stdin:1: invalid escape sequence near '"a\$'
> =#"a\\$b"
4

Do you want leaning toothpick syndrome? Cuz that's how you get leaning
toothpick syndrome. :-)

My solution in an earlier attempt at this was to provide a variable
'$dollar' which expands to '$'. Alternatives include $() (a variable
with the empty string for a name, which again expands to '$') or
having '$$' expand to just '$' (which means no recursion like $$foo,
but the OP specified not allowing that anyway).

-- 
Sent from my Game Boy.