@Override
public Object activate(IokeObject method, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
IokeObject target = IokeObject.as(Interpreter.send(context.runtime.asText, context, args.get(0)), context);
String arg = Text.getText(target);
Matcher m = ((Regexp)IokeObject.data(on)).regexp.matcher(arg);
if(m.find()) {
IokeObject match = regexpMatch.allocateCopy(message, context);
match.singleMimicsWithoutCheck(regexpMatch);
match.setData(new RegexpMatch(IokeObject.as(on, context), m, target));
return match;
} else {
return context.runtime.nil;
}
}
}));
obj.aliasMethod("match", "=~", null, null);
obj.registerMethod(runtime.newNativeMethod("Takes one argument that should be a text and returns a text that has all regexp meta characters quoted", new NativeMethod("quote") {
private final DefaultArgumentsDefinition ARGUMENTS = DefaultArgumentsDefinition
.builder()
.withRequiredPositional("text")
.getArguments();
@Override
public DefaultArgumentsDefinition getArguments() {
return ARGUMENTS;
}
@Override
public Object activate(IokeObject method, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
return context.runtime.newText(Pattern.quote(Text.getText(Interpreter.send(context.runtime.asText, context, args.get(0)))));
}
}));
obj.registerMethod(runtime.newNativeMethod("Takes one or two text arguments that describes the regular expression to create. the first text is the pattern and the second is the flags.", new NativeMethod("from") {
private final DefaultArgumentsDefinition ARGUMENTS = DefaultArgumentsDefinition
.builder()
.withRequiredPositional("pattern")
.withOptionalPositional("flags", "")
.getArguments();
@Override
public DefaultArgumentsDefinition getArguments() {
return ARGUMENTS;
}
@Override
public Object activate(IokeObject method, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
String pattern = Text.getText(Interpreter.send(context.runtime.asText, context, args.get(0)));
String flags = "";
if(args.size() > 1) {
flags = Text.getText(Interpreter.send(context.runtime.asText, context, args.get(1)));
}
return context.runtime.newRegexp(pattern, flags, context, message);
}
}));
obj.registerMethod(runtime.newNativeMethod("Takes one argument and tries to match that argument against the current pattern. Returns a list of all the texts that were matched.", new TypeCheckingNativeMethod("allMatches") {
private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
.builder()
.receiverMustMimic(runtime.regexp)
.withRequiredPositional("other")
.getArguments();
@Override
public TypeCheckingArgumentsDefinition getArguments() {
return ARGUMENTS;
}
@Override
public Object activate(IokeObject method, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
String arg = Text.getText(Interpreter.send(context.runtime.asText, context, args.get(0)));
Matcher m = ((Regexp)IokeObject.data(on)).regexp.matcher(arg);
List<Object> result = new ArrayList<Object>();
MatchIterator iter = m.findAll();
Runtime runtime = context.runtime;
while(iter.hasMore()) {
result.add(runtime.newText(iter.nextMatch().group(0)));
}