cpp-peglib/docs/index.js

219 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-02-09 06:00:30 +00:00
// Setup editros
function setupInfoArea(id) {
const e = ace.edit(id);
e.setShowPrintMargin(false);
e.setOptions({
readOnly: true,
highlightActiveLine: false,
highlightGutterLine: false
})
e.renderer.$cursorLayer.element.style.opacity=0;
return e;
}
function setupEditorArea(id, lsKey) {
const e = ace.edit(id);
e.setShowPrintMargin(false);
e.setValue(localStorage.getItem(lsKey) || '');
e.moveCursorTo(0, 0);
return e;
}
const grammar = setupEditorArea("grammar-editor", "grammarText");
const code = setupEditorArea("code-editor", "codeText");
const codeAst = setupInfoArea("code-ast");
const codeAstOptimized = setupInfoArea("code-ast-optimized");
2022-06-04 19:13:13 +00:00
const codeProfile = setupInfoArea("code-profile");
2019-02-09 06:00:30 +00:00
2022-06-04 02:23:55 +00:00
$('#opt-mode').val(localStorage.getItem('optimazationMode') || 'all');
$('#packrat').prop('checked', localStorage.getItem('packrat') === 'true');
$('#auto-refresh').prop('checked', localStorage.getItem('autoRefresh') === 'true');
$('#parse').prop('disabled', $('#auto-refresh').prop('checked'));
// Parse
2021-01-13 15:11:06 +00:00
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
2019-02-09 06:00:30 +00:00
function generateErrorListHTML(errors) {
let html = '<ul>';
html += $.map(errors, function (x) {
2022-07-05 23:34:08 +00:00
if (x.gln && x.gcol) {
return `<li data-ln="${x.ln}" data-col="${x.col}" data-gln="${x.gln}" data-gcol="${x.gcol}"><span>${x.ln}:${x.col}</span> <span>${escapeHtml(x.msg)}</span></li>`;
} else {
return `<li data-ln="${x.ln}" data-col="${x.col}"><span>${x.ln}:${x.col}</span> <span>${escapeHtml(x.msg)}</span></li>`;
}
2019-02-09 06:00:30 +00:00
}).join('');
html += '<ul>';
return html;
}
2022-06-04 02:23:55 +00:00
function updateLocalStorage() {
localStorage.setItem('grammarText', grammar.getValue());
localStorage.setItem('codeText', code.getValue());
localStorage.setItem('optimazationMode', $('#opt-mode').val());
localStorage.setItem('packrat', $('#packrat').prop('checked'));
localStorage.setItem('autoRefresh', $('#auto-refresh').prop('checked'));
}
2019-02-09 06:00:30 +00:00
function parse() {
const $grammarValidation = $('#grammar-validation');
const $grammarInfo = $('#grammar-info');
const grammarText = grammar.getValue();
const $codeValidation = $('#code-validation');
const $codeInfo = $('#code-info');
const codeText = code.getValue();
2022-06-04 02:23:55 +00:00
const optimazationMode = $('#opt-mode').val();
const packrat = $('#packrat').prop('checked');
2019-02-09 06:00:30 +00:00
$grammarInfo.html('');
$grammarValidation.hide();
$codeInfo.html('');
$codeValidation.hide();
codeAst.setValue('');
codeAstOptimized.setValue('');
2022-06-04 19:13:13 +00:00
codeProfile.setValue('');
2019-02-09 06:00:30 +00:00
if (grammarText.length === 0) {
return;
}
2020-05-25 21:31:22 +00:00
const mode = optimazationMode == 'all';
2019-02-09 06:00:30 +00:00
2022-06-11 00:19:48 +00:00
$('#overlay').css({
'z-index': '1',
'display': 'block',
'background-color': 'rgba(0, 0, 0, 0.1)'
});
window.setTimeout(() => {
const data = JSON.parse(Module.lint(grammarText, codeText, mode, packrat));
$('#overlay').css({
'z-index': '-1',
'display': 'none',
'background-color': 'rgba(1, 1, 1, 1.0)'
});
if (data.grammar_valid) {
$grammarValidation.removeClass('validation-invalid').show();
codeAst.insert(data.ast);
codeAstOptimized.insert(data.astOptimized);
codeProfile.insert(data.profile);
if (data.source_valid) {
$codeValidation.removeClass('validation-invalid').show();
} else {
$codeValidation.addClass('validation-invalid').show();
}
if (data.code.length > 0) {
const html = generateErrorListHTML(data.code);
$codeInfo.html(html);
}
} else {
2022-06-11 00:19:48 +00:00
$grammarValidation.addClass('validation-invalid').show();
}
2021-01-15 05:05:22 +00:00
2022-06-11 00:19:48 +00:00
if (data.grammar.length > 0) {
const html = generateErrorListHTML(data.grammar);
$grammarInfo.html(html);
2019-02-09 06:00:30 +00:00
}
2022-06-11 00:19:48 +00:00
}, 0);
2019-02-09 06:00:30 +00:00
}
// Event handing for text editiing
let timer;
function setupTimer() {
clearTimeout(timer);
2022-06-04 02:23:55 +00:00
timer = setTimeout(() => {
updateLocalStorage();
if ($('#auto-refresh').prop('checked')) {
parse();
}
}, 750);
2019-02-09 06:00:30 +00:00
};
grammar.getSession().on('change', setupTimer);
code.getSession().on('change', setupTimer);
// Event handing in the info area
function makeOnClickInInfo(editor) {
return function () {
const el = $(this);
editor.navigateTo(el.data('ln') - 1, el.data('col') - 1);
editor.scrollToLine(el.data('ln') - 1, true, false, null);
2019-02-09 06:00:30 +00:00
editor.focus();
2022-07-05 23:34:08 +00:00
if(el.data('gln') && el.data('gcol')) {
grammar.navigateTo(el.data('gln') - 1, el.data('gcol') - 1);
grammar.scrollToLine(el.data('gln') - 1, true, false, null);
}
2019-02-09 06:00:30 +00:00
}
};
$('#grammar-info').on('click', 'li', makeOnClickInInfo(grammar));
$('#code-info').on('click', 'li', makeOnClickInInfo(code));
// Event handing in the AST optimazation
2022-06-04 02:23:55 +00:00
$('#opt-mode').on('change', setupTimer);
$('#packrat').on('change', setupTimer);
$('#auto-refresh').on('change', () => {
updateLocalStorage();
$('#parse').prop('disabled', $('#auto-refresh').prop('checked'));
setupTimer();
});
$('#parse').on('click', parse);
2022-06-10 21:34:19 +00:00
// Resize editors to fit their parents
function resizeEditorsToParent() {
code.resize();
code.renderer.updateFull();
codeAst.resize();
codeAst.renderer.updateFull();
codeAstOptimized.resize();
codeAstOptimized.renderer.updateFull();
codeProfile.resize();
codeProfile.renderer.updateFull();
}
2022-06-04 19:13:13 +00:00
// Show windows
function setupToolWindow(lsKeyName, buttonSel, codeSel) {
let show = localStorage.getItem(lsKeyName) === 'true';
$(buttonSel).prop('checked', show);
$(codeSel).css({ 'display': show ? 'block' : 'none' });
$(buttonSel).on('change', () => {
show = !show;
localStorage.setItem(lsKeyName, show);
$(codeSel).css({ 'display': show ? 'block' : 'none' });
2022-06-10 21:34:19 +00:00
resizeEditorsToParent();
2022-06-04 19:13:13 +00:00
});
}
setupToolWindow('show-ast', '#show-ast', '#code-ast');
setupToolWindow('show-ast-optimized', '#show-ast-optimized', '#code-ast-optimized');
setupToolWindow('show-profile', '#show-profile', '#code-profile');
2019-02-09 06:00:30 +00:00
// Show page
$('#main').css({
'display': 'flex',
});
// WebAssembly
var Module = {
onRuntimeInitialized: function() {
// Initial parse
2022-06-11 00:19:48 +00:00
if ($('#auto-refresh').prop('checked')) {
parse();
}
2019-02-09 06:00:30 +00:00
}
};