5 public abstract class Grammar extends JS {
7 public JS action = null;
9 // means we call()ed a Grammar that hasn't been bound to a scope yet
10 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
11 throw new Error("this should never happen");
14 private static Object NULL = new Object();
16 public abstract int match(String s, int start, Hash v, JSScope scope) throws JSExn;
17 public int matchAndWrite(final String s, final int start, Hash v, JSScope scope, String key) throws JSExn {
18 final Hash v2 = new Hash();
19 final int ret = match(s, start, v2, scope);
20 Object result = ret == -1 ? NULL : action == null ?
21 s.substring(start, ret) :
22 JS.cloneWithNewParentScope(action, new JSScope(scope) {
23 public Object get(Object key) throws JSExn {
24 Object val = v2.get(key);
25 if (val == NULL) return null;
26 if (val != null) return val;
27 if (key.equals("whole")) return s.substring(start, ret);
28 return super.get(key);
30 }).call(null, null, null, null, 0);
32 Object old = v.get(key);
33 if (old == null || old == NULL) { }
34 else if (old instanceof JSArray) { if (result != NULL) { ((JSArray)old).addElement(result); result = old; } }
35 else if (result != NULL) { JSArray j = new JSArray(); j.addElement(old); j.addElement(result); result = j; }
41 public static class Alternative extends Grammar {
42 private Grammar r1, r2;
43 public Alternative(Grammar r1, Grammar r2) { this.r1 = r1; this.r2 = r2; }
44 public int match(String s, int start, Hash v, JSScope r) throws JSExn {
45 int s1 = r1.match(s, start, v, r);
46 if (s1 != -1) return s1;
47 int s2 = r2.match(s, start, v, r);
48 if (s2 != -1) return s2;
53 public static class Juxtaposition extends Grammar {
54 private Grammar r1, r2;
55 public Juxtaposition(Grammar r1, Grammar r2) { this.r1 = r1; this.r2 = r2; }
56 public int match(String s, int start, Hash v, JSScope r) throws JSExn {
57 int s1 = r1.match(s, start, v, r);
58 if (s1 == -1) return -1;
59 int s2 = r2.match(s, s1, v, r);
60 if (s2 == -1) return -1;
65 public static class Repetition extends Grammar {
68 public Repetition(Grammar r1, int min, int max) { this.r1 = r1; this.min = min; this.max = max; }
69 public int match(String s, int start, Hash v, JSScope r) throws JSExn {
71 for(i=0; i<max; i++) {
72 start = r1.match(s, start, v, r);
73 if (start == -1) return -1;
75 if (i < min) return -1;
80 public static class Literal extends Grammar {
82 public Literal(String str) { this.str = str; }
83 public int match(String s, int start, Hash v, JSScope r) {
84 if (!s.regionMatches(start, str, 0, str.length())) return -1;
85 return start + str.length();
89 public static class Range extends Grammar {
91 public Range(char min, char max) { this.min = min; this.max = max; }
92 public int match(String s, int start, Hash v, JSScope r) throws JSExn {
93 if (!(s.charAt(start) >= min && s.charAt(start) <= max)) return -1;
98 public static class Reference extends Grammar {
100 public Reference(String key) { this.key = key; }
101 public int match(String s, int start, Hash v, JSScope scope) throws JSExn {
102 return ((Grammar)scope.get(key)).matchAndWrite(s, start, v, scope, key);