Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
2be39cc
feat: start work on named arguments in tags
Strokkur424 Sep 17, 2025
0778118
feat: modify token parser to parse named arguments
Strokkur424 Sep 18, 2025
7811e0e
chore: introduce new TokenType to uniquely distinguish value-less tog…
Strokkur424 Sep 18, 2025
e3dfad9
feat: (WIP) abstracting away TagProvider into QueuedTagProvider and N…
Strokkur424 Sep 18, 2025
f44b15c
fix: (WIP) parser should now theoretically be able to distinguish nam…
Strokkur424 Sep 19, 2025
57437e6
feat: flesh out parsing logic further and fix a bunch of issues
Strokkur424 Sep 19, 2025
08cbeaf
feat: add test for basic named argument parsing
Strokkur424 Sep 19, 2025
265ad37
feat: start adding more tests
Strokkur424 Sep 19, 2025
2a67407
fix: <red > tag with space being recognized as valid tag
Strokkur424 Sep 19, 2025
84bd76c
feat: add a bunch more tests
Strokkur424 Sep 19, 2025
aac5f9a
chore: fix all compile time issues
Strokkur424 Sep 19, 2025
557be89
feat: add test
Strokkur424 Sep 19, 2025
bc563a1
chore: cleanup diff and rename to sequential
Strokkur424 Sep 19, 2025
dc151dc
chore: add a bunch more tests
Strokkur424 Sep 20, 2025
2fec655
feat: add inverted flag arguments
Strokkur424 Sep 20, 2025
8dfb4a3
feat: split the claiming resolvers into named and sequenced resolvers
Strokkur424 Sep 20, 2025
a23bb5f
feat: add missing context newException method and try to parse tag wi…
Strokkur424 Sep 20, 2025
70fdce2
feat: add isFlagPresent
Strokkur424 Sep 20, 2025
233e9fe
feat: add style tag
Strokkur424 Sep 20, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.kyori.adventure.pointer.Pointered;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import net.kyori.adventure.text.minimessage.tag.resolver.NamedArgumentMap;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -117,6 +118,19 @@ public interface Context {
final @NotNull ArgumentQueue tags
);

/**
* Create a new parsing exception.
*
* @param message a detail message describing the error
* @param tags the tag parts which caused the error
* @return the new parsing exception
* @since 4.25.0
*/
@NotNull ParsingException newException(
final @NotNull String message,
final @NotNull NamedArgumentMap tags
);

/**
* Create a new parsing exception without reference to a specific location.
*
Expand All @@ -141,6 +155,20 @@ public interface Context {
final @NotNull ArgumentQueue args
);

/**
* Create a new parsing exception.
*
* @param message a detail message describing the error
* @param cause the cause
* @param args arguments that caused the errors
* @return the new parsing exception
*/
@NotNull ParsingException newException(
final @NotNull String message,
final @Nullable Throwable cause,
final @NotNull NamedArgumentMap args
);

