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 () {
6 Clazz.instantialize (this, arguments);
7 }, com.stevesoft.pat, "ReplaceRule");
8 Clazz.prepareFields (c$, function () {
9 this.name = this.getClass ().getName ();
11 Clazz.defineMethod (c$, "clone1",
13 return new com.stevesoft.pat.RuleHolder (this);
15 Clazz.overrideMethod (c$, "clone",
17 var x = this.clone1 ();
20 while (y.next != null) {
21 x.next = y.next.clone1 ();
28 c$.add = Clazz.defineMethod (c$, "add",
29 function (head, adding) {
32 }head.addRule (adding);
34 }, "com.stevesoft.pat.ReplaceRule,com.stevesoft.pat.ReplaceRule");
35 Clazz.defineMethod (c$, "add",
37 return com.stevesoft.pat.ReplaceRule.add (this, adding);
38 }, "com.stevesoft.pat.ReplaceRule");
39 Clazz.defineMethod (c$, "addRule",
41 if (this.next == null) {
44 this.next.addRule (r);
45 }}, "com.stevesoft.pat.ReplaceRule");
46 c$.getv = Clazz.defineMethod (c$, "getv",
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",
59 var gv = com.stevesoft.pat.ReplaceRule.getv ();
62 while (gv.searchFrom (s, mt)) {
63 var off = com.stevesoft.pat.Regex.BackRefOffset - 1;
64 mf = gv.matchedFrom ();
66 head = com.stevesoft.pat.ReplaceRule.add (head, new com.stevesoft.pat.StringRule (s.substring (mt, mf)));
68 if (($var = gv.stringMatchedI (1 + off)) != null || ($var = gv.stringMatchedI (2 + off)) != null || ($var = gv.stringMatchedI (5 + off)) != null) {
70 for (var i = 0; i < $var.length; i++) {
71 d = 8 * d + ($var.charCodeAt (i) - 48);
73 if ($var.length == 1) {
74 head = com.stevesoft.pat.ReplaceRule.add (head, new com.stevesoft.pat.BackRefRule (d));
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)));
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) {
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));
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));
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);
117 }head = com.stevesoft.pat.ReplaceRule.add (head, (alt.clone ()));
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);
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 ();
146 if (mt <= s.length) {
147 head = com.stevesoft.pat.ReplaceRule.add (head, new com.stevesoft.pat.StringRule (s.substring (mt)));
152 c$.isDefined = Clazz.defineMethod (c$, "isDefined",
154 return com.stevesoft.pat.ReplaceRule.defs.get (s) != null;
156 c$.define = Clazz.defineMethod (c$, "define",
158 com.stevesoft.pat.ReplaceRule.defs.put (s, r);
159 }, "~S,com.stevesoft.pat.Regex");
160 c$.define = Clazz.defineMethod (c$, "define",
162 com.stevesoft.pat.ReplaceRule.defs.put (s, r);
164 }, "~S,com.stevesoft.pat.ReplaceRule");
165 c$.define = Clazz.defineMethod (c$, "define",
167 com.stevesoft.pat.ReplaceRule.defs.put (s, t);
168 }, "~S,com.stevesoft.pat.Transformer");
169 c$.undefine = Clazz.defineMethod (c$, "undefine",
171 com.stevesoft.pat.ReplaceRule.defs.remove (s);
173 Clazz.defineMethod (c$, "toString1",
175 return "${" + this.name + "}";
177 Clazz.overrideMethod (c$, "toString",
179 var sb = new StringBuffer ();
180 sb.append (this.toString1 ());
183 sb.append (rr.toString1 ());
186 return sb.toString ();
188 Clazz.defineMethod (c$, "arg",
192 c$.getHexDigit = Clazz.defineMethod (c$, "getHexDigit",
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;
200 Clazz.defineStatics (c$,
202 c$.defs = c$.prototype.defs = new java.util.Hashtable ();