forked from lix-project/lix
Add a symbol __curPos that expands to the current source location
I.e. an attribute set { file = <string>; line = <int>; column = <int>; }.
This commit is contained in:
parent
90b5e69284
commit
fc33fd86b7
|
@ -142,6 +142,9 @@ EvalState::EvalState()
|
|||
, sOutputs(symbols.create("outputs"))
|
||||
, sOutputName(symbols.create("outputName"))
|
||||
, sIgnoreNulls(symbols.create("__ignoreNulls"))
|
||||
, sFile(symbols.create("file"))
|
||||
, sLine(symbols.create("line"))
|
||||
, sColumn(symbols.create("column"))
|
||||
, repair(false)
|
||||
, baseEnv(allocEnv(128))
|
||||
, staticBaseEnv(false, 0)
|
||||
|
@ -1039,6 +1042,16 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
|
|||
}
|
||||
|
||||
|
||||
void ExprPos::eval(EvalState & state, Env & env, Value & v)
|
||||
{
|
||||
state.mkAttrs(v, 3);
|
||||
mkString(*state.allocAttr(v, state.sFile), pos.file);
|
||||
mkInt(*state.allocAttr(v, state.sLine), pos.line);
|
||||
mkInt(*state.allocAttr(v, state.sColumn), pos.column);
|
||||
v.attrs->sort();
|
||||
}
|
||||
|
||||
|
||||
void EvalState::strictForceValue(Value & v)
|
||||
{
|
||||
forceValue(v);
|
||||
|
|
|
@ -94,7 +94,8 @@ public:
|
|||
SymbolTable symbols;
|
||||
|
||||
const Symbol sWith, sOutPath, sDrvPath, sType, sMeta, sName, sValue,
|
||||
sSystem, sOverrides, sOutputs, sOutputName, sIgnoreNulls;
|
||||
sSystem, sOverrides, sOutputs, sOutputName, sIgnoreNulls,
|
||||
sFile, sLine, sColumn;
|
||||
Symbol sDerivationNix;
|
||||
|
||||
/* If set, force copying files to the Nix store even if they
|
||||
|
|
|
@ -130,6 +130,11 @@ void ExprConcatStrings::show(std::ostream & str)
|
|||
}
|
||||
}
|
||||
|
||||
void ExprPos::show(std::ostream & str)
|
||||
{
|
||||
str << "__curPos";
|
||||
}
|
||||
|
||||
|
||||
std::ostream & operator << (std::ostream & str, const Pos & pos)
|
||||
{
|
||||
|
@ -315,6 +320,10 @@ void ExprConcatStrings::bindVars(const StaticEnv & env)
|
|||
(*i)->bindVars(env);
|
||||
}
|
||||
|
||||
void ExprPos::bindVars(const StaticEnv & env)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/* Storing function names. */
|
||||
|
||||
|
|
|
@ -282,6 +282,13 @@ struct ExprConcatStrings : Expr
|
|||
COMMON_METHODS
|
||||
};
|
||||
|
||||
struct ExprPos : Expr
|
||||
{
|
||||
Pos pos;
|
||||
ExprPos(const Pos & pos) : pos(pos) { };
|
||||
COMMON_METHODS
|
||||
};
|
||||
|
||||
|
||||
/* Static environments are used to map variable names onto (level,
|
||||
displacement) pairs used to obtain the value of the variable at
|
||||
|
|
|
@ -355,7 +355,12 @@ expr_select
|
|||
;
|
||||
|
||||
expr_simple
|
||||
: ID { $$ = new ExprVar(CUR_POS, data->symbols.create($1)); }
|
||||
: ID {
|
||||
if (strcmp($1, "__curPos") == 0)
|
||||
$$ = new ExprPos(CUR_POS);
|
||||
else
|
||||
$$ = new ExprVar(CUR_POS, data->symbols.create($1));
|
||||
}
|
||||
| INT { $$ = new ExprInt($1); }
|
||||
| '"' string_parts '"' {
|
||||
/* For efficiency, and to simplify parse trees a bit. */
|
||||
|
|
1
tests/lang/eval-okay-curpos.exp
Normal file
1
tests/lang/eval-okay-curpos.exp
Normal file
|
@ -0,0 +1 @@
|
|||
[ 3 7 4 9 ]
|
5
tests/lang/eval-okay-curpos.nix
Normal file
5
tests/lang/eval-okay-curpos.nix
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Bla
|
||||
let
|
||||
x = __curPos;
|
||||
y = __curPos;
|
||||
in [ x.line x.column y.line y.column ]
|
Loading…
Reference in a new issue