[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: string dollar literals (for luamacro or tokenf)
- From: Jay Carlson <nop@...>
- Date: Sun, 22 Jan 2012 22:50:17 -0500
This README and source at http://place.org/~nop/lua/dollar/ . The talk about shell stuff from a while back had me panicking about compact syntax choices for quasi-quoted lists (never mind the jargon, that's what a shell language *is*). A slow panic I guess.
tl;dr: $"total: $count lines" is sugar for
{"total: $count lines", count=count}
$"done: $count pass ${pass:03d} $(hash)sum" is sugar for
{"done: $count pass ${pass:03d} $(hash)sum",
count=count, pass=pass, hash=hash}
my_interpolate$[[more $sugar!]] does just what you think.
$-strings are designed for little languages, not just generating
strings. An XML fragment could be compiled and memoized, and this
syntax reduces the temptation to write "<b>"..formdata.."</b>".
====
It's been bugging me for a long time there is no syntax for string
interpolation or compilation in Lua that does locals. There is not one
obvious good string interpolation syntax (see sh, make, tcl, oh god
perl etc) but the variety of blood-curdling interpolation *semantics*
is endless. Little Bobby Tables could have a more normal name if
non-string literal constructors were easier.
The Lua way is simple meta-mechanisms, slathered with sugar. OK!
I haven't quite figured out what I want from the core. Token filters
were designed to stop people from pestering for syntax. It worked.
I lied in the summary; I throw _G into the tables as well. It should
be getfenv() in Lua 5.1 and its analog in 5.2.
The current implementation looks for $ or %, followed by zero or more
"introducer" characters, currently:
([{<'"`?!
Any legal Lua identifier following that gets tossed in the table. Note
that as with regular table constructors {b=b} succeeds if either b is
in lexical scope or hitting globals for b doesn't error out.
$"I like to mix $($logic + $strings)" does the right thing--in the
sense that "$(" will not blow up; it has no Lua identifier following
it.
Both $$ and %% specifically do *not* trigger name inclusion, and are
designed as quoting mechanisms.
You need a tokenf-patched luac to use this; you may use it with
LuaMacro. I would have written this in metalua, but if you have
metalua why would you mess around with execution-time compilation of
string literals?? Anyway, $-strings are designed to be easy to patch
into the mainline Lua implementation.
I've included the usual string interpolators and strings-as-lambdas in
dinterp.lua. Somebody else can write the SQL prepared statement
parser ("SELECT * FROM $foo" -> {"SELECT * FROM ?", {foo}} etc)
Feel free to run as "lua -ldollar -ldinterp -i"; if LuaMacro is turned
on it has to be on the LUA_PATH too.
Jay