Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InterpolatedString #419

Merged
merged 8 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion src/LuauAST/impl/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum SyntaxKind {
BinaryExpression,
UnaryExpression,
IfExpression,
InterpolatedString,
Array,
Map,
Set,
Expand All @@ -44,6 +45,7 @@ export enum SyntaxKind {

// fields
MapField,
InterpolatedStringPart,

// used to detect what category a given kind falls into
FirstIndexableExpression = Identifier,
Expand All @@ -53,5 +55,5 @@ export enum SyntaxKind {
FirstStatement = Assignment,
LastStatement = Comment,
FirstField = MapField,
LastField = MapField,
LastField = InterpolatedStringPart,
}
2 changes: 2 additions & 0 deletions src/LuauAST/impl/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const isFunctionExpression = makeGuard(luau.SyntaxKind.FunctionExpression
export const isBinaryExpression = makeGuard(luau.SyntaxKind.BinaryExpression);
export const isUnaryExpression = makeGuard(luau.SyntaxKind.UnaryExpression);
export const isIfExpression = makeGuard(luau.SyntaxKind.IfExpression);
export const isInterpolatedString = makeGuard(luau.SyntaxKind.InterpolatedString);
export const isArray = makeGuard(luau.SyntaxKind.Array);
export const isMap = makeGuard(luau.SyntaxKind.Map);
export const isSet = makeGuard(luau.SyntaxKind.Set);
Expand Down Expand Up @@ -66,6 +67,7 @@ export function isStatement(node: luau.Node): node is luau.Statement {

// fields
export const isMapField = makeGuard(luau.SyntaxKind.MapField);
export const isInterpolatedStringPart = makeGuard(luau.SyntaxKind.InterpolatedStringPart);

export function isField(node: luau.Node): node is luau.Field {
return node.kind >= luau.SyntaxKind.FirstField && node.kind <= luau.SyntaxKind.LastField;
Expand Down
2 changes: 2 additions & 0 deletions src/LuauAST/types/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ExpressionByKind extends IndexableExpressionByKind {
[luau.SyntaxKind.BinaryExpression]: luau.BinaryExpression;
[luau.SyntaxKind.UnaryExpression]: luau.UnaryExpression;
[luau.SyntaxKind.IfExpression]: luau.IfExpression;
[luau.SyntaxKind.InterpolatedString]: luau.InterpolatedString;
[luau.SyntaxKind.Array]: luau.Array;
[luau.SyntaxKind.Map]: luau.Map;
[luau.SyntaxKind.Set]: luau.Set;
Expand All @@ -48,6 +49,7 @@ export interface StatementByKind {

export interface FieldByKind {
[luau.SyntaxKind.MapField]: luau.MapField;
[luau.SyntaxKind.InterpolatedStringPart]: luau.InterpolatedStringPart;
}

export interface NodeByKind extends luau.ExpressionByKind, luau.StatementByKind, luau.FieldByKind {}
Expand Down
8 changes: 8 additions & 0 deletions src/LuauAST/types/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export interface IfExpression extends luau.BaseExpression<luau.SyntaxKind.IfExpr
alternative: luau.Expression;
}

export interface InterpolatedString extends luau.BaseExpression<luau.SyntaxKind.InterpolatedString> {
parts: luau.List<luau.InterpolatedStringPart | luau.Expression>;
}

export interface Array extends luau.BaseExpression<luau.SyntaxKind.Array> {
members: luau.List<luau.Expression>;
}
Expand Down Expand Up @@ -207,3 +211,7 @@ export interface MapField extends luau.BaseField<luau.SyntaxKind.MapField> {
index: luau.Expression;
value: luau.Expression;
}

export interface InterpolatedStringPart extends luau.BaseField<luau.SyntaxKind.InterpolatedStringPart> {
text: string;
}
22 changes: 22 additions & 0 deletions src/LuauRenderer/nodes/expressions/renderInterpolatedString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import luau from "LuauAST";
import { render, RenderState } from "LuauRenderer";

export function renderInterpolatedString(state: RenderState, node: luau.InterpolatedString) {
let result = "`";
luau.list.forEach(node.parts, part => {
let expressionStr = render(state, part);
if (luau.isInterpolatedStringPart(part)) {
result += expressionStr;
} else {
result += "{";
// `{{}}` is invalid, so we wrap it in parenthesis
if (luau.isTable(part)) {
expressionStr = `(${expressionStr})`;
}
result += expressionStr;
result += "}";
}
});
result += "`";
return result;
}
9 changes: 9 additions & 0 deletions src/LuauRenderer/nodes/fields/renderInterpolatedStringPart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import luau from "LuauAST";
import { RenderState } from "LuauRenderer";

export function renderInterpolatedStringPart(state: RenderState, node: luau.InterpolatedStringPart) {
// braces and newlines are escaped to be valid in luau
// () captures result, [] to search for any of: {}
// $1 fills in capture from original search
return node.text.replace(/([{}])/g, "\\$1").replace(/\n/g, "\\\n");
}
4 changes: 4 additions & 0 deletions src/LuauRenderer/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import { renderArray } from "LuauRenderer/nodes/expressions/renderArray";
import { renderBinaryExpression } from "LuauRenderer/nodes/expressions/renderBinaryExpression";
import { renderFunctionExpression } from "LuauRenderer/nodes/expressions/renderFunctionExpression";
import { renderIfExpression } from "LuauRenderer/nodes/expressions/renderIfExpression";
import { renderInterpolatedString } from "LuauRenderer/nodes/expressions/renderInterpolatedString";
import { renderMap } from "LuauRenderer/nodes/expressions/renderMap";
import { renderMixedTable } from "LuauRenderer/nodes/expressions/renderMixedTable";
import { renderNumberLiteral } from "LuauRenderer/nodes/expressions/renderNumberLiteral";
import { renderSet } from "LuauRenderer/nodes/expressions/renderSet";
import { renderStringLiteral } from "LuauRenderer/nodes/expressions/renderStringLiteral";
import { renderUnaryExpression } from "LuauRenderer/nodes/expressions/renderUnaryExpression";
import { renderInterpolatedStringPart } from "LuauRenderer/nodes/fields/renderInterpolatedStringPart";
import { renderMapField } from "LuauRenderer/nodes/fields/renderMapField";
import { renderAssignment } from "LuauRenderer/nodes/statements/renderAssignment";
import { renderBreakStatement } from "LuauRenderer/nodes/statements/renderBreakStatement";
Expand Down Expand Up @@ -64,6 +66,7 @@ const KIND_TO_RENDERER = identity<{ [K in luau.SyntaxKind]: Renderer<K> }>({
[luau.SyntaxKind.BinaryExpression]: renderBinaryExpression,
[luau.SyntaxKind.UnaryExpression]: renderUnaryExpression,
[luau.SyntaxKind.IfExpression]: renderIfExpression,
[luau.SyntaxKind.InterpolatedString]: renderInterpolatedString,
[luau.SyntaxKind.Array]: renderArray,
[luau.SyntaxKind.Map]: renderMap,
[luau.SyntaxKind.Set]: renderSet,
Expand All @@ -88,6 +91,7 @@ const KIND_TO_RENDERER = identity<{ [K in luau.SyntaxKind]: Renderer<K> }>({

// fields
[luau.SyntaxKind.MapField]: renderMapField,
[luau.SyntaxKind.InterpolatedStringPart]: renderInterpolatedStringPart,
});

/**
Expand Down
2 changes: 2 additions & 0 deletions src/LuauRenderer/util/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const KIND_TO_VISITOR = identity<{ [K in luau.SyntaxKind]: VisitStrategy<K> }>({
visitNode(node.expression, visitor);
visitNode(node.alternative, visitor);
},
[luau.SyntaxKind.InterpolatedString]: (node, visitor) => visitList(node.parts, visitor),
[luau.SyntaxKind.Array]: (node, visitor) => visitList(node.members, visitor),
[luau.SyntaxKind.Map]: (node, visitor) => visitList(node.fields, visitor),
[luau.SyntaxKind.Set]: (node, visitor) => visitList(node.members, visitor),
Expand Down Expand Up @@ -141,6 +142,7 @@ const KIND_TO_VISITOR = identity<{ [K in luau.SyntaxKind]: VisitStrategy<K> }>({
visitNode(node.index, visitor);
visitNode(node.value, visitor);
},
[luau.SyntaxKind.InterpolatedStringPart]: NOOP,
});

function visitNode(node: luau.Node, visitor: Visitor) {
Expand Down
Loading