lix/src/libexpr
Shea Levy 18fefacf7d Dynamic attrs
This adds new syntax for attribute names:

* attrs."${name}" => getAttr name attrs
* attrs ? "${name}" => isAttrs attrs && hasAttr attrs name
* attrs."${name}" or def => if attrs ? "${name}" then attrs."${name}" else def
* { "${name}" = value; } => listToAttrs [{ inherit name value; }]

Of course, it's a bit more complicated than that. The attribute chains
can be arbitrarily long and contain combinations of static and dynamic
parts (e.g. attrs."${foo}".bar."${baz}" or qux), which is relatively
straightforward for the getAttrs/hasAttrs cases but is more complex for
the listToAttrs case due to rules about duplicate attribute definitions.

For attribute sets with dynamic attribute names, duplicate static
attributes are detected at parse time while duplicate dynamic attributes
are detected when the attribute set is forced. So, for example, { a =
null; a.b = null; "${"c"}" = true; } will be a parse-time error, while
{ a = {}; "${"a"}".b = null; c = true; } will be an eval-time error
(technically that case could theoretically be detected at parse time,
but the general case would require full evaluation). Moreover, duplicate
dynamic attributes are not allowed even in cases where they would be
with static attributes ({ a.b.d = true; a.b.c = false; } is legal, but {
a."${"b"}".d = true; a."${"b"}".c = false; } is not). This restriction
might be relaxed in the future in cases where the static variant would
not be an error, but it is not obvious that that is desirable.

Finally, recursive attribute sets with dynamic attributes have the
static attributes in scope but not the dynamic ones. So rec { a = true;
"${"b"}" = a; } is equivalent to { a = true; b = true; } but rec {
"${"a"}" = true; b = a; } would be an error or use a from the
surrounding scope if it exists.

Note that the getAttr, getAttr or default, and hasAttr are all
implemented purely in the parser as syntactic sugar, while attribute
sets with dynamic attribute names required changes to the AST to be
implemented cleanly.

This is an alternative solution to and closes #167

Signed-off-by: Shea Levy <shea@shealevy.com>
2013-12-31 20:59:49 +00:00
..
attr-path.cc Support quoted attribute names in -A 2013-11-18 10:21:12 +00:00
attr-path.hh Work on Values instead of Exprs 2013-09-03 13:17:51 +00:00
common-opts.cc Fix error message 2012-02-09 18:56:48 +01:00
common-opts.hh Use "#pragma once" to prevent repeated header file inclusion 2012-07-18 14:59:03 -04:00
eval-inline.hh Get rid of an intermediary on the stack 2013-11-12 11:32:23 +00:00
eval.cc Dynamic attrs 2013-12-31 20:59:49 +00:00
eval.hh Add a toJSON primop 2013-11-19 00:04:11 +01:00
get-drvs.cc Check meta values and warn about bad ones 2013-11-19 14:29:39 +01:00
get-drvs.hh Check meta values and warn about bad ones 2013-11-19 14:29:39 +01:00
lexer.l Fix whitespace 2013-09-02 16:29:15 +02:00
Makefile.am Add a toJSON primop 2013-11-19 00:04:11 +01:00
names.cc nix-env: Use wildcard match by default 2013-09-03 16:35:49 +02:00
names.hh Use "#pragma once" to prevent repeated header file inclusion 2012-07-18 14:59:03 -04:00
nix.sdf First attempt to update Nix SDF grammar to match the actual bison grammar 2008-06-04 14:36:46 +00:00
nixexpr.cc Dynamic attrs 2013-12-31 20:59:49 +00:00
nixexpr.hh Dynamic attrs 2013-12-31 20:59:49 +00:00
parser.y Dynamic attrs 2013-12-31 20:59:49 +00:00
primops.cc Bump language version for new storePath feature 2013-12-10 15:07:04 +01:00
symbol-table.hh Rename "attribute sets" to "sets" 2013-10-24 16:41:04 +02:00
value-to-json.cc Refactor JSON output 2013-11-19 00:33:06 +01:00
value-to-json.hh Refactor JSON output 2013-11-19 00:33:06 +01:00
value-to-xml.cc Fix compilation on FreeBSD 2012-03-05 22:04:40 +01:00
value-to-xml.hh Use "#pragma once" to prevent repeated header file inclusion 2012-07-18 14:59:03 -04:00
value.hh Add a primop unsafeGetAttrPos to return the position of an attribute 2013-11-18 22:22:35 +01:00