feat(muscript): port over StarScriptIngester
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2024-04-05 18:20:37 +02:00
parent 95658555cf
commit cfc6402788
Signed by: Johannes
GPG Key ID: E76429612C2929F4
6 changed files with 98 additions and 20 deletions

View File

@ -1,7 +1,9 @@
package io.gitlab.jfronny.muscript.core;
import java.util.Arrays;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class MuUtil {
public static final Set<String> RESERVED_IDS = Set.of("null", "true", "false");
@ -10,4 +12,14 @@ public class MuUtil {
public static boolean isValidId(String id) {
return IDENTIFIER.matcher(id).matches() && !RESERVED_IDS.contains(id);
}
/**
* Creates quotes around a string, supports strings containing quotes
*/
public static String enquote(String literalText) {
if (!literalText.contains("'")) return "'" + literalText + "'";
if (!literalText.contains("\"")) return "\"" + literalText + "\"";
return Arrays.stream(literalText.split("'")).map(s -> "'" + s + "'")
.collect(Collectors.joining(" || \"'\" || "));
}
}

View File

@ -0,0 +1,82 @@
package io.gitlab.jfronny.muscript.parser;
import io.gitlab.jfronny.muscript.core.MuUtil;
public class StarScriptIngester {
private static final System.Logger LOGGER = System.getLogger("MuScript");
/**
* Naive conversion of starscript syntax to muscript
*/
public static String starScriptToMu(String source) {
StringBuilder result = new StringBuilder();
StringBuilder currbuf = new StringBuilder();
State state = State.Surrounding;
for (char c : source.toCharArray()) {
switch (state) {
case Surrounding -> {
if (c == '{') {
if (!currbuf.isEmpty()) {
if (!result.isEmpty()) result.append(" || ");
result.append(MuUtil.enquote(currbuf.toString()));
}
currbuf = new StringBuilder();
state = State.UnquotedInner;
} else currbuf.append(c);
}
case UnquotedInner -> {
switch (c) {
case '}' -> {
if (!currbuf.isEmpty()) {
if (!result.isEmpty()) result.append(" || ");
result.append("(").append(currbuf).append(")");
}
currbuf = new StringBuilder();
state = State.Surrounding;
}
case '\'' -> {
currbuf.append(c);
state = State.SingleQuoteInner;
}
case '"' -> {
currbuf.append(c);
state = State.DoubleQuoteInner;
}
default -> currbuf.append(c);
}
}
case SingleQuoteInner -> {
currbuf.append(c);
if (c == '\'') state = State.UnquotedInner;
}
case DoubleQuoteInner -> {
currbuf.append(c);
if (c == '"') state = State.UnquotedInner;
}
}
}
if (!currbuf.isEmpty() && !result.isEmpty()) result.append(" || ");
switch (state) {
case Surrounding -> {
if (!currbuf.isEmpty()) result.append(MuUtil.enquote(currbuf.toString()));
}
case UnquotedInner -> {
LOGGER.log(System.Logger.Level.WARNING, "Starscript code segment improperly closed, closing automatically");
if (!currbuf.isEmpty()) result.append("(").append(currbuf).append(")");
}
case SingleQuoteInner -> {
LOGGER.log(System.Logger.Level.WARNING, "Quote in starscript swallows ending, completing with closing quote");
if (!currbuf.isEmpty()) result.append("(").append(currbuf).append("')");
}
case DoubleQuoteInner -> {
LOGGER.log(System.Logger.Level.WARNING, "Quote in starscript swallows ending, completing with closing quote");
if (!currbuf.isEmpty()) result.append("(").append(currbuf).append("\")");
}
}
return result.toString();
}
enum State {
Surrounding, UnquotedInner, SingleQuoteInner, DoubleQuoteInner
}
}

View File

@ -5,4 +5,5 @@ module io.gitlab.jfronny.commons.muscript.parser {
requires io.gitlab.jfronny.commons.muscript.core;
exports io.gitlab.jfronny.muscript.parser;
exports io.gitlab.jfronny.muscript.parser.lexer;
exports io.gitlab.jfronny.muscript.parser.util;
}

View File

@ -14,10 +14,8 @@ import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.data.dynamic.context.DynamicSerializer;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static io.gitlab.jfronny.muscript.ast.context.ExprUtils.asString;
@ -40,7 +38,7 @@ public abstract class Decompiler {
case NullLiteral nl -> writer.append("null");
case BoolLiteral(var location, var value) -> writer.append(value ? "true" : "false");
case NumberLiteral(var location, var value) -> writer.append(StringFormatter.toString(value));
case StringLiteral(var location, var value) -> writer.append(enquote(value));
case StringLiteral(var location, var value) -> writer.append(MuUtil.enquote(value));
case DynamicLiteral(var location, Dynamic value) -> DynamicSerializer.INSTANCE.serialize(writer, value);
case DynamicLiteral dl -> throw new IllegalArgumentException("Unexpected implementation of IDynamic");
case This t -> writer.append("this");
@ -289,18 +287,4 @@ public abstract class Decompiler {
decompile(writer, toWrite);
if (wrap) writer.append(')');
}
/**
* Creates quotes around a string, supports strings containing quotes
*/
public static String enquote(String literalText) {
if (!literalText.contains("'")) return "'" + literalText + "'";
if (!literalText.contains("\"")) return "\"" + literalText + "\"";
return Arrays.stream(literalText.split("'")).map(s -> "'" + s + "'")
.collect(Collectors.joining(" || \"'\" || "));
}
public static String toString(Expr expr) {
return ExprWriter.write(writer -> decompile(writer, expr), false);
}
}

View File

@ -2,11 +2,12 @@ package io.gitlab.jfronny.muscript.serialize.impl;
import io.gitlab.jfronny.muscript.ast.Expr;
import io.gitlab.jfronny.muscript.ast.context.IExprSerializer;
import io.gitlab.jfronny.muscript.core.ExprWriter;
import io.gitlab.jfronny.muscript.serialize.Decompiler;
public class ExprSerializerImpl implements IExprSerializer {
@Override
public String serialize(Expr expr) {
return Decompiler.toString(expr);
return ExprWriter.write(writer -> Decompiler.decompile(writer, expr), false);
}
}

View File

@ -7,14 +7,12 @@ import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.muscript.data.dynamic.DNull;
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.ast.*;
import io.gitlab.jfronny.muscript.annotations.CanThrow;
import io.gitlab.jfronny.muscript.ast.literal.BoolLiteral;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;
@CanThrow
public class UnresolvedConditional extends DynamicExpr {
private final BoolExpr condition;
private final Expr<?> trueExpr;