2006-07-26 18:05:15 +03:00
|
|
|
#include "attr-path.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2006-07-26 18:05:15 +03:00
|
|
|
|
|
|
|
|
2010-10-22 17:47:42 +03:00
|
|
|
// !!! Shouldn't we return a pointer to a Value?
|
2010-04-07 18:47:06 +03:00
|
|
|
void findAlongAttrPath(EvalState & state, const string & attrPath,
|
2010-10-22 17:47:42 +03:00
|
|
|
Bindings & autoArgs, Expr * e, Value & v)
|
2006-07-26 18:05:15 +03:00
|
|
|
{
|
|
|
|
Strings tokens = tokenizeString(attrPath, ".");
|
|
|
|
|
|
|
|
Error attrError =
|
|
|
|
Error(format("attribute selection path `%1%' does not match expression") % attrPath);
|
|
|
|
|
|
|
|
string curPath;
|
2010-04-07 18:47:06 +03:00
|
|
|
|
|
|
|
state.mkThunk_(v, e);
|
2006-07-26 18:05:15 +03:00
|
|
|
|
2010-04-07 18:47:06 +03:00
|
|
|
foreach (Strings::iterator, i, tokens) {
|
2006-07-26 18:05:15 +03:00
|
|
|
|
|
|
|
if (!curPath.empty()) curPath += ".";
|
|
|
|
curPath += *i;
|
|
|
|
|
|
|
|
/* Is *i an index (integer) or a normal attribute name? */
|
|
|
|
enum { apAttr, apIndex } apType = apAttr;
|
|
|
|
string attr = *i;
|
|
|
|
int attrIndex = -1;
|
|
|
|
if (string2Int(attr, attrIndex)) apType = apIndex;
|
|
|
|
|
|
|
|
/* Evaluate the expression. */
|
2010-04-07 18:47:06 +03:00
|
|
|
Value vTmp;
|
|
|
|
state.autoCallFunction(autoArgs, v, vTmp);
|
|
|
|
v = vTmp;
|
|
|
|
state.forceValue(v);
|
2006-07-26 18:05:15 +03:00
|
|
|
|
|
|
|
/* It should evaluate to either an attribute set or an
|
|
|
|
expression, according to what is specified in the
|
|
|
|
attrPath. */
|
|
|
|
|
|
|
|
if (apType == apAttr) {
|
|
|
|
|
2010-04-07 18:47:06 +03:00
|
|
|
if (v.type != tAttrs)
|
2006-07-26 18:05:15 +03:00
|
|
|
throw TypeError(
|
|
|
|
format("the expression selected by the selection path `%1%' should be an attribute set but is %2%")
|
2010-04-07 18:47:06 +03:00
|
|
|
% curPath % showType(v));
|
2006-07-26 18:05:15 +03:00
|
|
|
|
2010-04-13 15:25:42 +03:00
|
|
|
Bindings::iterator a = v.attrs->find(state.symbols.create(attr));
|
2010-04-07 18:47:06 +03:00
|
|
|
if (a == v.attrs->end())
|
|
|
|
throw Error(format("attribute `%1%' in selection path `%2%' not found") % attr % curPath);
|
2010-10-22 17:47:42 +03:00
|
|
|
v = *a->second.value;
|
2006-07-26 18:05:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (apType == apIndex) {
|
|
|
|
|
2010-04-07 18:47:06 +03:00
|
|
|
if (v.type != tList)
|
2006-07-26 18:05:15 +03:00
|
|
|
throw TypeError(
|
|
|
|
format("the expression selected by the selection path `%1%' should be a list but is %2%")
|
2010-04-07 18:47:06 +03:00
|
|
|
% curPath % showType(v));
|
2006-07-26 18:05:15 +03:00
|
|
|
|
2010-04-07 18:47:06 +03:00
|
|
|
if (attrIndex >= v.list.length)
|
|
|
|
throw Error(format("list index %1% in selection path `%2%' is out of range") % attrIndex % curPath);
|
|
|
|
|
2010-04-15 03:37:36 +03:00
|
|
|
v = *v.list.elems[attrIndex];
|
2006-07-26 18:05:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
}
|