-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
34 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
} | ||
} |