Add escape support to ShortcodeParserSkeleton

This commit is contained in:
JeremyStar™ 2024-07-25 20:00:32 +02:00
parent bbde2e9d2d
commit b9d4196b8b
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
8 changed files with 48 additions and 18 deletions

View file

@ -20,6 +20,7 @@
package de.staropensource.sosengine.base.classes;
import de.staropensource.sosengine.base.EngineConfiguration;
import de.staropensource.sosengine.base.exceptions.ParserException;
import de.staropensource.sosengine.base.logging.LoggerInstance;
import de.staropensource.sosengine.base.types.CodePart;
import de.staropensource.sosengine.base.types.logging.LogIssuer;
@ -76,28 +77,51 @@ public abstract class ShortcodeParserSkeleton {
* Constructs this class.
*
* @param string string to parse
* @param ignoreInvalidEscapes will ignore invalid escapes and print treat them like regular text
* @throws ParserException when parsing failed
* @since v1-alpha1
*/
public ShortcodeParserSkeleton(@NotNull String string) {
components = parse(string);
public ShortcodeParserSkeleton(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
components = parse(string, ignoreInvalidEscapes);
}
/**
* Parses an input string and spits out all of it's components.
*
* @param string string to parse
* @param ignoreInvalidEscapes will ignore invalid escapes and print treat them like regular text
* @return list of components
* @throws ParserException when parsing failed
* @see EngineConfiguration#errorShortcodeConverter
* @since v1-alpha1
*/
@NotNull
protected LinkedList<@NotNull String> parse(@NotNull String string) {
protected LinkedList<@NotNull String> parse(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
LinkedList<String> components = new LinkedList<>(); // List of components
boolean tagActive = false; // Indicates that a tag is being parsed
String part = ""; // Current part. May be a tag, may be regular text
boolean escape = false; // Indicates whether the last character was a \ character
String part = ""; // Temporary string. May be a tag, may be regular text
// Iterate through every character
for (char character : string.toCharArray()) {
// Escaping
if (escape) {
if (character == '\\' || character == '<')
part += character;
else if (!(character == 'r' || character == 'n'))
if (ignoreInvalidEscapes)
part += "\\" + character;
else
throw new ParserException("Invalid escape \\" + character);
escape = false;
continue;
}
if (character == '\\') {
escape = true;
continue;
}
if (tagActive) {
// A tag is being parsed
if (character == '>') {

View file

@ -26,7 +26,7 @@ import org.jetbrains.annotations.NotNull;
*
* @since v1-alpha2
*/
public class ParserException extends Exception {
public class ParserException extends RuntimeException {
public ParserException(@NotNull String message) {
super(message);
}

View file

@ -53,7 +53,7 @@ public final class IssuerInfo implements Placeholder {
@NotNull
@Override
public String replace(@NotNull String text) {
String replacement = "<none>";
String replacement = "\\<none>";
if (logIssuer.getAdditionalInformation() != null)
replacement = logIssuer.getAdditionalInformation();

View file

@ -75,14 +75,14 @@ public final class CrashHandler {
sos!engine crash
------------------------
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you <3
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you \\<3
Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party subsystems are causing problems.
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you <3
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you \\<3
%content%
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you <3
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you \\<3
Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party subsystems are causing problems.
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you <3
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you \\<3
------------------------
sos!engine crash

View file

@ -66,7 +66,7 @@ public class ColoredLoggerImpl implements LoggerImpl {
@Override
public void print(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String format) {
// Convert to Ansi
Ansi output = new AnsiShortcodeConverter(format).getAnsi();
Ansi output = new AnsiShortcodeConverter(format, true).getAnsi();
// Print message
if (level == LogLevel.ERROR || level == LogLevel.CRASH)

View file

@ -62,7 +62,7 @@ public class PlainLoggerImpl implements LoggerImpl {
/** {@inheritDoc} */
@Override
public void print(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String format) {
format = new EmptyShortcodeConverter(format).getClean();
format = new EmptyShortcodeConverter(format, true).getClean();
if (level == LogLevel.ERROR || level == LogLevel.CRASH)
if (EngineConfiguration.getInstance().isLoggerForceStandardOutput())
System.out.println(format);

View file

@ -20,6 +20,7 @@
package de.staropensource.sosengine.base.utility.converter;
import de.staropensource.sosengine.base.classes.ShortcodeParserSkeleton;
import de.staropensource.sosengine.base.exceptions.ParserException;
import org.fusesource.jansi.Ansi;
import org.jetbrains.annotations.NotNull;
@ -35,10 +36,12 @@ public final class AnsiShortcodeConverter extends ShortcodeParserSkeleton {
* Constructs this class.
*
* @param string string to convert
* @param ignoreInvalidEscapes will ignore invalid escapes and print treat them like regular text
* @throws ParserException when parsing failed
* @since v1-alpha0
*/
public AnsiShortcodeConverter(@NotNull String string) {
super(string);
public AnsiShortcodeConverter(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
super(string, ignoreInvalidEscapes);
}
/**

View file

@ -20,6 +20,7 @@
package de.staropensource.sosengine.base.utility.converter;
import de.staropensource.sosengine.base.classes.ShortcodeParserSkeleton;
import de.staropensource.sosengine.base.exceptions.ParserException;
import org.jetbrains.annotations.NotNull;
/**
@ -34,10 +35,12 @@ public final class EmptyShortcodeConverter extends ShortcodeParserSkeleton {
* Constructs this class.
*
* @param string string to convert
* @param ignoreInvalidEscapes will ignore invalid escapes and print treat them like regular text
* @throws ParserException when parsing failed
* @since v1-alpha1
*/
public EmptyShortcodeConverter(@NotNull String string) {
super(string);
public EmptyShortcodeConverter(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
super(string, ignoreInvalidEscapes);
}
/**