Skip to content

Commit

Permalink
fix: Parsing error when parsing list comprehension for without any ar…
Browse files Browse the repository at this point in the history
…guments (fixes #27)
  • Loading branch information
alufers committed Jun 2, 2024
1 parent 5c8572c commit 18b1e2b
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 107 deletions.
32 changes: 25 additions & 7 deletions src/ASTPrinter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ describe("ASTPrinter", () => {
new CodeFile("<test>", source)
);
errorCollector.throwIfAny();
if(!ast) {
if (!ast) {
throw new Error("No AST");
}
ast = new ASTScopePopulator(new Scope()).populate(ast) as ScadFile; // populating the scopes should not change anything
return new ASTPrinter(new FormattingConfiguration()).visitScadFile(ast);
}
function injectCommentsBetweenTokens(source: string): [string, string[]] {
const ec = new ErrorCollector()
const lexer = new Lexer(
new CodeFile("<test>", source),
ec
);
const ec = new ErrorCollector();
const lexer = new Lexer(new CodeFile("<test>", source), ec);
const tokens = lexer.scan();
ec.throwIfAny();
let injectId = 0;
Expand Down Expand Up @@ -63,6 +60,11 @@ describe("ASTPrinter", () => {
return [codeWithInjections, injectedStrings];
}

/**
* Inserts comments between all tokens and then formats the code,
* then checks if the comments are still there
* @param source the source code to check
*/
function doPreserveTest(source: string) {
const [codeWithInjections, injectedStrings] =
injectCommentsBetweenTokens(source);
Expand Down Expand Up @@ -221,6 +223,10 @@ echo(selector("mul")(5)); // ECHO: 26
expect(f).toStrictEqual(expect.stringContaining("^"));
});

test.only("formats 'for' comprehensions without variable names", ()=> {
expect(doFormat(`x = [for([0:3]) 1];`)).toStrictEqual(`x = [for([0 : 3]) 1];\n`);
})

test("does not add a newline after a semicolon in an assignment if a comment follows it", () => {
const f = doFormat(`
enum_value="a"; // [a:ayyyy, b:beee, c:seeee]
Expand All @@ -234,11 +240,23 @@ enum_value = "a"; // [a:ayyyy, b:beee, c:seeee]
`
enum_value="a";
// [a:ayyyy, b:beee, c:seeee]
`,
`
);
expect(f).toStrictEqual(`
enum_value = "a";
// [a:ayyyy, b:beee, c:seeee]
`);
});
it("preserves comments in 'for' list comprehensions", () => {
doPreserveTest(`
x = [for(a = [0:3]) 1];
`);
});
it("preserves comments in 'for' list comprehensions without variable names", () => {
doPreserveTest(`
x = [for([0:3]) 1];
x = [for("abc") 1];
x = [for(alpha) 1];
`);
});
});
50 changes: 46 additions & 4 deletions src/Parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,13 +1013,55 @@ describe("Parser", () => {
it("does not hang when parsing a declaration of a module called 'echo'", () => {
expect(() =>
doParse(`
module echo() {
echo("hello");
}`)
module echo() {
echo("hello");
}`)
).toThrow(ParsingError);
});

it("parses big_poly.scad in reasonable time", async () => {
const file = await CodeFile.load(resolve(__dirname, "testdata/big_poly.scad"));
const file = await CodeFile.load(
resolve(__dirname, "testdata/big_poly.scad")
);
ParsingHelper.parseFile(file);
});

it("errors out when an expression is used instead of an argument name in module declaration", () => {
expect(() =>
doParse(`
module t1(good_one, 2+2) {}
`)
).toThrow(ParsingError);
expect(() =>
doParse(`
module t1(2+2) {}
`)
).toThrow(ParsingError);
expect(() =>
doParse(`
module t1(2+2, good_one) {}
`)
).toThrow(ParsingError);
expect(() =>
doParse(`
module t1(good_one+2) {}
`)
).toThrow(ParsingError);
});

it("Can parse list comprehensions with unnamed variables", () => {
doParse(`
x = [for([0:3]) 1];
echo(x);
`);
doParse(`
x = [for("abc") 1];
echo(x);
`);
doParse(`
alpha = "abc";
x = [for(alpha) 1];
echo(x);
`);
});
});
Loading

0 comments on commit 18b1e2b

Please sign in to comment.