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