about summary refs log tree commit diff
path: root/src/fix-ng/parser.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/fix-ng/parser.cc')
-rw-r--r--src/fix-ng/parser.cc55
1 files changed, 53 insertions, 2 deletions
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 <sstream>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
 extern "C" {
 #include <sglr.h>
 #include <asfix2.h>
@@ -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(<str>)", &s)) {
+            string s2(s);
+            return ATmake("Str(<str>)",
+                string(s2, 1, s2.size() - 2).c_str());
+        }
+
+        if (ATmatch(e, "Path(<str>)", &s)) {
+            string path(s);
+            if (path[0] != '/')
+                path = basePath + "/" + path;
+            return ATmake("Str(<str>)", canonPath(path).c_str());
+        }
+
+        if (ATmatch(e, "Int(<str>)", &s)) {
+            istringstream s2(s);
+            int n;
+            s2 >> n;
+            return ATmake("Int(<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);
 }