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

View file

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

View file

@ -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 ")"
"(" TypeExpr ")"

View file

@ -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";
}

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 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();

View file

@ -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<t.Node>, aplTo: TreeNode<t.Node>) {

View file

@ -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 {

View file

@ -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 ":": {

View file

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

View file

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

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;
}