use >> and << for indent/dedent
authoradam <adam@megacz.com>
Sun, 27 May 2007 20:25:06 +0000 (16:25 -0400)
committeradam <adam@megacz.com>
Sun, 27 May 2007 20:25:06 +0000 (16:25 -0400)
darcs-hash:20070527202506-5007d-8407ad1f48c5283a2c890469a9d0edad8f8fbe21.gz

src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/chr/CharAtom.java
src/edu/berkeley/sbp/meta/GrammarAST.java
tests/meta.g

index 99e2d08..a3730b6 100644 (file)
@@ -66,10 +66,10 @@ public abstract class Parser<Token, NodeType> {
         _last = c;
         switch(c) {
             case edu.berkeley.sbp.chr.CharAtom.left:
         _last = c;
         switch(c) {
             case edu.berkeley.sbp.chr.CharAtom.left:
-                buf += "\033[31m{\033[0m";
+                buf += "\033[31m>\033[0m";
                 break;
             case edu.berkeley.sbp.chr.CharAtom.right:
                 break;
             case edu.berkeley.sbp.chr.CharAtom.right:
-                buf += "\033[31m}\033[0m";
+                buf += "\033[31m<\033[0m";
                 break;
             case -1: // FIXME 
             case '\n':
                 break;
             case -1: // FIXME 
             case '\n':
index 45c85b9..ee31675 100644 (file)
@@ -23,9 +23,9 @@ public class CharAtom extends Atom<Character> {
 
     public static final char left       = (char)9998;
     public static final char right      = (char)9999;
 
     public static final char left       = (char)9998;
     public static final char right      = (char)9999;
-    public static final Atom leftBrace  = new CharAtom(left,left)   { public String toString() { return "\\{"; } };
-    public static final Atom rightBrace = new CharAtom(right,right) { public String toString() { return "\\}"; } };
-    public static final Atom braces     = new CharAtom(left,right)   { public String toString() { return "[\\{\\}]"; } };
+    public static final Atom leftBrace  = new CharAtom(left,left)   { public String toString() { return ">>"; } };
+    public static final Atom rightBrace = new CharAtom(right,right) { public String toString() { return "<<"; } };
+    //public static final Atom braces     = new CharAtom(left,right)   { public String toString() { return "[\\{\\}]"; } };
 
     public static Atom set(Range.Set r) { return new CharAtom(new CharTopology(r)); }
     public String toString() { return t.toString(); }
 
     public static Atom set(Range.Set r) { return new CharAtom(new CharTopology(r)); }
     public String toString() { return t.toString(); }
index ca236e9..997d0b6 100644 (file)
@@ -100,25 +100,28 @@ public class GrammarAST {
             return ret;
         }
         if (head.equals("Quoted")) return stringifyChildren(t);
             return ret;
         }
         if (head.equals("Quoted")) return stringifyChildren(t);
-        if (head.equals("Literal")) return new LiteralNode((String)walk(t.child(0)));
-        if (head.equals("->")) return arrow((Seq)walk(t.child(0)), (ElementNode)walk(t.child(1)));
-        if (head.equals("DropNT")) return new NonTerminalNode((String)walk(t.child(0)), (Seq[][])walkChildren(t.child(1)), false, null, true);
-        if (head.equals("=") && t.size()==2) return new NonTerminalNode((String)walk(t.child(0)), (Seq[][])walk(t.child(1)), true, null, false);
-        if (head.equals("=")) return new NonTerminalNode((String)walk(t.child(0)), (Seq[][])walk(t.child(2)), true, (String)walk(t.child(1)), false);
-        if (head.equals("&")) return and2((Seq)walk(t.child(0)), (Seq)walk(t.child(1)));
-        if (head.equals("&~")) return andnot2((Seq)walk(t.child(0)), (Seq)walk(t.child(1)));
-        if (head.equals("/")) return ((Seq)walk(t.child(0))).separate((ElementNode)walk(t.child(1)));
-        if (head.equals("()")) return epsilon;
-        if (head.equals("[")) return new AtomNode((AtomNodeRange[])Reflection.rebuild(walkChildren(t), AtomNodeRange[].class));
-        if (head.equals("\\{")) return new DropNode(new AtomNode(new AtomNodeRange(CharAtom.left, CharAtom.left)));
-        if (head.equals("\\}")) return new DropNode(new AtomNode(new AtomNodeRange(CharAtom.right, CharAtom.right)));
-        if (head.equals("~")) return new TildeNode((ElementNode)walk(t.child(0)));
-        if (head.equals("~~")) {
-            Seq seq = new Seq(star(new TildeNode(new AtomNode())));
-            return seq.andnot((Seq)walk(t.child(0)));
-        }
-        if (head.equals("Range") && t.size()==1) return new AtomNodeRange(unescape(t).charAt(0));
-        if (head.equals("Range")) return new AtomNodeRange(unescape(t).charAt(0), unescape(t).charAt(1));
+        if (head.equals("Literal")) return new LiteralNode(walkString(t.child(0)));
+        if (head.equals("->")) return walkSeq(t.child(0)).follow(walkElement(t.child(1)));
+        if (head.equals("DropNT")) return new NonTerminalNode(walkString(t.child(0)), (Seq[][])walkChildren(t.child(1)), false, null, true);
+        if (head.equals("=")) return new NonTerminalNode(walkString(t.child(0)), (Seq[][])walk(t.child(2)),
+                                                         true, t.size()==2 ? null : walkString(t.child(1)), false);
+        if (head.equals("&"))   return and2(walkSeq(t.child(0)), walkSeq(t.child(1)));
+        if (head.equals("&~"))  return andnot2(walkSeq(t.child(0)), walkSeq(t.child(1)));
+        if (head.equals("/"))   return (walkSeq(t.child(0))).separate(walkElement(t.child(1)));
+        if (head.equals("()"))  return new LiteralNode("");
+        if (head.equals("["))   return new AtomNode((char[][])Reflection.rebuild(walkChildren(t), char[][].class));
+        if (head.equals("\\{")) return new DropNode(new AtomNode(new char[] { CharAtom.left, CharAtom.left }));
+        if (head.equals("\\}")) return new DropNode(new AtomNode(new char[] { CharAtom.right, CharAtom.right }));
+        if (head.equals(">>"))  return new DropNode(new AtomNode(new char[] { CharAtom.left, CharAtom.left }));
+        if (head.equals("<<"))  return new DropNode(new AtomNode(new char[] { CharAtom.right, CharAtom.right }));
+        if (head.equals("~"))   return new TildeNode(walkElement(t.child(0)));
+        if (head.equals("~~"))  return new Seq(new RepeatNode(new TildeNode(new AtomNode()), null, true,  true,  false)).andnot(walkSeq(t.child(0)));
+        if (head.equals("Range") && t.size()==2 && ">".equals(t.child(0).head()))
+            return new char[] { CharAtom.left, CharAtom.left };
+        if (head.equals("Range") && t.size()==2 && "<".equals(t.child(0).head()))
+            return new char[] { CharAtom.right, CharAtom.right };
+        if (head.equals("Range") && t.size()==1) return new char[] { unescape(t).charAt(0), unescape(t).charAt(0) };
+        if (head.equals("Range")) return new char[] { unescape(t).charAt(0), unescape(t).charAt(1) };
         if (head.equals("\"\"")) return "";
         if (head.equals("\n")) return "\n";
         if (head.equals("\r")) return "\r";
         if (head.equals("\"\"")) return "";
         if (head.equals("\n")) return "\n";
         if (head.equals("\r")) return "\r";
index 638fa71..f06eb2f 100644 (file)
@@ -15,7 +15,7 @@ NonTerminal   = NonTerminal:: Word            ws  "=" ws RHS
               |               Word  "*"       ws ^"=" ws RHS
               |               Word  "*/" Word ws ^"=" ws RHS
        
               |               Word  "*"       ws ^"=" ws RHS
               |               Word  "*/" Word ws ^"=" ws RHS
        
-RHS::         = ("|":: Sequence +/ (ws "|" ws)) +/ (ws ">" ws)
+RHS::         = ("|":: Sequence +/ (ws "|" ws)) +/ (ws (">" -> ~">") ws)
 
 Elements::    = e*/ws
 
 
 Elements::    = e*/ws
 
@@ -53,6 +53,8 @@ e             =                                   e ^"!"
               |                 "~":: ("~" -> ~"~")!  e
               |                  ^"\\{"
               |                  ^"\\}"
               |                 "~":: ("~" -> ~"~")!  e
               |                  ^"\\{"
               |                  ^"\\}"
+              |                  ^">>"
+              |                  ^"<<"
 
 Word::        = [.a-zA-Z0-9_]++ &~ "."+
 Quoted::      = "\"" (~[\"\\] | escaped)+ "\""
 
 Word::        = [.a-zA-Z0-9_]++ &~ "."+
 Quoted::      = "\"" (~[\"\\] | escaped)+ "\""
@@ -60,8 +62,12 @@ Quoted::      = "\"" (~[\"\\] | escaped)+ "\""
 
 Range::       = ec
               | ec "-" ec
 
 Range::       = ec
               | ec "-" ec
+              | "<<":: [<][<]
+              | ">>":: [>][>]
 
 
-ec            = ~[\-\]\\]
+ec            = ~[\-\]\\<>]
+              | [>] -> ~[>]
+              | [<] -> ~[<]
               | escaped
 escaped       = "\n"::  "\\n"
               | "\r"::  "\\r"
               | escaped
 escaped       = "\n"::  "\\n"
               | "\r"::  "\\r"