Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax string() type and add relaxedWord() #153

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/com/mojang/brigadier/StringReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ public static boolean isAllowedInUnquotedString(final char c) {
|| c == '.' || c == '+';
}

public static boolean isAllowedInUnquotedStringRelaxed(final char c) {
return c != ' ' && c != '\\' && !isQuotedStringStart(c);
}

public String readUnquotedString() {
final int start = cursor;
while (canRead() && isAllowedInUnquotedString(peek())) {
Expand All @@ -182,6 +186,14 @@ public String readUnquotedString() {
return string.substring(start, cursor);
}

public String readUnquotedStringRelaxed() {
final int start = cursor;
while (canRead() && isAllowedInUnquotedStringRelaxed(peek())) {
skip();
}
return string.substring(start, cursor);
}

public String readQuotedString() throws CommandSyntaxException {
if (!canRead()) {
return "";
Expand Down Expand Up @@ -228,7 +240,7 @@ public String readString() throws CommandSyntaxException {
skip();
return readStringUntil(next);
}
return readUnquotedString();
return readUnquotedStringRelaxed();
}

public boolean readBoolean() throws CommandSyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static StringArgumentType word() {
return new StringArgumentType(StringType.SINGLE_WORD);
}

public static StringArgumentType relaxedWord() {
return new StringArgumentType(StringType.SINGLE_WORD_RELAXED);
}

public static StringArgumentType string() {
return new StringArgumentType(StringType.QUOTABLE_PHRASE);
}
Expand All @@ -39,20 +43,23 @@ public StringType getType() {

@Override
public String parse(final StringReader reader) throws CommandSyntaxException {
if (type == StringType.GREEDY_PHRASE) {
final String text = reader.getRemaining();
reader.setCursor(reader.getTotalLength());
return text;
} else if (type == StringType.SINGLE_WORD) {
return reader.readUnquotedString();
} else {
return reader.readString();
switch (type) {
case SINGLE_WORD:
return reader.readUnquotedString();
case QUOTABLE_PHRASE:
return reader.readString();
case GREEDY_PHRASE:
final String text = reader.getRemaining();
reader.setCursor(reader.getTotalLength());
return text;
default:
return reader.readUnquotedStringRelaxed();
}
}

@Override
public String toString() {
return "string()";
return type.name;
}

@Override
Expand All @@ -62,7 +69,7 @@ public Collection<String> getExamples() {

public static String escapeIfRequired(final String input) {
for (final char c : input.toCharArray()) {
if (!StringReader.isAllowedInUnquotedString(c)) {
if (!StringReader.isAllowedInUnquotedStringRelaxed(c)) {
return escape(input);
}
}
Expand All @@ -85,16 +92,23 @@ private static String escape(final String input) {
}

public enum StringType {
SINGLE_WORD("word", "words_with_underscores"),
QUOTABLE_PHRASE("\"quoted phrase\"", "word", "\"\""),
GREEDY_PHRASE("word", "words with spaces", "\"and symbols\""),;
SINGLE_WORD("word()", "word", "words_with_underscores"),
QUOTABLE_PHRASE("string()", "\"quoted phrase\"", "word", "wörd!", "\"\""),
GREEDY_PHRASE("greedyString()", "word", "words with spaces", "\"and symbols\""),
SINGLE_WORD_RELAXED("wordRelaxed()", "word", "wörd!");

private final String name;
private final Collection<String> examples;

StringType(final String... examples) {
StringType(final String name, final String... examples) {
this.name = name;
this.examples = Arrays.asList(examples);
}

public String getName() {
return name;
}

public Collection<String> getExamples() {
return examples;
}
Expand Down
28 changes: 18 additions & 10 deletions src/test/java/com/mojang/brigadier/StringReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ public void readUnquotedString_empty_withRemaining() throws Exception {
assertThat(reader.getRemaining(), equalTo(" hello world"));
}

@Test
public void readUnquotedStringRelaxed() throws Exception {
final StringReader reader = new StringReader("héllö world");
assertThat(reader.readUnquotedStringRelaxed(), equalTo("héllö"));
assertThat(reader.getRead(), equalTo("héllö"));
assertThat(reader.getRemaining(), equalTo(" world"));
}

@Test
public void readQuotedString() throws Exception {
final StringReader reader = new StringReader("\"hello world\"");
Expand Down Expand Up @@ -278,25 +286,25 @@ public void readQuotedString_invalidQuoteEscape() throws Exception {

@Test
public void readString_noQuotes() throws Exception {
final StringReader reader = new StringReader("hello world");
assertThat(reader.readString(), equalTo("hello"));
assertThat(reader.getRead(), equalTo("hello"));
assertThat(reader.getRemaining(), equalTo(" world"));
final StringReader reader = new StringReader("héllo wörld");
assertThat(reader.readString(), equalTo("héllo"));
assertThat(reader.getRead(), equalTo("héllo"));
assertThat(reader.getRemaining(), equalTo(" wörld"));
}

@Test
public void readString_singleQuotes() throws Exception {
final StringReader reader = new StringReader("'hello world'");
assertThat(reader.readString(), equalTo("hello world"));
assertThat(reader.getRead(), equalTo("'hello world'"));
final StringReader reader = new StringReader("'héllo wörld'");
assertThat(reader.readString(), equalTo("héllo wörld"));
assertThat(reader.getRead(), equalTo("'héllo wörld'"));
assertThat(reader.getRemaining(), equalTo(""));
}

@Test
public void readString_doubleQuotes() throws Exception {
final StringReader reader = new StringReader("\"hello world\"");
assertThat(reader.readString(), equalTo("hello world"));
assertThat(reader.getRead(), equalTo("\"hello world\""));
final StringReader reader = new StringReader("\"héllo wörld\"");
assertThat(reader.readString(), equalTo("héllo wörld"));
assertThat(reader.getRead(), equalTo("\"héllo wörld\""));
assertThat(reader.getRemaining(), equalTo(""));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static com.mojang.brigadier.arguments.StringArgumentType.escapeIfRequired;
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
import static com.mojang.brigadier.arguments.StringArgumentType.string;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static com.mojang.brigadier.arguments.StringArgumentType.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.is;
Expand All @@ -35,6 +32,14 @@ public void testParseWord() throws Exception {
verify(reader).readUnquotedString();
}

@Test
public void testParseRelaxedWord() throws Exception {
final StringReader reader = mock(StringReader.class);
when(reader.readUnquotedStringRelaxed()).thenReturn("héllo");
assertThat(relaxedWord().parse(reader), equalTo("héllo"));
verify(reader).readUnquotedStringRelaxed();
}

@Test
public void testParseString() throws Exception {
final StringReader reader = mock(StringReader.class);
Expand Down