2019-02-09 06:00:30 +00:00
|
|
|
// Setup editros
|
|
|
|
const grammar = ace.edit("grammar-editor");
|
|
|
|
grammar.setShowPrintMargin(false);
|
2019-02-27 14:04:58 +00:00
|
|
|
grammar.setValue(localStorage.getItem('grammarText') || '');
|
2019-02-09 06:00:30 +00:00
|
|
|
grammar.moveCursorTo(0, 0);
|
|
|
|
|
|
|
|
const code = ace.edit("code-editor");
|
|
|
|
code.setShowPrintMargin(false);
|
2019-02-27 14:04:58 +00:00
|
|
|
code.setValue(localStorage.getItem('codeText') || '');
|
2019-02-09 06:00:30 +00:00
|
|
|
code.moveCursorTo(0, 0);
|
|
|
|
|
|
|
|
const codeAst = ace.edit("code-ast");
|
|
|
|
codeAst.setShowPrintMargin(false);
|
|
|
|
codeAst.setOptions({
|
|
|
|
readOnly: true,
|
|
|
|
highlightActiveLine: false,
|
|
|
|
highlightGutterLine: false
|
|
|
|
})
|
|
|
|
codeAst.renderer.$cursorLayer.element.style.opacity=0;
|
|
|
|
|
|
|
|
const codeAstOptimized = ace.edit("code-ast-optimized");
|
|
|
|
codeAstOptimized.setShowPrintMargin(false);
|
|
|
|
codeAstOptimized.setOptions({
|
|
|
|
readOnly: true,
|
|
|
|
highlightActiveLine: false,
|
|
|
|
highlightGutterLine: false
|
|
|
|
})
|
|
|
|
codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0;
|
|
|
|
|
2020-05-26 22:30:19 +00:00
|
|
|
$('#opt_mode').val(localStorage.getItem('optimazationMode') || 'all');
|
2020-05-25 02:14:43 +00:00
|
|
|
|
2021-01-13 15:11:06 +00:00
|
|
|
function escapeHtml(unsafe) {
|
|
|
|
return unsafe
|
|
|
|
.replace(/&/g, "&")
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/"/g, """)
|
|
|
|
.replace(/'/g, "'");
|
|
|
|
}
|
|
|
|
|
2019-02-09 06:00:30 +00:00
|
|
|
function generateErrorListHTML(errors) {
|
|
|
|
let html = '<ul>';
|
|
|
|
|
|
|
|
html += $.map(errors, function (x) {
|
2021-01-13 15:11:06 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-05-25 21:31:22 +00:00
|
|
|
const optimazationMode = $('#opt_mode').val();
|
2020-05-25 02:14:43 +00:00
|
|
|
|
2019-02-09 06:00:30 +00:00
|
|
|
localStorage.setItem('grammarText', grammarText);
|
|
|
|
localStorage.setItem('codeText', codeText);
|
2020-05-25 21:31:22 +00:00
|
|
|
localStorage.setItem('optimazationMode', optimazationMode);
|
2019-02-09 06:00:30 +00:00
|
|
|
|
|
|
|
$grammarInfo.html('');
|
|
|
|
$grammarValidation.hide();
|
|
|
|
$codeInfo.html('');
|
|
|
|
$codeValidation.hide();
|
|
|
|
codeAst.setValue('');
|
|
|
|
codeAstOptimized.setValue('');
|
|
|
|
|
|
|
|
if (grammarText.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-25 21:31:22 +00:00
|
|
|
const mode = optimazationMode == 'all';
|
2021-01-22 01:56:05 +00:00
|
|
|
const data = JSON.parse(Module.lint(grammarText, codeText, mode));
|
2019-02-09 06:00:30 +00:00
|
|
|
|
2021-04-13 15:10:52 +00:00
|
|
|
if (data.grammar_valid) {
|
2019-02-09 06:00:30 +00:00
|
|
|
$grammarValidation.removeClass('editor-validation-invalid').text('Valid').show();
|
|
|
|
|
2021-01-15 05:05:22 +00:00
|
|
|
codeAst.insert(data.ast);
|
|
|
|
codeAstOptimized.insert(data.astOptimized);
|
2021-04-24 21:21:07 +00:00
|
|
|
|
|
|
|
if (data.source_valid) {
|
|
|
|
$codeValidation.removeClass('editor-validation-invalid').text('Valid').show();
|
|
|
|
} else {
|
|
|
|
$codeValidation.addClass('editor-validation-invalid').text('Invalid').show();
|
|
|
|
}
|
2021-01-15 05:05:22 +00:00
|
|
|
|
2021-04-13 15:10:52 +00:00
|
|
|
if (data.code.length > 0) {
|
2019-02-09 06:00:30 +00:00
|
|
|
const html = generateErrorListHTML(data.code);
|
|
|
|
$codeInfo.html(html);
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-13 15:10:52 +00:00
|
|
|
$grammarValidation.addClass('editor-validation-invalid').text('Invalid').show();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.grammar.length > 0) {
|
2019-02-09 06:00:30 +00:00
|
|
|
const html = generateErrorListHTML(data.grammar);
|
|
|
|
$grammarInfo.html(html);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event handing for text editiing
|
|
|
|
let timer;
|
|
|
|
function setupTimer() {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = setTimeout(parse, 750);
|
|
|
|
};
|
|
|
|
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);
|
2022-05-06 14:22:45 +00:00
|
|
|
editor.scrollToLine(el.data('ln') - 1, true, false, null);
|
2019-02-09 06:00:30 +00:00
|
|
|
editor.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$('#grammar-info').on('click', 'li', makeOnClickInInfo(grammar));
|
|
|
|
$('#code-info').on('click', 'li', makeOnClickInInfo(code));
|
|
|
|
|
2020-05-25 02:14:43 +00:00
|
|
|
// Event handing in the AST optimazation
|
2020-05-25 21:31:22 +00:00
|
|
|
$('#opt_mode').on('change', setupTimer);
|
2020-05-25 02:14:43 +00:00
|
|
|
|
2019-02-09 06:00:30 +00:00
|
|
|
// Show page
|
|
|
|
$('#main').css({
|
|
|
|
'display': 'flex',
|
|
|
|
});
|
|
|
|
|
|
|
|
// WebAssembly
|
|
|
|
var Module = {
|
|
|
|
onRuntimeInitialized: function() {
|
|
|
|
// Initial parse
|
|
|
|
parse();
|
|
|
|
}
|
|
|
|
};
|