UNPKG

@designliquido/delegua

Version:

Linguagem de programação simples e moderna usando português estruturado.

308 lines 11.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LexadorPortugolIpt = void 0; const simbolo_1 = require("../simbolo"); const portugol_ipt_1 = require("./palavras-reservadas/portugol-ipt"); const portugol_ipt_2 = __importDefault(require("../../tipos-de-simbolos/portugol-ipt")); class LexadorPortugolIpt { eDigito(caractere) { return caractere >= '0' && caractere <= '9'; } eAlfabeto(caractere) { const acentuacoes = [ 'á', 'Á', 'ã', 'Ã', 'â', 'Â', 'à', 'À', 'é', 'É', 'ê', 'Ê', 'í', 'Í', 'ó', 'Ó', 'õ', 'Õ', 'ô', 'Ô', 'ú', 'Ú', 'ç', 'Ç', '_', ]; return ((caractere >= 'a' && caractere <= 'z') || (caractere >= 'A' && caractere <= 'Z') || acentuacoes.includes(caractere)); } eAlfabetoOuDigito(caractere) { return this.eDigito(caractere) || this.eAlfabeto(caractere); } eFinalDoCodigo() { if (this.linha > this.codigo.length - 1) return true; return (this.linha == this.codigo.length - 1 && this.codigo[this.codigo.length - 1].length <= this.atual); } /** * Indica se o código está na última linha. * @returns Verdadeiro se contador de linhas está na última linha. * Falso caso contrário. */ eUltimaLinha() { return this.linha >= this.codigo.length - 1; } avancar() { this.atual += 1; if (this.eFinalDaLinha() && !this.eUltimaLinha()) { this.linha++; this.atual = 0; } } adicionarSimbolo(tipo, literal) { const texto = this.codigo[this.linha].substring(this.inicioSimbolo, this.atual); const lexema = literal || texto; const comprimentoLexema = typeof lexema === 'string' ? lexema.length : 0; const comprimento = Math.max(comprimentoLexema, texto.length) || 1; const colunaInicio = this.inicioSimbolo + 1; const colunaFim = this.inicioSimbolo + comprimento; this.simbolos.push(new simbolo_1.Simbolo(tipo, lexema, literal, this.linha + 1, this.hashArquivo, colunaInicio, colunaFim)); } eFinalDaLinha() { return this.atual >= this.codigo[this.linha].length; } simboloAtual() { if (this.eFinalDaLinha()) return '\0'; if (this.linha > this.codigo.length - 1) return '\0'; return this.codigo[this.linha].charAt(this.atual); } proximoSimbolo() { if (this.atual + 1 >= this.codigo[this.linha].length) return '\0'; return this.codigo[this.linha].charAt(this.atual + 1); } simboloAnterior() { return this.codigo[this.linha].charAt(this.atual - 1); } analisarTexto(delimitador) { const linhaPrimeiroCaracter = this.linha; while (this.simboloAtual() !== delimitador && !this.eFinalDoCodigo()) { this.avancar(); } if (this.eFinalDoCodigo()) { this.erros.push({ linha: this.linha + 1, caractere: this.simboloAnterior(), mensagem: 'Texto não finalizado.', }); return; } const textoCompleto = this.codigo[this.linha].substring(this.inicioSimbolo + 1, this.atual); this.simbolos.push(new simbolo_1.Simbolo(portugol_ipt_2.default.TEXTO, textoCompleto, textoCompleto, linhaPrimeiroCaracter + 1, this.hashArquivo)); } analisarNumero() { const linhaPrimeiroDigito = this.linha; while (this.eDigito(this.simboloAtual()) && this.linha === linhaPrimeiroDigito) { this.avancar(); } if (this.simboloAtual() == '.' && this.eDigito(this.proximoSimbolo())) { this.avancar(); while (this.eDigito(this.simboloAtual())) { this.avancar(); } } let numeroCompleto; if (linhaPrimeiroDigito < this.linha) { const linhaNumero = this.codigo[linhaPrimeiroDigito]; numeroCompleto = linhaNumero.substring(this.inicioSimbolo, linhaNumero.length); } else { numeroCompleto = this.codigo[this.linha].substring(this.inicioSimbolo, this.atual); } this.simbolos.push(new simbolo_1.Simbolo(portugol_ipt_2.default.INTEIRO, numeroCompleto, parseFloat(numeroCompleto), linhaPrimeiroDigito + 1, this.hashArquivo)); } identificarPalavraChave() { const linhaPrimeiroCaracter = this.linha; while (this.eAlfabetoOuDigito(this.simboloAtual()) && this.linha === linhaPrimeiroCaracter) { this.avancar(); } let textoPalavraChave; if (linhaPrimeiroCaracter < this.linha) { const linhaPalavraChave = this.codigo[linhaPrimeiroCaracter]; textoPalavraChave = linhaPalavraChave.substring(this.inicioSimbolo, linhaPalavraChave.length); } else { textoPalavraChave = this.codigo[this.linha].substring(this.inicioSimbolo, this.atual); } const palavrasReservadasNormalizadas = portugol_ipt_1.palavrasReservadas; const palavraNormalizada = textoPalavraChave.toLowerCase(); let tipo = palavraNormalizada in palavrasReservadasNormalizadas ? palavrasReservadasNormalizadas[palavraNormalizada] : portugol_ipt_2.default.IDENTIFICADOR; // 'enquanto' sem 'faz' no final da linha é o fechamento de um faz...enquanto. if (tipo === portugol_ipt_2.default.ENQUANTO) { const linhaAtual = this.codigo[linhaPrimeiroCaracter].trim().toUpperCase(); if (!linhaAtual.startsWith('FIM ') && !linhaAtual.endsWith('FAZ')) { tipo = portugol_ipt_2.default.FAZENQUANTO; } } this.simbolos.push(new simbolo_1.Simbolo(tipo, textoPalavraChave, null, linhaPrimeiroCaracter + 1, this.hashArquivo)); } analisarToken() { const caractere = this.simboloAtual(); switch (caractere) { case ';': // Ponto-e-vírgula não é exatamente tolerado em Portugol IPT, // mas toleramos para evitar erros desnecessários. this.avancar(); break; case ' ': case '\t': case '\0': this.avancar(); break; case '\r': case '\n': this.adicionarSimbolo(portugol_ipt_2.default.QUEBRA_LINHA); this.avancar(); break; case '"': this.avancar(); this.analisarTexto('"'); this.avancar(); break; case '+': this.adicionarSimbolo(portugol_ipt_2.default.ADICAO); this.avancar(); break; case '-': this.adicionarSimbolo(portugol_ipt_2.default.SUBTRACAO); this.avancar(); break; case '*': this.adicionarSimbolo(portugol_ipt_2.default.MULTIPLICACAO); this.avancar(); break; case '/': this.adicionarSimbolo(portugol_ipt_2.default.DIVISAO); this.avancar(); break; case '%': this.adicionarSimbolo(portugol_ipt_2.default.MODULO); this.avancar(); break; case '^': this.adicionarSimbolo(portugol_ipt_2.default.EXPONENCIACAO); this.avancar(); break; case '=': this.avancar(); // Operador de diferença: =/= if (this.simboloAtual() === '/' && this.proximoSimbolo() === '=') { this.avancar(); // consome '/' this.avancar(); // consome '=' this.adicionarSimbolo(portugol_ipt_2.default.DIFERENTE); } else { this.adicionarSimbolo(portugol_ipt_2.default.IGUAL); } break; case '<': this.avancar(); switch (this.simboloAtual()) { case '-': this.adicionarSimbolo(portugol_ipt_2.default.SETA_ATRIBUICAO); this.avancar(); break; case '=': this.adicionarSimbolo(portugol_ipt_2.default.MENOR_IGUAL); this.avancar(); break; default: this.adicionarSimbolo(portugol_ipt_2.default.MENOR); break; } break; case '>': this.avancar(); if (this.simboloAtual() === '=') { this.adicionarSimbolo(portugol_ipt_2.default.MAIOR_IGUAL); this.avancar(); } else { this.adicionarSimbolo(portugol_ipt_2.default.MAIOR); } break; case '(': this.adicionarSimbolo(portugol_ipt_2.default.PARENTESE_ESQUERDO); this.avancar(); break; case ')': this.adicionarSimbolo(portugol_ipt_2.default.PARENTESE_DIREITO); this.avancar(); break; case '[': this.adicionarSimbolo(portugol_ipt_2.default.COLCHETE_ESQUERDO); this.avancar(); break; case ']': this.adicionarSimbolo(portugol_ipt_2.default.COLCHETE_DIREITO); this.avancar(); break; case ',': this.adicionarSimbolo(portugol_ipt_2.default.VIRGULA); this.avancar(); break; case ':': this.adicionarSimbolo(portugol_ipt_2.default.DOIS_PONTOS); this.avancar(); break; default: if (this.eDigito(caractere)) this.analisarNumero(); else if (this.eAlfabeto(caractere)) this.identificarPalavraChave(); else { this.erros.push({ linha: this.linha + 1, caractere: caractere, mensagem: 'Caractere inesperado.', }); this.avancar(); } } } mapear(codigo, hashArquivo) { this.simbolos = []; this.erros = []; this.inicioSimbolo = 0; this.atual = 0; this.linha = 0; this.codigo = codigo || ['']; this.hashArquivo = hashArquivo; for (let iterador = 0; iterador < this.codigo.length; iterador++) { this.codigo[iterador] += '\n'; } while (!this.eFinalDoCodigo()) { this.inicioSimbolo = this.atual; this.analisarToken(); } return { simbolos: this.simbolos, erros: this.erros, }; } } exports.LexadorPortugolIpt = LexadorPortugolIpt; //# sourceMappingURL=lexador-portugol-ipt.js.map