Skip to content

Commit

Permalink
feat(tsl): add multi-line comment support
Browse files Browse the repository at this point in the history
- TSL parser will now skip anything between `#* ... *#`
- Fixed RulesetNameArgumentType not parsing the string properly
  • Loading branch information
iGoodie committed Sep 10, 2019
1 parent 7a01cf3 commit c2b85e3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -5,7 +5,7 @@ org.gradle.daemon=false

mod_id=twitchspawn
mod_group=net.programmer.igoodie
mod_version=1.3.2
mod_version=1.3.3

minecraft_version=1.14.4
forge_version=28.0.83
Expand Down
Expand Up @@ -26,7 +26,7 @@ private RulesetNameArgumentType() { }

@Override
public String parse(StringReader reader) throws CommandSyntaxException {
return null;
return reader.readUnquotedString(); // Only 1 word is allowed
}

@Override
Expand Down
Expand Up @@ -41,9 +41,9 @@ public static void register(CommandDispatcher<CommandSource> dispatcher) {
root.then(Commands.literal("reloadcfg").executes(TwitchSpawnCommand::reloadModule));

root.then(Commands.literal("rules")
.executes(TwitchSpawnCommand::rulesModule)
.then(CommandArguments.rulesetName("streamer_nick")
.executes(TwitchSpawnCommand::rulesOfPlayerModule))
.executes(context -> rulesModule(context, null))
.then(CommandArguments.rulesetName("ruleset_name")
.executes(context -> rulesModule(context, RulesetNameArgumentType.getRulesetName(context, "ruleset_name"))))
);

root.then(Commands.literal("simulate")
Expand Down Expand Up @@ -153,28 +153,27 @@ public static int reloadModule(CommandContext<CommandSource> context) {

/* ------------------------------------------------------------ */

public static int rulesModule(CommandContext<CommandSource> context) {
context.getSource().sendFeedback(new TranslationTextComponent(
"commands.twitchspawn.rules.list",
ConfigManager.RULESET_COLLECTION.getStreamers()), true);
return 1;
}
public static int rulesModule(CommandContext<CommandSource> context, String rulesetName) {
if (rulesetName == null) {
context.getSource().sendFeedback(new TranslationTextComponent(
"commands.twitchspawn.rules.list",
ConfigManager.RULESET_COLLECTION.getStreamers()), true);
return 1;
}

public static int rulesOfPlayerModule(CommandContext<CommandSource> context) {
String streamerNick = StreamerArgumentType.getStreamer(context, "streamer_nick");
TSLRuleset ruleset = ConfigManager.RULESET_COLLECTION.getRuleset(streamerNick);
TSLRuleset ruleset = ConfigManager.RULESET_COLLECTION.getRuleset(rulesetName);

if (ruleset == null) {
context.getSource().sendFeedback(new TranslationTextComponent(
"commands.twitchspawn.rules.one.fail",
streamerNick), true);
rulesetName), true);
return 0;
}

String translationKey = streamerNick.equalsIgnoreCase("default") ?
String translationKey = rulesetName.equalsIgnoreCase("default") ?
"commands.twitchspawn.rules.default" : "commands.twitchspawn.rules.one";
context.getSource().sendFeedback(new TranslationTextComponent(translationKey,
streamerNick, ruleset.toString()), true);
rulesetName, ruleset.toString()), true);
return 1;
}

Expand Down
Expand Up @@ -2,9 +2,12 @@

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

public class TSLTokenizer {

public static final String MULTI_LINE_COMMENT_BEGIN = "#*";
public static final String MULTI_LINE_COMMENT_END = "*#";
public static final char COMMENT = '#';
public static final char SPACE = ' ';
public static final char GROUPING = '%';
Expand Down Expand Up @@ -65,7 +68,18 @@ public String getRule(int index) {

public List<String> intoRules() throws TSLSyntaxError {
this.rules = new LinkedList<>();
String[] lines = script.split("\\R");
String[] lines = script
.replaceAll("(?s)" + quoteRegex(MULTI_LINE_COMMENT_BEGIN) + ".*?" + quoteRegex(MULTI_LINE_COMMENT_END), "") // Discard multi-line comments
.split("\\R"); // Split newlines

// Check for unexpected beginning or ending of a multi-line comment
for (String line : lines) {
if (line.contains(MULTI_LINE_COMMENT_BEGIN))
throw new TSLSyntaxError("Unclosed multiline comment -> %s", line);
if (line.contains(MULTI_LINE_COMMENT_END))
throw new TSLSyntaxError("Unexpected comment closing -> %s", line);
}

StringBuilder rule = new StringBuilder();

// Traverse every line
Expand Down Expand Up @@ -188,6 +202,10 @@ private String quoteRegex(char character) {
return "\\" + character;
}

private String quoteRegex(String string) {
return Pattern.quote(string);
}

private String trimComments(String line) {
StringBuilder trimmed = new StringBuilder();

Expand Down

0 comments on commit c2b85e3

Please sign in to comment.