-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser-5.dom.test.ts
94 lines (78 loc) · 2.03 KB
/
parser-5.dom.test.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
// Puts textareas into the DOM that can be used to test parser-5.peg.test.ts.
import * as Z from "./parser-5.js"
import { Grammar } from "./parser-5.peg.test.js"
Object.assign(window, { Z })
const entry = document.createElement("textarea")
const js = document.createElement("textarea")
const input = document.createElement("textarea")
const output = document.createElement("textarea")
function setStyle(style: CSSStyleDeclaration) {
style.boxSizing = "border-box"
style.height = "calc(50vh - 24px)"
style.width = "calc(50vw - 24px)"
style.resize = "none"
style.backgroundColor = "#303030"
style.borderRadius = "8px"
style.outline = "none"
style.color = "white"
style.padding = "8px 12px"
}
setStyle(entry.style)
setStyle(js.style)
setStyle(input.style)
setStyle(output.style)
entry.style.marginBottom = "16px"
entry.style.marginRight = "16px"
input.style.marginBottom = "16px"
js.style.marginRight = "16px"
let grammar: Z.AnyParser | string
entry.oninput = () => {
const result = Grammar.parse(entry.value)
js.value = result.value
if (!result.ok) {
grammar = result.value
return
}
try {
grammar = (0, eval)(result.value)
} catch (error) {
grammar = String(error)
}
try {
if (grammar instanceof Z.Parser) {
output.value = JSON.stringify(
grammar.parse(input.value).value,
undefined,
2,
)
} else {
output.value = grammar
}
} catch (error) {
output.value = String(error)
}
}
input.oninput = () => {
try {
if (grammar instanceof Z.Parser) {
output.value = JSON.stringify(
grammar.parse(input.value).value,
undefined,
2,
)
} else {
output.value = grammar
}
} catch (error) {
output.value = String(error)
}
}
document.head.remove()
document.body.remove()
document.documentElement.append(
document.createElement("head"),
document.createElement("body"),
)
document.documentElement.style.background = "#151515"
document.body.append(entry, input, js, output)
document.body.style.margin = "16px"