-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdingus.html
168 lines (155 loc) · 4.86 KB
/
dingus.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta lang=en charset="UTF-8">
<style>
.box { display:flex; flex-wrap: wrap; }
.grammar, .input, .output {
flex:auto;
width:400px; margin: 2px;
border:thin solid gray;
}
.header { position: relative; top:0px; height:20px;
padding:5px;
border-bottom: thin solid gray;
background: whitesmoke;
}
.header > button {float:right;}
.header > .examples {float:right;}
#grammar, #input, #output {
position: relative;
height:250px;
font-family: "Courier Line Draw", "Courier Prime", "Courier New", monospace;
white-space:pre-wrap; margin:2px;
padding:5px;
resize:both; overflow:auto;
}
</style>
<script type='module'>
import peg from './pPEG.mjs';
window.dingus = {
run: () => {
const grammar = document.getElementById('grammar'),
parser = peg.compile(grammar.textContent),
input = document.getElementById('input'),
parse = parser.parse(input.innerText), //textContent),
output = document.getElementById('output');
if (parse.ok) output.textContent = parse.show_ptree(); // TODO json?
else output.textContent = parse.show_err();
},
eg: () => {
const eg = document.getElementById('examples'),
grammar = document.getElementById('grammar'),
input = document.getElementById('input');
grammar.textContent = document.getElementById(eg.value+'-grammar').textContent,
input.textContent = document.getElementById(eg.value+'-input').textContent;
dingus.run();
},
}
dingus.run();
</script>
</head>
<body>
<h1>pPEG Dingus</h1>
<div class='box'>
<div class='grammar'>
<div class='bag'>
<div class='header'>
<label for="grammar">Grammar</label>
<span class='examples'>
<label for="examples">Examples: </label>
<select name="examples" id="examples" onChange='dingus.eg()'>
<option value="date">Date</option>
<option value="url">URL</option>
<option value="csv">CSV</option>
<option value="json">JSON</option>
</select>
</span>
</div>
<div id='grammar' contenteditable='true'>date = year '-' month '-' day
year = [1-2][0-9]*3
month = '0'[1-9] / '1'[0-2]
day = [0-3][0-9]
# A simple example to play around with.
# More examples in menu at top.
# This Dingus is only a toy web-page.
# To try your own pPEG grammars use
# the peg-play.mjs command line tool,
# that lets you work on your own files.
</div>
</div>
</div>
<div class='input'>
<div class='bag'>
<div class='header'>
<label for="input">Input</label>
<button onClick='dingus.run()'>Parse..</button>
</div>
<div id='input' contenteditable='true'>2021-03-04</div>
</div>
</div>
<div class='output'>
<div class='bag'>
<div class='header'>Parse Tree</div>
<div id='output'></div>
</div>
</div>
</div>
<div hidden id='date-grammar'>date = year '-' month '-' day
year = [1-2][0-9]*3
month = '0'[1-9] / '1'[0-2]
day = [0-3][0-9]
# A simple example to play around with.
# More examples in menu at top.
# This Dingus is only a toy web-page.
# To try your own pPEG grammars use
# the peg-play.mjs command line tool,
# that lets you work on your own files.
</div>
<div hidden id='date-input'>2021-02-03</div>
<div hidden id='url-grammar'># Equivalent to the regular expression for
# well-formed URI's in RFC 3986.
URI = (scheme ':')? ('//' auth)?
path ('?' query)? ('#' frag)?
scheme = ~[:/?#]+
auth = ~[/?#]*
path = ~[?#]*
query = ~'#'*
frag = ~[ \t\n\r]*
</div>
<div hidden id='url-input'>http://www.ics.uci.edu/pub/ietf/uri/#Related</div>
<div hidden id='csv-grammar'>CSV = Hdr Row+
Hdr = Row
Row = field (',' field)* '\r'? '\n'
field = _string / _text / ''
_text = ~[,\n\r]+
_string = '"' (~["] / '""')* '"'
</div>
<div hidden id='csv-input'>A,B,C
a1,b1,c1
a2,"b,2",c2
a3,b3,c3
</div>
<div hidden id='json-grammar'>json = _ value _
value = Str / Arr / Obj / num / lit
Obj = '{'_ (memb (_','_ memb)*)? _'}'
memb = Str _':'_ value
Arr = '['_ (value (_','_ value)*)? _']'
Str = '"' chars* '"'
chars = ~[\u0000-\u001F"\]+ / '\' esc
esc = ["\/bfnrt] / 'u' [0-9a-fA-F]*4
num = _int _frac? _exp?
_int = '-'? ([1-9] [0-9]* / '0')
_frac = '.' [0-9]+
_exp = [eE] [+-]? [0-9]+
lit = 'true' / 'false' / 'null'
_ = [ \t\n\r]*
</div>
<div hidden id='json-input'>{
"answer": 42,
"mixed": [1, 2.3, "a\tstring", true, [4, 5]],
"empty": {}
}</div>
<div>Copyright 2024</div>
</body>
</html>