Skip to content

Commit d0eff07

Browse files
authored
Merge pull request #26 from bluelhf/fix/alternative-signature-parsing
Add more variants when parsing function property expressions.
2 parents 3bfab5e + 328aed1 commit d0eff07

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/main/java/org/byteskript/skript/lang/syntax/function/ExprFunctionProperty.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
import org.byteskript.skript.lang.element.StandardElements;
1414
import org.byteskript.skript.runtime.internal.Metafactory;
1515

16-
import java.util.ArrayList;
17-
import java.util.Arrays;
18-
import java.util.List;
16+
import java.util.*;
1917
import java.util.regex.Matcher;
2018

2119
@Documentation(
@@ -48,6 +46,37 @@ public Pattern.Match match(String thing, Context context) {
4846
private Pattern.Match createMatch(String thing, Context context) {
4947
final Matcher matcher = PATTERN.matcher(thing);
5048
if (!matcher.find()) return null;
49+
50+
final LinkedHashSet<Pattern.Match.Variant> variants = new LinkedHashSet<>();
51+
final Matcher second = matcher.pattern().matcher(matcher.group());
52+
for (int end = matcher.group().length(); end >= 0; end = matcher.group().lastIndexOf(" of ", end - 1)) {
53+
second.reset();
54+
second.region(0, end);
55+
while (second.find()) {
56+
final String name = second.group("name");
57+
final String params = second.group("params");
58+
final Type[] parameters = getParams(params);
59+
60+
final Matcher dummy = java.util.regex.Pattern.compile(buildDummyPattern(name, parameters.length))
61+
.matcher(matcher.group()); // Given this alternative signature for the method, what would the full matcher now find?
62+
if (!dummy.find()) continue;
63+
64+
final List<String> strings = new ArrayList<>();
65+
for (int i = 1; i <= dummy.groupCount(); i++) {
66+
final String group = dummy.group(i);
67+
if (group != null) strings.add(group.trim());
68+
}
69+
final String[] groups = strings.toArray(new String[0]);
70+
71+
final List<Type> types = new ArrayList<>();
72+
for (int i = 0; i < parameters.length; i++) {
73+
types.add(CommonTypes.OBJECT);
74+
}
75+
types.add(CommonTypes.OBJECT);
76+
variants.add(new Pattern.Match.Variant(dummy, types.toArray(new Type[0]), groups));
77+
}
78+
}
79+
5180
final String name = matcher.group("name");
5281
final String params = matcher.group("params");
5382
final Type[] parameters = getParams(params);
@@ -59,7 +88,7 @@ private Pattern.Match createMatch(String thing, Context context) {
5988
types.add(CommonTypes.OBJECT);
6089
}
6190
types.add(CommonTypes.OBJECT);
62-
return new Pattern.Match(dummy, new FunctionDetails(name, parameters), types.toArray(new Type[0]));
91+
return new Pattern.Match(dummy, variants.toArray(Pattern.Match.Variant[]::new), 0, new FunctionDetails(name, parameters), types.toArray(new Type[0]));
6392
}
6493

6594
private Type[] getParams(String params) {

0 commit comments

Comments
 (0)