[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Syntactic sugar for sets
- From: Fabien <fleutot+lua@...>
- Date: Sun, 24 Feb 2008 20:24:35 +0100
If that's really what you want, the piece of metalua code below will do it (put it in your code between "-{block: ...}" in the file you want to hack, or put it in lib/extension/ and call the extension from the file with a "-{ extension '...'}").
However, In practice, I don't think you need this. I use table.transpose(), in metalua base lib, to exchange keys and values, so table.transpose{ "foo", "bar" } will eval to { foo=1, bar=2 }. Such statements typically aren't in performance bottlenecks, so there's nothing meaningful to gain by precompiling them off-line.
In case you want to understand the code below:
- mlp.table_field is the parser that read... table fields :) The module mlp contains the metalua parser.
- I overload it with a new definition, which is a gg.multisequence (it chooses a parser according to the initial keyword). The module gg contains grammar generators, i.e. everything to create and manipulate parsers.
- by default, if no special keyword is recognized, I fallback to the initial parser mlp.table_field
- if the first keyword is "[":
- the key is read as an mlp expr
- if it's followed by keyword "=" (gg.onkeyword()), read an additional mlp expr
- build the resulting table field AST:
- if there was no 2nd arg (no "=" keyword), replace it with +{true}
- if there was one, just put it in the pair
-{extension 'xmatch'}
mlp.table_field = gg.multisequence{
default = mlp.table_field,
{ '[', mlp.expr, ']', gg.onkeyword { '=', mlp.expr }, builder =
match function
| { e, false } -> return `Pair{ e, `True }
| { k, v } -> return `Pair{ k, v }
end } }
-- Fabien.