From c20434533887618820985cbb25081255475852f2 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Wed, 29 Oct 2025 20:22:19 -0700 Subject: references --- src/lib/core/parser.ts | 87 +++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 51 deletions(-) (limited to 'src/lib/core/parser.ts') diff --git a/src/lib/core/parser.ts b/src/lib/core/parser.ts index fbbbdc2..0f095fe 100644 --- a/src/lib/core/parser.ts +++ b/src/lib/core/parser.ts @@ -1,5 +1,12 @@ -import { ASTNode, NodeType, ParserCtx, Token, TokenType } from "./common"; -import { IsWhitespace, Lex } from "./lexer"; +import { + ASTNode, + NodeType, + ParserCtx, + Token, + TokenSubType, + TokenType, +} from "./common"; +import { Lex } from "./lexer"; /* start @@ -46,10 +53,16 @@ finally: */ -export type Error = ASTNode; +export type Error = ASTNode< + NodeType.PARSER_ERROR, + "Error", + T, + [] +>; export type PushChild = { type: Node["type"]; + value: Node["value"]; name: Node["name"]; children: [...Node["children"], Child]; }; @@ -94,22 +107,27 @@ export type StackWithoutLast = Stack extends [ ? [] : never; +export type ResolveNodeFromToken<_Token extends Token> = + _Token["subType"] extends TokenSubType.LITERAL + ? ASTNode + : ASTNode; + export type _Parse = Ctx["remainingTokens"] extends [ infer Head extends Token, ...infer Tail extends readonly Token[] ] - ? Ctx["lastName"] extends string - ? Head["type"] extends TokenType.UNIQUE_SYMBOL + ? Ctx["lastToken"] extends Token + ? Head["type"] extends TokenType.NAME ? // we already have a lastName // mutate last element of stack to push lastName as child // lastName = nextToken // goto start _Parse<{ - lastName: Head["name"]; + lastToken: Head; remainingTokens: Tail; stack: PushChildToLastElementOfStack< Ctx["stack"], - ASTNode + ResolveNodeFromToken >; }> : //nextToken is openParen or close paren @@ -120,7 +138,7 @@ export type _Parse = Ctx["remainingTokens"] extends [ // [stack[last - 1].children.push(stack.pop) // goto start _Parse<{ - lastName: null; + lastToken: null; remainingTokens: Tail; // first push the last name onto the children of the top // then push the top onto the children of the next @@ -130,7 +148,7 @@ export type _Parse = Ctx["remainingTokens"] extends [ Ctx["stack"], PushChild< GetLastOnStack, - ASTNode + ResolveNodeFromToken > > >; @@ -139,23 +157,23 @@ export type _Parse = Ctx["remainingTokens"] extends [ ? // push lastName onto stack // goto start _Parse<{ - lastName: null; + lastToken: null; remainingTokens: Tail; - stack: [...Ctx["stack"], ASTNode]; + stack: [...Ctx["stack"], ResolveNodeFromToken]; }> : Ctx & Error<`Was not expecting ${Head["type"]}`> : // expect nextToken to be a name or close paren - Head["type"] extends TokenType.UNIQUE_SYMBOL + Head["type"] extends TokenType.NAME ? // lastName = nextToken // goto start _Parse<{ - lastName: Head["name"]; + lastToken: Head; remainingTokens: Tail; stack: Ctx["stack"]; }> : Head["type"] extends TokenType.CLOSE_PAREN ? _Parse<{ - lastName: null; + lastToken: null; remainingTokens: Tail; // push the top onto the children of the next // then remove the top @@ -168,45 +186,12 @@ export type _Parse = Ctx["remainingTokens"] extends [ }> : Ctx & Error<`Expected nextToken to be a name or close paren at ${Head["type"]}`> - : Ctx["stack"]; - -// v1 -// ? Ctx["isCollecting"] extends true -// ? Head["type"] extends TokenType.CLOSE_PAREN -// ? "return" -// : Head["type"] extends TokenType.UNIQUE_SYMBOL -// ? _Parse<{ -// remainingTokens: Tail; -// isCollecting: true; -// name: Ctx["name"]; -// // fuck, how to do this without advancing seek pointer?? -// ret: [...Ctx["ret"], ASTNode; -// }> -// : Error<"Expected another name, or `)`"> -// : Head["type"] extends TokenType.OPEN_PAREN -// ? _Parse<{ -// remainingTokens: Tail; -// isCollecting: true; -// name: Ctx["name"]; -// ret: Ctx["ret"]; -// }> -// : Error<"Expected open paren"> -// : Ctx["ret"]; - -//v2 -// ? Ctx["previousName"] extends null -// ? Head["type"] extends TokenType.UNIQUE_SYMBOL -// ? _Parse<{ remainingTokens: Tail; previousName: Head; node: Ctx["node"] }> -// : ASTNode -// : IsWhitespace<`${Head["type"]}`> extends true -// ? ASTNode -// : ASTNode -// : Ctx["node"]; + : Ctx["stack"][0]; export type Parse = _Parse<{ - lastName: null; + lastToken: null; remainingTokens: Raw; - stack: [ASTNode]; + stack: [ASTNode]; }>; -const a = "a" as any as Parse>; +const test_result = null as unknown as Parse>; -- cgit v1.2.3-70-g09d2