JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / com / stevesoft / pat / ReplaceRule.js
1 Clazz.declarePackage ("com.stevesoft.pat");\r
2 Clazz.load (["java.util.Hashtable"], "com.stevesoft.pat.ReplaceRule", ["com.stevesoft.pat.Ctrl", "$.Regex", "$.Transformer", "java.lang.StringBuffer"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.next = null;\r
5 this.name = null;\r
6 Clazz.instantialize (this, arguments);\r
7 }, com.stevesoft.pat, "ReplaceRule");\r
8 Clazz.prepareFields (c$, function () {\r
9 this.name = this.getClass ().getName ();\r
10 });\r
11 Clazz.defineMethod (c$, "clone1", \r
12 function () {\r
13 return  new com.stevesoft.pat.RuleHolder (this);\r
14 });\r
15 Clazz.overrideMethod (c$, "clone", \r
16 function () {\r
17 var x = this.clone1 ();\r
18 var xsav = x;\r
19 var y = this;\r
20 while (y.next != null) {\r
21 x.next = y.next.clone1 ();\r
22 x.name = y.name;\r
23 x = x.next;\r
24 y = y.next;\r
25 }\r
26 return xsav;\r
27 });\r
28 c$.add = Clazz.defineMethod (c$, "add", \r
29 function (head, adding) {\r
30 if (head == null) {\r
31 return head = adding;\r
32 }head.addRule (adding);\r
33 return head;\r
34 }, "com.stevesoft.pat.ReplaceRule,com.stevesoft.pat.ReplaceRule");\r
35 Clazz.defineMethod (c$, "add", \r
36 function (adding) {\r
37 return com.stevesoft.pat.ReplaceRule.add (this, adding);\r
38 }, "com.stevesoft.pat.ReplaceRule");\r
39 Clazz.defineMethod (c$, "addRule", \r
40 function (r) {\r
41 if (this.next == null) {\r
42 this.next = r;\r
43 } else {\r
44 this.next.addRule (r);\r
45 }}, "com.stevesoft.pat.ReplaceRule");\r
46 c$.getv = Clazz.defineMethod (c$, "getv", \r
47 ($fz = function () {\r
48 if (com.stevesoft.pat.ReplaceRule.getvar != null) {\r
49 return com.stevesoft.pat.ReplaceRule.getvar.clone ();\r
50 }com.stevesoft.pat.ReplaceRule.getvar =  new com.stevesoft.pat.Regex ("(?:\\\\(\\d+)|\\$(?:(\\d+)|(\\w+)|([&\'`])|\\{(?:(\\d+)|([^\n}\\\\]+))})|\\\\([nrbtaef])|\\\\c([\u0000-\uffff])|\\\\x([A-Fa-f0-9]{2})|\\\\([\u0000-\uffff]))", "");\r
51 com.stevesoft.pat.ReplaceRule.getvar.optimize ();\r
52 return com.stevesoft.pat.ReplaceRule.getvar;\r
53 }, $fz.isPrivate = true, $fz));\r
54 c$.perlCode = Clazz.defineMethod (c$, "perlCode", \r
55 function (s) {\r
56 try {\r
57 var mf = 0;\r
58 var mt = 0;\r
59 var gv = com.stevesoft.pat.ReplaceRule.getv ();\r
60 var head = null;\r
61 var tmp = null;\r
62 while (gv.searchFrom (s, mt)) {\r
63 var off = com.stevesoft.pat.Regex.BackRefOffset - 1;\r
64 mf = gv.matchedFrom ();\r
65 if (mf > mt) {\r
66 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule (s.substring (mt, mf)));\r
67 }var $var = null;\r
68 if (($var = gv.stringMatchedI (1 + off)) != null || ($var = gv.stringMatchedI (2 + off)) != null || ($var = gv.stringMatchedI (5 + off)) != null) {\r
69 var d = 0;\r
70 for (var i = 0; i < $var.length; i++) {\r
71 d = 8 * d + ($var.charCodeAt (i) - 48);\r
72 }\r
73 if ($var.length == 1) {\r
74 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.BackRefRule (d));\r
75 } else {\r
76 head =  new com.stevesoft.pat.StringRule ("" + String.fromCharCode (d));\r
77 }} else if (($var = gv.stringMatchedI (10 + off)) != null) {\r
78 if ("QELlUu".indexOf ($var) >= 0) {\r
79 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.CodeRule ($var.charAt (0)));\r
80 } else {\r
81 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ($var));\r
82 }} else if (($var = gv.stringMatchedI (3 + off)) != null || ($var = gv.stringMatchedI (4 + off)) != null || ($var = gv.stringMatchedI (6 + off)) != null) {\r
83 var arg = "";\r
84 var pc;\r
85 if ((pc = $var.indexOf (':')) > 0) {\r
86 arg = $var.substring (pc + 1);\r
87 $var = $var.substring (0, pc);\r
88 }if ($var.equals ("&") || $var.equals ("MATCH")) {\r
89 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.AmpersandRule ());\r
90 } else if ($var.equals ("`") || $var.equals ("PREMATCH")) {\r
91 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.LeftRule ());\r
92 } else if ($var.equals ("'") || $var.equals ("POSTMATCH")) {\r
93 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.RightRule ());\r
94 } else if ($var.equals ("WANT_MORE_TEXT")) {\r
95 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.WantMoreTextReplaceRule ());\r
96 } else if ($var.equals ("POP")) {\r
97 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.PopRule ());\r
98 } else if ($var.startsWith ("+") && (tmp = com.stevesoft.pat.ReplaceRule.defs.get ($var.substring (1))) != null) {\r
99 if (Clazz.instanceOf (tmp, com.stevesoft.pat.Regex)) {\r
100 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.PushRule ($var.substring (1), tmp));\r
101 } else if (Clazz.instanceOf (tmp, com.stevesoft.pat.Transformer)) {\r
102 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.PushRule ($var.substring (1), tmp));\r
103 } else {\r
104 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("${" + $var + "}"));\r
105 }} else if ($var.startsWith ("=") && (tmp = com.stevesoft.pat.ReplaceRule.defs.get ($var.substring (1))) != null) {\r
106 if (Clazz.instanceOf (tmp, com.stevesoft.pat.Regex)) {\r
107 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.ChangeRule ($var.substring (1), tmp));\r
108 } else if (Clazz.instanceOf (tmp, com.stevesoft.pat.Transformer)) {\r
109 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.ChangeRule ($var.substring (1), tmp));\r
110 } else {\r
111 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("${" + $var + "}"));\r
112 }} else if ((tmp = com.stevesoft.pat.ReplaceRule.defs.get ($var)) != null) {\r
113 if (Clazz.instanceOf (tmp, com.stevesoft.pat.ReplaceRule)) {\r
114 var alt = (tmp).arg (arg);\r
115 if (alt == null) {\r
116 alt = (tmp);\r
117 }head = com.stevesoft.pat.ReplaceRule.add (head, (alt.clone ()));\r
118 }} else {\r
119 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("${" + $var + "}"));\r
120 }} else if (($var = gv.stringMatchedI (7 + off)) != null) {\r
121 var c = $var.charAt (0);\r
122 if (c == 'n') {\r
123 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("\n"));\r
124 } else if (c == 't') {\r
125 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("\t"));\r
126 } else if (c == 'r') {\r
127 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("\r"));\r
128 } else if (c == 'b') {\r
129 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("\r"));\r
130 } else if (c == 'a') {\r
131 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("\u0007"));\r
132 } else if (c == 'e') {\r
133 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("\u001b"));\r
134 } else if (c == 'f') {\r
135 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("\f"));\r
136 }} else if (($var = gv.stringMatchedI (8 + off)) != null) {\r
137 var c = $var.charAt (0);\r
138 if (c.charCodeAt (0) < com.stevesoft.pat.Ctrl.cmap.length) {\r
139 c = com.stevesoft.pat.Ctrl.cmap[c.charCodeAt (0)];\r
140 }head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("" + c));\r
141 } else if (($var = gv.stringMatchedI (9 + off)) != null) {\r
142 var d = 16 * com.stevesoft.pat.ReplaceRule.getHexDigit ($var.charAt (0)) + com.stevesoft.pat.ReplaceRule.getHexDigit ($var.charAt (1));\r
143 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule ("" + String.fromCharCode (d)));\r
144 }mt = gv.matchedTo ();\r
145 }\r
146 if (mt <= s.length) {\r
147 head = com.stevesoft.pat.ReplaceRule.add (head,  new com.stevesoft.pat.StringRule (s.substring (mt)));\r
148 }return head;\r
149 } finally {\r
150 }\r
151 }, "~S");\r
152 c$.isDefined = Clazz.defineMethod (c$, "isDefined", \r
153 function (s) {\r
154 return com.stevesoft.pat.ReplaceRule.defs.get (s) != null;\r
155 }, "~S");\r
156 c$.define = Clazz.defineMethod (c$, "define", \r
157 function (s, r) {\r
158 com.stevesoft.pat.ReplaceRule.defs.put (s, r);\r
159 }, "~S,com.stevesoft.pat.Regex");\r
160 c$.define = Clazz.defineMethod (c$, "define", \r
161 function (s, r) {\r
162 com.stevesoft.pat.ReplaceRule.defs.put (s, r);\r
163 r.name = s;\r
164 }, "~S,com.stevesoft.pat.ReplaceRule");\r
165 c$.define = Clazz.defineMethod (c$, "define", \r
166 function (s, t) {\r
167 com.stevesoft.pat.ReplaceRule.defs.put (s, t);\r
168 }, "~S,com.stevesoft.pat.Transformer");\r
169 c$.undefine = Clazz.defineMethod (c$, "undefine", \r
170 function (s) {\r
171 com.stevesoft.pat.ReplaceRule.defs.remove (s);\r
172 }, "~S");\r
173 Clazz.defineMethod (c$, "toString1", \r
174 function () {\r
175 return "${" + this.name + "}";\r
176 });\r
177 Clazz.overrideMethod (c$, "toString", \r
178 function () {\r
179 var sb =  new StringBuffer ();\r
180 sb.append (this.toString1 ());\r
181 var rr = this.next;\r
182 while (rr != null) {\r
183 sb.append (rr.toString1 ());\r
184 rr = rr.next;\r
185 }\r
186 return sb.toString ();\r
187 });\r
188 Clazz.defineMethod (c$, "arg", \r
189 function (s) {\r
190 return null;\r
191 }, "~S");\r
192 c$.getHexDigit = Clazz.defineMethod (c$, "getHexDigit", \r
193 function (c) {\r
194 if (c >= '0' && c <= '9') {\r
195 return c.charCodeAt (0) - 48;\r
196 }if (c >= 'a' && c <= 'f') {\r
197 return c.charCodeAt (0) - 97 + 10;\r
198 }return c.charCodeAt (0) - 65 + 10;\r
199 }, "~S");\r
200 Clazz.defineStatics (c$,\r
201 "getvar", null);\r
202 c$.defs = c$.prototype.defs =  new java.util.Hashtable ();\r
203 });\r