From 56040f3ff85e77311f0c864a89afd63fcf1bdb50 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Mon, 3 Nov 2025 23:40:02 -0800 Subject: add js-lang, refactor some ts-lang stuff --- src/ts-lang/core/lexer.ts | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/ts-lang/core/lexer.ts (limited to 'src/ts-lang/core/lexer.ts') diff --git a/src/ts-lang/core/lexer.ts b/src/ts-lang/core/lexer.ts new file mode 100644 index 0000000..bcd5785 --- /dev/null +++ b/src/ts-lang/core/lexer.ts @@ -0,0 +1,84 @@ +import { LexerCtx, Token, TokenType } from "./common"; + +export type IsWhitespace = T extends `${TokenType.SPACE}` + ? true + : T extends `${TokenType.COMMA}` + ? true + : T extends `${TokenType.SEMICOLON}` + ? true + : false; + +export type ProcessNameCollection< + Ctx extends LexerCtx, + Tail extends string, + _Token extends Token | null +> = { + next: Tail; + nameCollection: ""; + tokens: _Token extends null + ? [ + ...Ctx["tokens"], + ...(Ctx["nameCollection"] extends "" + ? [] + : [Token]) + ] + : [ + ...Ctx["tokens"], + ...(Ctx["nameCollection"] extends "" + ? [_Token] + : [Token, _Token]) + ]; +}; + +export type IsOpen = T extends `${TokenType.OPEN_PAREN}` ? true : false; +export type IsClose = T extends `${TokenType.CLOSE_PAREN}` ? true : false; + +export type ChunkedLex< + Ctx extends LexerCtx, + Depth extends any[] = [] +> = Depth["length"] extends 50 + ? Ctx & { + endChunk: true; + } + : Ctx["next"] extends `${infer Head}${infer Tail}` + ? IsWhitespace extends true + ? ChunkedLex, [0, ...Depth]> + : IsOpen extends true + ? ChunkedLex< + ProcessNameCollection>, + [0, ...Depth] + > + : IsClose extends true + ? ChunkedLex< + ProcessNameCollection>, + [0, ...Depth] + > + : ChunkedLex< + { + next: Tail; + nameCollection: `${Ctx["nameCollection"]}${Head}`; + tokens: Ctx["tokens"]; + }, + [0, ...Depth] + > + : Ctx; + +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 extends LexerCtx + ? [...AccTokens, ...U["tokens"]] + : never + : never; + +export type Lex = InnerLex<`${Raw};`>; -- cgit v1.2.3-70-g09d2