Compare commits

..

21 commits

Author SHA1 Message Date
7028df3d37 Basically finito, needs some final polishing and writing a README 2024-05-19 19:53:14 +02:00
d468787983 Almost finito, just missing adding the * 2024-05-15 21:11:52 +02:00
1aa8956fb2 added boolean logic on the DSL types :) 2024-05-15 20:55:03 +02:00
bf3abd232c idk dude, something is very bad with the transformation 2024-05-13 19:12:23 +02:00
3d3865d625 cleanup 2024-05-12 20:08:10 +02:00
438d4589d4 IT WORKS 2024-05-12 20:06:37 +02:00
b740b469d4 Finished up Langium stuffs and more more more 2024-05-11 16:09:03 +02:00
03d906da5a wow, been a long time since i commited to github :) 2024-02-24 22:30:30 +01:00
b6a7c645f2 lol 2023-12-11 19:40:19 +01:00
8cb01d1bd0 starting to be something xDDDDD 2023-12-11 19:38:30 +01:00
e906095363 fixed babel submodule setup 2023-12-06 18:48:30 +01:00
2e268835e5 removed properly 2023-12-06 18:32:23 +01:00
0334431f32 removed babel submodule 2023-12-06 18:30:58 +01:00
52c3c8e24e Langium stuff 2023-12-06 17:34:00 +01:00
96673b993c Initialized Langium, i am however having some issues with it, perhaps writing my own parser is better? 2023-12-06 13:44:49 +01:00
60bc21d8ab Removed babel submodule 2023-12-06 10:24:09 +01:00
2ef239b466 Finally working token 2023-10-11 16:29:59 +02:00
38fe05af7f LESSGOOOO Finally i added some stupid custom syntax :) 2023-10-11 16:28:24 +02:00
4ae79e6d92 Added submodule babel 2023-10-09 20:00:22 +02:00
975030d7ce moved to babel 2023-10-09 18:26:31 +02:00
4b03caa360 Smack my ass and call me larry the cable guy 2023-09-25 13:45:29 +02:00
42 changed files with 610 additions and 914 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
@ -166,5 +167,3 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
output_testing/

Binary file not shown.

View file

@ -18,26 +18,26 @@ export function registerValidationChecks(services: JstqlServices) {
* Implementation of custom validations.
*/
export class JstqlValidator {
validateWildcards(case_: Case, accept: ValidationAcceptor): void {
validateWildcards(pair: Case, accept: ValidationAcceptor): void {
try {
let validationResultAplTo = validateWildcardAplTo(
collectWildcard(case_.aplTo.apl_to_code.split(""))
collectWildcard(pair.aplTo.apl_to_code.split(""))
);
if (validationResultAplTo.errors.length != 0) {
accept("error", validationResultAplTo.errors.join("\n"), {
node: case_.aplTo,
node: pair.aplTo,
property: "apl_to_code",
});
}
let validationResultTraTo = validateWildcardTraTo(
collectWildcard(case_.traTo.transform_to_code.split("")),
collectWildcard(pair.traTo.transform_to_code.split("")),
validationResultAplTo.env
);
if (validationResultTraTo.length != 0) {
accept("error", validationResultTraTo.join("\n"), {
node: case_.traTo,
node: pair.traTo,
property: "transform_to_code",
});
}
@ -75,7 +75,7 @@ function validateWildcardAplTo(wildcards: string[]): ValidationResultAplTo {
}
if (!types) {
errors.push("No types given for wildcard " + identifier);
errors.push("No types given for value");
}
}
return { env, errors };

View file

@ -1,58 +1 @@
# JSTQL JavaScript Transform tool
This tool is created to transform JavaScript based on definitions written in a custom DSL called JSTQL
## JSTQL
This is a DSL created to define proposal transformations using templates. A transformation is defined by two templates, a template of what code snippets to search for **applicable to** and a template of how to transform those snippets **transform to**.
Examples of definitions for proposals can be found in [dsl_files](./dsl_files/)
## Running this project
This project exposes an api in the file [transform/transfrom.ts](./src/transform/transform.ts), which is meant to be used as this library's entry point.
To run this code as a standalone project, a file [index.ts](./index.ts) exists to facilitate running this project.
This project is created using TypeScript, and it is recommended to use [Bun](https://bun.sh/) to compile and run it.
### Building Langium required files
Firstly, building langium is required, this is done by the following:
```sh
# cd into JSTQL
cd JSTQL
# Install dependencies
npm i
# Build langium generated files
npm run langium:generate
# Build final JS source
npm run build
```
### Running a transformation
Now we are ready to run this project with the pipeline on a test JS file:
```sh
# cd into top level
cd ..
# Install dependencies
bun i
# Run an example transformation
bun run index.ts dsl_files/pipeline.jstql test_files/test.js
```
### Arguments of index.ts
Arguments
- First positional is path to JSTQL
- Second positional is path to JS file to transform
- -o is optional output path, if not given path is **./out.js**
# didactic-chainsaw

BIN
bun.lockb

Binary file not shown.

19
demo-site/index.html Normal file
View file

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<h1>Demo site</h1>
<p></p>
</body>
</html>

View file

@ -2,15 +2,15 @@ proposal awaitToPomise{
case single{
applicable to {
"let <<ident:Identifier>> = await <<awaitedExpr: Expression>>;
<<statements: (Statement && !ReturnStatement && !ContinueStatement &&!BreakStatement)+>>
<<statements: (Statement && !ReturnStatement)*>>
return <<returnExpr: Expression>>
"
}
transform to{
"return <<awaitedExpr>>.then(async <<ident>> => {
"return <<awaitedExpr>>.then((<<ident>>) => {
<<statements>>
return <<returnExpr>>
<<returnExpr>>
});"
}
}

View file

@ -1,32 +1,33 @@
proposal DoExpression{
case arrowFunction{
applicable to {
"(() => {
<<statements: (Statement && !ReturnStatement)+>>
"let <<ident:Identifier>> = () => {
<<statements: (Statement && !ReturnStatement)*>>
return <<returnVal : Expression>>;
})();"
}
"
}
transform to {
"(do {
"let <<ident>> = do {
<<statements>>
<<returnVal>>
})"
}"
}
}
case immediatelyInvokedUnnamedFunction {
applicable to {
"(function(){
<<statements: (Statement && !ReturnStatement)+>>
"let <<ident:Identifier>> = function(){
<<statements: (Statement && !ReturnStatement)*>>
return <<returnVal : Expression>>;
})();"
}();"
}
transform to {
"(do {
"let <<ident>> = do {
<<statements>>
<<returnVal>>
})"
}"
}
}
}

View file

