Changed from * to +

This commit is contained in:
Rolf Martin Glomsrud 2024-05-22 14:44:45 +02:00
parent bb3cafb476
commit 7a1f099b33
13 changed files with 62 additions and 132 deletions

View file

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

View file

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

View file

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

View file

@ -1,100 +1,9 @@
// "fast-glob" and `createTwoFilesPatch` are bundled here since the API uses `micromatch` and `diff` too async function something() {
import { createTwoFilesPatch } from "diff/lib/patch/create.js"; let a = 100;
import fastGlob from "fast-glob"; a *= 100000;
import * as vnopts from "vnopts"; return fetch("https://uib.no").then(uib => {
import * as errors from "./common/errors.js"; a += 100000;
import getFileInfoWithoutPlugins from "./common/get-file-info.js"; a -= 1000;
import mockable from "./common/mockable.js"; return [a, uib];
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";

8
output_files/test2.js Normal file
View file

@ -0,0 +1,8 @@
async function lol() {
let a = 0;
return gc().then(async (b) => {
let c = 100;
let d = await l();
return 1;
});
}

9
output_files/testing.js Normal file
View file

@ -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];
});
}

View file

@ -12,20 +12,20 @@ import { parseJSTQL } from "./langium/langiumRunner";
const dir = "../prettier/src"; const dir = "../prettier/src";
const path = "../prettier/src/index.js"; const path = "test_files/test2.js";
const file = Bun.file(path); const file = Bun.file(path);
const codeFromFile = await file.text(); const codeFromFile = await file.text();
const main = async () => { const main = async () => {
//transform(selfHostedTransformExampleMultiStmt, codeFromFile); //transform(selfHostedTransformExampleMultiStmt, codeFromFile);
console.log(codeFromFile); console.log(codeFromFile);
const jstql_file = 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_file = Bun.file(jstql_file);
const test_JSTQL = await test_file.text(); const test_JSTQL = await test_file.text();
let proposals = await parseJSTQL(test_JSTQL); let proposals = await parseJSTQL(test_JSTQL);
let code = transform(proposals[0].cases, codeFromFile); let code = transform(proposals[0].cases, codeFromFile);
await Bun.write("output_files/testingLOL.js", code); await Bun.write("output_files/test2.js", code);
}; };
main(); main();

View file

@ -200,17 +200,10 @@ export class Matcher {
return MatchResult.NoMatch; return MatchResult.NoMatch;
} }
} }
for (let key of Object.keys(aplToNode)) {
if (key in keys_to_ignore) {
continue;
}
if (!Object.keys(codeNode).includes(key)) { return codeNode.type === aplToNode.type
return MatchResult.NoMatch; ? MatchResult.Matched
} : MatchResult.NoMatch;
}
return MatchResult.Matched;
} }
multiStatementMatcher(code: TreeNode<t.Node>, aplTo: TreeNode<t.Node>) { multiStatementMatcher(code: TreeNode<t.Node>, aplTo: TreeNode<t.Node>) {

View file

@ -59,7 +59,7 @@ export function parseInternalAplTo(code: string): InternalParseResult {
let wildcard = new WildcardParser( let wildcard = new WildcardParser(
new WildcardTokenizer(temp).tokenize() new WildcardTokenizer(temp).tokenize()
).parse(); ).parse();
//wildcard.identifier.name = "_" + wildcard.identifier.name + "_";
cleanedJS += wildcard.identifier.name; cleanedJS += wildcard.identifier.name;
prelude.push(wildcard); prelude.push(wildcard);
@ -74,9 +74,9 @@ export function parseInternalAplTo(code: string): InternalParseResult {
cleanedJS += code[i]; cleanedJS += code[i];
} }
} }
console.log(prelude, cleanedJS);
return { prelude, cleanedJS }; return { prelude, cleanedJS };
} }
export interface Identifier extends WildcardNode { export interface Identifier extends WildcardNode {
nodeType: "Identifier"; nodeType: "Identifier";
name: string; name: string;
@ -154,7 +154,7 @@ export class WildcardParser {
let identifier = this.Identifier(); let identifier = this.Identifier();
this.Semicolon(); this.Semicolon();
let multidenoted = this.TypeExpr(); let multidenoted = this.TypeExpr();
let star = this.Star(); let star = this.Pluss();
return { return {
nodeType: "Wildcard", nodeType: "Wildcard",
identifier, identifier,
@ -163,8 +163,8 @@ export class WildcardParser {
}; };
} }
private Star(): boolean { private Pluss(): boolean {
if (this.peek() && this.peek().tokenKind === "Star") { if (this.peek() && this.peek().tokenKind === "Pluss") {
this.advance(); this.advance();
return true; return true;
} else { } else {

View file

@ -4,7 +4,7 @@ type TokenKind =
| "Identifier" | "Identifier"
| "OpeningParenthesis" | "OpeningParenthesis"
| "ClosingParenthesis" | "ClosingParenthesis"
| "Star" | "Pluss"
| "Semicolon"; | "Semicolon";
export interface WildcardToken { export interface WildcardToken {
@ -79,8 +79,8 @@ export class WildcardTokenizer {
} }
break; break;
} }
case "*": { case "+": {
this.consumeToken("Star", char); this.consumeToken("Pluss", char);
break; break;
} }
case ":": { case ":": {

View file

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

View file

@ -32,13 +32,13 @@ test("Test code: do", () => {
}); });
let awaitToPromise = await runTest( let awaitToPromise = await runTest(
"test_files/do_test.js", "test_files/awaitToPromise.js",
"dsl_files/do.jstql" "dsl_files/awaitToPromise.jstql"
); );
let awaitToPromiseOutput = await Bun.file( let awaitToPromiseOutput = await Bun.file(
"src/test/test_outputs/do_output.js" "src/test/test_outputs/awaitToPromise_output.js"
).text(); ).text();
test("Test code: do", () => { test("Test code: await to promise", () => {
expect(awaitToPromise).toBe(awaitToPromiseOutput); expect(awaitToPromise).toBe(awaitToPromiseOutput);
}); });

7
test_files/test2.js Normal file
View file

@ -0,0 +1,7 @@
async function lol() {
let a = 0;
let b = await gc();
let c = 100;
let d = await l();
return 1;
}