summaryrefslogtreecommitdiff
path: root/src/js-lang/core/parser.ts
blob: f193d6acbeb1114a0863fde16aa8629fdf27fffa (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Token, Parse, TokenType, ASTNode, NodeType } from "../../ts-lang";
import { lex } from "./lexer";

const resolveNodeFromToken = (token: Token): ASTNode => {
  // FIXME not correct
  if (!isNaN(Number(token.name))) {
    return {
      type: NodeType.INT,
      name: "",
      value: Number(token.name),
      children: [],
    };
  }
  if (token.name[0] === '"' && token.name[token.name.length - 1] === '"') {
    return {
      type: NodeType.INT,
      name: "",
      value: token.name.slice(1, token.name.length - 1),
      children: [],
    };
  }
  return {
    type: NodeType.EXT,
    name: token.name,
    value: null,
    children: [],
  };
};

const _parse = ({
  remainingTokens,
  lastToken,
  stack,
}: {
  remainingTokens: Token[];
  lastToken: Token | null;
  stack: ASTNode[];
}): ASTNode | null => {
  const head = remainingTokens.shift();
  if (!head) {
    if (lastToken) {
      (stack[stack.length - 1].children as ASTNode[]).push(
        resolveNodeFromToken(lastToken)
      );

      return _parse({
        lastToken: null,
        remainingTokens: [],
        stack,
      });
    }

    return stack[0] ?? null;
  }

  if (lastToken) {
    if (head.type === TokenType.NAME) {
      (stack[stack.length - 1].children as ASTNode[]).push(
        resolveNodeFromToken(lastToken)
      );
      return _parse({
        lastToken: head,
        remainingTokens,
        stack,
      });
    }

    if (head.type === TokenType.CLOSE_PAREN) {
      const top = stack.pop()!;
      (top.children as ASTNode[]).push(resolveNodeFromToken(lastToken));
      (stack[stack.length - 1].children as ASTNode[]).push(top);

      return _parse({
        lastToken: null,
        remainingTokens,
        stack,
      });
    }

    if (head.type === TokenType.OPEN_PAREN) {
      return _parse({
        lastToken: null,
        remainingTokens,
        stack: [...stack, resolveNodeFromToken(lastToken)],
      });
    }

    throw new Error(
      `${JSON.stringify({
        lastToken,
        remainingTokens,
        stack,
      })} Was not expecting ${head.type}`
    );
  }

  if (head.type === TokenType.NAME) {
    return _parse({
      lastToken: head,
      remainingTokens,
      stack,
    });
  }

  if (head.type === TokenType.CLOSE_PAREN) {
    const top = stack.pop()!;
    (stack[stack.length - 1].children as ASTNode[]).push(top);

    return _parse({
      lastToken: null,
      remainingTokens,
      stack,
    });
  }

  throw new Error(
    `${JSON.stringify({
      lastToken,
      remainingTokens,
      stack,
    })} Expected nextToken to be a name or close paren at ${head.type}`
  );
};

export const parse = <const Raw extends readonly Token[]>(
  raw: Raw
): Parse<Raw> =>
  _parse({
    remainingTokens: raw as unknown as Token[],
    lastToken: null,
    stack: [{ type: NodeType.EXT, name: "arr", value: null, children: [] }],
  }) as Parse<Raw>;