@ -0,0 +1,16 @@
proposal MultiStmt{
case Smthn{
applicable to{
"let <<ident1:Identifier>> = <<funcIdent:Identifier | MemberExpression>>();
let <<ident2:Identifier>> = <<expr:Expression>>;
"
}
transform to {
"const ident2 = () => {
let <<ident1>> = <<funcIdent>>();
return <<expr>>;
}"
}
}
}

17
dsl_files/star.jstql Normal file
View file

@ -0,0 +1,17 @@
proposal Star{
case a {
applicable to {
"let <<ident:Identifier>> = () => {
<<statements: Statement>>
return <<returnVal : Expression>>;
}
"
}
transform to {
"let <<ident>> = do {
<<statements>>
<<returnVal>>
}"
}
}
}

View file

@ -1,19 +0,0 @@
proposal lol {
case a {
applicable to {
"let <<a: Identifier>> = Iterator.from(<<e: Expression>>);
let <<aa: Identifier>> = Iterator.from(<<ee: Expression>>);
let <<bbbb: Identifier>> = function* () {
yield* <<aaa: Identifier>>;
yield* <<aaaa: Identifier>>;
}();"
}
transform to {
"let <<a>> = Iterator.from(<<e>>);
let <<aa>> = Iterator.from(<<ee>>);
let <<bbbb>> = Iterator.concat(<<aaa>>, <<aaaa>>);"
}
}
}

View file

@ -0,0 +1,10 @@
proposal test_single_stmt{
pair one {
applicable to {
"let <<aaaa: Identifier >> = <<bbbb: Expression | Identifier>>"
}
transform to {
"let <<aaaa>> = 1 + <<bbbb>>;"
}
}
}

View file

@ -3,19 +3,15 @@ Wildcard:
Identifier ":" TypeExpr ("*"?)
TypeExpr:
BinaryExpr
| UnaryExpr
| PrimitiveExpr
BinaryExpr:
TypeExpr { Operator TypeExpr }*
UnaryExpr { Operator PrimitiveExpr }*
UnaryExpr:
{UnaryOperator}? TypeExpr
{UnaryOperator}? PrimitiveExpr
PrimitiveExpr:
GroupExpr | Identifier
GroupExpr:
"(" TypeExpr ")"
"(" TypeExpr ")"

View file

@ -1,55 +0,0 @@
import {
transform,
} from "./src/transform/transform";
import { parseJSTQL } from "./src/langium/langiumRunner";
import { parseArgs } from "util";
const { values: argVals, tokens: positional } = parseArgs({
options: {
o: {
type: "string",
},
},
tokens: true,
allowPositionals: true,
});
const main = async () => {
//transform(selfHostedTransformExampleMultiStmt, codeFromFile);
console.log(positional);
console.log(argVals);
if (!positional) {
throw new Error("Something is wrong with args");
}
if (
!positional[0] &&
positional[0].kind === "positional" &&
!positional[0].value.endsWith(".jstql")
) {
throw new Error("First positional argument is current JSTQL file");
}
if (!positional[1] || !positional[1].value.endsWith(".js")) {
throw new Error(
"Second positional argument is JS code to be transformed"
);
}
const jstql_file = positional[0].value;
const code_file = positional[1].value;
let jstqlString = await Bun.file(jstql_file).text();
let codeString = await Bun.file(code_file).text();
let parsedJSTQL = await parseJSTQL(jstqlString);
for (let proposal of parsedJSTQL) {
let [resultString, matches] = transform(proposal.cases, codeString);
let path = "./out.js";
if (argVals["o"]) {
path = argVals["o"];
}
await Bun.write(path, resultString);
}
};
main();

3
out.js
View file

@ -1,3 +0,0 @@
let something = Iterator.from([1, 2, 3]);
let someOtherThing = Iterator.from([4, 5, 6]);
let generate = Iterator.concat(something, someOtherThing);

94
output_files/output.js Normal file
View file

@ -0,0 +1,94 @@
function parse() {
const input = ("input" |> document.getElementById(%)).value;
const data = 32 |> input.slice(%);
const compressedData = data |> decode_base64(%);
const uncompressed = pako.inflate(compressedData, {
to: "string"
});
const json = uncompressed |> JSON.parse(%);
json |> console.log(%);
json |> convertToDesktop(%);
}
function convertToDesktop(json) {
const newValues = {
crb: false,
newClanRaidClassId: 0,
newClanRaidClassLevel: 0,
pendingImmortalSouls: 0,
pendingRaidRubies: 0,
immortalSouls: 0,
lastPurchaseTime: 0,
lastRaidAttemptTimestamp: 0,
lastRaidRewardCheckTimestamp: 0,
shouldShowHZERoster: false,
lastBonusRewardCheckTimestamp: 0
};
const mappedValues = {
rubies: json.rubies / 10 |> Math.round(%)
};
const pcSpecificValues = {
readPatchNumber: "1.0e12",
saveOrigin: "pc"
};
const hash = "7a990d405d2c6fb93aa8fbb0ec1a3b23";
const newData = {
...newValues,
...json,
...mappedValues,
...pcSpecificValues
};
const compressed = pako.deflate(newData |> JSON.stringify(%), {
to: "string"
});
const base64 = compressed |> btoa(%);
const finalSaveString = hash + base64;
("output_output" |> document.getElementById(%)).innerText = finalSaveString;
showOutput();
}
function showOutput() {
("outputs" |> document.getElementById(%)).style.visibility = "visible";
}
function copyOutput() {
const output = "output_output" |> document.getElementById(%);
output.disabled = false;
output.focus();
output.select();
"copy" |> document.execCommand(%);
output.disabled = true;
const successElement = "copy_success_msg" |> document.getElementById(%);
successElement.style.visibility = "visible";
setTimeout(() => successElement.style.visibility = "hidden", 4000);
}
function decode_base64(s) {
let e = {},
i,
k,
v = [],
r = "",
w = String.fromCharCode;
let n = [[65, 91], [97, 123], [48, 58], [43, 44], [47, 48]];
for (z in n) {
for (i = n[z][0]; i < n[z][1]; i++) {
i |> w(%) |> v.push(%);
}
}
for (i = 0; i < 64; i++) {
e[v[i]] = i;
}
for (i = 0; i < s.length; i += 72) {
let b = 0,
c,
x,
l = 0,
o = s.substring(i, i + 72);
for (x = 0; x < o.length; x++) {
c = e[x |> o.charAt(%)];
b = (b << 6) + c;
l += 6;
while (l >= 8) {
r += (b >>> (l -= 8)) % 256 |> w(%);
}
}
}
return r;
}

View file

