|
| 1 | +package com.annimon.ownlang.modules.regex; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.TypeException; |
| 4 | +import com.annimon.ownlang.lib.*; |
| 5 | +import java.util.regex.Matcher; |
| 6 | + |
| 7 | +public class MatcherValue extends MapValue { |
| 8 | + |
| 9 | + private final Matcher value; |
| 10 | + |
| 11 | + public MatcherValue(Matcher value) { |
| 12 | + super(22); |
| 13 | + this.value = value; |
| 14 | + init(); |
| 15 | + } |
| 16 | + |
| 17 | + private void init() { |
| 18 | + set("start", this::start); |
| 19 | + set("end", this::end); |
| 20 | + set("find", this::find); |
| 21 | + set("group", this::group); |
| 22 | + set("pattern", this::pattern); |
| 23 | + set("region", this::region); |
| 24 | + set("replaceFirst", this::replaceFirst); |
| 25 | + set("replaceAll", this::replaceAll); |
| 26 | + set("replaceCallback", this::replaceCallback); |
| 27 | + set("reset", this::reset); |
| 28 | + set("usePattern", this::usePattern); |
| 29 | + set("useAnchoringBounds", this::useAnchoringBounds); |
| 30 | + set("hasAnchoringBounds", Converters.voidToBoolean(value::hasAnchoringBounds)); |
| 31 | + set("useTransparentBounds", this::useTransparentBounds); |
| 32 | + set("hasTransparentBounds", Converters.voidToBoolean(value::hasTransparentBounds)); |
| 33 | + set("hitEnd", Converters.voidToBoolean(value::hitEnd)); |
| 34 | + set("lookingAt", Converters.voidToBoolean(value::lookingAt)); |
| 35 | + set("matches", Converters.voidToBoolean(value::matches)); |
| 36 | + set("groupCount", Converters.voidToInt(value::groupCount)); |
| 37 | + set("regionStart", Converters.voidToInt(value::regionStart)); |
| 38 | + set("regionEnd", Converters.voidToInt(value::regionEnd)); |
| 39 | + } |
| 40 | + |
| 41 | + private Value start(Value[] args) { |
| 42 | + Arguments.checkOrOr(0, 1, args.length); |
| 43 | + final int result; |
| 44 | + if (args.length == 0) { |
| 45 | + result = value.start(); |
| 46 | + } else { |
| 47 | + final Value arg = args[0]; |
| 48 | + if (arg.type() == Types.NUMBER) { |
| 49 | + result = value.start(arg.asInt()); |
| 50 | + } else { |
| 51 | + result = value.start(arg.asString()); |
| 52 | + } |
| 53 | + } |
| 54 | + return NumberValue.of(result); |
| 55 | + } |
| 56 | + |
| 57 | + private Value end(Value[] args) { |
| 58 | + Arguments.checkOrOr(0, 1, args.length); |
| 59 | + final int result; |
| 60 | + if (args.length == 0) { |
| 61 | + result = value.end(); |
| 62 | + } else { |
| 63 | + final Value arg = args[0]; |
| 64 | + if (arg.type() == Types.NUMBER) { |
| 65 | + result = value.end(arg.asInt()); |
| 66 | + } else { |
| 67 | + result = value.end(arg.asString()); |
| 68 | + } |
| 69 | + } |
| 70 | + return NumberValue.of(result); |
| 71 | + } |
| 72 | + |
| 73 | + private Value find(Value[] args) { |
| 74 | + Arguments.checkOrOr(0, 1, args.length); |
| 75 | + final boolean result; |
| 76 | + if (args.length == 0) { |
| 77 | + result = value.find(); |
| 78 | + } else { |
| 79 | + result = value.find(args[0].asInt()); |
| 80 | + } |
| 81 | + return NumberValue.fromBoolean(result); |
| 82 | + } |
| 83 | + |
| 84 | + private Value group(Value[] args) { |
| 85 | + Arguments.checkOrOr(0, 1, args.length); |
| 86 | + final String result; |
| 87 | + if (args.length == 0) { |
| 88 | + result = value.group(); |
| 89 | + } else { |
| 90 | + final Value arg = args[0]; |
| 91 | + if (arg.type() == Types.NUMBER) { |
| 92 | + result = value.group(arg.asInt()); |
| 93 | + } else { |
| 94 | + result = value.group(arg.asString()); |
| 95 | + } |
| 96 | + } |
| 97 | + return new StringValue(result); |
| 98 | + } |
| 99 | + |
| 100 | + private Value pattern(Value[] args) { |
| 101 | + return new PatternValue(value.pattern()); |
| 102 | + } |
| 103 | + |
| 104 | + private Value region(Value[] args) { |
| 105 | + Arguments.check(2, args.length); |
| 106 | + value.region(args[0].asInt(), args[1].asInt()); |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + private Value replaceFirst(Value[] args) { |
| 111 | + Arguments.check(1, args.length); |
| 112 | + return new StringValue(value.replaceFirst(args[0].asString())); |
| 113 | + } |
| 114 | + |
| 115 | + private Value replaceAll(Value[] args) { |
| 116 | + Arguments.check(1, args.length); |
| 117 | + return new StringValue(value.replaceAll(args[0].asString())); |
| 118 | + } |
| 119 | + |
| 120 | + static StringValue replaceCallback(MatcherValue matcherValue, Function callback) { |
| 121 | + final Matcher matcher = matcherValue.value; |
| 122 | + final StringBuffer sb = new StringBuffer(); |
| 123 | + while (matcher.find()) { |
| 124 | + String replacement = callback.execute(matcherValue).asString(); |
| 125 | + matcher.appendReplacement(sb, replacement); |
| 126 | + } |
| 127 | + matcher.appendTail(sb); |
| 128 | + return new StringValue(sb.toString()); |
| 129 | + } |
| 130 | + |
| 131 | + private Value replaceCallback(Value[] args) { |
| 132 | + Arguments.check(1, args.length); |
| 133 | + if (args[0].type() != Types.FUNCTION) { |
| 134 | + throw new TypeException(args[0].toString() + " is not a function"); |
| 135 | + } |
| 136 | + return replaceCallback(this, ((FunctionValue) args[0]).getValue()); |
| 137 | + } |
| 138 | + |
| 139 | + private Value reset(Value[] args) { |
| 140 | + Arguments.checkOrOr(0, 1, args.length); |
| 141 | + if (args.length == 0) { |
| 142 | + value.reset(); |
| 143 | + } else { |
| 144 | + value.reset(args[0].asString()); |
| 145 | + } |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + private Value useAnchoringBounds(Value[] args) { |
| 150 | + Arguments.check(1, args.length); |
| 151 | + value.useAnchoringBounds(args[0].asInt() != 0); |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + private Value useTransparentBounds(Value[] args) { |
| 156 | + Arguments.check(1, args.length); |
| 157 | + value.useTransparentBounds(args[0].asInt() != 0); |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + private Value usePattern(Value[] args) { |
| 162 | + Arguments.check(1, args.length); |
| 163 | + final Value arg = args[0]; |
| 164 | + if (arg.type() == Types.MAP && (arg instanceof PatternValue)) { |
| 165 | + value.usePattern(((PatternValue) arg).getValue()); |
| 166 | + } else { |
| 167 | + throw new TypeException("Pattern value expected"); |
| 168 | + } |
| 169 | + return this; |
| 170 | + } |
| 171 | +} |
0 commit comments