diff --git a/src/fix-ng/Makefile.am b/src/fix-ng/Makefile.am index 3672c3dc9..88f1f4fe9 100644 --- a/src/fix-ng/Makefile.am +++ b/src/fix-ng/Makefile.am @@ -1,6 +1,6 @@ bin_PROGRAMS = fix-ng -fix_ng_SOURCES = fix.cc parser.cc +fix_ng_SOURCES = fix-expr.cc parser.cc fix.cc fix_ng_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../boost/format/libformat.a \ -L../../externals/inst/lib -ldb_cxx -lsglr -lATB -lconversion -lasfix2 -lmept -lATerm diff --git a/src/fix-ng/fix-expr.cc b/src/fix-ng/fix-expr.cc index 00795da4c..8d47817ff 100644 --- a/src/fix-ng/fix-expr.cc +++ b/src/fix-ng/fix-expr.cc @@ -29,5 +29,5 @@ ATerm bottomupRewrite(TermFun & f, ATerm e) return (ATerm) ATreverse(out); } - throw badTerm("cannot rewrite", e); + return e; } diff --git a/src/fix-ng/parser.cc b/src/fix-ng/parser.cc index b2f0ed05d..e0812817a 100644 --- a/src/fix-ng/parser.cc +++ b/src/fix-ng/parser.cc @@ -1,3 +1,9 @@ +#include + +#include +#include +#include + extern "C" { #include #include @@ -5,15 +11,58 @@ extern "C" { #include "parser.hh" #include "shared.hh" +#include "fix-expr.hh" #include "expr.hh" #include "parse-table.h" -Expr parseExprFromFile(const Path & path) +struct Cleanup : TermFun { + string basePath; + + virtual ATerm operator () (ATerm e) + { + char * s; + + if (ATmatch(e, "Str()", &s)) { + string s2(s); + return ATmake("Str()", + string(s2, 1, s2.size() - 2).c_str()); + } + + if (ATmatch(e, "Path()", &s)) { + string path(s); + if (path[0] != '/') + path = basePath + "/" + path; + return ATmake("Str()", canonPath(path).c_str()); + } + + if (ATmatch(e, "Int()", &s)) { + istringstream s2(s); + int n; + s2 >> n; + return ATmake("Int()", n); + } + + return e; + } +}; + + +Expr parseExprFromFile(Path path) +{ +#if 0 /* Perhaps this is already an imploded parse tree? */ Expr e = ATreadFromNamedFile(path.c_str()); if (e) return e; +#endif + + /* If `path' refers to a directory, append `/default.fix'. */ + struct stat st; + if (stat(path.c_str(), &st)) + throw SysError(format("getting status of `%1%'") % path); + if (S_ISDIR(st.st_mode)) + path = canonPath(path + "/default.fix"); /* Initialise the SDF libraries. */ static bool initialised = false; @@ -72,5 +121,7 @@ Expr parseExprFromFile(const Path & path) if (!imploded) throw Error(format("cannot implode parse tree")); - return imploded; + Cleanup cleanup; + cleanup.basePath = dirOf(path); + return bottomupRewrite(cleanup, imploded); } diff --git a/src/fix-ng/parser.hh b/src/fix-ng/parser.hh index 80e266f2d..c56a339a3 100644 --- a/src/fix-ng/parser.hh +++ b/src/fix-ng/parser.hh @@ -1,15 +1,10 @@ #ifndef __PARSER_H #define __PARSER_H -#include -#include - -#include "util.hh" +#include "fix-expr.hh" -typedef ATerm Expr; - -Expr parseExprFromFile(const Path & path); +Expr parseExprFromFile(Path path); #endif /* !__PARSER_H */