checkpoint
authoradam <adam@megacz.com>
Thu, 25 May 2006 06:24:43 +0000 (02:24 -0400)
committeradam <adam@megacz.com>
Thu, 25 May 2006 06:24:43 +0000 (02:24 -0400)
darcs-hash:20060525062443-5007d-21539debbd53341633a0f3de06d7ee39b0d2637a.gz

src/edu/berkeley/sbp/misc/Cartesian.java [new file with mode: 0644]
src/edu/berkeley/sbp/misc/Demo.java [new file with mode: 0644]
src/edu/berkeley/sbp/misc/ParserServlet.java [new file with mode: 0644]

diff --git a/src/edu/berkeley/sbp/misc/Cartesian.java b/src/edu/berkeley/sbp/misc/Cartesian.java
new file mode 100644 (file)
index 0000000..6627fbf
--- /dev/null
@@ -0,0 +1,63 @@
+package edu.berkeley.sbp.misc;
+import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+import java.lang.ref.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.Input.Location;
+import edu.berkeley.sbp.util.*;
+
+public class Cartesian {
+
+    public static abstract class Input<Token> implements edu.berkeley.sbp.Input<Token> {
+
+        public abstract Token   next() throws IOException;
+        public abstract boolean isCR();
+
+        long then = 0;
+        private Cartesian.Location location = new Cartesian.Location(0, 1);
+        public  edu.berkeley.sbp.Input.Location    getLocation() { return location; }
+
+        public Token next(int numstates, int resets, int waits) throws IOException {
+            int line  = location.getRow();
+            int col   = location.getCol();
+            Token t = next();
+            if (t==null) return null;
+            String s = "  line "+line+", col " + col;
+            while(s.length() < 20) s += " ";
+            s += "[ambiguity level: " + (numstates-1) + "] [resets: " + resets + "] [waits: " + waits + "]";
+            long now = System.currentTimeMillis();
+            if (now-then > 10) {
+                then = now;
+                System.out.print(s + "                                \r");
+            }
+            if (isCR()) { 
+                line++;
+                col = 1;
+            } else {
+                col++;
+            }
+            location = new Cartesian.Location(col, line);
+            return t;
+        }
+    }
+
+    /** an implementation of Location for a cartesian grid (row, col) */
+    public static class Location<Tok> implements Input.Location<Tok>, Comparable<Input.Location> {
+        protected final int row;
+        protected final int col;
+        public String toString() { return row+":"+col; }
+        public int getCol() { return col; }
+        public int getRow() { return row; }
+        public Location(int col, int row) { this.row = row; this.col = col; }
+        public int compareTo(Input.Location loc) throws ClassCastException {
+            if (!(loc instanceof Cartesian)) throw new ClassCastException();
+            Location<Tok> c = (Location<Tok>)loc;
+            if (row < c.row) return -1;
+            if (row > c.row) return  1;
+            if (col < c.col) return -1;
+            if (col > c.col) return  1;
+            return 0;
+        }
+    }
+}
diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java
new file mode 100644 (file)
index 0000000..9272618
--- /dev/null
@@ -0,0 +1,19 @@
+package edu.berkeley.sbp.misc;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.chr.*;
+import java.util.*;
+import java.io.*;
+
+public class Demo {
+    public static void main(String[] s) throws Exception {
+        Tree<String> gram = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
+        MetaGrammar g = (MetaGrammar)new MetaGrammar().walk(gram);
+        Union meta = g.done();
+        //Tree<String> out = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
+        Forest<String> out = new CharParser(meta).parse(new FileInputStream(s[1]));
+        GraphViz gv = new GraphViz();
+        out.toGraphViz(gv);
+        gv.dump(new PrintWriter(new OutputStreamWriter(System.out)));
+    }
+}
diff --git a/src/edu/berkeley/sbp/misc/ParserServlet.java b/src/edu/berkeley/sbp/misc/ParserServlet.java
new file mode 100644 (file)
index 0000000..47f8e58
--- /dev/null
@@ -0,0 +1,75 @@
+package edu.berkeley.sbp.misc;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.chr.*;
+import java.util.*;
+import java.io.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+public class ParserServlet extends HttpServlet {
+
+    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
+        doPost(req, resp);
+    }
+    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
+        try {
+        try {
+            System.out.println("a");
+           
+            Tree<String> gram =
+                new CharParser(MetaGrammar.make()).parse(new StringReader(req.getParameter("grammar").trim())).expand1();
+            System.out.println("b");
+            MetaGrammar g = (MetaGrammar)new MetaGrammar().walk(gram);
+            Union meta = g.done();
+            Forest<String> out = new CharParser(meta).parse(new StringReader(req.getParameter("input").trim()));
+            System.out.println(out);
+            
+            final GraphViz gv = new GraphViz();
+            out.toGraphViz(gv);
+
+            resp.setContentType("image/svg+xml");
+            final Process proc = Runtime.getRuntime().exec(new String[] { "dot", "-Tsvg" });
+            new Thread() {
+                public void run() {
+                    try {
+                        PrintWriter pw = new PrintWriter(new OutputStreamWriter(proc.getOutputStream()));
+                        gv.dump(pw);
+                        pw.flush();
+                        pw.close();
+                    } catch (Exception e) {
+                        e.printStackTrace(); 
+                   }
+                }
+            }.start();
+
+            byte[] buf = new byte[1024];
+            InputStream is = proc.getInputStream();
+            OutputStream os = resp.getOutputStream();
+
+            while(true) {
+                int numread = is.read(buf, 0, buf.length);
+                if (numread==-1) break;
+                os.write(buf, 0, numread);
+            }
+            os.flush();
+            os.close();
+
+        } catch (ParseFailed e) {
+            e.printStackTrace();
+            resp.setContentType("text/plain");
+            PrintWriter pw = new PrintWriter(new OutputStreamWriter(resp.getOutputStream()));
+            pw.println(e);
+            pw.flush();
+            pw.close();
+        }
+        
+        } catch (Exception e) {
+            throw new ServletException(e);
+        }
+    }
+
+}