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-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;
|
2003-10-30 16:18:40 +00:00
|
|
|
string basePath;
|
2004-01-30 15:21:42 +00:00
|
|
|
string location;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return string2ATerm(absPath(aterm2String(t), data->basePath).c_str());
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
data->error = (format("%1%, at line %2%, column %3%, of %4%")
|
|
|
|
% error % line % column % data->location).str();
|
|
|
|
}
|
2004-01-30 15:21:42 +00:00
|
|
|
|
2004-02-02 21:39:33 +00:00
|
|
|
ATerm fixAttrs(int recursive, ATermList as)
|
|
|
|
{
|
|
|
|
ATMatcher m;
|
|
|
|
ATermList bs = ATempty, cs = ATempty;
|
|
|
|
ATermList * is = recursive ? &cs : &bs;
|
|
|
|
for (ATermIterator i(as); i; ++i) {
|
|
|
|
ATermList names;
|
|
|
|
if (atMatch(m, *i) >> "Inherit" >> names)
|
|
|
|
for (ATermIterator j(names); j; ++j)
|
|
|
|
*is = ATinsert(*is,
|
|
|
|
ATmake("Bind(<term>, Var(<term>))", *j, *j));
|
|
|
|
else bs = ATinsert(bs, *i);
|
|
|
|
}
|
|
|
|
if (recursive)
|
|
|
|
return ATmake("Rec(<term>, <term>)", bs, cs);
|
|
|
|
else
|
|
|
|
return ATmake("Attrs(<term>)", bs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int yyparse(yyscan_t scanner, ParseData * data);
|
2004-01-30 15:21:42 +00:00
|
|
|
}
|
2003-10-30 16:18:40 +00:00
|
|
|
|
|
|
|
|
2003-11-22 18:45:56 +00:00
|
|
|
static Expr parse(const char * text, const string & location,
|
|
|
|
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;
|
|
|
|
data.location = location;
|
|
|
|
|
|
|
|
yylex_init(&scanner);
|
|
|
|
yy_scan_string(text, scanner);
|
|
|
|
int res = yyparse(scanner, &data);
|
|
|
|
yylex_destroy(scanner);
|
2003-10-29 16:05:03 +00:00
|
|
|
|
2004-01-30 15:21:42 +00:00
|
|
|
if (res) throw Error(data.error);
|
|
|
|
|
|
|
|
return data.result;
|
2003-10-29 16:05:03 +00:00
|
|
|
}
|
2003-11-22 18:45:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
Expr parseExprFromFile(Path path)
|
|
|
|
{
|
|
|
|
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-01-30 15:21:42 +00:00
|
|
|
return parse(text, "`" + path + "'", dirOf(path));
|
2003-11-22 18:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr parseExprFromString(const string & s, const Path & basePath)
|
|
|
|
{
|
|
|
|
return parse(s.c_str(), "(string)", basePath);
|
|
|
|
}
|