+ Object val = replace.get(tok);
+ if (val == null) {
+ ret += tok;
+ i = j - 1;
+ } else if (val instanceof JSFunctionMacro) {
+ if (s.charAt(j) != '(') { err.add(new Error("open paren must follow macro binding for macro " + tok)); continue; }
+ ret += ((JSFunctionMacro)val).process(s.substring(j+1, s.indexOf(')', j)));
+ i = s.indexOf(')', j);
+ } else {
+ ret += val;
+ i = j - 1;
+ }
+ }
+ if (!deleteLineEndings) ret += "\n";
+ return ret;
+ }
+
+ private static int indexOfWS(String s) { return indexOfWS(s, 0); }
+ private static int indexOfWS(String s, int beginIndex) {
+ if (s == null || beginIndex >= s.length()) return -1;
+ for (; beginIndex < s.length(); beginIndex++) {
+ if (s.charAt(beginIndex) == ' ') return beginIndex;
+ }
+ return s.length();
+ }
+
+ private static int indexOfNotWS(String s) { return indexOfWS(s, 0); }
+ private static int indexOfNotWS(String s, int beginIndex) {
+ if (s == null || beginIndex >= s.length()) return -1;
+ for (; beginIndex < s.length(); beginIndex++) {
+ if (s.charAt(beginIndex) != ' ') return beginIndex;
+ }
+ return -1;
+ }
+
+ public class Warning {
+ protected String msg;
+ protected int line;
+
+ public Warning() { msg = ""; }
+ public Warning(String m) { msg = m; if (in != null) line = in.getLineNumber(); }
+
+ public String toString() { return "WARNING Line "+line+": "+msg; }
+ }
+
+ public class Error extends Warning {
+ public Error() { super(); }
+ public Error(String m) { super(m); }
+ public String toString() { return "ERROR Line "+line+": "+msg; }
+ }
+
+ public static class JSFunctionMacro {
+ public String unbound1 = null;
+ public String unbound2 = null;
+ public String expression = null;
+ public String process(String args) {
+ String bound1 = null;
+ String bound2 = null;
+ if (unbound2 == null) {
+ bound1 = args;
+ return expression.replaceAll(unbound1, bound1);
+ } else {
+ bound1 = args.substring(0, args.indexOf(','));
+ bound2 = args.substring(args.indexOf(',') + 1);
+ return (expression.replaceAll(unbound1, bound1).replaceAll(unbound2, bound2));
+ }