Skip to content

Commit

Permalink
feat: add transformer back in
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikossor committed Jul 16, 2022
1 parent 8c26d7e commit 4950c28
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 54 deletions.
11 changes: 8 additions & 3 deletions src/Codestain.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { InputLanguage } from "./types";
import { createParser, createTransformer } from "./factories";
import { NodeType } from "./enums";

export const Codestain = (language: InputLanguage, input: string) => {
const parser = createParser(language);
const ast = parser.parse(input);
// TODO: remove type cast in the future
const ast = parser.parse(input).result as unknown as Array<
[NodeType, string]
>;

console.log(ast);
const transformer = createTransformer("HTML");
const output = transformer.transform(ast);

// TODO: transform result of parser here
return output;
};
4 changes: 2 additions & 2 deletions src/Interfaces/ITransformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractSyntaxTree } from ".";
import { NodeType } from "../enums";

export interface ITransformer {
transform(ast: AbstractSyntaxTree): string;
transform(ast: Array<[NodeType, string]>): string;
}
73 changes: 24 additions & 49 deletions src/transformer/HtmlTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,29 @@
import { NodeType } from "../enums";
import { AbstractSyntaxTree, Node, ITransformer } from "../Interfaces";
import { ITransformer } from "../Interfaces";

export class HtmlTransformer implements ITransformer {
transform(ast: AbstractSyntaxTree): string {
let current = 0;
let transformed: Array<string> = [];

transformed.push(
`<pre style="color:#fff; background: #1e1e1e; padding: 2rem; font-family: Consolas, Monospace;">`,
);

const nodes = ast.body;

function walk(paramNode?: Node) {
const node = paramNode == null ? nodes[current++] : paramNode;

switch (node.type) {
case NodeType.Identifier:
return `<span style="color:#4FC1FF">${node.value}</span>`;
case NodeType.Keyword:
return `<span style="color:#499cd5">${node.value}</span>`;
case NodeType.StringLiteral:
return `<span style="color:#ce9178">"${node.value}"</span>`;
case NodeType.NumberLiteral:
return `<span style="color:#b5cea8">${node.value}</span>`;
case NodeType.CallExpression:
const transformedParams = node.params
.map(param => walk(param))
.join("");

return `<span style="color:#DCDCAA">${
node.name || ""
}(${transformedParams})</span>`;
case NodeType.Separator:
return `<span style="color:lime">${node.value}</span>`;
case NodeType.Regex:
return `<span style="color:red">${node.value}</span>`;
case NodeType.InlineComment:
return `<span style="color:grey">${node.value}</span>`;
default:
return node.value;
}
}

while (current < nodes.length) {
transformed.push(walk());
}

transformed.push(`</pre>`);

return transformed.join("");
transform(ast: Array<[NodeType, string]>): string {
return ast
.map(([type, value]) => {
switch (type) {
case NodeType.Identifier:
return `<span style="color:#4FC1FF">${value}</span>`;
case NodeType.Keyword:
return `<span style="color:#499cd5">${value}</span>`;
case NodeType.StringLiteral:
return `<span style="color:#ce9178">"${value}"</span>`;
case NodeType.NumberLiteral:
return `<span style="color:#b5cea8">${value}</span>`;
case NodeType.Separator:
return `<span style="color:lime">${value}</span>`;
case NodeType.Regex:
return `<span style="color:red">${value}</span>`;
case NodeType.InlineComment:
return `<span style="color:grey">${value}</span>`;
default:
return value;
}
})
.join("");
}
}

0 comments on commit 4950c28

Please sign in to comment.