bugfix in url parsing for HTTP.java
[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     // 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");
12     }
13
14     private static Object NULL = new Object();
15     
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);
29                     }
30                 }).call(null, null, null, null, 0);
31         if (key != null) {
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; }
36             v.put(key, result);
37         }
38         return ret;
39     }
40
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;
49             return -1;
50         }
51     }
52
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;
61             return s2;
62         }
63     }
64
65     public static class Repetition extends Grammar {
66         private Grammar r1;
67         private int min, max;
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 {
70             int i;
71             for(i=0; i<max; i++) {
72                 start = r1.match(s, start, v, r);
73                 if (start == -1) return -1;
74             }
75             if (i < min) return -1;
76             return start;
77         }
78     }
79
80     public static class Literal extends Grammar {
81         String str;
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();
86         }
87     }
88
89     public static class Range extends Grammar {
90         char min, max;
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;
94             return start + 1;
95         }
96     }
97
98     public static class Reference extends Grammar {
99         String key;
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);
103         }
104     }
105 }