Skip to content

Commit 95528e3

Browse files
committed
Regex simple matcher comparison.
1 parent 2247d8c commit 95528e3

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

src/main/java/org/byteskript/skript/compiler/SkriptLangSpec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ private SkriptLangSpec() {
175175
new NotEqual(),
176176
new IsEqual(),
177177
new Contains(),
178+
new Matches(),
178179
new TernaryOtherwiseExpression(),
179180
new BinaryOtherwiseExpression(),
180181
new StringLiteral(),
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.lang.syntax.comparison;
8+
9+
import mx.kenzie.foundation.MethodBuilder;
10+
import mx.kenzie.foundation.Type;
11+
import mx.kenzie.foundation.WriteInstruction;
12+
import org.byteskript.skript.api.syntax.RelationalExpression;
13+
import org.byteskript.skript.compiler.*;
14+
import org.byteskript.skript.lang.element.StandardElements;
15+
import org.byteskript.skript.runtime.internal.OperatorHandler;
16+
17+
import java.lang.reflect.Method;
18+
19+
public class Matches extends RelationalExpression {
20+
21+
public Matches() {
22+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%String% matches %Pattern%");
23+
}
24+
25+
@Override
26+
public Pattern.Match match(String thing, Context context) {
27+
if (!thing.contains(" matches ")) return null;
28+
return super.match(thing, context);
29+
}
30+
31+
@Override
32+
public void compile(Context context, Pattern.Match match) {
33+
try {
34+
final MethodBuilder method = context.getMethod();
35+
assert method != null;
36+
final Method target = OperatorHandler.class.getDeclaredMethod("matches", Object.class, Object.class);
37+
method.writeCode(WriteInstruction.invokeStatic(target));
38+
context.setState(CompileState.STATEMENT);
39+
} catch (NoSuchMethodException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
44+
@Override
45+
public Type getReturnType() {
46+
return CommonTypes.BOOLEAN;
47+
}
48+
49+
@Override
50+
public String description() {
51+
return """
52+
Check whether the first string matches the given RegEx pattern.""";
53+
}
54+
55+
}

src/main/java/org/byteskript/skript/runtime/internal/OperatorHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.byteskript.skript.error.ScriptRuntimeError;
1010

1111
import java.util.*;
12+
import java.util.regex.Pattern;
1213

1314
public class OperatorHandler {
1415

@@ -209,6 +210,13 @@ public static Boolean contains(Object a, Object b) {
209210
if (a instanceof Map<?, ?> map) return map.containsKey(b) || map.containsValue(b);
210211
return (a + "").contains(b + "");
211212
}
213+
214+
public static Boolean matches(Object a, Object b) {
215+
if (a == null) return false;
216+
if (b == null) return false;
217+
if (!(b instanceof Pattern pattern)) return equals(a, b); // probably a mistaken use?
218+
return pattern.matcher(a + "").matches();
219+
}
212220
//endregion
213221

214222
}

src/test/resources/generic.bsk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ function generic_expressions:
1919
assert newline is a string: "New line is not a string."
2020
set {regex} to /hello there/
2121
assert {regex} exists: "Regex literal failed to load."
22+
assert "hello there" matches {regex}: "Regex failed to match exact."
2223
set {regex} to /.+/
2324
assert {regex} exists: "Regex literal failed to load."
25+
assert "blob blob" matches {regex}: "Regex failed to match anything."
2426

2527

2628
function test_system:

0 commit comments

Comments
 (0)