@ -0,0 +1,100 @@
// "fast-glob" and `createTwoFilesPatch` are bundled here since the API uses `micromatch` and `diff` too
import { createTwoFilesPatch } from "diff/lib/patch/create.js";
import fastGlob from "fast-glob";
import * as vnopts from "vnopts";
import * as errors from "./common/errors.js";
import getFileInfoWithoutPlugins from "./common/get-file-info.js";
import mockable from "./common/mockable.js";
import { clearCache as clearConfigCache, resolveConfig, resolveConfigFile } from "./config/resolve-config.js";
import * as core from "./main/core.js";
import { formatOptionsHiddenDefaults } from "./main/normalize-format-options.js";
import normalizeOptions from "./main/normalize-options.js";
import * as optionCategories from "./main/option-categories.js";
import { clearCache as clearPluginCache, loadBuiltinPlugins, loadPlugins } from "./main/plugins/index.js";
import { getSupportInfo as getSupportInfoWithoutPlugins, normalizeOptionSettings } from "./main/support.js";
import { createIsIgnoredFunction } from "./utils/ignore.js";
import isNonEmptyArray from "./utils/is-non-empty-array.js";
import omit from "./utils/object-omit.js";
import partition from "./utils/partition.js";
/**
* @param {*} fn
* @param {number} [optionsArgumentIndex]
* @returns {*}
*/
function withPlugins(fn, optionsArgumentIndex = 1 // Usually `options` is the 2nd argument
) {
return async (...args) => {
const options = args[optionsArgumentIndex] ?? {};
const {
plugins = []
} = options;
args[optionsArgumentIndex] = {
...options,
plugins: (await Promise.all([loadBuiltinPlugins(),
// TODO: standalone version allow `plugins` to be `prettierPlugins` which is an object, should allow that too
loadPlugins(plugins)])).flat()
};
return fn(...args);
};
}
const formatWithCursor = withPlugins(core.formatWithCursor);
async function format(text, options) {
const {
formatted
} = await formatWithCursor(text, {
...options,
cursorOffset: -1
});
return formatted;
}
async function check(text, options) {
return (await format(text, options)) === text;
}
// eslint-disable-next-line require-await
async function clearCache() {
clearConfigCache();
clearPluginCache();
}
/** @type {typeof getFileInfoWithoutPlugins} */
const getFileInfo = withPlugins(getFileInfoWithoutPlugins);
/** @type {typeof getSupportInfoWithoutPlugins} */
const getSupportInfo = withPlugins(getSupportInfoWithoutPlugins, 0);
// Internal shared with cli
const sharedWithCli = {
errors,
optionCategories,
createIsIgnoredFunction,
formatOptionsHiddenDefaults,
normalizeOptions,
getSupportInfoWithoutPlugins,
normalizeOptionSettings,
vnopts: {
ChoiceSchema: vnopts.ChoiceSchema,
apiDescriptor: vnopts.apiDescriptor
},
fastGlob,
createTwoFilesPatch,
utils: {
isNonEmptyArray,
partition,
omit
},
mockable
};
const debugApis = {
parse: withPlugins(core.parse),
formatAST: withPlugins(core.formatAst),
formatDoc: withPlugins(core.formatDoc),
printToDoc: withPlugins(core.printToDoc),
printDocToString: withPlugins(core.printDocToString),
mockable
};
export { debugApis as __debug, sharedWithCli as __internal, check, clearCache as clearConfigCache, format, formatWithCursor, getFileInfo, getSupportInfo, resolveConfig, resolveConfigFile };
export * as doc from "./document/public.js";
export { default as version } from "./main/version.evaluate.cjs";
export * as util from "./utils/public.js";

12
output_files/output_do.js Normal file
View file

@ -0,0 +1,12 @@
let aaaa = do {
let g = 100;
let ff = 10;
let ggg = a(b);
100;
};
let bbaaa = do {
let lllll = 1 + 1;
100 + 100;
const aaaaa = aaaa(bb);
lllll;
};

View file

@ -0,0 +1,2 @@
let ThisTest = LOOOOOOOOL();
let HAHHAHAH = 1 + 1;

View file

@ -0,0 +1,4 @@
a |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%);
a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, b)))))));
a |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a);
b(b(b(b(a, a, a), a, a), a, a), a, a);

View file

@ -0,0 +1,98 @@
// "fast-glob" and `createTwoFilesPatch` are bundled here since the API uses `micromatch` and `diff` too
import { createTwoFilesPatch } from "diff/lib/patch/create.js";
import fastGlob from "fast-glob";
import * as vnopts from "vnopts";
import * as errors from "./common/errors.js";
import getFileInfoWithoutPlugins from "./common/get-file-info.js";
import mockable from "./common/mockable.js";
import { clearCache as clearConfigCache, resolveConfig, resolveConfigFile } from "./config/resolve-config.js";
import * as core from "./main/core.js";
import { formatOptionsHiddenDefaults } from "./main/normalize-format-options.js";
import normalizeOptions from "./main/normalize-options.js";
import * as optionCategories from "./main/option-categories.js";
import { clearCache as clearPluginCache, loadBuiltinPlugins, loadPlugins } from "./main/plugins/index.js";
import { getSupportInfo as getSupportInfoWithoutPlugins, normalizeOptionSettings } from "./main/support.js";
import { createIsIgnoredFunction } from "./utils/ignore.js";
import isNonEmptyArray from "./utils/is-non-empty-array.js";
import omit from "./utils/object-omit.js";
import partition from "./utils/partition.js";
/**
* @param {*} fn
* @param {number} [optionsArgumentIndex]
* @returns {*}
*/
function withPlugins(fn, optionsArgumentIndex = 1 // Usually `options` is the 2nd argument
) {
return async (...args) => {
const options = args[optionsArgumentIndex] ?? {};
const {
plugins = []
} = options;
args[optionsArgumentIndex] = {
...options,
plugins: (await ([loadBuiltinPlugins(), plugins |> loadPlugins(%)] |> Promise.all(%))).flat()
};
return fn(...args);
};
}
const formatWithCursor = core.formatWithCursor |> withPlugins(%);
async function format(text, options) {
const {
formatted
} = await (text |> formatWithCursor(%, {
...options,
cursorOffset: -1
}));
return formatted;
}
async function check(text, options) {
return (await (text |> format(%, options))) === text;
}
// eslint-disable-next-line require-await
async function clearCache() {
clearConfigCache();
clearPluginCache();
}
/** @type {typeof getFileInfoWithoutPlugins} */
const getFileInfo = getFileInfoWithoutPlugins |> withPlugins(%);
/** @type {typeof getSupportInfoWithoutPlugins} */
const getSupportInfo = getSupportInfoWithoutPlugins |> withPlugins(%, 0);
// Internal shared with cli
const sharedWithCli = {
errors,
optionCategories,
createIsIgnoredFunction,
formatOptionsHiddenDefaults,
normalizeOptions,
getSupportInfoWithoutPlugins,
normalizeOptionSettings,
vnopts: {
ChoiceSchema: vnopts.ChoiceSchema,
apiDescriptor: vnopts.apiDescriptor
},
fastGlob,
createTwoFilesPatch,
utils: {
isNonEmptyArray,
partition,
omit
},
mockable
};
const debugApis = {
parse: core.parse |> withPlugins(%),
formatAST: core.formatAst |> withPlugins(%),
formatDoc: core.formatDoc |> withPlugins(%),
printToDoc: core.printToDoc |> withPlugins(%),
printDocToString: core.printDocToString |> withPlugins(%),
mockable
};
export { debugApis as __debug, sharedWithCli as __internal, check, clearCache as clearConfigCache, format, formatWithCursor, getFileInfo, getSupportInfo, resolveConfig, resolveConfigFile };
export * as doc from "./document/public.js";
export { default as version } from "./main/version.evaluate.cjs";
export * as util from "./utils/public.js";

