-
Notifications
You must be signed in to change notification settings - Fork 0
/
_nav.blog._post.2023-lezer-playground.tsx
225 lines (203 loc) · 5.36 KB
/
_nav.blog._post.2023-lezer-playground.tsx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import CodeMirror from "@uiw/react-codemirror";
import { lezerLanguage } from "@codemirror/lang-lezer";
import type { ReactNode } from "react";
import { Component, useState } from "react";
import { buildParser } from "@lezer/generator";
import { LRLanguage, LanguageSupport } from "@codemirror/language";
import { styleTags, Tag } from "@lezer/highlight";
import { basicSetup } from "codemirror";
import { createTheme } from "@uiw/codemirror-themes";
const initialGrammar = `
@top File { Line+ }
Chord { chordStart ChordContent chordEnd }
Line {
(SectionStart | character | Chord)
(text | Chord)*
(newline | EOF)
}
@tokens {
SectionStart { $[0-9] "." }
newline { '\\n' }
character { ![\\n\\[] }
chordStart { "[" }
chordEnd { "]" }
ChordContent { ![\\n\\]]+ }
text { ![\\n\\[]+ }
EOF { @eof }
}
`.trim();
const initialExample = `
1. When [C]the days are cold, And [G]the cards all fold
And [Ami]the saints we see, Are [F]all made of gold
When [C]your dreams all fail, And [G]the ones we hail
Are [Ami]the worst of all, And [F]the blood’s run stale
`.trim();
export default function PostOutlet() {
const [grammar, setValue] = useState(initialGrammar);
const [example, setExample] = useState(initialExample);
return (
<div className="no-prose">
<h2 className="prose dark:prose-invert">Grammar</h2>
<CodeMirror
extensions={[lezerLanguage]}
value={grammar}
onChange={(val) => setValue(val)}
theme="dark"
/>
<ErrorBoundary
key={grammar}
fallback={
<PlaygroundNoGrammar
value={example}
onChange={(v) => setExample(v)}
/>
}
>
<Playground
grammar={grammar}
value={example}
onChange={(v) => setExample(v)}
/>
</ErrorBoundary>
</div>
);
}
function Playground({
grammar,
value,
onChange,
}: {
grammar: string;
value: string;
onChange: (v: string) => void;
}) {
const warnings: any[] = [];
const stash = console.warn;
console.warn = (w) => warnings.push(w);
const parser = buildParser(grammar);
console.warn = stash;
const t = Object.fromEntries(
parser.nodeSet.types.map((t) => [t.name, Tag.define()]),
);
let parserWithMetadata = parser.configure({
props: [styleTags(t)],
});
const exampleLanguage = LRLanguage.define({
parser: parserWithMetadata,
languageData: {
commentTokens: { line: ";" },
},
});
// prettier-ignore
const colors = ["#e6194b","#3cb44b","#ffe119","#4363d8","#f58231","#911eb4","#46f0f0","#f032e6","#bcf60c","#fabebe","#008080","#e6beff","#9a6324","#fffac8","#800000","#aaffc3","#808000","#ffd8b1","#000075","#808080","#ffffff","#000000"];
const styles = Object.entries(t).map(([k, tag], i) => {
const bg = colors[i % colors.length];
const sum = [bg.slice(1, 3), bg.slice(3, 5), bg.slice(5, 7)]
.map((v) => Number.parseInt(v, 16))
.reduce((a, b) => a + b, 0);
return {
tag,
style: {
background: bg,
color: sum > 400 ? "black" : "white",
},
name: k,
};
});
const theme = createTheme({
theme: "light",
settings: {},
styles: styles.map((s) => ({ tag: s.tag, ...s.style })),
});
const parsed = parser.parse(value);
return (
<>
{warnings.length ? (
<>
<h2 className="prose dark:prose-invert">Warnings</h2>
{warnings.map((w, i) => (
<div key={i}>{w}</div>
))}
</>
) : null}
<h2 className="prose dark:prose-invert">Example code</h2>
<CodeMirror
extensions={[
basicSetup,
new LanguageSupport(exampleLanguage, []),
theme,
]}
value={value}
onChange={onChange}
theme="dark"
/>
<div className="flex gap-1 mt-2">
<div>Highlighting:</div>
{styles.map((s) => (
<span key={s.name} style={s.style}>
{s.name}
</span>
))}
</div>
<h2 className="prose dark:prose-invert">Parsed tree</h2>
<pre>
<code className="hljs">{prettyfi(parsed.toString())}</code>
</pre>
</>
);
}
function prettyfi(tree: string) {
const lines = tree
.replace(/([(,])/g, "$1\n")
.replace(/[)]/g, "\n)")
.split("\n");
let indent = 0;
for (let i = 0; i < lines.length; ++i) {
const line = lines[i];
if (line.endsWith(")") || line.endsWith("),")) indent--;
lines[i] = " ".repeat(indent * 2) + line;
if (line.endsWith("(")) indent++;
}
return lines.join("\n");
}
function PlaygroundNoGrammar({
value,
onChange,
}: {
value: string;
onChange: (v: string) => void;
}) {
return (
<>
<h2 className="prose dark:prose-invert">Example code</h2>
<CodeMirror
extensions={[basicSetup]}
value={value}
onChange={onChange}
theme="dark"
/>
</>
);
}
class ErrorBoundary extends Component<{
children: ReactNode;
fallback: ReactNode;
}> {
state = { hasError: false };
static getDerivedStateFromError(error: any) {
return { hasError: error.message };
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return (
<>
<h2 className="prose dark:prose-invert">Error</h2>
<div>{this.state.hasError}</div>
{this.props.fallback}
</>
);
}
return this.props.children;
}
}