-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
134 lines (125 loc) · 4.31 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Escaner } from './Analizador/Escaner.js';
import { ListCastegory } from './Analizador/ListCategory.js';
document.addEventListener('DOMContentLoaded', () => {
let ModListener = false;
const checkbox_modo = document.querySelector('#listener_checkbox');
const btnAnalizar = document.querySelector('#btn-analizar');
const btnFile = document.querySelector('#btn-filetxt');
const btnClean = document.querySelector('#btn-clean');
const txtInput = document.querySelector('#txt-input');
checkbox_modo.addEventListener('change', () => {
ModListener = !ModListener;
});
txtInput.addEventListener('input', () => {
setTimeout(() => {
if (ModListener) {
StartAnalizador(txtInput);
}
}, 1000);
});
btnAnalizar.addEventListener('click', () => {
StartAnalizador(txtInput);
});
btnFile.addEventListener('click', () => {
let fileToLoad = document.getElementById('fileToLoad').files[0];
if (fileToLoad) {
let fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
let textFromFileLoaded = fileLoadedEvent.target.result;
txtInput.value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, 'UTF-8');
}
});
btnClean.addEventListener('click', () => {
Clean_MainTable();
txtInput.value = '';
document.getElementById('fileToLoad').value = '';
document.querySelector('#table-results').innerHTML = '';
});
});
const Clean_MainTable = () => {
document.querySelector('#n_tokens').innerHTML = 0;
document.querySelector(`#PalabrasReservadas`).innerHTML = 0;
document.querySelector(`#LiteralNumerico`).innerHTML = 0;
document.querySelector(`#LiteralCadena`).innerHTML = 0;
document.querySelector(`#Operadores`).innerHTML = 0;
document.querySelector(`#Delimitadores`).innerHTML = 0;
document.querySelector(`#SimbolosEspeciales`).innerHTML = 0;
document.querySelector(`#Identificadores`).innerHTML = 0;
document.querySelector(`#Importadores`).innerHTML = 0;
document.querySelector(`#Comentarios`).innerHTML = 0;
};
const StartAnalizador = (txtInput) => {
Clean_MainTable();
const ListTokens = Escaner(txtInput.value);
//console.log(ListTokens);
//Actualizar Tabla Resumen:
let MainTable = {};
ListTokens.map((Token) => {
if (!MainTable.hasOwnProperty(Token.get_CategoryToken())) {
MainTable = {
...MainTable,
[Token.get_CategoryToken()]: 1,
};
} else {
MainTable = {
...MainTable,
[Token.get_CategoryToken()]: MainTable[Token.get_CategoryToken()] + 1,
};
}
});
document.querySelector('#n_tokens').innerHTML = ListTokens.length.toString();
for (const category in MainTable) {
switch (category) {
case ListCastegory.PalabrasReservadas:
document.querySelector(`#PalabrasReservadas`).innerHTML = MainTable[category].toString();
break;
case ListCastegory.LiteralNumerico:
document.querySelector(`#LiteralNumerico`).innerHTML = MainTable[category].toString();
break;
case ListCastegory.LiteralCadena:
document.querySelector(`#LiteralCadena`).innerHTML = MainTable[category].toString();
break;
case ListCastegory.Operadores:
document.querySelector(`#Operadores`).innerHTML = MainTable[category].toString();
break;
case ListCastegory.Delimitadores:
document.querySelector(`#Delimitadores`).innerHTML = MainTable[category].toString();
break;
case ListCastegory.SimbolosEspeciales:
document.querySelector(`#SimbolosEspeciales`).innerHTML = MainTable[category].toString();
break;
case ListCastegory.Identificadores:
document.querySelector(`#Identificadores`).innerHTML = MainTable[category].toString();
break;
case ListCastegory.Importadores:
document.querySelector(`#Importadores`).innerHTML = MainTable[category].toString();
break;
case ListCastegory.Comentarios:
document.querySelector(`#Comentarios`).innerHTML = MainTable[category].toString();
break;
default:
break;
}
}
//Crear Tabla Dinamica
const table_results = document.querySelector('#table-results');
console.log(ListTokens);
const tbodyString = ListTokens.reduce((total, value) => {
let newcell =
'<tr>' +
'<td>' +
`${value.get_ValueToken()}` +
'</td>' +
'<td><b><pre><' +
`${value.get_TypeToken()}` +
'></pre></b></td>' +
'<td>' +
`${value.get_CategoryToken()}` +
'</td>' +
'</tr>';
return (total = total + newcell);
}, '');
table_results.innerHTML = tbodyString;
};