306
package-lock.json generated
View file

@ -15,8 +15,7 @@
"@types/babel-traverse": "^6.25.10",
"babel": "^6.23.0",
"bun": "^1.0.4",
"ts-node": "^10.9.1",
"util": "^0.12.5"
"ts-node": "^10.9.1"
},
"devDependencies": {
"@babel/plugin-proposal-pipeline-operator": "^7.23.3",
@ -1002,21 +1001,6 @@
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
"integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA=="
},
"node_modules/available-typed-arrays": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/available-typed-arrays/-/available-typed-arrays-1.0.7.tgz",
"integrity": "sha512-wvUjBtSGN7+7SjNpq/9M2Tg350UZD3q62IFZLbRAR1bSMlCo1ZaeW+BJ+D090e4hIIZLBcTDWe4Mh4jvUDajzQ==",
"license": "MIT",
"dependencies": {
"possible-typed-array-names": "^1.0.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/babel": {
"version": "6.23.0",
"resolved": "https://registry.npmjs.org/babel/-/babel-6.23.0.tgz",
@ -1309,25 +1293,6 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/call-bind": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.7.tgz",
"integrity": "sha512-GHTSNSYICQ7scH7sZ+M2rFopRoLh8t2bLSW6BbgrtLsahOIB5iyAVJf9GjWK3cYTDaMj4XdBpM1cA6pIS0Kv2w==",
"license": "MIT",
"dependencies": {
"es-define-property": "^1.0.0",
"es-errors": "^1.3.0",
"function-bind": "^1.1.2",
"get-intrinsic": "^1.2.4",
"set-function-length": "^1.2.1"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/caniuse-lite": {
"version": "1.0.30001574",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001574.tgz",
@ -1487,23 +1452,6 @@
"node": ">=10"
}
},
"node_modules/define-data-property": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz",
"integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==",
"license": "MIT",
"dependencies": {
"es-define-property": "^1.0.0",
"es-errors": "^1.3.0",
"gopd": "^1.0.1"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
@ -1526,27 +1474,6 @@
"once": "^1.4.0"
}
},
"node_modules/es-define-property": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.0.tgz",
"integrity": "sha512-jxayLKShrEqqzJ0eumQbVhTYQM27CfT1T35+gCgDFoL82JLsXqTJ76zv6A0YLOgEnLUMvLzsDsGIrl8NFpT2gQ==",
"license": "MIT",
"dependencies": {
"get-intrinsic": "^1.2.4"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-errors": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",
"integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/escalade": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz",
@ -1720,24 +1647,6 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/for-each": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz",
"integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==",
"license": "MIT",
"dependencies": {
"is-callable": "^1.1.3"
}
},
"node_modules/function-bind": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/gensync": {
"version": "1.0.0-beta.2",
"resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz",
@ -1746,25 +1655,6 @@
"node": ">=6.9.0"
}
},
"node_modules/get-intrinsic": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz",
"integrity": "sha512-5uYhsJH8VJBTv7oslg4BznJYhDoRI6waYCxMmCdnTrcCrHA/fCFKoTFz2JKKE0HdDFUF7/oQuhzumXJK7paBRQ==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0",
"function-bind": "^1.1.2",
"has-proto": "^1.0.1",
"has-symbols": "^1.0.3",
"hasown": "^2.0.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/get-stream": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",
@ -1794,18 +1684,6 @@
"node": ">=4"
}
},
"node_modules/gopd": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz",
"integrity": "sha512-d65bNlIadxvpb/A2abVdlqKqV563juRnZ1Wtk6s1sIR8uNsXR70xqIzVqxVf1eTqDunwT2MkczEeaezCKTZhwA==",
"license": "MIT",
"dependencies": {
"get-intrinsic": "^1.1.3"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/got": {
"version": "11.8.6",
"resolved": "https://registry.npmjs.org/got/-/got-11.8.6.tgz",
@ -1839,69 +1717,6 @@
"node": ">=4"
}
},
"node_modules/has-property-descriptors": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz",
"integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==",
"license": "MIT",
"dependencies": {
"es-define-property": "^1.0.0"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/has-proto": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.3.tgz",
"integrity": "sha512-SJ1amZAJUiZS+PhsVLf5tGydlaVB8EdFpaSO4gmiUKUOxk8qzn5AIy4ZeJUmh22znIdk/uMAUT2pl3FxzVUH+Q==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/has-symbols": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz",
"integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/has-tostringtag": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz",
"integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==",
"license": "MIT",
"dependencies": {
"has-symbols": "^1.0.3"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/hasown": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz",
"integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==",
"license": "MIT",
"dependencies": {
"function-bind": "^1.1.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/http-cache-semantics": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz",
@ -1953,35 +1768,8 @@
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"node_modules/is-arguments": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.1.1.tgz",
"integrity": "sha512-8Q7EARjzEnKpt/PCD7e1cgUS0a6X8u5tdSiMqXhojOdoV9TsMsiO+9VLC5vAmO8N7/GmXn7yjR8qnA6bVAEzfA==",
"license": "MIT",
"dependencies": {
"call-bind": "^1.0.2",
"has-tostringtag": "^1.0.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/is-callable": {
"version": "1.2.7",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.7.tgz",
"integrity": "sha512-1BC0BVFhS/p0qtw6enp8e+8OD0UrK0oFLztSjNzhcKA3WDuJxxAPXzPuPtKkjEY9UUoEWlX/8fgKeu2S8i9JTA==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"dev": true
},
"node_modules/is-extglob": {
"version": "2.1.1",
@ -1992,21 +1780,6 @@
"node": ">=0.10.0"
}
},
"node_modules/is-generator-function": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.10.tgz",
"integrity": "sha512-jsEjy9l3yiXEQ+PsXdmBwEPcOxaXWLspKdplFUVI9vq1iZgIekeC0L167qeu86czQaxed3q/Uzuw0swL0irL8A==",
"license": "MIT",
"dependencies": {
"has-tostringtag": "^1.0.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/is-glob": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
@ -2046,21 +1819,6 @@
"node": ">=0.10.0"
}
},
"node_modules/is-typed-array": {
"version": "1.1.13",
"resolved": "https://registry.npmjs.org/is-typed-array/-/is-typed-array-1.1.13.tgz",
"integrity": "sha512-uZ25/bUAlUY5fR4OKT4rZQEBrzQWYV9ZJYGGsUmEJ6thodVJ1HX64ePQ6Z0qPWP+m+Uq6e9UugrE38jeYsDSMw==",
"license": "MIT",
"dependencies": {
"which-typed-array": "^1.1.14"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/isexe": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
@ -2324,15 +2082,6 @@
"node": ">=0.10.0"
}
},
"node_modules/possible-typed-array-names": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz",
"integrity": "sha512-d7Uw+eZoloe0EHDIYoe+bQ5WXnGMOpmiZFTuMWCwpjzzkL2nTjcKiAk4hh8TjnGye2TwWOk3UXucZ+3rbmBa8Q==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/pseudomap": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz",
@ -2542,23 +2291,6 @@
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
"dev": true
},
"node_modules/set-function-length": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.2.tgz",
"integrity": "sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==",
"license": "MIT",
"dependencies": {
"define-data-property": "^1.1.4",
"es-errors": "^1.3.0",
"function-bind": "^1.1.2",
"get-intrinsic": "^1.2.4",
"gopd": "^1.0.1",
"has-property-descriptors": "^1.0.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/shebang-command": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz",
@ -2827,19 +2559,6 @@
"browserslist": ">= 4.21.0"
}
},
"node_modules/util": {
"version": "0.12.5",
"resolved": "https://registry.npmjs.org/util/-/util-0.12.5.tgz",
"integrity": "sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==",
"license": "MIT",
"dependencies": {
"inherits": "^2.0.3",
"is-arguments": "^1.0.4",
"is-generator-function": "^1.0.7",
"is-typed-array": "^1.1.3",
"which-typed-array": "^1.1.2"
}
},
"node_modules/util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
@ -2863,25 +2582,6 @@
"which": "bin/which"
}
},
"node_modules/which-typed-array": {
"version": "1.1.15",
"resolved": "https://registry.npmjs.org/which-typed-array/-/which-typed-array-1.1.15.tgz",
"integrity": "sha512-oV0jmFtUky6CXfkqehVvBP/LSWJ2sy4vWMioiENyJLePrBO/yKyV9OyJySfAKosh+RYkIl5zJCNZ8/4JncrpdA==",
"license": "MIT",
"dependencies": {
"available-typed-arrays": "^1.0.7",
"call-bind": "^1.0.7",
"for-each": "^0.3.3",
"gopd": "^1.0.1",
"has-tostringtag": "^1.0.2"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",

View file

@ -20,11 +20,10 @@
"@babel/generator": "^7.23.0",
"@babel/parser": "^7.23.0",
"@babel/traverse": "^7.23.0",
"@types/babel-traverse": "^6.25.10",
"@types/babel__traverse": "^7.20.5",
"@types/babel-traverse": "^6.25.10",
"babel": "^6.23.0",
"bun": "^1.0.4",
"ts-node": "^10.9.1",
"util": "^0.12.5"
"ts-node": "^10.9.1"
}
}