/**
* Dictates if transformations may emit virtual components or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package net.kyori.adventure.text.minimessage;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import net.kyori.adventure.pointer.Pointered;
Expand All @@ -33,6 +34,7 @@
import net.kyori.adventure.text.minimessage.internal.parser.node.TagPart;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import net.kyori.adventure.text.minimessage.tag.resolver.NamedArgumentMap;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -149,7 +151,7 @@ public UnaryOperator<String> preProcessor() {
}

@Override
public @NotNull Component deserialize(final @NotNull String message, final @NotNull TagResolver@NotNull... resolvers) {
public @NotNull Component deserialize(final @NotNull String message, final @NotNull TagResolver @NotNull ... resolvers) {
requireNonNull(message, "message");
final TagResolver combinedResolver = TagResolver.builder().resolver(this.tagResolver).resolvers(resolvers).build();
return this.deserializeWithOptionalTarget(message, combinedResolver);
Expand All @@ -165,11 +167,21 @@ public UnaryOperator<String> preProcessor() {
return new ParsingExceptionImpl(message, this.message, null, false, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
}

@Override
public @NotNull ParsingException newException(final @NotNull String message, final @NotNull NamedArgumentMap tags) {
return new ParsingExceptionImpl(message, this.message, null, false, tagsToTokens(((NamedArgumentMapImpl<?>) tags).args));
}

@Override
public @NotNull ParsingException newException(final @NotNull String message, final @Nullable Throwable cause, final @NotNull ArgumentQueue tags) {
return new ParsingExceptionImpl(message, this.message, cause, false, tagsToTokens(((ArgumentQueueImpl<?>) tags).args));
}

@Override
public @NotNull ParsingException newException(final @NotNull String message, final @Nullable Throwable cause, final @NotNull NamedArgumentMap args) {
return new ParsingExceptionImpl(message, this.message, cause, false, tagsToTokens(((NamedArgumentMapImpl<?>) args).args));
}

private @NotNull Component deserializeWithOptionalTarget(final @NotNull String message, final @NotNull TagResolver tagResolver) {
if (this.target != null) {
return this.miniMessage.deserialize(message, this.target, tagResolver);
Expand All @@ -186,4 +198,13 @@ private static Token[] tagsToTokens(final List<? extends Tag.Argument> tags) {
return tokens;
}

private static Token[] tagsToTokens(final Map<String, ? extends Tag.Argument> tags) {
final Token[] tokens = new Token[tags.size()];

int index = 0;
for (final Tag.Argument value : tags.values()) {
tokens[index++] = ((TagPart) value).token();
}
return tokens;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin
debug.accept("\n");
}

final TokenParser.TagProvider transformationFactory;
final TokenParser.SequentialTagProvider sequentialTagProvider;
final TokenParser.NamedTagProvider namedTagProvider;

if (debug != null) {
transformationFactory = (name, args, token) -> {
sequentialTagProvider = (name, args, token) -> {
try {
debug.accept("Attempting to match node '");
debug.accept("Attempting to match node as sequential '");
debug.accept(name);
debug.accept("'");
if (token != null) {
Expand Down Expand Up @@ -167,7 +169,48 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin
if (token != null && e instanceof ParsingExceptionImpl) {
final ParsingExceptionImpl impl = (ParsingExceptionImpl) e;
if (impl.tokens().length == 0) {
impl.tokens(new Token[] {token});
impl.tokens(new Token[]{token});
}
}
debug.accept("Could not match node '");
debug.accept(name);
debug.accept("' - ");
debug.accept(e.getMessage());
debug.accept("\n");
return null;
}
};
namedTagProvider = (name, args, token) -> {
try {
debug.accept("Attempting to match node as named '");
debug.accept(name);
debug.accept("'");
if (token != null) {
debug.accept(" at column ");
debug.accept(String.valueOf(token.startIndex()));
}
debug.accept("\n");

final @Nullable Tag transformation = combinedResolver.resolveNamed(name, new NamedArgumentMapImpl<>(context, args), context);

if (transformation == null) {
debug.accept("Could not match node '");
debug.accept(name);
debug.accept("'\n");
} else {
debug.accept("Successfully matched node '");
debug.accept(name);
debug.accept("' to tag ");
debug.accept(transformation instanceof Examinable ? ((Examinable) transformation).examinableName() : transformation.getClass().getName());
debug.accept("\n");
}

return transformation;
} catch (final ParsingException e) {
if (token != null && e instanceof ParsingExceptionImpl) {
final ParsingExceptionImpl impl = (ParsingExceptionImpl) e;
if (impl.tokens().length == 0) {
impl.tokens(new Token[]{token});
}
}
debug.accept("Could not match node '");
Expand All @@ -179,14 +222,23 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Strin
}
};
} else {
transformationFactory = (name, args, token) -> {
sequentialTagProvider = (name, args, token) -> {
try {
return combinedResolver.resolve(name, new ArgumentQueueImpl<>(context, args), context);
} catch (final ParsingException ignored) {
return null;
}
};
namedTagProvider = (name, args, token) -> {
try {
return combinedResolver.resolveNamed(name, new NamedArgumentMapImpl<>(context, args), context);
} catch (final ParsingException ignored) {
return null;
}
};
}

final TokenParser.TagProvider transformationFactory = new TokenParser.TagProviderImpl(sequentialTagProvider, namedTagProvider);
final Predicate<String> tagNameChecker = name -> {
final String sanitized = TokenParser.TagProvider.sanitizePlaceholderName(name);
return combinedResolver.has(sanitized);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2025 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.minimessage;

import java.util.Map;
import java.util.function.Supplier;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.NamedArgumentMap;
import net.kyori.adventure.util.TriState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.util.Objects.requireNonNull;

final class NamedArgumentMapImpl<T extends Tag.Argument> implements NamedArgumentMap {
private final Context context;
final Map<String, T> args;

NamedArgumentMapImpl(final Context context, final Map<String, T> args) {
this.context = context;
this.args = args;
}

@Override
public boolean isPresent(final @NotNull String name) {
requireNonNull(name, "name");
return this.args.containsKey(name);
}

@Override
public int size() {
return this.args.size();
}

@Override
public Tag.@Nullable Argument get(final @NotNull String name) {
requireNonNull(name, "name");
return this.args.get(name);
}

@Override
public @NotNull TriState flag(final @NotNull String name) {
final Tag.Argument argument = this.get(name);
if (argument == null) {
// The normal flag is not preset, so try the inverted flag
final Tag.Argument invertedArgument = this.get('!' + name);
if (invertedArgument == null) {
return TriState.NOT_SET;
}

return TriState.FALSE;
}

return TriState.TRUE;
}

@Override
public boolean isFlagPresent(final @NotNull String name) {
if (this.isPresent(name)) {
return true;
}
return this.isPresent('!' + name);
}

@Override
public Tag.@NotNull Argument orThrow(final @NotNull String name, final @NotNull String errorMessage) {
requireNonNull(errorMessage, "errorMessage");
final Tag.Argument arg = this.get(name);
if (arg == null) {
throw this.context.newException(errorMessage);
}
return arg;
}

@Override
public Tag.@NotNull Argument orThrow(final @NotNull String name, final @NotNull Supplier<String> errorMessage) {
requireNonNull(errorMessage, "errorMessage");
final Tag.Argument arg = this.get(name);
if (arg == null) {
throw this.context.newException(errorMessage.get());
}
return arg;
}
}
Loading
Loading