-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
168 lines (143 loc) · 4.2 KB
/
index.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { joinRegExp } from 'join-regexp'
import { createLexer, Lexer, LexerError, LexerErrorCauses, RegExpToken, Token } from 'lexer-next'
import * as ParserErrorCauses from './causes'
export { Token }
export { ParserErrorCauses }
export { joinRegExp }
export type { TokenJson } from 'lexer-next'
export type { LexerError }
export interface NodeArray extends Array<Node | NodeArray | Token> {
[k: number]: Node | NodeArray | Token
}
export type Node = (NodeArray | Token) & {
lexer?: Lexer
toString(): string
}
const nodeToString = (node: Node): string =>
Array.isArray(node) ? '(' + node.map(child => nodeToString(child)).join(' ') + ')' : node != null ? node.value : '()'
interface Fn {
(t: Token, r: number, x?: unknown): unknown
}
export interface Impl {
led?: Fn
nud?: Fn
}
interface ImplTable {
[k: string]: [number[], Impl]
}
interface ImplTableFactoryProps {
peek: Lexer['peek']
advance: Lexer['advance']
accept: Lexer['accept']
expect: Lexer['expect']
unknown: Lexer['unknown']
never: Impl
pass: Impl
bin: Fn
pre: Fn
post: (bp: number) => Fn
until: (op: string, min_bp: number, fn: (t: Token, x: unknown, y: unknown, r: number) => unknown) => Fn
expr: (min: number) => Node
}
interface ImplTableFactory {
(props: ImplTableFactoryProps): ImplTable
}
export interface ParserError extends Error {
cause:
| ParserErrorCauses.UnexpectedToken
| ParserErrorCauses.BadImpl
| ParserErrorCauses.BadToken
| ParserErrorCauses.BadOp
}
export class ParserError extends Error {
name = 'ParserError'
constructor(cause: Error) {
super(cause.message, { cause })
}
}
export const createParser = (regexp: RegExp, fn: ImplTableFactory) => {
const tokenizer = (input: string) => input.matchAll(new RegExpToken(regexp))
const Lexer = createLexer(tokenizer)
const parse = (input: string) => {
const lexer = Lexer(input)
const { onerror, filter, peek, advance, expect, accept, unknown } = lexer
filter((token: Token) => token.group !== 'nul')
onerror((error: LexerError) => {
if (error.cause instanceof LexerErrorCauses.UnexpectedToken) {
throw new ParserError(
new ParserErrorCauses.UnexpectedToken(
error.cause.currentToken,
error.cause.expectedGroup,
error.cause.expectedValue
)
)
} else {
throw error
}
})
const post = (bp: number): Fn => t => [t, expr(bp)]
const pre: Fn = (t, r, x) => {
if (r) throw new ParserError(new ParserErrorCauses.BadToken(t))
else return [t, x]
}
const bin: Fn = (t, r, x) => [t, x, expr(r)]
const pass: Impl = { nud: t => t, led: (t, _, x) => [x, t] }
const never: Impl = {
nud: (t, rbp) => {
if (rbp) throw new ParserError(new ParserErrorCauses.BadOp(t))
},
}
const until = (op: string, min_bp: number, fn: (t: Token, x: unknown, y: unknown, r: number) => unknown): Fn =>
(t: Token, r: number, L: unknown) => {
const m = !peek('ops', op) ? expr(min_bp) : null
expect('ops', op)
return fn(t, L, m, r)
}
const bp = (t: Token) => desc(t)[0]
const desc = (t: Token) => {
const ctx = impl[`${t}`] ?? impl[t.group]
if (!ctx) throw new ParserError(new ParserErrorCauses.BadOp(t))
return ctx
}
const denom = (impl: 'led' | 'nud', t: Token, rbp: number, left?: unknown) => {
const fn = desc(t)[1][impl]
if (fn) return fn(t, rbp, left)
else throw new ParserError(new ParserErrorCauses.BadImpl(t, impl))
}
const nud = denom.bind(null, 'nud')
const led = denom.bind(null, 'led')
const expr = (min: number) => {
let t = advance()
let left = nud(t, min)
let lbp, rbp
while (min < (([lbp, rbp] = bp(peek())), lbp)) {
t = advance()
left = led(t, rbp, left)
}
return left as Node
}
const impl: ImplTable = fn({
peek,
advance,
accept,
expect,
unknown,
never,
pass,
bin,
pre,
post,
until,
expr,
})
const node = expr(0)
if (node) {
node.lexer = lexer
node.toString = function() {
return nodeToString(this)
}
}
return node
}
return parse
}