From 54945a2950174ded83d58336061b4a9990cdbbfd Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sat, 6 Aug 2011 13:02:55 +0000 Subject: * Refactoring: move parseExprFromFile() and parseExprFromString() into the EvalState class. --- src/libexpr/Makefile.am | 2 +- src/libexpr/common-opts.cc | 3 +-- src/libexpr/eval.cc | 9 ++------- src/libexpr/eval.hh | 10 ++++++++++ src/libexpr/parser.hh | 21 --------------------- src/libexpr/parser.y | 24 +++++++++++++++--------- src/libexpr/primops.cc | 3 +-- 7 files changed, 30 insertions(+), 42 deletions(-) delete mode 100644 src/libexpr/parser.hh (limited to 'src/libexpr') diff --git a/src/libexpr/Makefile.am b/src/libexpr/Makefile.am index f9da9ac01b5e..c15975777493 100644 --- a/src/libexpr/Makefile.am +++ b/src/libexpr/Makefile.am @@ -6,7 +6,7 @@ libexpr_la_SOURCES = \ names.cc 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 \ names.hh symbol-table.hh diff --git a/src/libexpr/common-opts.cc b/src/libexpr/common-opts.cc index c364aa9cb599..bab31f4935db 100644 --- a/src/libexpr/common-opts.cc +++ b/src/libexpr/common-opts.cc @@ -1,7 +1,6 @@ #include "common-opts.hh" #include "../libmain/shared.hh" #include "util.hh" -#include "parser.hh" namespace nix { @@ -25,7 +24,7 @@ bool parseOptionArg(const string & arg, Strings::iterator & i, autoArgs.push_back(Attr(state.symbols.create(name), v)); if (arg == "--arg") - state.mkThunk_(*v, parseExprFromString(state, value, absPath("."))); + state.mkThunk_(*v, state.parseExprFromString(value, absPath("."))); else mkString(*v, value); diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index acf5d7a8aed9..5701452f94f1 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -1,5 +1,4 @@ #include "eval.hh" -#include "parser.hh" #include "hash.hh" #include "util.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); - Expr * e = parseTrees[path]; - - if (!e) { - e = parseExprFromFile(*this, path); - parseTrees[path] = e; - } + Expr * e = parseExprFromFile(path); try { + /* !!! Maybe we should cache the evaluation result. */ eval(e, v); } catch (Error & e) { addErrorPrefix(e, "while evaluating the file `%1%':\n", path); diff --git a/src/libexpr/eval.hh b/src/libexpr/eval.hh index 7b7fea934501..e900217fa457 100644 --- a/src/libexpr/eval.hh +++ b/src/libexpr/eval.hh @@ -218,6 +218,13 @@ public: 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 form. */ void evalFile(const Path & path, Value & v); @@ -296,6 +303,9 @@ private: friend class ExprAttrs; friend class ExprLet; + Expr * parse(const char * text, + const Path & path, const Path & basePath); + public: /* Do a deep equality test between two values. That is, list diff --git a/src/libexpr/parser.hh b/src/libexpr/parser.hh deleted file mode 100644 index c8c8ad8090be..000000000000 --- a/src/libexpr/parser.hh +++ /dev/null @@ -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 */ diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index e5c586266f78..a64d327b454b 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -450,11 +450,11 @@ formal namespace nix { -static Expr * parse(EvalState & state, const char * text, +Expr * EvalState::parse(const char * text, const Path & path, const Path & basePath) { yyscan_t scanner; - ParseData data(state.symbols); + ParseData data(symbols); data.basePath = basePath; data.path = path; @@ -466,7 +466,7 @@ static Expr * parse(EvalState & state, const char * text, if (res) throw ParseError(data.error); try { - data.result->bindVars(state.staticBaseEnv); + data.result->bindVars(staticBaseEnv); } catch (Error & e) { 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] == '/'); @@ -493,15 +493,21 @@ Expr * parseExprFromFile(EvalState & state, Path path) if (S_ISDIR(st.st_mode)) path = canonPath(path + "/default.nix"); - /* Read and parse the input file. */ - return parse(state, readFile(path).c_str(), path, dirOf(path)); + /* Read and parse the input file, unless it's already in the parse + 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, - const string & s, const Path & basePath) +Expr * EvalState::parseExprFromString(const string & s, const Path & basePath) { - return parse(state, s.c_str(), "(string)", basePath); + return parse(s.c_str(), "(string)", basePath); } diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 11c67e1383cc..9d226a327017 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -5,7 +5,6 @@ #include "util.hh" #include "archive.hh" #include "value-to-xml.hh" -#include "parser.hh" #include "names.hh" #include @@ -1059,7 +1058,7 @@ void EvalState::createBaseEnv() /* Add a wrapper around the derivation primop that computes the `drvPath' and `outPath' attributes lazily. */ 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); // Paths -- cgit 1.4.1