2003-10-30 16:18:40 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2003-10-31 11:22:56 +00:00
|
|
|
#include <fcntl.h>
|
2003-10-30 16:18:40 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2003-11-18 11:22:29 +00:00
|
|
|
#include "aterm.hh"
|
2003-10-29 16:05:03 +00:00
|
|
|
#include "parser.hh"
|
2004-10-29 11:22:49 +00:00
|
|
|
#include "nixexpr-ast.hh"
|
2003-10-29 16:05:03 +00:00
|
|
|
|
|
|
|
|
2004-01-30 15:21:42 +00:00
|
|
|
struct ParseData
|
2003-10-30 16:18:40 +00:00
|
|
|
{
|
2004-01-30 15:21:42 +00:00
|
|
|
Expr result;
|
2004-04-05 22:27:41 +00:00
|
|
|
Path basePath;
|
|
|
|
Path path;
|
2004-01-30 15:21:42 +00:00
|
|
|
string error;
|
|
|
|
};
|
2003-10-30 16:18:40 +00:00
|
|
|
|
2004-02-02 21:39:33 +00:00
|
|
|
|
2004-01-30 15:21:42 +00:00
|
|
|
extern "C" {
|
2003-11-03 11:59:35 +00:00
|
|
|
|
2004-01-30 15:21:42 +00:00
|
|
|
#include "parser-tab.h"
|
|
|
|
#include "lexer-tab.h"
|
|
|
|
|
2004-02-02 21:39:33 +00: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 11:59:35 +00:00
|
|
|
|
2004-02-02 21:39:33 +00:00
|
|
|
void setParseResult(ParseData * data, ATerm t)
|
|
|
|
{
|
|
|
|
data->result = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
ATerm absParsedPath(ParseData * data, ATerm t)
|
|
|
|
{
|
2004-11-03 18:12:03 +00:00
|
|
|
return toATerm(absPath(aterm2String(t), data->basePath));
|
2004-02-02 21:39:33 +00:00
|
|
|
}
|
2004-01-30 15:21:42 +00:00
|
|
|
|
2004-02-02 21:39:33 +00:00
|
|
|
void parseError(ParseData * data, char * error, int line, int column)
|
|
|
|
{
|
2004-04-05 22:27:41 +00:00
|
|
|
data->error = (format("%1%, at `%2%':%3%:%4%")
|
|
|
|
% error % data->path % line % column).str();
|
2004-02-02 21:39:33 +00:00
|
|
|
}
|
2004-01-30 15:21:42 +00:00
|
|
|
|
2004-02-02 21:39:33 +00: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 17:23:26 +00:00
|
|
|
Expr src;
|
2004-04-05 22:27:41 +00:00
|
|
|
ATerm pos;
|
2004-10-26 22:54:26 +00:00
|
|
|
if (matchInherit(*i, src, names, pos)) {
|
|
|
|
bool fromScope = matchScope(src);
|
2004-02-04 17:23:26 +00:00
|
|
|
for (ATermIterator j(names); j; ++j) {
|
2004-10-26 22:54:26 +00:00
|
|
|
Expr rhs = fromScope ? makeVar(*j) : makeSelect(src, *j);
|
|
|
|
*is = ATinsert(*is, makeBind(*j, rhs, pos));
|
2004-02-04 17:23:26 +00:00
|
|
|
}
|
|
|
|
} else bs = ATinsert(bs, *i);
|
2004-02-02 21:39:33 +00:00
|
|
|
}
|
|
|
|
if (recursive)
|
2004-10-26 22:54:26 +00:00
|
|
|
return makeRec(bs, cs);
|
2004-02-02 21:39:33 +00:00
|
|
|
else
|
2004-10-26 22:54:26 +00:00
|
|
|
return makeAttrs(bs);
|
2004-02-02 21:39:33 +00:00
|
|
|
}
|
|
|
|
|
2004-04-05 22:27:41 +00:00
|
|
|
const char * getPath(ParseData * data)
|
|
|
|
{
|
|
|
|
return data->path.c_str();
|
|
|
|
}
|
|
|
|
|
2006-05-01 14:01:47 +00:00
|
|
|
Expr unescapeStr(const char * s)
|
|
|
|
{
|
|
|
|
string t;
|
|
|
|
char c;
|
2006-08-04 17:07:13 +00:00
|
|
|
while ((c = *s++)) {
|
2006-05-01 14:01:47 +00:00
|
|
|
if (c == '\\') {
|
|
|
|
assert(*s);
|
|
|
|
c = *s++;
|
2006-08-16 10:28:44 +00:00
|
|
|
if (c == 'n') t += '\n';
|
|
|
|
else if (c == 'r') t += '\r';
|
|
|
|
else if (c == 't') t += '\t';
|
2006-05-01 14:01:47 +00:00
|
|
|
else t += c;
|
|
|
|
}
|
2006-08-16 10:28:44 +00:00
|
|
|
else if (c == '\r') {
|
|
|
|
/* Normalise CR and CR/LF into LF. */
|
|
|
|
t += '\n';
|
|
|
|
if (*s == '\n') s++; /* cr/lf */
|
|
|
|
}
|
2006-05-01 14:01:47 +00:00
|
|
|
else t += c;
|
|
|
|
}
|
|
|
|
return makeStr(toATerm(t));
|
2004-01-30 15:21:42 +00:00
|
|
|
}
|
2003-10-30 16:18:40 +00:00
|
|
|
|
2006-05-01 14:01:47 +00:00
|
|
|
int yyparse(yyscan_t scanner, ParseData * data);
|
|
|
|
|
|
|
|
|
|
|
|
} /* end of C functions */
|
|
|
|
|
2003-10-30 16:18:40 +00:00
|
|
|
|
2005-03-10 11:33:46 +00: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))
|
2006-07-19 15:36:15 +00:00
|
|
|
throw EvalError(format("duplicate attribute `%1%' at %2%")
|
2005-03-10 11:33:46 +00:00
|
|
|
% aterm2String(name) % showPos(pos));
|
|
|
|
names.set(name, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void checkAttrSets(ATerm e)
|
|
|
|
{
|
|
|
|
ATermList formals;
|
|
|
|
ATerm body, pos;
|
|
|
|
if (matchFunction(e, formals, body, pos)) {
|
2006-05-04 12:21:08 +00:00
|
|
|
ATermMap names(ATgetLength(formals));
|
2005-03-10 11:33:46 +00:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
ATerm name;
|
2006-07-24 15:16:03 +00:00
|
|
|
ATerm d1, d2;
|
|
|
|
if (!matchFormal(*i, name, d1, d2)) abort();
|
2005-03-10 11:33:46 +00:00
|
|
|
if (names.get(name))
|
2006-07-19 15:36:15 +00:00
|
|
|
throw EvalError(format("duplicate formal function argument `%1%' at %2%")
|
2005-03-10 11:33:46 +00:00
|
|
|
% aterm2String(name) % showPos(pos));
|
|
|
|
names.set(name, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ATermList bnds;
|
|
|
|
if (matchAttrs(e, bnds)) {
|
2006-05-04 12:21:08 +00:00
|
|
|
ATermMap names(ATgetLength(bnds));
|
2005-03-10 11:33:46 +00:00
|
|
|
checkAttrs(names, bnds);
|
|
|
|
}
|
|
|
|
|
|
|
|
ATermList rbnds, nrbnds;
|
|
|
|
if (matchRec(e, rbnds, nrbnds)) {
|
2006-05-04 12:21:08 +00:00
|
|
|
ATermMap names(ATgetLength(rbnds) + ATgetLength(nrbnds));
|
2005-03-10 11:33:46 +00:00
|
|
|
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 16:03:29 +00:00
|
|
|
static Expr parse(EvalState & state,
|
2004-04-05 22:27:41 +00:00
|
|
|
const char * text, const Path & path,
|
2003-11-22 18:45:56 +00:00
|
|
|
const Path & basePath)
|
2003-10-29 16:05:03 +00:00
|
|
|
{
|
2004-01-30 15:21:42 +00:00
|
|
|
yyscan_t scanner;
|
|
|
|
ParseData data;
|
|
|
|
data.basePath = basePath;
|
2004-04-05 22:27:41 +00:00
|
|
|
data.path = path;
|
2004-01-30 15:21:42 +00:00
|
|
|
|
|
|
|
yylex_init(&scanner);
|
|
|
|
yy_scan_string(text, scanner);
|
|
|
|
int res = yyparse(scanner, &data);
|
|
|
|
yylex_destroy(scanner);
|
2003-10-29 16:05:03 +00:00
|
|
|
|
2006-07-19 15:36:15 +00:00
|
|
|
if (res) throw EvalError(data.error);
|
2004-01-30 15:21:42 +00:00
|
|
|
|
2004-02-03 14:45:34 +00:00
|
|
|
try {
|
2004-08-04 10:59:20 +00:00
|
|
|
checkVarDefs(state.primOps, data.result);
|
2004-02-03 14:45:34 +00:00
|
|
|
} catch (Error & e) {
|
2006-07-19 15:36:15 +00:00
|
|
|
throw EvalError(format("%1%, in `%2%'") % e.msg() % path);
|
2004-02-03 14:45:34 +00:00
|
|
|
}
|
2005-03-10 11:33:46 +00:00
|
|
|
|
|
|
|
checkAttrSets(data.result);
|
2004-02-03 14:45:34 +00:00
|
|
|
|
2004-01-30 15:21:42 +00:00
|
|
|
return data.result;
|
2003-10-29 16:05:03 +00:00
|
|
|
}
|
2003-11-22 18:45:56 +00:00
|
|
|
|
|
|
|
|
2004-02-04 16:03:29 +00:00
|
|
|
Expr parseExprFromFile(EvalState & state, Path path)
|
2003-11-22 18:45:56 +00:00
|
|
|
{
|
2004-09-09 21:12:53 +00:00
|
|
|
SwitchToOriginalUser sw;
|
|
|
|
|
2003-11-22 18:45:56 +00: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 16:26:43 +00:00
|
|
|
/* If `path' is a symlink, follow it. This is so that relative
|
|
|
|
path references work. */
|
2003-11-22 18:45:56 +00:00
|
|
|
struct stat st;
|
2004-01-05 16:26:43 +00: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 18:45:56 +00: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-05 22:27:41 +00:00
|
|
|
return parse(state, text, path, dirOf(path));
|
2003-11-22 18:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-04 16:03:29 +00:00
|
|
|
Expr parseExprFromString(EvalState & state,
|
|
|
|
const string & s, const Path & basePath)
|
2003-11-22 18:45:56 +00:00
|
|
|
{
|
2004-02-04 16:03:29 +00:00
|
|
|
return parse(state, s.c_str(), "(string)", basePath);
|
2003-11-22 18:45:56 +00:00
|
|
|
}
|