summaryrefslogtreecommitdiff
path: root/src/lang/core/lexer.ts
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2025-11-03 21:24:05 -0800
committerKai Stevenson <kai@kaistevenson.com>2025-11-03 21:24:05 -0800
commita11e6780fbb8bd4143dfec44e2ce147b795772d8 (patch)
tree701b0528e07fd6444cee6e6ce3d1a01eaf85516c /src/lang/core/lexer.ts
parent632c153b974ee9c553d08beb27d5e4d60396a2ac (diff)
fix lexer depth limit
Diffstat (limited to 'src/lang/core/lexer.ts')
-rw-r--r--src/lang/core/lexer.ts61
1 files changed, 45 insertions, 16 deletions
diff --git a/src/lang/core/lexer.ts b/src/lang/core/lexer.ts
index 33a408a..567964f 100644
--- a/src/lang/core/lexer.ts
+++ b/src/lang/core/lexer.ts
@@ -40,23 +40,52 @@ export type ProcessNameCollection<
export type IsOpen<T> = T extends `${TokenType.OPEN_PAREN}` ? true : false;
export type IsClose<T> = T extends `${TokenType.CLOSE_PAREN}` ? true : false;
-export type _Lex<Ctx extends LexerCtx> =
- Ctx["next"] extends `${infer Head}${infer Tail}`
- ? IsWhitespace<Head> extends true
- ? _Lex<ProcessNameCollection<Ctx, Tail, null>>
- : IsOpen<Head> extends true
- ? _Lex<ProcessNameCollection<Ctx, Tail, Token<TokenType.OPEN_PAREN>>>
- : IsClose<Head> extends true
- ? _Lex<ProcessNameCollection<Ctx, Tail, Token<TokenType.CLOSE_PAREN>>>
- : _Lex<{
+export type ChunkedLex<
+ Ctx extends LexerCtx,
+ Depth extends any[] = []
+> = Depth["length"] extends 50
+ ? Ctx & {
+ endChunk: true;
+ }
+ : Ctx["next"] extends `${infer Head}${infer Tail}`
+ ? IsWhitespace<Head> extends true
+ ? ChunkedLex<ProcessNameCollection<Ctx, Tail, null>, [0, ...Depth]>
+ : IsOpen<Head> extends true
+ ? ChunkedLex<
+ ProcessNameCollection<Ctx, Tail, Token<TokenType.OPEN_PAREN>>,
+ [0, ...Depth]
+ >
+ : IsClose<Head> extends true
+ ? ChunkedLex<
+ ProcessNameCollection<Ctx, Tail, Token<TokenType.CLOSE_PAREN>>,
+ [0, ...Depth]
+ >
+ : ChunkedLex<
+ {
next: Tail;
nameCollection: `${Ctx["nameCollection"]}${Head}`;
tokens: Ctx["tokens"];
- }>
- : Ctx["tokens"];
+ },
+ [0, ...Depth]
+ >
+ : Ctx;
-export type Lex<Raw extends string> = _Lex<{
- next: `${Raw};`;
- tokens: [];
- nameCollection: "";
-}>;
+export type InnerLex<
+ Next extends string,
+ NameCollection extends LexerCtx["nameCollection"] = "",
+ AccTokens extends Token[] = []
+> = Next extends ""
+ ? AccTokens
+ : ChunkedLex<{
+ next: Next;
+ tokens: [];
+ nameCollection: NameCollection;
+ }> extends infer U
+ ? U extends LexerCtx & { endChunk: true }
+ ? InnerLex<U["next"], U["nameCollection"], [...AccTokens, ...U["tokens"]]>
+ : U extends LexerCtx
+ ? [...AccTokens, ...U["tokens"]]
+ : never
+ : never;
+
+export type Lex<Raw extends string> = InnerLex<Raw>;