forked from SkriptLang/Skript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for String literals (SkriptLang#6718)
- Loading branch information
Showing
5 changed files
with
202 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||
*/ | ||
package ch.njol.skript.lang; | ||
|
||
import ch.njol.skript.lang.util.ConvertedLiteral; | ||
import ch.njol.skript.util.Utils; | ||
import ch.njol.util.coll.CollectionUtils; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.converter.Converters; | ||
|
||
import java.util.Optional; | ||
|
||
public class LiteralString extends VariableString implements Literal<String> { | ||
|
||
/** | ||
* Creates a new VariableString which does not contain variables. | ||
* | ||
* @param input Content for string. | ||
*/ | ||
protected LiteralString(String input) { | ||
super(input); | ||
} | ||
|
||
@Override | ||
public String[] getArray() { | ||
return new String[]{original}; | ||
} | ||
|
||
@Override | ||
public String getSingle() { | ||
return original; | ||
} | ||
|
||
@Override | ||
public String[] getAll() { | ||
return new String[]{original}; | ||
} | ||
|
||
@Override | ||
public Optional<String> getOptionalSingle(Event event) { | ||
return Optional.of(original); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <R> @Nullable Literal<? extends R> getConvertedExpression(Class<R>... to) { | ||
if (CollectionUtils.containsSuperclass(to, String.class)) | ||
return (Literal<? extends R>) this; | ||
Class<R> superType = (Class<R>) Utils.getSuperType(to); | ||
R[] parsedData = Converters.convert(this.getArray(), to, superType); | ||
if (parsedData.length != 1) | ||
return null; | ||
return new ConvertedLiteral<>(this, parsedData, superType); | ||
} | ||
|
||
/** | ||
* Use {@link #toString(Event)} to get the actual string. This method is for debugging. | ||
*/ | ||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return '"' + original + '"'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/ch/njol/skript/test/runner/ExprTestStringLiteral.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||
*/ | ||
package ch.njol.skript.test.runner; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.NoDoc; | ||
import ch.njol.skript.lang.*; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.lang.util.SimpleExpression; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@Name("Test String Literal") | ||
@Description("Accepts only a string literal. Used for testing correct parsing & literal treatment. Returns the value.") | ||
@NoDoc | ||
public class ExprTestStringLiteral extends SimpleExpression<String> { | ||
|
||
static { | ||
if (TestMode.ENABLED) | ||
Skript.registerExpression(ExprTestStringLiteral.class, String.class, ExpressionType.SIMPLE, "test string literal %*string%"); | ||
} | ||
|
||
private Expression<String> literal; | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
this.literal = (Expression<String>) expressions[0]; | ||
return literal instanceof LiteralString; | ||
} | ||
|
||
@Override | ||
protected @Nullable String[] get(Event event) { | ||
return literal.getArray(event); | ||
} | ||
|
||
@Override | ||
public Class<? extends String> getReturnType() { | ||
return String.class; | ||
} | ||
|
||
@Override | ||
public boolean isSingle() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "test string literal " + literal.toString(event, debug); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
test "string literals": | ||
set {_c} to (test string literal "blob blob blob") | ||
assert {_c} is "blob blob blob" with "string literal value wrong" | ||
assert test string literal "blob blob blob" is "blob blob blob" with "string literal value wrong" | ||
|
||
test "string literals (parsing)": | ||
|
||
parse: | ||
set {_test} to test string literal "hello there this is a long string blah blah blah" | ||
assert last parse logs is not set with "skript should be able to understand literal: %last parse logs%" | ||
|
||
parse: | ||
set {_test} to test string literal "hello %% percent" | ||
assert last parse logs is not set with "percents shouldn't invalidate literal: %last parse logs%" | ||
|
||
parse: | ||
set {_test} to test string literal "hello %now%" | ||
assert last parse logs is set with "the non-literal should not have been accepted" | ||
|
||
assert (test string literal "hello") is "hello" with "string literal value wrong" |