* Refactoring: move parseExprFromFile() and parseExprFromString() into

the EvalState class.
This commit is contained in:
Eelco Dolstra 2011-08-06 13:02:55 +00:00
parent c8606664ab
commit 54945a2950
10 changed files with 37 additions and 52 deletions

View File

@ -6,7 +6,7 @@ libexpr_la_SOURCES = \
names.cc names.cc
pkginclude_HEADERS = \ pkginclude_HEADERS = \
nixexpr.hh eval.hh parser.hh lexer-tab.hh parser-tab.hh \ nixexpr.hh eval.hh lexer-tab.hh parser-tab.hh \
get-drvs.hh attr-path.hh value-to-xml.hh common-opts.hh \ get-drvs.hh attr-path.hh value-to-xml.hh common-opts.hh \
names.hh symbol-table.hh names.hh symbol-table.hh

View File

@ -1,7 +1,6 @@
#include "common-opts.hh" #include "common-opts.hh"
#include "../libmain/shared.hh" #include "../libmain/shared.hh"
#include "util.hh" #include "util.hh"
#include "parser.hh"
namespace nix { namespace nix {
@ -25,7 +24,7 @@ bool parseOptionArg(const string & arg, Strings::iterator & i,
autoArgs.push_back(Attr(state.symbols.create(name), v)); autoArgs.push_back(Attr(state.symbols.create(name), v));
if (arg == "--arg") if (arg == "--arg")
state.mkThunk_(*v, parseExprFromString(state, value, absPath("."))); state.mkThunk_(*v, state.parseExprFromString(value, absPath(".")));
else else
mkString(*v, value); mkString(*v, value);

View File

@ -1,5 +1,4 @@
#include "eval.hh" #include "eval.hh"
#include "parser.hh"
#include "hash.hh" #include "hash.hh"
#include "util.hh" #include "util.hh"
#include "store-api.hh" #include "store-api.hh"
@ -427,14 +426,10 @@ void EvalState::evalFile(const Path & path, Value & v)
{ {
startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path); startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path);
Expr * e = parseTrees[path]; Expr * e = parseExprFromFile(path);
if (!e) {
e = parseExprFromFile(*this, path);
parseTrees[path] = e;
}
try { try {
/* !!! Maybe we should cache the evaluation result. */
eval(e, v); eval(e, v);
} catch (Error & e) { } catch (Error & e) {
addErrorPrefix(e, "while evaluating the file `%1%':\n", path); addErrorPrefix(e, "while evaluating the file `%1%':\n", path);

View File

@ -218,6 +218,13 @@ public:
EvalState(); EvalState();
~EvalState(); ~EvalState();
/* Parse a Nix expression from the specified file. If `path'
refers to a directory, then "/default.nix" is appended. */
Expr * parseExprFromFile(Path path);
/* Parse a Nix expression from the specified string. */
Expr * parseExprFromString(const string & s, const Path & basePath);
/* Evaluate an expression read from the given file to normal /* Evaluate an expression read from the given file to normal
form. */ form. */
void evalFile(const Path & path, Value & v); void evalFile(const Path & path, Value & v);
@ -296,6 +303,9 @@ private:
friend class ExprAttrs; friend class ExprAttrs;
friend class ExprLet; friend class ExprLet;
Expr * parse(const char * text,
const Path & path, const Path & basePath);
public: public:
/* Do a deep equality test between two values. That is, list /* Do a deep equality test between two values. That is, list

View File

@ -1,21 +0,0 @@
#ifndef __PARSER_H
#define __PARSER_H
#include "eval.hh"
namespace nix {
/* Parse a Nix expression from the specified file. If `path' refers
to a directory, then "/default.nix" is appended. */
Expr * parseExprFromFile(EvalState & state, Path path);
/* Parse a Nix expression from the specified string. */
Expr * parseExprFromString(EvalState & state, const string & s, const Path & basePath);
}
#endif /* !__PARSER_H */

View File

@ -450,11 +450,11 @@ formal
namespace nix { namespace nix {
static Expr * parse(EvalState & state, const char * text, Expr * EvalState::parse(const char * text,
const Path & path, const Path & basePath) const Path & path, const Path & basePath)
{ {
yyscan_t scanner; yyscan_t scanner;
ParseData data(state.symbols); ParseData data(symbols);
data.basePath = basePath; data.basePath = basePath;
data.path = path; data.path = path;
@ -466,7 +466,7 @@ static Expr * parse(EvalState & state, const char * text,
if (res) throw ParseError(data.error); if (res) throw ParseError(data.error);
try { try {
data.result->bindVars(state.staticBaseEnv); data.result->bindVars(staticBaseEnv);
} catch (Error & e) { } catch (Error & e) {
throw ParseError(format("%1%, in `%2%'") % e.msg() % path); throw ParseError(format("%1%, in `%2%'") % e.msg() % path);
} }
@ -475,7 +475,7 @@ static Expr * parse(EvalState & state, const char * text,
} }
Expr * parseExprFromFile(EvalState & state, Path path) Expr * EvalState::parseExprFromFile(Path path)
{ {
assert(path[0] == '/'); assert(path[0] == '/');
@ -493,15 +493,21 @@ Expr * parseExprFromFile(EvalState & state, Path path)
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
path = canonPath(path + "/default.nix"); path = canonPath(path + "/default.nix");
/* Read and parse the input file. */ /* Read and parse the input file, unless it's already in the parse
return parse(state, readFile(path).c_str(), path, dirOf(path)); tree cache. */
Expr * e = parseTrees[path];
if (!e) {
e = parse(readFile(path).c_str(), path, dirOf(path));
parseTrees[path] = e;
}
return e;
} }
Expr * parseExprFromString(EvalState & state, Expr * EvalState::parseExprFromString(const string & s, const Path & basePath)
const string & s, const Path & basePath)
{ {
return parse(state, s.c_str(), "(string)", basePath); return parse(s.c_str(), "(string)", basePath);
} }

View File

@ -5,7 +5,6 @@
#include "util.hh" #include "util.hh"
#include "archive.hh" #include "archive.hh"
#include "value-to-xml.hh" #include "value-to-xml.hh"
#include "parser.hh"
#include "names.hh" #include "names.hh"
#include <sys/types.h> #include <sys/types.h>
@ -1059,7 +1058,7 @@ void EvalState::createBaseEnv()
/* Add a wrapper around the derivation primop that computes the /* Add a wrapper around the derivation primop that computes the
`drvPath' and `outPath' attributes lazily. */ `drvPath' and `outPath' attributes lazily. */
string s = "attrs: let res = derivationStrict attrs; in attrs // { drvPath = res.drvPath; outPath = res.outPath; type = \"derivation\"; }"; string s = "attrs: let res = derivationStrict attrs; in attrs // { drvPath = res.drvPath; outPath = res.outPath; type = \"derivation\"; }";
mkThunk_(v, parseExprFromString(*this, s, "/")); mkThunk_(v, parseExprFromString(s, "/"));
addConstant("derivation", v); addConstant("derivation", v);
// Paths // Paths

View File

@ -3,7 +3,6 @@
#include "globals.hh" #include "globals.hh"
#include "misc.hh" #include "misc.hh"
#include "shared.hh" #include "shared.hh"
#include "parser.hh"
#include "eval.hh" #include "eval.hh"
#include "help.txt.hh" #include "help.txt.hh"
#include "get-drvs.hh" #include "get-drvs.hh"
@ -129,7 +128,7 @@ static void getAllExprs(EvalState & state,
if (hasSuffix(attrName, ".nix")) if (hasSuffix(attrName, ".nix"))
attrName = string(attrName, 0, attrName.size() - 4); attrName = string(attrName, 0, attrName.size() - 4);
attrs.attrs[state.symbols.create(attrName)] = attrs.attrs[state.symbols.create(attrName)] =
ExprAttrs::AttrDef(parseExprFromFile(state, absPath(path2)), noPos); ExprAttrs::AttrDef(state.parseExprFromFile(absPath(path2)), noPos);
} }
else else
/* `path2' is a directory (with no default.nix in it); /* `path2' is a directory (with no default.nix in it);
@ -141,7 +140,7 @@ static void getAllExprs(EvalState & state,
static Expr * loadSourceExpr(EvalState & state, const Path & path) static Expr * loadSourceExpr(EvalState & state, const Path & path)
{ {
if (isNixExpr(path)) return parseExprFromFile(state, absPath(path)); if (isNixExpr(path)) return state.parseExprFromFile(absPath(path));
/* The path is a directory. Put the Nix expressions in the /* The path is a directory. Put the Nix expressions in the
directory in an attribute set, with the file name of each directory in an attribute set, with the file name of each
@ -354,7 +353,7 @@ static void queryInstSources(EvalState & state,
Expr * e1 = loadSourceExpr(state, instSource.nixExprPath); Expr * e1 = loadSourceExpr(state, instSource.nixExprPath);
foreach (Strings::const_iterator, i, args) { foreach (Strings::const_iterator, i, args) {
Expr * e2 = parseExprFromString(state, *i, absPath(".")); Expr * e2 = state.parseExprFromString(*i, absPath("."));
Expr * call = new ExprApp(e2, e1); Expr * call = new ExprApp(e2, e1);
Value v; state.eval(call, v); Value v; state.eval(call, v);
getDerivations(state, v, "", instSource.autoArgs, elems); getDerivations(state, v, "", instSource.autoArgs, elems);

View File

@ -5,7 +5,6 @@
#include "globals.hh" #include "globals.hh"
#include "shared.hh" #include "shared.hh"
#include "eval.hh" #include "eval.hh"
#include "parser.hh"
#include "profiles.hh" #include "profiles.hh"
@ -24,7 +23,7 @@ DrvInfos queryInstalled(EvalState & state, const Path & userEnv)
if (pathExists(manifestFile)) { if (pathExists(manifestFile)) {
Value v; Value v;
state.eval(parseExprFromFile(state, manifestFile), v); state.evalFile(manifestFile, v);
Bindings bindings; Bindings bindings;
getDerivations(state, v, "", bindings, elems); getDerivations(state, v, "", bindings, elems);
} else if (pathExists(oldManifestFile)) } else if (pathExists(oldManifestFile))
@ -113,7 +112,7 @@ bool createUserEnv(EvalState & state, DrvInfos & elems,
/* Get the environment builder expression. */ /* Get the environment builder expression. */
Value envBuilder; Value envBuilder;
state.eval(parseExprFromFile(state, nixDataDir + "/nix/corepkgs/buildenv"), envBuilder); state.evalFile(nixDataDir + "/nix/corepkgs/buildenv", envBuilder);
/* Construct a Nix expression that calls the user environment /* Construct a Nix expression that calls the user environment
builder with the manifest as argument. */ builder with the manifest as argument. */

View File

@ -1,7 +1,6 @@
#include "globals.hh" #include "globals.hh"
#include "shared.hh" #include "shared.hh"
#include "eval.hh" #include "eval.hh"
#include "parser.hh"
#include "get-drvs.hh" #include "get-drvs.hh"
#include "attr-path.hh" #include "attr-path.hh"
#include "value-to-xml.hh" #include "value-to-xml.hh"
@ -28,7 +27,7 @@ static Expr * parseStdin(EvalState & state)
startNest(nest, lvlTalkative, format("parsing standard input")); startNest(nest, lvlTalkative, format("parsing standard input"));
string s, s2; string s, s2;
while (getline(std::cin, s2)) s += s2 + "\n"; while (getline(std::cin, s2)) s += s2 + "\n";
return parseExprFromString(state, s, absPath(".")); return state.parseExprFromString(s, absPath("."));
} }
@ -139,7 +138,7 @@ void run(Strings args)
foreach (Strings::iterator, i, files) { foreach (Strings::iterator, i, files) {
Path path = absPath(*i); Path path = absPath(*i);
Expr * e = parseExprFromFile(state, path); Expr * e = state.parseExprFromFile(path);
processExpr(state, attrPaths, parseOnly, strict, autoArgs, processExpr(state, attrPaths, parseOnly, strict, autoArgs,
evalOnly, xmlOutput, xmlOutputSourceLocation, e); evalOnly, xmlOutput, xmlOutputSourceLocation, e);
} }