preliminary core conversion
[org.ibex.core.git] / src / org / ibex / util / Grammar.java
1 package org.ibex.util;
2
3 import org.ibex.js.*;
4
5 public abstract class Grammar extends JS {
6
7     public JS action = null;
8
9     // FIXME: Updae for new api
10     
11     // means we call()ed a Grammar that hasn't been bound to a scope yet
12     /*public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
13         throw new Error("this should never happen");
14     }
15
16     private static Object NULL = new Object();
17     
18     public abstract int match(String s, int start, Hash v, JSScope scope) throws JSExn;
19     public int matchAndWrite(final String s, final int start, Hash v, JSScope scope, String key) throws JSExn {
20         final Hash v2 = new Hash();
21         final int ret = match(s, start, v2, scope);
22         Object result = ret == -1 ? NULL : action == null ?
23             s.substring(start, ret) :
24             JS.cloneWithNewParentScope(action, new JSScope(scope) {
25                     public Object get(Object key) throws JSExn {
26                         Object val = v2.get(key);
27                         if (val == NULL) return null;
28                         if (val != null) return val;
29                         if (key.equals("whole")) return s.substring(start, ret);
30                         return super.get(key);
31                     }
32                 }).call(null, null, null, null, 0);
33         if (key != null) {
34             Object old = v.get(key);
35             if (old == null || old == NULL) { }
36             else if (old instanceof JSArray) { if (result != NULL) { ((JSArray)old).addElement(result); result = old; } }
37             else if (result != NULL) { JSArray j = new JSArray(); j.addElement(old); j.addElement(result); result = j; }
38             v.put(key, result);
39         }
40         return ret;
41     }
42
43     public static class Alternative extends Grammar {
44         private Grammar r1, r2;
45         public Alternative(Grammar r1, Grammar r2) { this.r1 = r1; this.r2 = r2; }
46         public int match(String s, int start, Hash v, JSScope r) throws JSExn {
47             int s1 = r1.match(s, start, v, r);
48             if (s1 != -1) return s1;
49             int s2 = r2.match(s, start, v, r);
50             if (s2 != -1) return s2;
51             return -1;
52         }
53     }
54
55     public static class Juxtaposition extends Grammar {
56         private Grammar r1, r2;
57         public Juxtaposition(Grammar r1, Grammar r2) { this.r1 = r1; this.r2 = r2; }
58         public int match(String s, int start, Hash v, JSScope r) throws JSExn {
59             int s1 = r1.match(s, start, v, r);
60             if (s1 == -1) return -1;
61             int s2 = r2.match(s, s1, v, r);
62             if (s2 == -1) return -1;
63             return s2;
64         }
65     }
66
67     public static class Repetition extends Grammar {
68         private Grammar r1;
69         private int min, max;
70         public Repetition(Grammar r1, int min, int max) { this.r1 = r1; this.min = min; this.max = max; }
71         public int match(String s, int start, Hash v, JSScope r) throws JSExn {
72             int i;
73             for(i=0; i<max; i++) {
74                 start = r1.match(s, start, v, r);
75                 if (start == -1) return -1;
76             }
77             if (i < min) return -1;
78             return start;
79         }
80     }
81
82     public static class Literal extends Grammar {
83         String str;
84         public Literal(String str) { this.str = str; }
85         public int match(String s, int start, Hash v, JSScope r) {
86             if (!s.regionMatches(start, str, 0, str.length())) return -1;
87             return start + str.length();
88         }
89     }
90
91     public static class Range extends Grammar {
92         char min, max;
93         public Range(char min, char max) { this.min = min; this.max = max; }
94         public int match(String s, int start, Hash v, JSScope r) throws JSExn {
95             if (!(s.charAt(start) >= min && s.charAt(start) <= max)) return -1;
96             return start + 1;
97         }
98     }
99
100     public static class Reference extends Grammar {
101         String key;
102         public Reference(String key) { this.key = key; }
103         public int match(String s, int start, Hash v, JSScope scope) throws JSExn {
104             return ((Grammar)scope.get(key)).matchAndWrite(s, start, v, scope, key);
105         }
106     }*/
107 }