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:
Eelco Dolstra 2013-11-18 20:14:54 +01:00
parent 90b5e69284
commit fc33fd86b7
7 changed files with 43 additions and 2 deletions

View file

@ -142,6 +142,9 @@ EvalState::EvalState()
, sOutputs(symbols.create("outputs")) , sOutputs(symbols.create("outputs"))
, sOutputName(symbols.create("outputName")) , sOutputName(symbols.create("outputName"))
, sIgnoreNulls(symbols.create("__ignoreNulls")) , sIgnoreNulls(symbols.create("__ignoreNulls"))
, sFile(symbols.create("file"))
, sLine(symbols.create("line"))
, sColumn(symbols.create("column"))
, repair(false) , repair(false)
, baseEnv(allocEnv(128)) , baseEnv(allocEnv(128))
, staticBaseEnv(false, 0) , 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) void EvalState::strictForceValue(Value & v)
{ {
forceValue(v); forceValue(v);

View file

@ -94,7 +94,8 @@ public:
SymbolTable symbols; SymbolTable symbols;
const Symbol sWith, sOutPath, sDrvPath, sType, sMeta, sName, sValue, const Symbol sWith, sOutPath, sDrvPath, sType, sMeta, sName, sValue,
sSystem, sOverrides, sOutputs, sOutputName, sIgnoreNulls; sSystem, sOverrides, sOutputs, sOutputName, sIgnoreNulls,
sFile, sLine, sColumn;
Symbol sDerivationNix; Symbol sDerivationNix;
/* If set, force copying files to the Nix store even if they /* If set, force copying files to the Nix store even if they

View file

@ -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) std::ostream & operator << (std::ostream & str, const Pos & pos)
{ {
@ -315,6 +320,10 @@ void ExprConcatStrings::bindVars(const StaticEnv & env)
(*i)->bindVars(env); (*i)->bindVars(env);
} }
void ExprPos::bindVars(const StaticEnv & env)
{
}
/* Storing function names. */ /* Storing function names. */

View file

@ -282,6 +282,13 @@ struct ExprConcatStrings : Expr
COMMON_METHODS 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, /* Static environments are used to map variable names onto (level,
displacement) pairs used to obtain the value of the variable at displacement) pairs used to obtain the value of the variable at

View file

@ -355,7 +355,12 @@ expr_select
; ;
expr_simple 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); } | INT { $$ = new ExprInt($1); }
| '"' string_parts '"' { | '"' string_parts '"' {
/* For efficiency, and to simplify parse trees a bit. */ /* For efficiency, and to simplify parse trees a bit. */

View file

@ -0,0 +1 @@
[ 3 7 4 9 ]

View file

@ -0,0 +1,5 @@
# Bla
let
x = __curPos;
y = __curPos;
in [ x.line x.column y.line y.column ]