View file

@ -1,8 +0,0 @@
import { SelfHostedRecipe } from "../transform/transform";
import { transform } from "../transform/transform";
export function transformSH(spec: SelfHostedRecipe[], code: string) {
let res = transform(spec, code);
return res;
}

View file

@ -1,66 +0,0 @@
import { transform } from "./transform/transform";
import { readdir } from "node:fs/promises";
import { parseJSTQL } from "./langium/langiumRunner";
const main = async () => {
let basepathExamplesJSFiles = "../three.js";
let examples = (await readdir(basepathExamplesJSFiles, { recursive: true }))
.filter((x) => x.endsWith(".js"))
.map((x) => basepathExamplesJSFiles + "/" + x);
console.log(examples);
let result = [];
for (let proposalFile of [
"pipeline.jstql",
"do.jstql",
"awaitToPromise.jstql",
]) {
const jstql_file = "dsl_files/" + proposalFile;
const test_file = Bun.file(jstql_file);
const test_JSTQL = await test_file.text();
let proposals = await parseJSTQL(test_JSTQL);
let sum = 0;
let failures = 0;
let filesSucceeded = 0;
console.log("Scripts found ", sum, "matches!");
let count = 0;
for (let examplesFile of examples) {
try {
if (examplesFile.split("/").includes("compiled")) {
//continue;
}
console.log(examplesFile);
let script = await Bun.file(examplesFile).text();
let [resultString, matches] = transform(
proposals[0].cases,
script
);
sum += matches;
console.log(matches);
if (matches > 0) {
await Bun.write(
"output_testing/" +
count +
examplesFile.split("/").join("_"),
resultString
);
count += 1;
}
filesSucceeded += 1;
} catch (e) {
failures += 1;
//console.log(e);
}
console.log("current sum", sum);
}
result.push(
proposalFile + ", " + sum + ", " + count + ", " + filesSucceeded
);
}
for (let res of result) {
console.log(res);
}
};
main();

31
src/index.ts Normal file
View file

@ -0,0 +1,31 @@
//import * as babelparser from "../babel/packages/babel-parser";
import * as babelparser from "@babel/parser";
//import core from "../babel/packages/babel-core";
import { parse_with_plugins } from "./parser/parse";
import {
SelfHostedRecipe,
TransformRecipe,
transform,
} from "./transform/transform";
import { parseJSTQL } from "./langium/langiumRunner";
const dir = "../prettier/src";
const path = "../prettier/src/index.js";
const file = Bun.file(path);
const codeFromFile = await file.text();
const main = async () => {
//transform(selfHostedTransformExampleMultiStmt, codeFromFile);
console.log(codeFromFile);
const jstql_file =
"/home/rolfmg/Coding/Master/didactic-chainsaw/dsl_files/pipeline.jstql";
const test_file = Bun.file(jstql_file);
const test_JSTQL = await test_file.text();
let proposals = await parseJSTQL(test_JSTQL);
let code = transform(proposals[0].cases, codeFromFile);
await Bun.write("output_files/testingLOL.js", code);
};
main();

View file

