Skip to content

Commit 52d58fd

Browse files
MweximMwexim
and
Mwexim
authored
feature: general syntax (#91)
* feature: LitScriptName * feature: ExprVariableIndices Co-authored-by: Mwexim <[email protected]>
1 parent 227f9f3 commit 52d58fd

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.github.syst3ms.skriptparser.expressions;
2+
3+
import io.github.syst3ms.skriptparser.Parser;
4+
import io.github.syst3ms.skriptparser.lang.Expression;
5+
import io.github.syst3ms.skriptparser.lang.TriggerContext;
6+
import io.github.syst3ms.skriptparser.lang.Variable;
7+
import io.github.syst3ms.skriptparser.log.ErrorType;
8+
import io.github.syst3ms.skriptparser.parsing.ParseContext;
9+
import org.jetbrains.annotations.Nullable;
10+
11+
import java.util.Map;
12+
13+
/**
14+
* All indices of a given list variable.
15+
*
16+
* @name Indices of List
17+
* @type EXPRESSION
18+
* @pattern [all [of] [the]] ind(exes|ices) of %^objects%
19+
* @since ALPHA
20+
* @author Mwexim
21+
*/
22+
public class ExprVariableIndices implements Expression<String> {
23+
static {
24+
Parser.getMainRegistration().addExpression(
25+
ExprVariableIndices.class,
26+
String.class,
27+
false,
28+
"[all [of] [the]] ind(exes|ices) of %^objects%"
29+
);
30+
}
31+
32+
private Variable<Object> value;
33+
34+
@SuppressWarnings("unchecked")
35+
@Override
36+
public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext) {
37+
value = (Variable<Object>) expressions[0];
38+
if (value.isSingle()) {
39+
var logger = parseContext.getLogger();
40+
logger.error(
41+
"Only list variables are allowed, found '" + value.toString(TriggerContext.DUMMY, logger.isDebug()) + "'",
42+
ErrorType.SEMANTIC_ERROR
43+
);
44+
return false;
45+
}
46+
return true;
47+
}
48+
49+
@SuppressWarnings("unchecked")
50+
@Override
51+
public String[] getValues(TriggerContext ctx) {
52+
return value.getRaw(ctx)
53+
.map(val -> (Map<String, Object>) val) // We know for a fact it is a list variable
54+
.map(val -> val.keySet().toArray(new String[0]))
55+
.orElse(new String[0]);
56+
57+
}
58+
59+
@Override
60+
public String toString(@Nullable TriggerContext ctx, boolean debug) {
61+
return "indices of " + value.toString(ctx, debug);
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.syst3ms.skriptparser.expressions;
2+
3+
import io.github.syst3ms.skriptparser.Parser;
4+
import io.github.syst3ms.skriptparser.lang.Expression;
5+
import io.github.syst3ms.skriptparser.lang.Literal;
6+
import io.github.syst3ms.skriptparser.lang.TriggerContext;
7+
import io.github.syst3ms.skriptparser.log.SkriptLogger;
8+
import io.github.syst3ms.skriptparser.parsing.ParseContext;
9+
import io.github.syst3ms.skriptparser.util.FileUtils;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
/**
13+
* The name of the current executed script, without the extension.
14+
*
15+
* @name Script Name
16+
* @pattern [the] [current] script[['s] name]
17+
* @pattern name of [the] [current] script
18+
* @since ALPHA
19+
* @author Mwexim
20+
*/
21+
public class LitScriptName implements Literal<String> {
22+
static {
23+
Parser.getMainRegistration().addExpression(
24+
LitScriptName.class,
25+
String.class,
26+
true,
27+
"[the] [current] script[['s] name]",
28+
"name of [the] [current] script"
29+
);
30+
}
31+
32+
private SkriptLogger logger;
33+
34+
@Override
35+
public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext) {
36+
logger = parseContext.getLogger();
37+
return true;
38+
}
39+
40+
@Override
41+
public String[] getValues() {
42+
return new String[] {FileUtils.removeExtension(logger.getFileName())};
43+
}
44+
45+
@Override
46+
public String toString(@Nullable TriggerContext ctx, boolean debug) {
47+
return "script name";
48+
}
49+
}

src/main/java/io/github/syst3ms/skriptparser/util/FileUtils.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class FileUtils {
2424
/**
2525
* Parses a file and returns a list containing all of its lines.
2626
* <p>
27-
* This parser offers the possiblity to stretch out code across multiple lines by simply adding a single backslash
27+
* This parser offers the possibility to stretch out code across multiple lines by simply adding a single backslash
2828
* before a line break to indicate to the parser that it should be considered as a single line. For example :
2929
* <pre>
3030
* set {large_list::*} to "one long string", \
@@ -107,6 +107,26 @@ private static String trimMultilineIndent(String multilineText) {
107107
return sb.toString();
108108
}
109109

110+
public static String removeExtension(String s) {
111+
String separator = System.getProperty("file.separator");
112+
String filename;
113+
114+
// Remove the path up until the filename.
115+
int lastSeparatorIndex = s.lastIndexOf(separator);
116+
if (lastSeparatorIndex == -1) {
117+
filename = s;
118+
} else {
119+
filename = s.substring(lastSeparatorIndex + 1);
120+
}
121+
122+
// Remove the extension.
123+
int extensionIndex = filename.lastIndexOf(".");
124+
if (extensionIndex == -1)
125+
return filename;
126+
127+
return filename.substring(0, extensionIndex);
128+
}
129+
110130
/**
111131
* Loads all classes of selected packages of the provided JAR file.
112132
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Author(s):
2+
# - Mwexim
3+
# Date: 2021/01/01
4+
5+
test:
6+
set {list::1} to "Hello"
7+
set {list::test} to "World"
8+
set {list::foo} to 5
9+
set {list::bar::boo} to 5 hours
10+
11+
assert indices of {list::*} = "1", "test", "foo" and "bar" with "Indices should be '1', 'test', 'foo' and 'bar': %indices of {list::*}%"
12+
throws indices of {var}
13+
throws indices of "Hello" and "World"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Author(s):
2+
# - Mwexim
3+
# Date: 2021/01/01
4+
5+
test:
6+
assert current script name = "LitScriptName" with "Current script name should be 'LitScriptName': %script name%"

0 commit comments

Comments
 (0)