summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hobgoblin/src/output/lang/typescript/ast.hob5
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java20
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteral.java32
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java26
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteralMatcher.java41
6 files changed, 125 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/typescript/ast.hob b/hobgoblin/src/output/lang/typescript/ast.hob
index ca37980..654e354 100644
--- a/hobgoblin/src/output/lang/typescript/ast.hob
+++ b/hobgoblin/src/output/lang/typescript/ast.hob
@@ -116,6 +116,7 @@ sum TypescriptExpression {
variant TypescriptNumberLiteral;
variant TypescriptObjectLiteral;
variant TypescriptRef;
+ variant TypescriptStringLiteral;
variant TypescriptTypeof;
variant TypescriptTypeUnion;
}
@@ -172,6 +173,10 @@ struct TypescriptRef {
field name: TypescriptIdentifier;
}
+struct TypescriptStringLiteral {
+ field value: String;
+}
+
struct TypescriptTypeof {
field expression: TypescriptExpression;
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java
index b2aa2a9..46ccdc1 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java
@@ -348,6 +348,10 @@ public class TypescriptWriter implements AutoCloseable {
this.writeRef(ref);
}
+ case TypescriptStringLiteral stringLiteral -> {
+ this.writeStringLiteral(stringLiteral);
+ }
+
case TypescriptTypeof typeof -> {
this.writeTypeof(typeof);
}
@@ -445,6 +449,22 @@ public class TypescriptWriter implements AutoCloseable {
this.writeIdentifier(ref.name());
}
+ private void writeStringLiteral(TypescriptStringLiteral stringLiteral) throws IOException {
+ this.writer.write("\"");
+ this.writer.write(
+ stringLiteral.value()
+ .replace("\\", "\\\\")
+ .replace("\"", "\\\"")
+ .replace("\n", "\\n")
+ .replace("\r", "\\r")
+ .replace("\u000b", "\\v")
+ .replace("\t", "\\t")
+ .replace("\b", "\\b")
+ .replace("\f", "\\f")
+ );
+ this.writer.write("\"");
+ }
+
private void writeTypeof(TypescriptTypeof typeof) throws IOException {
this.writer.write("typeof ");
this.writeExpression(typeof.expression());
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java
index aa23783..a525040 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptExpression.java
@@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast;
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression imports
// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression imports
-public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeUnion {
+public sealed interface TypescriptExpression permits org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptCast, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptConstructedType, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptFieldAccess, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNewCall, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptNumberLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeof, org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptTypeUnion {
public interface Builder {
public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression build();
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteral.java
new file mode 100644
index 0000000..830064b
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteral.java
@@ -0,0 +1,32 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral imports
+
+public record TypescriptStringLiteral(
+ String value
+) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression {
+ public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral.Builder("");
+ }
+
+ public record Builder(
+ String value
+ ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral(value);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral.Builder withValue(String value) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral.Builder(value);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java
index 4963e09..86a94ea 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java
@@ -567,6 +567,26 @@ public class TypescriptWriterTests {
assertThat(string, equalTo("x"));
}
+ // === String literals ===
+
+ @Test
+ public void stringLiteral() throws IOException {
+ var typescript = string("hello");
+
+ var string = write(writer -> writer.writeExpression(typescript));
+
+ assertThat(string, equalTo("\"hello\""));
+ }
+
+ @Test
+ public void stringLiteralWithSpecialCharacters() throws IOException {
+ var typescript = string("\"\\\n\r\u000b\t\b\f");
+
+ var string = write(writer -> writer.writeExpression(typescript));
+
+ assertThat(string, equalTo("\"\\\"\\\\\\n\\r\\v\\t\\b\\f\""));
+ }
+
// === typeof ===
@Test
@@ -615,6 +635,12 @@ public class TypescriptWriterTests {
.build();
}
+ private static TypescriptStringLiteral string(String value) {
+ return TypescriptStringLiteral.arbitrary()
+ .withValue(value)
+ .build();
+ }
+
private String write(TypescriptWriter.Write write) throws IOException {
return write(write, Optional.empty());
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteralMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteralMatcher.java
new file mode 100644
index 0000000..8dd0f6a
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteralMatcher.java
@@ -0,0 +1,41 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher imports
+
+public final class TypescriptStringLiteralMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher isTypescriptStringLiteral() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral>> submatchers;
+
+ private TypescriptStringLiteralMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral>> submatchers
+ ) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher withValue(org.zwobble.precisely.Matcher<? super String> value) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral::value, (org.zwobble.precisely.Matcher) value));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher body
+}