2003-10-30 18:18:40 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2003-10-31 13:22:56 +02:00
|
|
|
#include <fcntl.h>
|
2003-10-30 18:18:40 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2003-11-18 13:22:29 +02:00
|
|
|
#include "aterm.hh"
|
2003-10-29 18:05:03 +02:00
|
|
|
#include "parser.hh"
|
2004-10-29 14:22:49 +03:00
|
|
|
#include "nixexpr-ast.hh"
|
2003-10-29 18:05:03 +02:00
|
|
|
|
|
|
|
|
2004-01-30 17:21:42 +02:00
|
|
|
struct ParseData
|
2003-10-30 18:18:40 +02:00
|
|
|
{
|
2004-01-30 17:21:42 +02:00
|
|
|
Expr result;
|
2004-04-06 01:27:41 +03:00
|
|
|
Path basePath;
|
|
|
|
Path path;
|
2004-01-30 17:21:42 +02:00
|
|
|
string error;
|
|
|
|
};
|
2003-10-30 18:18:40 +02:00
|
|
|
|
2004-02-02 23:39:33 +02:00
|
|
|
|
2004-01-30 17:21:42 +02:00
|
|
|
extern "C" {
|
2003-11-03 13:59:35 +02:00
|
|
|
|
2004-01-30 17:21:42 +02:00
|
|
|
#include "parser-tab.h"
|
|
|
|
#include "lexer-tab.h"
|
|
|
|
|
2004-02-02 23:39:33 +02:00
|
|
|
/* Callbacks for getting from C to C++. Due to a (small) bug in the
|
|
|
|
GLR code of Bison we cannot currently compile the parser as C++
|
|
|
|
code. */
|
2003-11-03 13:59:35 +02:00
|
|
|
|
2004-02-02 23:39:33 +02:00
|
|
|
void setParseResult(ParseData * data, ATerm t)
|
|
|
|
{
|
|
|
|
data->result = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
ATerm absParsedPath(ParseData * data, ATerm t)
|
|
|
|
{
|
2004-11-03 20:12:03 +02:00
|
|
|
return toATerm(absPath(aterm2String(t), data->basePath));
|
2004-02-02 23:39:33 +02:00
|
|
|
}
|
2004-01-30 17:21:42 +02:00
|
|
|
|
2004-02-02 23:39:33 +02:00
|
|
|
void parseError(ParseData * data, char * error, int line, int column)
|
|
|
|
{
|
2004-04-06 01:27:41 +03:00
|
|
|
data->error = (format("%1%, at `%2%':%3%:%4%")
|
|
|
|
% error % data->path % line % column).str();
|
2004-02-02 23:39:33 +02:00
|
|
|
}
|
2004-01-30 17:21:42 +02:00
|
|
|
|
2004-02-02 23:39:33 +02:00
|
|
|
ATerm fixAttrs(int recursive, ATermList as)
|
|
|
|
{
|
|
|
|
ATermList bs = ATempty, cs = ATempty;
|
|
|
|
ATermList * is = recursive ? &cs : &bs;
|
|
|
|
for (ATermIterator i(as); i; ++i) {
|
|
|
|
ATermList names;
|
2004-02-04 19:23:26 +02:00
|
|
|
Expr src;
|
2004-04-06 01:27:41 +03:00
|
|
|
ATerm pos;
|
2004-10-27 01:54:26 +03:00
|
|
|
if (matchInherit(*i, src, names, pos)) {
|
|
|
|
bool fromScope = matchScope(src);
|
2004-02-04 19:23:26 +02:00
|
|
|
for (ATermIterator j(names); j; ++j) {
|
2004-10-27 01:54:26 +03:00
|
|
|
Expr rhs = fromScope ? makeVar(*j) : makeSelect(src, *j);
|
|
|
|
*is = ATinsert(*is, makeBind(*j, rhs, pos));
|
2004-02-04 19:23:26 +02:00
|
|
|
}
|
|
|
|
} else bs = ATinsert(bs, *i);
|
2004-02-02 23:39:33 +02:00
|
|
|
}
|
|
|
|
if (recursive)
|
2004-10-27 01:54:26 +03:00
|
|
|
return makeRec(bs, cs);
|
2004-02-02 23:39:33 +02:00
|
|
|
else
|
2004-10-27 01:54:26 +03:00
|
|
|
return makeAttrs(bs);
|
2004-02-02 23:39:33 +02:00
|
|
|
}
|
|
|
|
|
2004-04-06 01:27:41 +03:00
|
|
|
const char * getPath(ParseData * data)
|
|
|
|
{
|
|
|
|
return data->path.c_str();
|
|
|
|
}
|
|
|
|
|
2006-05-01 17:01:47 +03:00
|
|
|
Expr unescapeStr(const char * s)
|
|
|
|
{
|
|
|
|
string t;
|
|
|
|
char c;
|
|
|
|
while (c = *s++) {
|
|
|
|
if (c == '\\') {
|
|
|
|
assert(*s);
|
|
|
|
c = *s++;
|
|
|
|
if (c == 'n') t += "\n";
|
|
|
|
else if (c == 'r') t += "\r";
|
|
|
|
else if (c == 't') t += "\t";
|
|
|
|
else t += c;
|
|
|
|
}
|
|
|
|
else t += c;
|
|
|
|
}
|
|
|
|
return makeStr(toATerm(t));
|
2004-01-30 17:21:42 +02:00
|
|
|
}
|
2003-10-30 18:18:40 +02:00
|
|
|
|
2006-05-01 17:01:47 +03:00
|
|
|
int yyparse(yyscan_t scanner, ParseData * data);
|
|
|
|
|
|
|
|
|
|
|
|
} /* end of C functions */
|
|
|
|
|
2003-10-30 18:18:40 +02:00
|
|
|
|
2005-03-10 13:33:46 +02:00
|
|
|
static void checkAttrs(ATermMap & names, ATermList bnds)
|
|
|
|
{
|
|
|
|
for (ATermIterator i(bnds); i; ++i) {
|
|
|
|
ATerm name;
|
|
|
|
Expr e;
|
|
|
|
ATerm pos;
|
|
|
|
if (!matchBind(*i, name, e, pos)) abort(); /* can't happen */
|
|
|
|
if (names.get(name))
|
|
|
|
throw Error(format("duplicate attribute `%1%' at %2%")
|
|
|
|
% aterm2String(name) % showPos(pos));
|
|
|
|
names.set(name, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void checkAttrSets(ATerm e)
|
|
|
|
{
|
|
|
|
ATermList formals;
|
|
|
|
ATerm body, pos;
|
|
|
|
if (matchFunction(e, formals, body, pos)) {
|
|
|
|
ATermMap names;
|
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
ATerm name;
|
|
|
|
Expr deflt;
|
|
|
|
if (!matchNoDefFormal(*i, name) &&
|
|
|
|
!matchDefFormal(*i, name, deflt))
|
|
|
|
abort();
|
|
|
|
if (names.get(name))
|
|
|
|
throw Error(format("duplicate formal function argument `%1%' at %2%")
|
|
|
|
% aterm2String(name) % showPos(pos));
|
|
|
|
names.set(name, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ATermList bnds;
|
|
|
|
if (matchAttrs(e, bnds)) {
|
|
|
|
ATermMap names;
|
|
|
|
checkAttrs(names, bnds);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATermList rbnds, nrbnds;
|
|
|
|
if (matchRec(e, rbnds, nrbnds)) {
|
|
|
|
ATermMap names;
|
|
|
|
checkAttrs(names, rbnds);
|
|
|
|
checkAttrs(names, nrbnds);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ATgetType(e) == AT_APPL) {
|
|
|
|
int arity = ATgetArity(ATgetAFun(e));
|
|
|
|
for (int i = 0; i < arity; ++i)
|
|
|
|
checkAttrSets(ATgetArgument(e, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ATgetType(e) == AT_LIST)
|
|
|
|
for (ATermIterator i((ATermList) e); i; ++i)
|
|
|
|
checkAttrSets(*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-04 18:03:29 +02:00
|
|
|
static Expr parse(EvalState & state,
|
2004-04-06 01:27:41 +03:00
|
|
|
const char * text, const Path & path,
|
2003-11-22 20:45:56 +02:00
|
|
|
const Path & basePath)
|
2003-10-29 18:05:03 +02:00
|
|
|
{
|
2004-01-30 17:21:42 +02:00
|
|
|
yyscan_t scanner;
|
|
|
|
ParseData data;
|
|
|
|
data.basePath = basePath;
|
2004-04-06 01:27:41 +03:00
|
|
|
data.path = path;
|
2004-01-30 17:21:42 +02:00
|
|
|
|
|
|
|
yylex_init(&scanner);
|
|
|
|
yy_scan_string(text, scanner);
|
|
|
|
int res = yyparse(scanner, &data);
|
|
|
|
yylex_destroy(scanner);
|
2003-10-29 18:05:03 +02:00
|
|
|
|
2004-01-30 17:21:42 +02:00
|
|
|
if (res) throw Error(data.error);
|
|
|
|
|
2004-02-03 16:45:34 +02:00
|
|
|
try {
|
2004-08-04 13:59:20 +03:00
|
|
|
checkVarDefs(state.primOps, data.result);
|
2004-02-03 16:45:34 +02:00
|
|
|
} catch (Error & e) {
|
2004-04-06 01:27:41 +03:00
|
|
|
throw Error(format("%1%, in `%2%'") % e.msg() % path);
|
2004-02-03 16:45:34 +02:00
|
|
|
}
|
2005-03-10 13:33:46 +02:00
|
|
|
|
|
|
|
checkAttrSets(data.result);
|
2004-02-03 16:45:34 +02:00
|
|
|
|
2004-01-30 17:21:42 +02:00
|
|
|
return data.result;
|
2003-10-29 18:05:03 +02:00
|
|
|
}
|
2003-11-22 20:45:56 +02:00
|
|
|
|
|
|
|
|
2004-02-04 18:03:29 +02:00
|
|
|
Expr parseExprFromFile(EvalState & state, Path path)
|
2003-11-22 20:45:56 +02:00
|
|
|
{
|
2004-09-10 00:12:53 +03:00
|
|
|
SwitchToOriginalUser sw;
|
|
|
|
|
2003-11-22 20:45:56 +02:00
|
|
|
assert(path[0] == '/');
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Perhaps this is already an imploded parse tree? */
|
|
|
|
Expr e = ATreadFromNamedFile(path.c_str());
|
|
|
|
if (e) return e;
|
|
|
|
#endif
|
|
|
|
|
2004-01-05 18:26:43 +02:00
|
|
|
/* If `path' is a symlink, follow it. This is so that relative
|
|
|
|
path references work. */
|
2003-11-22 20:45:56 +02:00
|
|
|
struct stat st;
|
2004-01-05 18:26:43 +02:00
|
|
|
if (lstat(path.c_str(), &st))
|
|
|
|
throw SysError(format("getting status of `%1%'") % path);
|
|
|
|
if (S_ISLNK(st.st_mode)) path = absPath(readLink(path), dirOf(path));
|
|
|
|
|
|
|
|
/* If `path' refers to a directory, append `/default.nix'. */
|
2003-11-22 20:45:56 +02:00
|
|
|
if (stat(path.c_str(), &st))
|
|
|
|
throw SysError(format("getting status of `%1%'") % path);
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
path = canonPath(path + "/default.nix");
|
|
|
|
|
|
|
|
/* Read the input file. We can't use SGparseFile() because it's
|
|
|
|
broken, so we read the input ourselves and call
|
|
|
|
SGparseString(). */
|
|
|
|
AutoCloseFD fd = open(path.c_str(), O_RDONLY);
|
|
|
|
if (fd == -1) throw SysError(format("opening `%1%'") % path);
|
|
|
|
|
|
|
|
if (fstat(fd, &st) == -1)
|
|
|
|
throw SysError(format("statting `%1%'") % path);
|
|
|
|
|
|
|
|
char text[st.st_size + 1];
|
|
|
|
readFull(fd, (unsigned char *) text, st.st_size);
|
|
|
|
text[st.st_size] = 0;
|
|
|
|
|
2004-04-06 01:27:41 +03:00
|
|
|
return parse(state, text, path, dirOf(path));
|
2003-11-22 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-04 18:03:29 +02:00
|
|
|
Expr parseExprFromString(EvalState & state,
|
|
|
|
const string & s, const Path & basePath)
|
2003-11-22 20:45:56 +02:00
|
|
|
{
|
2004-02-04 18:03:29 +02:00
|
|
|
return parse(state, s.c_str(), "(string)", basePath);
|
2003-11-22 20:45:56 +02:00
|
|
|
}
|