Skip to content

Commit

Permalink
Fix performance problem with parsing large number of numeric literals (
Browse files Browse the repository at this point in the history
…fixes #23)
  • Loading branch information
alufers committed Jan 9, 2024
1 parent 7c189fa commit 5fb6c70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import TokenType from "./TokenType";
/**
* The lexer is responsible for turning a string of characters into a stream of
* tokens. The tokens are then used by the parser to build an abstract syntax
* tree.
*
* tree.
*
* The lexer handles parsing of string literals, digraphs (e.g. `<=`), and numbers.
* It also handles detecting keywords and identifiers.
*/
Expand Down Expand Up @@ -331,9 +331,9 @@ export default class Lexer {
}

const possibleNumberStarts = [
this.peekRegex(/[0-9]+/),
this.peekRegex(/[0-9]+[.]/),
this.peekRegex(/[0-9]+[eE][+-]?[0-9]+/),
this.peekRegex(/^[0-9]+/),
this.peekRegex(/^[0-9]+[.]/),
this.peekRegex(/^[0-9]+[eE][+-]?[0-9]+/),
];
const numberLength = Math.max(...possibleNumberStarts.map((x) => x.length));

Expand Down Expand Up @@ -392,7 +392,10 @@ export default class Lexer {
*
* Additionally it handles clearing and attaching the extra tokens.
*/
protected addToken<TValue = any>(tokenType: TokenType, value: TValue | null= null) {
protected addToken<TValue = any>(
tokenType: TokenType,
value: TValue | null = null
) {
const lexeme = this.codeFile.code.substring(
this.start.char,
this.charOffset
Expand All @@ -406,7 +409,11 @@ export default class Lexer {
value
);
} else {
token = new Token(tokenType, new CodeSpan(this.start, this.getLoc()), lexeme);
token = new Token(
tokenType,
new CodeSpan(this.start, this.getLoc()),
lexeme
);
}
token.extraTokens = this.currentExtraTokens;
token.startWithWhitespace = this.startWithWhitespace;
Expand Down
4 changes: 4 additions & 0 deletions src/Parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,4 +1018,8 @@ describe("Parser", () => {
}`)
).toThrow(ParsingError);
});
it("parses big_poly.scad in reasonable time", async () => {
const file = await CodeFile.load(resolve(__dirname, "testdata/big_poly.scad"));
ParsingHelper.parseFile(file);
});
});
1 change: 1 addition & 0 deletions src/testdata/big_poly.scad

Large diffs are not rendered by default.

0 comments on commit 5fb6c70

Please sign in to comment.