lua-users home
lua-l archive

patch: C-style string lexing

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


I just posted a small patch to the wiki that allows C-style string lexing,
where string literals separated by whitespace are collected as a single
string. I've always appreciated that in C, and miss it when using languages
that don't lex strings that way (lika Java or Lua).
For instance, if you have a code block like:
 if something then
 for k,v in pairs(foo) do
 if v then
 bar()
 else
 io.write "This is a big chunk of text "
 "that for whatever reason (typically "
 "readability) is broken into multiple "
 "lines in the source code.\n"
 end
 end
You can format strings this way now using concatenation, but that has
performance costs. Doing it during lexing is essentially free.
It's also a useful alternative to long strings in some situations because:
 1) it allows you to use regular string escape patterns, so you can embed
 control characters, control EOL format, etc.
 2) it doesn't force you to left-justify the text in your source (which
 usually messes up your indentation ^_^)
Anyway, I only wanted to mention it here because it would be nice to see it in
the Lua core. It's a minimal change to the lexer with no real downside (as far
as I can tell). It just gives you a bit more freedom in how you can
format string
literals.
Patch is here (scroll to bottom):
http://lua-users.org/wiki/LuaPowerPatches
Cheers,
Eric

AltStyle によって変換されたページ (->オリジナル) /