Search in sources :

Example 1 with ParsingExceptionImpl

use of net.kyori.adventure.text.minimessage.internal.parser.ParsingExceptionImpl in project adventure by KyoriPowered.

the class MiniMessageParser method parseToTree.

@NotNull
RootNode parseToTree(@NotNull final ContextImpl context) {
    final TagResolver combinedResolver = TagResolver.resolver(this.tagResolver, context.extraTags());
    final String processedMessage = context.preProcessor().apply(context.message());
    final Consumer<String> debug = context.debugOutput();
    if (debug != null) {
        debug.accept("Beginning parsing message ");
        debug.accept(processedMessage);
        debug.accept("\n");
    }
    final TokenParser.TagProvider transformationFactory;
    if (debug != null) {
        transformationFactory = (name, args, token) -> {
            try {
                debug.accept("Attempting to match node '");
                debug.accept(name);
                debug.accept("'");
                if (token != null) {
                    debug.accept(" at column ");
                    debug.accept(String.valueOf(token.startIndex()));
                }
                debug.accept("\n");
                @Nullable final Tag transformation = combinedResolver.resolve(name, new ArgumentQueueImpl<>(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 '");
                debug.accept(name);
                debug.accept("' - ");
                debug.accept(e.getMessage());
                debug.accept("\n");
                return null;
            }
        };
    } else {
        transformationFactory = (name, args, token) -> {
            try {
                return combinedResolver.resolve(name, new ArgumentQueueImpl<>(context, args), context);
            } catch (final ParsingException ignored) {
                return null;
            }
        };
    }
    final Predicate<String> tagNameChecker = name -> {
        final String sanitized = TokenParser.TagProvider.sanitizePlaceholderName(name);
        return combinedResolver.has(sanitized);
    };
    final String preProcessed = TokenParser.resolvePreProcessTags(processedMessage, transformationFactory);
    context.message(preProcessed);
    // Then, once MiniMessage placeholders have been inserted, we can do the real parse
    final RootNode root = TokenParser.parse(transformationFactory, tagNameChecker, preProcessed, processedMessage, context.strict());
    if (debug != null) {
        debug.accept("Text parsed into element tree:\n");
        debug.accept(root.toString());
    }
    return root;
}
Also used : Tag(net.kyori.adventure.text.minimessage.tag.Tag) ElementNode(net.kyori.adventure.text.minimessage.internal.parser.node.ElementNode) Token(net.kyori.adventure.text.minimessage.internal.parser.Token) RootNode(net.kyori.adventure.text.minimessage.internal.parser.node.RootNode) Predicate(java.util.function.Predicate) TagNode(net.kyori.adventure.text.minimessage.internal.parser.node.TagNode) Collectors(java.util.stream.Collectors) ParsingExceptionImpl(net.kyori.adventure.text.minimessage.internal.parser.ParsingExceptionImpl) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Examinable(net.kyori.examination.Examinable) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) TokenParser(net.kyori.adventure.text.minimessage.internal.parser.TokenParser) Component(net.kyori.adventure.text.Component) MultiLineStringExaminer(net.kyori.examination.string.MultiLineStringExaminer) BiConsumer(java.util.function.BiConsumer) TokenType(net.kyori.adventure.text.minimessage.internal.parser.TokenType) ValueNode(net.kyori.adventure.text.minimessage.internal.parser.node.ValueNode) NotNull(org.jetbrains.annotations.NotNull) Modifying(net.kyori.adventure.text.minimessage.tag.Modifying) Inserting(net.kyori.adventure.text.minimessage.tag.Inserting) TagResolver(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver) RootNode(net.kyori.adventure.text.minimessage.internal.parser.node.RootNode) TagResolver(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver) TokenParser(net.kyori.adventure.text.minimessage.internal.parser.TokenParser) Token(net.kyori.adventure.text.minimessage.internal.parser.Token) Tag(net.kyori.adventure.text.minimessage.tag.Tag) ParsingExceptionImpl(net.kyori.adventure.text.minimessage.internal.parser.ParsingExceptionImpl) Nullable(org.jetbrains.annotations.Nullable) Examinable(net.kyori.examination.Examinable) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ParsingExceptionImpl

use of net.kyori.adventure.text.minimessage.internal.parser.ParsingExceptionImpl in project adventure by KyoriPowered.

the class MiniMessageParser method parseToTree.

@NotNull
RootNode parseToTree(@NotNull final String richMessage, @NotNull final ContextImpl context) {
    final TagResolver combinedResolver = TagResolver.resolver(this.tagResolver, context.extraTags());
    final Consumer<String> debug = context.debugOutput();
    if (debug != null) {
        debug.accept("Beginning parsing message ");
        debug.accept(richMessage);
        debug.accept("\n");
    }
    final TokenParser.TagProvider transformationFactory;
    if (debug != null) {
        transformationFactory = (name, args, token) -> {
            try {
                debug.accept("Attempting to match node '");
                debug.accept(name);
                debug.accept("'");
                if (token != null) {
                    debug.accept(" at column ");
                    debug.accept(String.valueOf(token.startIndex()));
                }
                debug.accept("\n");
                @Nullable final Tag transformation = combinedResolver.resolve(name, new ArgumentQueueImpl<>(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 '");
                debug.accept(name);
                debug.accept("' - ");
                debug.accept(e.getMessage());
                debug.accept("\n");
                return null;
            }
        };
    } else {
        transformationFactory = (name, args, token) -> {
            try {
                return combinedResolver.resolve(name, new ArgumentQueueImpl<>(context, args), context);
            } catch (final ParsingException ignored) {
                return null;
            }
        };
    }
    final Predicate<String> tagNameChecker = name -> {
        final String sanitized = TokenParser.TagProvider.sanitizePlaceholderName(name);
        return combinedResolver.has(sanitized);
    };
    final String preProcessed = TokenParser.resolvePreProcessTags(richMessage, transformationFactory);
    context.message(preProcessed);
    // Then, once MiniMessage placeholders have been inserted, we can do the real parse
    final RootNode root = TokenParser.parse(transformationFactory, tagNameChecker, preProcessed, richMessage, context.strict());
    if (debug != null) {
        debug.accept("Text parsed into element tree:\n");
        debug.accept(root.toString());
    }
    return root;
}
Also used : Tag(net.kyori.adventure.text.minimessage.tag.Tag) ElementNode(net.kyori.adventure.text.minimessage.internal.parser.node.ElementNode) Token(net.kyori.adventure.text.minimessage.internal.parser.Token) RootNode(net.kyori.adventure.text.minimessage.internal.parser.node.RootNode) Predicate(java.util.function.Predicate) TagNode(net.kyori.adventure.text.minimessage.internal.parser.node.TagNode) Collectors(java.util.stream.Collectors) ParsingExceptionImpl(net.kyori.adventure.text.minimessage.internal.parser.ParsingExceptionImpl) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Examinable(net.kyori.examination.Examinable) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) TokenParser(net.kyori.adventure.text.minimessage.internal.parser.TokenParser) Component(net.kyori.adventure.text.Component) MultiLineStringExaminer(net.kyori.examination.string.MultiLineStringExaminer) BiConsumer(java.util.function.BiConsumer) TokenType(net.kyori.adventure.text.minimessage.internal.parser.TokenType) ValueNode(net.kyori.adventure.text.minimessage.internal.parser.node.ValueNode) NotNull(org.jetbrains.annotations.NotNull) Modifying(net.kyori.adventure.text.minimessage.tag.Modifying) Inserting(net.kyori.adventure.text.minimessage.tag.Inserting) TagResolver(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver) RootNode(net.kyori.adventure.text.minimessage.internal.parser.node.RootNode) TagResolver(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver) TokenParser(net.kyori.adventure.text.minimessage.internal.parser.TokenParser) Token(net.kyori.adventure.text.minimessage.internal.parser.Token) Tag(net.kyori.adventure.text.minimessage.tag.Tag) ParsingExceptionImpl(net.kyori.adventure.text.minimessage.internal.parser.ParsingExceptionImpl) Nullable(org.jetbrains.annotations.Nullable) Examinable(net.kyori.examination.Examinable) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects (java.util.Objects)2 BiConsumer (java.util.function.BiConsumer)2 Consumer (java.util.function.Consumer)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 Component (net.kyori.adventure.text.Component)2 ParsingExceptionImpl (net.kyori.adventure.text.minimessage.internal.parser.ParsingExceptionImpl)2 Token (net.kyori.adventure.text.minimessage.internal.parser.Token)2 TokenParser (net.kyori.adventure.text.minimessage.internal.parser.TokenParser)2 TokenType (net.kyori.adventure.text.minimessage.internal.parser.TokenType)2 ElementNode (net.kyori.adventure.text.minimessage.internal.parser.node.ElementNode)2 RootNode (net.kyori.adventure.text.minimessage.internal.parser.node.RootNode)2 TagNode (net.kyori.adventure.text.minimessage.internal.parser.node.TagNode)2 ValueNode (net.kyori.adventure.text.minimessage.internal.parser.node.ValueNode)2 Inserting (net.kyori.adventure.text.minimessage.tag.Inserting)2 Modifying (net.kyori.adventure.text.minimessage.tag.Modifying)2 Tag (net.kyori.adventure.text.minimessage.tag.Tag)2 TagResolver (net.kyori.adventure.text.minimessage.tag.resolver.TagResolver)2