5 public abstract class Grammar extends JS {
7 public JS action = null;
9 // FIXME: Updae for new api
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");
16 private static Object NULL = new Object();
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);
32 }).call(null, null, null, null, 0);
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; }
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;
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;
67 public static class Repetition extends Grammar {
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 {
73 for(i=0; i<max; i++) {
74 start = r1.match(s, start, v, r);
75 if (start == -1) return -1;
77 if (i < min) return -1;
82 public static class Literal extends Grammar {
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();
91 public static class Range extends Grammar {
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;
100 public static class Reference extends Grammar {
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);