From 4950c28265dd7ecc3a0cfaa461a17ba95cb7a77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Lichtenth=C3=A4ler?= Date: Sat, 16 Jul 2022 13:57:40 +0200 Subject: [PATCH] feat: add transformer back in --- src/Codestain.ts | 11 +++-- src/Interfaces/ITransformer.ts | 4 +- src/transformer/HtmlTransformer.ts | 73 ++++++++++-------------------- 3 files changed, 34 insertions(+), 54 deletions(-) diff --git a/src/Codestain.ts b/src/Codestain.ts index ba92c02..0754529 100644 --- a/src/Codestain.ts +++ b/src/Codestain.ts @@ -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; }; diff --git a/src/Interfaces/ITransformer.ts b/src/Interfaces/ITransformer.ts index 8be207a..f73bd9b 100644 --- a/src/Interfaces/ITransformer.ts +++ b/src/Interfaces/ITransformer.ts @@ -1,5 +1,5 @@ -import { AbstractSyntaxTree } from "."; +import { NodeType } from "../enums"; export interface ITransformer { - transform(ast: AbstractSyntaxTree): string; + transform(ast: Array<[NodeType, string]>): string; } diff --git a/src/transformer/HtmlTransformer.ts b/src/transformer/HtmlTransformer.ts index 1525048..24de03c 100644 --- a/src/transformer/HtmlTransformer.ts +++ b/src/transformer/HtmlTransformer.ts @@ -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 = []; - - transformed.push( - `
`,
-    );
-
-    const nodes = ast.body;
-
-    function walk(paramNode?: Node) {
-      const node = paramNode == null ? nodes[current++] : paramNode;
-
-      switch (node.type) {
-        case NodeType.Identifier:
-          return `${node.value}`;
-        case NodeType.Keyword:
-          return `${node.value}`;
-        case NodeType.StringLiteral:
-          return `"${node.value}"`;
-        case NodeType.NumberLiteral:
-          return `${node.value}`;
-        case NodeType.CallExpression:
-          const transformedParams = node.params
-            .map(param => walk(param))
-            .join("");
-
-          return `${
-            node.name || ""
-          }(${transformedParams})`;
-        case NodeType.Separator:
-          return `${node.value}`;
-        case NodeType.Regex:
-          return `${node.value}`;
-        case NodeType.InlineComment:
-          return `${node.value}`;
-        default:
-          return node.value;
-      }
-    }
-
-    while (current < nodes.length) {
-      transformed.push(walk());
-    }
-
-    transformed.push(`
`); - - return transformed.join(""); + transform(ast: Array<[NodeType, string]>): string { + return ast + .map(([type, value]) => { + switch (type) { + case NodeType.Identifier: + return `${value}`; + case NodeType.Keyword: + return `${value}`; + case NodeType.StringLiteral: + return `"${value}"`; + case NodeType.NumberLiteral: + return `${value}`; + case NodeType.Separator: + return `${value}`; + case NodeType.Regex: + return `${value}`; + case NodeType.InlineComment: + return `${value}`; + default: + return value; + } + }) + .join(""); } }