From 9f8f39aa3cdb54532a85e41f14985fc6a530fb36 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 30 Oct 2003 16:18:40 +0000 Subject: * Clean up the imploded parse tree. Quotes around strings are removed, paths are absolutised relative to the path containing the expression we just parsed, and integer literals are converted to actual integers. --- src/fix-ng/parser.cc | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'src/fix-ng/parser.cc') diff --git a/src/fix-ng/parser.cc b/src/fix-ng/parser.cc index b2f0ed05df50..e0812817af59 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); } -- cgit 1.4.1