summaryrefslogtreecommitdiff
path: root/src/ts-lang/core/common.ts
blob: fa5cb7ce6d248ec2476dab602a1ed92fd91cc975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
export enum TokenType {
  OPEN_PAREN = "(",
  CLOSE_PAREN = ")",
  SPACE = " ",
  SEMICOLON = ";",
  COMMA = ",",
  NAME = "NAME",
}

export type Token<
  Type extends TokenType = TokenType,
  Name extends string = string
> = {
  type: Type;
  name: Name;
};

export type LexerCtx = {
  next: string;
  nameCollection: string;
  tokens: readonly Token[];
};

export enum NodeType {
  INT = "INT",
  EXT = "EXT",
  PARSER_ERROR = "PARSER_ERROR",
}

export type ASTNode<
  Type extends NodeType = NodeType,
  Name extends string = string,
  Value extends any = any,
  Children extends readonly ASTNode[] = readonly ASTNode<
    NodeType,
    string,
    any,
    any
  >[]
> = {
  type: Type;
  name: Name;
  value: Value;
  children: Children;
};

export type ParserCtx = {
  remainingTokens: readonly Token[];
  lastToken: Token | null;
  stack: readonly ASTNode[];
};

export type StackFrame<
  Bindings extends Record<ASTNode["name"], any> = Record<ASTNode["name"], any>,
  Parent extends StackFrame | null = any
> = {
  bindings: Bindings;
  parent: Parent;
};

export type EmptyStackFrame = StackFrame<{}, null>;

export type WithPushedBindings<
  OldFrame extends StackFrame,
  Bindings extends StackFrame["bindings"]
> = StackFrame<Bindings, OldFrame>;