From e9f3c782bc10d4c5c44faf768aa60cd6bcc66574 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sun, 2 Nov 2025 18:08:16 -0800 Subject: refactor --- src/lang/core/lexer.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/lang/core/lexer.ts (limited to 'src/lang/core/lexer.ts') diff --git a/src/lang/core/lexer.ts b/src/lang/core/lexer.ts new file mode 100644 index 0000000..33a408a --- /dev/null +++ b/src/lang/core/lexer.ts @@ -0,0 +1,62 @@ +import { LexerCtx, Token, TokenSubType, TokenType } from "./common"; + +export type BreakingToken = + | TokenType.OPEN_PAREN + | TokenType.CLOSE_PAREN + | TokenType.COMMA + | TokenType.SEMICOLON + | TokenType.SPACE; + +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 _Lex = + Ctx["next"] extends `${infer Head}${infer Tail}` + ? IsWhitespace extends true + ? _Lex> + : IsOpen extends true + ? _Lex>> + : IsClose extends true + ? _Lex>> + : _Lex<{ + next: Tail; + nameCollection: `${Ctx["nameCollection"]}${Head}`; + tokens: Ctx["tokens"]; + }> + : Ctx["tokens"]; + +export type Lex = _Lex<{ + next: `${Raw};`; + tokens: []; + nameCollection: ""; +}>; -- cgit v1.2.3-70-g09d2