-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
112 lines (101 loc) · 3.76 KB
/
index.js
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
import * as monaco from 'https://cdn.jsdelivr.net/npm/monaco-editor@0.50.0/+esm';
import { parse } from './parser/gramatica.js';
import { generateParser } from './parser/compiler/utils.js';
export let ids = [];
export let usos = [];
export let errores = [];
// Crear el editor principal
const editor = monaco.editor.create(document.getElementById('editor'), {
value: '',
language: 'java',
theme: 'tema',
automaticLayout: true,
});
// Crear el editor para la salida
const salida = monaco.editor.create(document.getElementById('salida'), {
value: '',
language: 'java',
readOnly: true,
automaticLayout: true,
});
let decorations = [];
// Analizar contenido del editor
let cst;
const analizar = async () => {
const entrada = editor.getValue();
ids.length = 0;
usos.length = 0;
errores.length = 0;
try {
cst = parse(entrada);
if (errores.length > 0) {
salida.setValue(`Error: ${errores[0].message}`);
cst = null;
return;
} else {
// salida.setValue('Análisis Exitoso');
const fileContents = await generateParser(cst); // Llamada correcta a generateParser
salida.setValue(fileContents); // Mostrar el contenido generado en la salida
// Preparar el botón de descarga
const blob = new Blob([fileContents], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const button = document.getElementById('BotonDescarga'); // Asegúrate de que el ID coincide con el HTML
button.href = url;
}
// salida.setValue("Análisis Exitoso");
// Limpiar decoraciones previas si la validación es exitosa
decorations = editor.deltaDecorations(decorations, []);
} catch (e) {
cst = null;
if (e.location === undefined) {
salida.setValue(`Error: ${e.message}`);
} else {
// Mostrar mensaje de error en el editor de salida
salida.setValue(
`Error: ${e.message}\nin line ${e.location.start.line} column ${e.location.start.column}`
);
// Resaltar el error en el editor de entrada
decorations = editor.deltaDecorations(decorations, [
{
range: new monaco.Range(
e.location.start.line,
e.location.start.column,
e.location.start.line,
e.location.start.column + 1
),
options: {
inlineClassName: 'errorHighlight', // Clase CSS personalizada para cambiar color de letra
},
},
{
range: new monaco.Range(
e.location.start.line,
e.location.start.column,
e.location.start.line,
e.location.start.column
),
options: {
glyphMarginClassName: 'warningGlyph', // Clase CSS para mostrar un warning en el margen
},
},
]);
}
}
};
// Escuchar cambios en el contenido del editor
editor.onDidChangeModelContent(() => {
analizar();
});
// CSS personalizado para resaltar el error y agregar un warning
const style = document.createElement('style');
style.innerHTML = `
.errorHighlight {
color: red !important;
font-weight: bold;
}
.warningGlyph {
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill="orange" d="M8 1l7 14H1L8 1z"/></svg>') no-repeat center center;
background-size: contain;
}
`;
document.head.appendChild(style);