@ -200,10 +200,17 @@ export class Matcher {
return MatchResult.NoMatch;
}
}
for (let key of Object.keys(aplToNode)) {
if (key in keys_to_ignore) {
continue;
}
return codeNode.type === aplToNode.type
? MatchResult.Matched
: MatchResult.NoMatch;
if (!Object.keys(codeNode).includes(key)) {
return MatchResult.NoMatch;
}
}
return MatchResult.Matched;
}
multiStatementMatcher(code: TreeNode<t.Node>, aplTo: TreeNode<t.Node>) {
@ -226,7 +233,7 @@ export class Matcher {
let statements: TreeNode<PairedNodes>[] = [];
let aplToi = 0;
let codei = 0;
while (aplToi < aplTo.length && codei + y < code.length) {
while (aplToi < aplTo.length) {
let [paired, matchResult] = this.exactExprMatcher(
code[codei + y],
aplTo[aplToi]
@ -260,9 +267,6 @@ export class Matcher {
aplToi += 1;
codei += 1;
}
if (aplToi !== aplTo.length) {
fullMatch = false;
}
if (fullMatch) {
this.matches.push({ statements });
}
@ -320,7 +324,6 @@ export class Matcher {
i -= 1;
break;
}
pairedChild.element.codeNode.push(
...maybeChild.element.codeNode
);
@ -331,10 +334,6 @@ export class Matcher {
i += 1;
aplToi += 1;
}
// Verify it is a full match
if (aplToi < aplTo.children.length) {
return [undefined, MatchResult.NoMatch];
}
if (i < code.children.length) {
return [undefined, MatchResult.NoMatch];
}

View file

@ -24,7 +24,7 @@ export function parseInternalTraTo(code: string): string {
// We encountered a closing tag
flag = false;
cleanedJS += "_$$_" + temp + "_$$_";
cleanedJS += temp;
i += 1;
temp = "";
@ -59,8 +59,7 @@ export function parseInternalAplTo(code: string): InternalParseResult {
let wildcard = new WildcardParser(
new WildcardTokenizer(temp).tokenize()
).parse();
wildcard.identifier.name =
"_$$_" + wildcard.identifier.name + "_$$_";
cleanedJS += wildcard.identifier.name;
prelude.push(wildcard);
@ -77,6 +76,7 @@ export function parseInternalAplTo(code: string): InternalParseResult {
}
return { prelude, cleanedJS };
}
export interface Identifier extends WildcardNode {
nodeType: "Identifier";
name: string;
@ -154,7 +154,7 @@ export class WildcardParser {
let identifier = this.Identifier();
this.Semicolon();
let multidenoted = this.TypeExpr();
let star = this.Pluss();
let star = this.Star();
return {
nodeType: "Wildcard",
identifier,
@ -163,8 +163,8 @@ export class WildcardParser {
};
}
private Pluss(): boolean {
if (this.peek() && this.peek().tokenKind === "Pluss") {
private Star(): boolean {
if (this.peek() && this.peek().tokenKind === "Star") {
this.advance();
return true;
} else {

View file

@ -19,13 +19,13 @@ function extractValues(types: t.Statement[]): Wildcard[] {
if (init) {
if (init.type == "StringLiteral") {
init = <t.StringLiteral>init;
let wildcard = new WildcardParser(
new WildcardTokenizer(
innerDSLVariableName + ":" + init
).tokenize()
).parse();
prelude.push(wildcard);
prelude.push(
new WildcardParser(
new WildcardTokenizer(
innerDSLVariableName + ":" + init
).tokenize()
).parse()
);
} else {
throw new Error(
"Invalid usage of right side declaration in prelude"

View file

@ -4,7 +4,7 @@ type TokenKind =
| "Identifier"
| "OpeningParenthesis"
| "ClosingParenthesis"
| "Pluss"
| "Star"
| "Semicolon";
export interface WildcardToken {
@ -79,8 +79,8 @@ export class WildcardTokenizer {
}
break;
}
case "+": {
this.consumeToken("Pluss", char);
case "*": {
this.consumeToken("Star", char);
break;
}
case ":": {
@ -116,7 +116,7 @@ export class WildcardTokenizer {
}
private isAlpha(val: string): boolean {
let alphabet = new Set(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890".split("")
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_".split("")
);
return alphabet.has(val);
}

View file

@ -1,9 +1,9 @@
async function something() {
let a = 100;
a *= 100000;
return fetch("https://uib.no").then(async uib => {
return fetch("https://uib.no").then(uib => {
a += 100000;
a -= 1000;
return [a, uib];
[a, uib];
});
}

View file

@ -2,11 +2,11 @@ let aaaa = do {
let g = 100;
let ff = 10;
let ggg = a(b);
100
100;
};
var bbaaa = do {
let bbaaa = do {
let lllll = 1 + 1;
100 + 100;
const aaaaa = aaaa(bb);
lllll
lllll;
};

View file

@ -22,23 +22,23 @@ let pipelineResFile = await Bun.file(
"src/test/test_outputs/pipeline_output.js"
).text();
test("Test code: pipeline", () => {
expect(pipelineRes).toEqual([pipelineResFile, 29]);
expect(pipelineRes).toBe(pipelineResFile);
});
let doRes = await runTest("test_files/do_test.js", "dsl_files/do.jstql");
let doResFile = await Bun.file("src/test/test_outputs/do_output.js").text();
test("Test code: do", () => {
expect(doRes).toEqual([doResFile, 2]);
expect(doRes).toBe(doResFile);
});
let awaitToPromise = await runTest(
"test_files/awaitToPromise.js",
"dsl_files/awaitToPromise.jstql"
"test_files/do_test.js",
"dsl_files/do.jstql"
);
let awaitToPromiseOutput = await Bun.file(
"src/test/test_outputs/awaitToPromise_output.js"
"src/test/test_outputs/do_output.js"
).text();
test("Test code: await to promise", () => {
expect(awaitToPromise).toEqual([awaitToPromiseOutput, 1]);
test("Test code: do", () => {
expect(awaitToPromise).toBe(awaitToPromiseOutput);
});

View file

@ -28,16 +28,13 @@ export interface TransformRecipe {
export interface SelfHostedRecipe extends TransformRecipe {
prelude: string;
}
export function transform(
recipes: TransformRecipe[],
code: string
): [string, number] {
export function transform(recipes: TransformRecipe[], code: string): string {
let codeAST: t.Node = parse_with_plugins(code);
let amount = 0;
for (let recipe of recipes) {
if ((<SelfHostedRecipe>recipe).prelude !== undefined) {
// We are using the self hosted version
let temp = transformSelfHosted(
codeAST = transformSelfHosted(
{
applicableTo: recipe.applicableTo,
transformTo: recipe.transformTo,
@ -45,8 +42,6 @@ export function transform(
preludeBuilder((recipe as SelfHostedRecipe).prelude),
codeAST
);
codeAST = temp[0];
amount += temp[1];
} else {
// We are using JSTQL
// We have to parse JSTQL to the self hosted version
@ -56,26 +51,24 @@ export function transform(
);
let transformTo = parseInternalTraTo(recipe.transformTo);
let temp = transformSelfHosted(
codeAST = transformSelfHosted(
{ applicableTo, transformTo },
prelude,
codeAST
);
codeAST = temp[0];
amount += temp[1];
}
}
let output = generate(codeAST, { topicToken: "%" }).code;
//showTree(transformToTree);
return [output, amount];
return output;
}
export function transformSelfHosted(
function transformSelfHosted(
recipe: TransformRecipe,
internals: Wildcard[],
codeAST: t.Node
): [t.Node, number] {
): t.Node {
let codeTree = makeTree(codeAST as babelparser.ParseResult<t.File>);
let applicabelToAST = parse_with_plugins(recipe.applicableTo);
@ -90,9 +83,9 @@ export function transformSelfHosted(
) {
throw new Error("This no worky LOL");
}
let matches = runMatch(codeTree, applicableToTree, internals);
console.log("We found", matches.length, "matches");
let outputAST = transformer(matches, transformToTree, codeAST, transformTo);
return [outputAST, matches.length];
return outputAST;
}

View file

@ -12,7 +12,6 @@ import { Match, MatchedTreeNode, PairedNodes } from "../matcher/matcher";
import traverse from "@babel/traverse";
import generate from "@babel/generator";
import { TransformRecipe } from "./transform";
import { Wildcard } from "../parser/parse";
export function transformer(
matches: Match[],
@ -20,122 +19,132 @@ export function transformer(
codeAST: t.Node,
traToAST: t.File
): t.Node {
let transformedTransformTo: Map<t.Node, [t.File, Match]> = new Map();
for (let match of matches.reverse()) {
try {
let traToWithWildcards = structuredClone(traToAST);
let wildcardMatches = extractWildcardPairs(match);
for (let match_stmt of match.statements) {
transformMatch(match_stmt, transformTo, traToWithWildcards);
}
traverse(codeAST, {
enter(path) {
if (
!(
path.node.type === "Program" ||
path.node.type === "File"
)
) {
if (
path.node ===
match.statements[0].element.codeNode[0]
) {
path.replaceWithMultiple(
traToWithWildcards.program.body
);
let siblings = path.getAllNextSiblings();
transformedTransformTo.set(
match.statements[0].element.codeNode[0],
[
transformMatchFaster(wildcardMatches, traToWithWildcards),
match,
]
);
// For multi line applicable to
for (
let i = 0;
i < match.statements.length - 1;
i++
) {
siblings[i].remove();
}
// For when we have matched with *
for (let matchStmt of match.statements) {
for (let [
i,
stmtMatchedWithStar,
] of matchStmt.element.codeNode.entries()) {
let siblingnodes = siblings.map(
(a) => a.node
);
if (
siblingnodes.includes(
stmtMatchedWithStar
)
) {
let index =
siblingnodes.indexOf(
stmtMatchedWithStar
);
siblings[index].remove();
}
}
}
}
}
},
});
} catch (e) {
console.log(e);
}
}
traverse(codeAST, {
enter(path) {
if (!(path.node.type === "Program" || path.node.type === "File")) {
if (transformedTransformTo.has(path.node)) {
let [traToWithWildcards, match] =
transformedTransformTo.get(path.node) as [
t.File,
Match
];
path.replaceWithMultiple(traToWithWildcards.program.body);
let siblings = path.getAllNextSiblings();
// For multi line applicable to
for (let i = 0; i < match.statements.length - 1; i++) {
//siblings[i].remove();
}
// For when we have matched with +
for (let matchStmt of match.statements) {
for (let stmtMatchedWithPlus of matchStmt.element
.codeNode) {
let siblingnodes = siblings.map((a) => a.node);
if (siblingnodes.includes(stmtMatchedWithPlus)) {
let index =
siblingnodes.indexOf(stmtMatchedWithPlus);
siblings[index].remove();
}
}
}
}
}
},
});
return codeAST;
}
function transformMatchFaster(
wildcardMatches: Map<string, t.Node[]>,
transformTo: t.File
): t.File {
traverse(transformTo, {
Identifier: (path) => {
if (wildcardMatches.has(path.node.name)) {
let toReplaceWith = wildcardMatches.get(path.node.name);
if (toReplaceWith) {
path.replaceWithMultiple(toReplaceWith);
export function transformMatch(
match: TreeNode<PairedNodes>,
trnTo: TreeNode<t.Node>,
output: t.Node
) {
let isMatchingIdentifier = matchNode(
match.element.aplToNode,
trnTo.element
);
if (isMatchingIdentifier) {
traverse(output, {
Identifier: (path) => {
if (path.node.name === (<t.Identifier>trnTo.element).name) {
path.replaceWithMultiple(match.element.codeNode);
}
}
},
ExpressionStatement: (path) => {
if (path.node.expression.type === "Identifier") {
let name = path.node.expression.name;
if (wildcardMatches.has(name)) {
let toReplaceWith = wildcardMatches.get(name);
if (toReplaceWith) {
path.replaceWithMultiple(toReplaceWith);
},
ExpressionStatement: (path) => {
if (trnTo.element.type === "ExpressionStatement") {
if (
path.node.expression.type === "Identifier" &&
trnTo.element.expression.type === "Identifier"
) {
let ident = path.node.expression;
if (ident.name === trnTo.element.expression.name) {
path.replaceWithMultiple(match.element.codeNode);
}
}
}
}
},
});
return transformTo;
},
});
} else {
for (let match_child of match.children) {
transformMatch(match_child, trnTo, output);
}
for (let trnTo_child of trnTo.children) {
transformMatch(match, trnTo_child, output);
}
}
}
function extractWildcardPairs(match: Match): Map<string, t.Node[]> {
let map: Map<string, t.Node[]> = new Map();
function matchNode(aplTo: t.Node, trnTo: t.Node): boolean {
//console.log(trnTo);
function recursiveSearch(node: TreeNode<PairedNodes>) {
let name: null | string = null;
if (node.element.aplToNode.type === "Identifier") {
name = node.element.aplToNode.name;
} else if (
node.element.aplToNode.type === "ExpressionStatement" &&
node.element.aplToNode.expression.type === "Identifier"
if (
trnTo.type === "ExpressionStatement" &&
aplTo.type == "ExpressionStatement"
) {
if (
trnTo.expression.type === "Identifier" &&
aplTo.expression.type === "Identifier"
) {
name = node.element.aplToNode.expression.name;
}
if (name && name.startsWith("_$$_")) {
if (map.has(name)) {
console.log(name);
console.log(map.get(name));
throw new Error("Wildcard encountered twice!");
}
map.set(name, node.element.codeNode);
}
for (let child of node.children) {
recursiveSearch(child);
return aplTo.expression.name === trnTo.expression.name;
}
}
for (let stmt of match.statements) {
recursiveSearch(stmt);
if (trnTo.type == "Identifier" && aplTo.type == "Identifier") {
return aplTo.name === trnTo.name;
}
return map;
return false;
}
function washName(name: string): string {

View file

@ -1,9 +1,9 @@
let aaaa = (() => {
let aaaa = () => {
let g = 100;
let ff = 10;
let ggg = a(b);
return 100;
})();
};
var bbaaa = (function () {
let lllll = 1 + 1;

View file

@ -0,0 +1,7 @@
let a = LOOOOOOOOL();
let b = (999 * 128) / 12;
const haha = () => {
let a = LOOOOOOOOL();
let b = (999 * 128) / 12;
};

View file

@ -0,0 +1,8 @@
let something = 1 + 1;
let yikers = hahahah;
let lol = () => 100 + 100;
function haha() {
let fhdsjkfhdsjkfhds = fjhdkslfjhdsklfjdskl;
}

4
test_files/star_test.js Normal file
View file

@ -0,0 +1,4 @@
let x = () => {
let b = 0;
return b;
};

View file

@ -1,208 +0,0 @@
let _geometry;
const _intersectPoint = /*@__PURE__*/ new Vector3();
const _worldScale = /*@__PURE__*/ new Vector3();
const _mvPosition = /*@__PURE__*/ new Vector3();
const _alignedPosition = /*@__PURE__*/ new Vector2();
const _rotatedPosition = /*@__PURE__*/ new Vector2();
const _viewWorldMatrix = /*@__PURE__*/ new Matrix4();
const _vA = /*@__PURE__*/ new Vector3();
const _vB = /*@__PURE__*/ new Vector3();
const _vC = /*@__PURE__*/ new Vector3();
const _uvA = /*@__PURE__*/ new Vector2();
const _uvB = /*@__PURE__*/ new Vector2();
const _uvC = /*@__PURE__*/ new Vector2();
class Sprite extends Object3D {
constructor(material = new SpriteMaterial()) {
super();
this.isSprite = true;
this.type = "Sprite";
if (_geometry === undefined) {
_geometry = new BufferGeometry();
const float32Array = new Float32Array([
-0.5, -0.5, 0, 0, 0, 0.5, -0.5, 0, 1, 0, 0.5, 0.5, 0, 1, 1,
-0.5, 0.5, 0, 0, 1,
]);
const interleavedBuffer = new InterleavedBuffer(float32Array, 5);
_geometry.setIndex([0, 1, 2, 0, 2, 3]);
_geometry.setAttribute(
"position",
new InterleavedBufferAttribute(interleavedBuffer, 3, 0, false)
);
_geometry.setAttribute(
"uv",
new InterleavedBufferAttribute(interleavedBuffer, 2, 3, false)
);
}
this.geometry = _geometry;
this.material = material;
this.center = new Vector2(0.5, 0.5);
}
raycast(raycaster, intersects) {
if (raycaster.camera === null) {
console.error(
'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.'
);
}
_worldScale.setFromMatrixScale(this.matrixWorld);
_viewWorldMatrix.copy(raycaster.camera.matrixWorld);
this.modelViewMatrix.multiplyMatrices(
raycaster.camera.matrixWorldInverse,
this.matrixWorld
);
_mvPosition.setFromMatrixPosition(this.modelViewMatrix);
if (
raycaster.camera.isPerspectiveCamera &&
this.material.sizeAttenuation === false
) {
_worldScale.multiplyScalar(-_mvPosition.z);
}
const rotation = this.material.rotation;
let sin, cos;
if (rotation !== 0) {
cos = Math.cos(rotation);
sin = Math.sin(rotation);
}
const center = this.center;
transformVertex(
_vA.set(-0.5, -0.5, 0),
_mvPosition,
center,
_worldScale,
sin,
cos
);
transformVertex(
_vB.set(0.5, -0.5, 0),
_mvPosition,
center,
_worldScale,
sin,
cos
);
transformVertex(
_vC.set(0.5, 0.5, 0),
_mvPosition,
center,
_worldScale,
sin,
cos
);
_uvA.set(0, 0);
_uvB.set(1, 0);
_uvC.set(1, 1);
// check first triangle
let intersect = raycaster.ray.intersectTriangle(
_vA,
_vB,
_vC,
false,
_intersectPoint
);
if (intersect === null) {
// check second triangle
transformVertex(
_vB.set(-0.5, 0.5, 0),
_mvPosition,
center,
_worldScale,
sin,
cos
);
_uvB.set(0, 1);
intersect = raycaster.ray.intersectTriangle(
_vA,
_vC,
_vB,
false,
_intersectPoint
);
if (intersect === null) {
return;
}
}
const distance = raycaster.ray.origin.distanceTo(_intersectPoint);
if (distance < raycaster.near || distance > raycaster.far) return;
intersects.push({
distance: distance,
point: _intersectPoint.clone(),
uv: Triangle.getInterpolation(
_intersectPoint,
_vA,
_vB,
_vC,
_uvA,
_uvB,
_uvC,
new Vector2()
),
face: null,
object: this,
});
}
copy(source, recursive) {
super.copy(source, recursive);
if (source.center !== undefined) this.center.copy(source.center);
this.material = source.material;
return this;
}
}
function transformVertex(vertexPosition, mvPosition, center, scale, sin, cos) {
// compute position in camera space
_alignedPosition
.subVectors(vertexPosition, center)
.addScalar(0.5)
.multiply(scale);
// to check if rotation is not zero
if (sin !== undefined) {
_rotatedPosition.x =
cos * _alignedPosition.x - sin * _alignedPosition.y;
_rotatedPosition.y =
sin * _alignedPosition.x + cos * _alignedPosition.y;
} else {
_rotatedPosition.copy(_alignedPosition);
}
vertexPosition.copy(mvPosition);
vertexPosition.x += _rotatedPosition.x;
vertexPosition.y += _rotatedPosition.y;
// transform to world space
vertexPosition.applyMatrix4(_viewWorldMatrix);
}
export { Sprite };

View file

@ -1,6 +0,0 @@
let something = Iterator.from([1,2,3]);
let someOtherThing = Iterator.from([4,5,6]);
let generate = function* () {
yield* something;
yield* someOtherThing;
}();

View file

@ -2,7 +2,7 @@
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"strict": false,
"strict": true,
"target": "es2017",
"module": "commonjs",
"sourceMap": true,
@ -10,6 +10,6 @@
"moduleResolution": "node",
"types": ["bun-types"]
},
"include": ["src", "index.ts"],
"include": ["src"],
"exclude": ["babel"]
}