-
Notifications
You must be signed in to change notification settings - Fork 2
/
json2ebnf.html
499 lines (464 loc) · 15.9 KB
/
json2ebnf.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<html>
<head>
<title>Tree-sitter converter to EBNF for railroad diagram</title>
<meta name="author" content="Domingo Alvarez Duarte">
<meta name="contact" content="mingodad _at_ gmail _dot_ com">
<script type="text/javascript">
const INDENT = "\t";
function pattern2ebnf(pattern) {
let inClass = false;
let inQuote = false;
let hasDQ = false;
let hasSQ = false;
let imax = pattern.length;
let quote = "'";
let inString = "";
let ch = "";
var result = "";
var chAt = function(idx) {if(idx < imax) return pattern[idx]; return null;}
var addCh = function(ach) {if(inClass) result += ach; else inString += ach;}
var checkAddStr = function() {
if(inString.length > 0) {
if(hasSQ) quote = '"';
let new_str = quote + inString + quote; inString = "";
result += new_str
if(hasSQ) quote = "'";
}
}
var addClass = function(aclass) {if(inClass) result += aclass; else { checkAddStr(); result += '[' + aclass + ']';}}
for (let i = 0; i < imax; ++i) {
ch = pattern[i];
//print(ch);
switch(ch) {
case '[': {
checkAddStr();
result += ch; inClass = true;
}
break;
case ']': {
result += ch; inClass = false;
}
break;
case '|':
case '(': {
checkAddStr();
result += ch;
}
break;
case ')': {
checkAddStr();
result += ch;
}
break;
case '#': {
if(inClass) result += "#x23";
else addCh(ch);
}
break;
case '\t':
if(inClass) result += "#x09";
else addCh("\\t");
break;
case '\n':
if(inClass) result += "#x0A";
else addCh("\\n");
break;
case '\r':
if(inClass) result += "#x0D";
else addCh("\\r");
break;
case '\\': {
let i2 = i+1;
let ch2 = chAt(i2);
if(ch2 != null) {
//print(ch, ch2);
switch(ch2) {
case '[': addCh("#x5B"); i=i2; break;
case ']': addCh("#x5D"); i=i2; break;
case 'a': addCh("#x07"); i=i2; break;
case 'b': addCh("#x08"); i=i2; break;
case 't': addCh("#x09"); i=i2; break;
case 'n': addCh("#x0A"); i=i2; break;
case 'v': addCh("#x0B"); i=i2; break;
case 'f': addCh("#x0C"); i=i2; break;
case 'r': addCh("#x0D"); i=i2; break;
case '\\': addCh("\\"); i=i2; break;
case 's': addClass(" #x09#x0A#x0B#x0C#x0D");i=i2; break;
case 'd': addClass("0-9");i=i2; break;
case 'w': addClass("a-zA-Z_");i=i2; break;
case '-': addCh("#x2D"); ++i2; break;
case '.': ++i2; break;
case 'u': {
if( chAt(i2+2) == '{') {
i2 += 3;
addCh("#x");
for (; i2 < imax; ++i2) {
ch2 = chAt(i2);
if(ch2 == '}') break;
addCh(ch2);
}
}
else {addCh("\\u"); i=i2;}
}
break;
default:
addCh(ch);
}
}
}
break;
case '*':
case '+':
case '?': {
checkAddStr();
result += ch;
}
break;
case '"': hasDQ = true; addCh(ch); break;
case "'": hasSQ = true; addCh(ch); break;
default:
addCh(ch);
break;
}
}
checkAddStr();
return result;
}
function parseJsonGrammar(json, lang_raw_url, rule_sep, choice_sep, isEbnfRR)
{
json = JSON.parse(json);
//print(json);
var result = "";
result += "//\n// EBNF to generate railroad diagram at\n";
result += "// (IPV6) https://www.bottlecaps.de/rr/ui\n";
result += "// (IPV4) https://rr.red-dove.com/ui\n";
result += "//\n// From " + lang_raw_url + "\n//\n\n";
let isRuleOptional = function(rule) {
let isChoice = rule.type == "CHOICE";
let isOptional = isChoice && rule.members.length == 2 && rule.members[1].type == "BLANK";
return isOptional;
};
let manageRule = function (name, rule, depth) {
//print(name, rule.type); //, typeof rule);
switch(rule.type)
{
case "ALIAS": {
let needGroup = rule.content.hasOwnProperty('members');
let isOptional = isRuleOptional(rule.content);
if(isOptional && !rule.content.members[0].hasOwnProperty('members')) {
needGroup = false;
}
if(needGroup) result += " (";
manageRule(rule.type, rule.content, depth+1);
if(needGroup) result += " )";
if(isOptional) result += "?";
}
break;
case "BLANK":
result += " /*empty*/";
break;
case "CHOICE": {
let members = rule.members;
let isOptional = isRuleOptional(rule);
if(isOptional) {
manageRule(rule.type, members[0], depth+1);
}
else
{
for(var idx in members) {
let child = members[idx];
let isChildChoice = child.type == "CHOICE";
if(idx > 0) {
if((depth <1)) {
result += "\n" + INDENT + choice_sep;
}
else
{
result += " " + choice_sep;
}
}
manageRule(rule.type, child, (isChildChoice ? depth : depth+1));
}
}
}
break;
case "FIELD": {
//print(rule.type, rule.name);
let needGroup = rule.content.hasOwnProperty('members');
let isOptional = isRuleOptional(rule.content);
if(isOptional && !rule.content.members[0].hasOwnProperty('members')) {
needGroup = false;
}
if(needGroup) result += " (";
manageRule(rule.type, rule.content, depth+1);
if(needGroup) result += " )";
if(isOptional) result += "?";
}
break;
case "PATTERN": {
//result += " " + pattern2str(rule.value);
result += " " + pattern2ebnf(rule.value);
//let value = rule.value.replaceAll(/\\d/g, "[0-9]");
//value = value.replaceAll(/\[\[0\-9\]/g, "[0-9");
//if(isEbnfRR) {
// value = value.replaceAll(/\\w/g, "[a-zA-Z]");
// value = value.replaceAll(/\\([.*?+'])/g, "'$1'");
// //value = value.replaceAll("\\$", "'$'");
// value = value.replaceAll(/([^\\])\\]/g, "$1#x5D");
// value = value.replaceAll("\t", "#x09");
// value = value.replaceAll("\r", "#x0D");
// value = value.replaceAll("\n", "#x0A");
// value = value.replaceAll("_|", "'_'|");
// value = value.replace(/^(0x|0b)/, "'$1'");
// value = value.replaceAll(/({[^}]+?})/g, "+/*$1*/");
//}
//value = value.replaceAll("[0-9]]", "0-9]");
//value = value.replaceAll("![", "[^");
//result += " " + value;
}
break;
case "PREC":
case "PREC_DYNAMIC":
case "PREC_LEFT":
case "PREC_RIGHT":
//result += " (";
manageRule(rule.type, rule.content, depth+1);
//result += " )";
break;
case "REPEAT": {
let needGroup = rule.content.hasOwnProperty('members');
if(needGroup) result += " (";
manageRule(rule.type, rule.content, 1, depth+1);
if(needGroup) result += " )";
result += "*";
}
break;
case "REPEAT1": {
let needGroup = rule.content.hasOwnProperty('members');
if(needGroup) result += " (";
manageRule(rule.type, rule.content, depth+1);
if(needGroup) result += " )";
result += "+";
}
break;
case "SEQ": {
let members = rule.members;
for(var idx in members) {
let child = members[idx];
let isChoice = child.type == "CHOICE";
let needGroup = isChoice;
let isOptional = isRuleOptional(child);
if(isOptional && !child.members[0].hasOwnProperty('members')) {
needGroup = false;
}
if(needGroup) result += " (";
manageRule(rule.type, child, depth+1);
if(needGroup) result += " )";
if(isOptional) result += "?";
}
}
break;
case "STRING": {
let value = rule.value;
//print(rule.type, value);
switch(value) {
case "\0": result += " '\\0' "; break;
case "\b": result += " '\\b' "; break;
case "\f": result += " '\\f' "; break;
case "\n": result += " '\\n' "; break;
case "\r": result += " '\\r' "; break;
case "\t": result += " '\\t' "; break;
case "\v": result += " '\\v' "; break;
//case "\\": result += " '\\\\' "; break;
case "'": result += " \"'\" "; break;
case "\"": result += " '\"' "; break;
default:
//value = value.replaceAll(/\\/g, "\\\\");
value = value.replaceAll(/\t/g, "\\t"); //order matter
value = value.replaceAll(/\n/g, "\\n"); //order matter
value = value.replaceAll(/\r/g, "\\r"); //order matter
if(value.indexOf("'") >= 0) result += " \"" + value + "\"";
else result += " '" + value + "'";
}
}
break;
case "SYMBOL":
//print(rule.type, rule.name);
result += " " + rule.name;
break;
case "IMMEDIATE_TOKEN": {
let needGroup = rule.content.hasOwnProperty('members');
if(needGroup) result += " (";
manageRule(rule.type, rule.content, depth+1);
if(needGroup) result += " )";
}
break;
case "TOKEN": {
let needGroup = rule.content.hasOwnProperty('members');
if(needGroup) result += " (";
manageRule(rule.type, rule.content, depth+1);
if(needGroup) result += " )";
}
break;
//case "IMMEDIATE_TOKEN":
default:
throw("Unknown rule type: " + rule.type);
}
}
let rules = json.rules;
for(var idx in rules) {
let rule = rules[idx];
//print(rule);
result += idx + " " + rule_sep + "\n" + INDENT;
manageRule(idx, rule, 0);
result += "\n\n";
}
return result;
}
//const base_url = "https://github.com/";
const base_url = "https://raw.githubusercontent.com/";
//https://raw.githubusercontent.com/tree-sitter/tree-sitter/master/docs/index.md
const tree_sitter_list_url = "https://raw.githubusercontent.com/wiki/tree-sitter/tree-sitter/List-of-parsers.md";
//const base_url = window.location.origin + "/";
//const tree_sitter_list_url = base_url + "tree-sitter/docs/index.md";
const completed_state = 4;
function getGramarJson(element_id, lang_raw_url, first_request)
{
let req_json = new XMLHttpRequest();
req_json.onreadystatechange = function()
{
if ( this.readyState == completed_state )
{
if ( this.status == 404 )
{
if(first_request) {
lang_raw_url = lang_raw_url.replace("/master/", "/main/")
getGramarJson(element_id, lang_raw_url, false);
}
else {
alert("Could not find \"grammar.json\" on default path !");
}
}
else if(this.status == 200)
{
let myTextarea = document.getElementById( element_id );
let txt_json = this.responseText;
let ebnf = parseJsonGrammar(txt_json, lang_raw_url, "::=", "|", true);
myTextarea.value = ebnf;
/*
var formData = new FormData();
formData.append("name", "compilation");
formData.append("text", ebnf);
formData.append("task", "VIEW");
formData.append("options", "factoring");
formData.append("options", "eliminaterecursion");
formData.append("options", "inline");
formData.append("options", "keep");
formData.append("uri", "");
var req_railroad = new XMLHttpRequest();
req_railroad.onreadystatechange = function()
{
if ( this.readyState == completed_state && this.status == 200 )
{
let railroad = document.getElementById( "railroad" );
railroad.innerText = this.responseText;
}
}
req_railroad.open("POST", "https://bottlecaps.de/rr/ui", true);
//req_railroad.withCredentials = true;
//req_railroad.setRequestHeader('Cookie', "options=eliminaterecursion+factoring+inline+keep; width=992; spread=0; download-options=xhtml+svg+embedded; color=#FFDB4D");
req_railroad.send(formData);
*/
}
}
}
req_json.open( "GET", lang_raw_url, true );
req_json.send( );
}
function ajax_submit( element_id, submit_url )
{
let request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if ( this.readyState == completed_state && this.status == 200 )
{
let mydiv = document.getElementById( element_id );
let txt = this.responseText;
let parser_list = txt;
const lang_regexp = /- \[([^\]]+)\]\(([^)]+)\)/g;
let match, lang_list = "<option>Select one</option>\n";
const https_github_len = "https://github".length;
const https_github_com_len = "https://github.com/".length;
//https://github.com/maxxnino/tree-sitter-zig
//https://raw.githubusercontent.com/maxxnino/tree-sitter-zig/main/src/grammar.json
while ((match = lang_regexp.exec(parser_list)) !== null) {
let lang_name = match[1];
let lang_url = match[2];
let author_lang = lang_url.slice(https_github_com_len).replace("/", "_");
//https://raw.githubusercontent.com/briot/tree-sitter-ada/master/src/grammar.json
//https://raw.githubusercontent.com/tree-sitter/tree-sitter-agda/main/src/grammar.json
let lang_raw_url = "https://raw.githubusercontent" + lang_url.slice(https_github_len) + "/master/src/grammar.json";
//lang_list += `${lang_name} -> ${lang_url} -> ${lang_raw_url} -> ${author_lang}\n`;
//lang_list += `<p><a href="${lang_url}">${lang_name} -> ${author_lang} -> ${lang_raw_url}</a></p>\n`;
lang_list += `<option value="${lang_raw_url}">${lang_name} -> ${author_lang}</option>\n`;
//getGramarJson("gramarList2", lang_raw_url, true);
//break;
}
mydiv.
innerHTML
//innerText
= lang_list;
}
}
request.open( "GET", submit_url, true );
request.send( );
}
function loadParseGrammar(obj) {
let lang_raw_url = obj.options[obj.selectedIndex].value;
//console.log(lang_raw_url);
getGramarJson("gramarEBNF", lang_raw_url, true);
}
function loadParseGrammarUrl() {
let lang_raw_url = document.getElementById("raw_grammar_url");
if(lang_raw_url.value.length > 0) {
getGramarJson("gramarEBNF", lang_raw_url.value, true);
}
}
function showTreesitterGrammarList() {
let gramarListDiv = document.getElementById("gramarList");
ajax_submit("tsgrm", tree_sitter_list_url);
//gramarListDiv.innerHTML = "Hello World !";
}
</script>
<style type="text/css">
#gramarEBNF {width: 100%;height:65%; margin-top: 1em; padding: 1em;}
</style>
</head>
<body>
<h2><a href="https://tree-sitter.github.io/tree-sitter/" target="_blank">Tree-sitter</a> converter to <a href="https://www.bottlecaps.de/rr/ui" target="_blank">(IPV6) EBNF for railroad diagram</a></h2>
<div id="gramarExamples">
<p>Examples <select id="tsgrm" onchange="loadParseGrammar(this);"><option>Select one</option></select></p>
<p>Or type the raw url to 'grammar.json' here <input id="raw_grammar_url" placeholder="https://raw.githubusercontent.com/tree-sitter/tree-sitter-javascript/master/src/grammar.json"></input> <button onclick="loadParseGrammarUrl();">Go</button></p>
<p>
Copy and paste the <b>EBNF</b> shown bellow on <a href="https://www.bottlecaps.de/rr/ui">(IPV6) https://www.bottlecaps.de/rr/ui</a> or <a href="https://rr.red-dove.com/ui">(IPV4) https://rr.red-dove.com/ui</a> on the tab <b>'Edit Grammar'</b> the click on the tab <b>'View Diagram'</b> to see/download a navigable railroad diagram.
</p>
</div>
<form method="POST" action="https://rr.red-dove.com/ui" enctype="multipart/form-data" target="_blank">
<input type="hidden" name="name" value="compilation">
<input type="hidden" name="task" value="VIEW">
<input type="hidden" name="options" value="factoring">
<input type="hidden" name="options" value="eliminaterecursion">
<input type="hidden" name="options" value="inline">
<input type="hidden" name="options" value="keep">
<input type="hidden" name="uri" value="">
<p>
Or press <input type="submit" value="Show Railroad Diagram"> to automatically send it to https://rr.red-dove.com/ui .
</p>
<textarea id="gramarEBNF" name="text"></textarea>
</form>
<p>Noticed that from the same author (<a href="https://github.com/GuntherRademacher" target="_blank">Gunther Rademacher</a>) of the railroad diagram generator there is:</p>
<p><a href="https://www.bottlecaps.de/convert/" target="_blank">(IPV6) Convert2EBNF</a> that convert to EBNF from ABNF, ANTLR 3, ANTLR 4, Bison, GOLD, Instaparse, Invisible XML, JavaCC, Jison, PEG.js, Xtext .</p>
<p><a href="https://www.bottlecaps.de/rex/" target="_blank">(IPV6) REx Parser Generator</a> that generates parsers from EBNF in the following languages: C++, C#, Haxe, Java, JavaScript, TypeScript, Scala, XQuery, XSLT, XML .</p>
<script type="text/javascript">showTreesitterGrammarList()</script>
</body>
</html>