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