about summary refs log tree commit diff
path: root/src/fix-ng/parser.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-10-30T16·18+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-10-30T16·18+0000
commit9f8f39aa3cdb54532a85e41f14985fc6a530fb36 (patch)
treee06ef3485f7f8bd27a7acf1e1ab694c532660218 /src/fix-ng/parser.cc
parente537844f4ebc53df13f52722fb16bbeb1f4cbd18 (diff)
* 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.

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 b2f0ed05df..e0812817af 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);
 }