diff --git a/dsl_files/awaitToPromise.jstql b/dsl_files/awaitToPromise.jstql index cc78a41..1045e26 100644 --- a/dsl_files/awaitToPromise.jstql +++ b/dsl_files/awaitToPromise.jstql @@ -2,7 +2,7 @@ proposal awaitToPomise{ case single{ applicable to { "let <> = await <>; - <> + <> return <> " } @@ -10,7 +10,7 @@ proposal awaitToPomise{ transform to{ "return <>.then((<>) => { <> - <> + return <> });" } } diff --git a/dsl_files/do.jstql b/dsl_files/do.jstql index e1d1213..360105b 100644 --- a/dsl_files/do.jstql +++ b/dsl_files/do.jstql @@ -2,7 +2,7 @@ proposal DoExpression{ case arrowFunction{ applicable to { "let <> = () => { - <> + <> return <>; } " @@ -18,7 +18,7 @@ proposal DoExpression{ case immediatelyInvokedUnnamedFunction { applicable to { "let <> = function(){ - <> + <> return <>; }();" } diff --git a/grammars/wildcard_grammar.txt b/grammars/wildcard_grammar.txt index baf071f..3c6d9db 100644 --- a/grammars/wildcard_grammar.txt +++ b/grammars/wildcard_grammar.txt @@ -3,15 +3,19 @@ Wildcard: Identifier ":" TypeExpr ("*"?) TypeExpr: BinaryExpr + | UnaryExpr + | PrimitiveExpr BinaryExpr: - UnaryExpr { Operator PrimitiveExpr }* + TypeExpr { Operator TypeExpr }* UnaryExpr: - {UnaryOperator}? PrimitiveExpr + {UnaryOperator}? TypeExpr PrimitiveExpr: GroupExpr | Identifier GroupExpr: - "(" TypeExpr ")" \ No newline at end of file + "(" TypeExpr ")" + + diff --git a/output_files/output_await_to_promise.js b/output_files/output_await_to_promise.js index 161d609..dd7fdef 100644 --- a/output_files/output_await_to_promise.js +++ b/output_files/output_await_to_promise.js @@ -1,100 +1,9 @@ -// "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 +async function something() { + let a = 100; + a *= 100000; + return fetch("https://uib.no").then(uib => { + a += 100000; + a -= 1000; + return [a, uib]; }); - 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"; \ No newline at end of file +} \ No newline at end of file diff --git a/output_files/test2.js b/output_files/test2.js new file mode 100644 index 0000000..cf9d18f --- /dev/null +++ b/output_files/test2.js @@ -0,0 +1,8 @@ +async function lol() { + let a = 0; + return gc().then(async (b) => { + let c = 100; + let d = await l(); + return 1; + }); +} diff --git a/output_files/testing.js b/output_files/testing.js new file mode 100644 index 0000000..dd7fdef --- /dev/null +++ b/output_files/testing.js @@ -0,0 +1,9 @@ +async function something() { + let a = 100; + a *= 100000; + return fetch("https://uib.no").then(uib => { + a += 100000; + a -= 1000; + return [a, uib]; + }); +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index eeac046..137a56e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,20 +12,20 @@ import { parseJSTQL } from "./langium/langiumRunner"; const dir = "../prettier/src"; -const path = "../prettier/src/index.js"; +const path = "test_files/test2.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"; + "/home/rolfmg/Coding/Master/didactic-chainsaw/dsl_files/awaitToPromise.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); + await Bun.write("output_files/test2.js", code); }; main(); diff --git a/src/matcher/matcher.ts b/src/matcher/matcher.ts index d69cb57..7ac51ab 100644 --- a/src/matcher/matcher.ts +++ b/src/matcher/matcher.ts @@ -200,17 +200,10 @@ export class Matcher { return MatchResult.NoMatch; } } - for (let key of Object.keys(aplToNode)) { - if (key in keys_to_ignore) { - continue; - } - if (!Object.keys(codeNode).includes(key)) { - return MatchResult.NoMatch; - } - } - - return MatchResult.Matched; + return codeNode.type === aplToNode.type + ? MatchResult.Matched + : MatchResult.NoMatch; } multiStatementMatcher(code: TreeNode, aplTo: TreeNode) { diff --git a/src/parser/parse.ts b/src/parser/parse.ts index 6fe7264..f27c48e 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -59,7 +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); @@ -74,9 +74,9 @@ export function parseInternalAplTo(code: string): InternalParseResult { cleanedJS += code[i]; } } + console.log(prelude, cleanedJS); 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.Star(); + let star = this.Pluss(); return { nodeType: "Wildcard", identifier, @@ -163,8 +163,8 @@ export class WildcardParser { }; } - private Star(): boolean { - if (this.peek() && this.peek().tokenKind === "Star") { + private Pluss(): boolean { + if (this.peek() && this.peek().tokenKind === "Pluss") { this.advance(); return true; } else { diff --git a/src/parser/wildcardTokenizer.ts b/src/parser/wildcardTokenizer.ts index 60aa9ed..3cc65a5 100644 --- a/src/parser/wildcardTokenizer.ts +++ b/src/parser/wildcardTokenizer.ts @@ -4,7 +4,7 @@ type TokenKind = | "Identifier" | "OpeningParenthesis" | "ClosingParenthesis" - | "Star" + | "Pluss" | "Semicolon"; export interface WildcardToken { @@ -79,8 +79,8 @@ export class WildcardTokenizer { } break; } - case "*": { - this.consumeToken("Star", char); + case "+": { + this.consumeToken("Pluss", char); break; } case ":": { diff --git a/src/test/test_outputs/awaitToPromise_output.js b/src/test/test_outputs/awaitToPromise_output.js index 18ef1b8..dd7fdef 100644 --- a/src/test/test_outputs/awaitToPromise_output.js +++ b/src/test/test_outputs/awaitToPromise_output.js @@ -4,6 +4,6 @@ async function something() { return fetch("https://uib.no").then(uib => { a += 100000; a -= 1000; - [a, uib]; + return [a, uib]; }); } \ No newline at end of file diff --git a/src/test/test_transform.test.ts b/src/test/test_transform.test.ts index 905fae6..ae01a26 100644 --- a/src/test/test_transform.test.ts +++ b/src/test/test_transform.test.ts @@ -32,13 +32,13 @@ test("Test code: do", () => { }); let awaitToPromise = await runTest( - "test_files/do_test.js", - "dsl_files/do.jstql" + "test_files/awaitToPromise.js", + "dsl_files/awaitToPromise.jstql" ); let awaitToPromiseOutput = await Bun.file( - "src/test/test_outputs/do_output.js" + "src/test/test_outputs/awaitToPromise_output.js" ).text(); -test("Test code: do", () => { +test("Test code: await to promise", () => { expect(awaitToPromise).toBe(awaitToPromiseOutput); }); diff --git a/test_files/test2.js b/test_files/test2.js new file mode 100644 index 0000000..5e058c1 --- /dev/null +++ b/test_files/test2.js @@ -0,0 +1,7 @@ +async function lol() { + let a = 0; + let b = await gc(); + let c = 100; + let d = await l(); + return 1; +}