123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import * as ObjectClass from "./../enums/elements-class-enum";
- const TYPE = 0; // identifica se Ponto, Texto, Reta...
- const ID = 1; // identificador unico do objeto
- const DEFINITION = 2; // parametros dos quais o objeto depende (exemplo Reta depende de Ponto e Ponto => "2:IDPONTO1 IDPONTO2")
- const LIST = 3; // lista de identificadores de objetos dependentes do atual
- const LABEL = 4; // definicao do rotulo (valor/nome e se escondido ou nao)
- const DEFINED = 6; // 0 ou 1 (???)
- const COLOR = 5; // cor usando valor inteiro
- const HIDDEN = 7; // 0 (visivel) ou 1 (escondido)
- const PIXEL = 8; // LeScript usa (coordenada X e Y - inteiros)
- const FONT = 9; // 'Arial' T Tam: T = FONT.PLAIN, FONT.BOLD, FONT.ITALIC; Tam = 8, 9,...
- // PROIBIDO: "Arial" T Tam, pois em caso contrario nao sera lido como parametro
- // usa MARCAFNT = "\'" nao pode ser "\"" <[28/09/2006] agora vazio "">
- const LABEL_COLOR = 10; // cor de rotulo (tambem um inteiro) - inserido apos iGeom versao >= 2.9.9.31
- const OBJECT_LINE_REGEX = /^\{\w*(?:(?:\d+:@\|[^@]+@\|)|(?:\d+:[^,@|]+))\w*(?:,\w*(?:(?:\d+:@\|[^@]+@\|)|(?:\d+:[^,@\|]+)))+\w*\}!?$/.compile();
- export class FileParser {
- /**
- *
- * @param {string} content
- */
- constructor(content) {
- this.content = content;
- }
- parse () {
- const objects_list = []
- this.content.split('\n')
- .filter(str => !str.match(/^[#\[]/))
- .map(str => {
- if(!OBJECT_LINE_REGEX.test(str.trim())) {
- console.error("Malformed object string in igeom file: ",str);
- } else {
- const object = this.parseObjectLine(str.trim());
- objects_list.push(object);
- }
- });
- return objects_list;
- }
- /**
- *
- * @param {string} object_line
- */
- parseObjectLine (object_line) {
- let current_char = '';
- let current_pos = 0;
- const map = new Map();
- while(1==1) {
- current_char = object_line[current_pos];
- if(current_char == '}') {
- current_pos += 1;
- if(current_pos == object_line.length)
- break;
- current_char = object_line[current_pos]
- current_pos += 1;
- if(current_char != '!') {
- console.warn("Malformed object string: ",object_line);
- }
- break;
- }
- if(current_char == TYPE) {
- current_pos += 2;
- let type = '';
- while(object_line[current_pos] != ',') {
- type += object_line[current_pos];
- current_pos += 1;
- }
- map.set("type", ObjectClass.fromCodeToClass(parseInt(type)));
- } else if(current_char == ID) {
- current_pos += 2;
- let id = '';
- while(object_line[current_pos] != ',') {
- id += object_line[current_pos];
- current_pos += 1;
- }
- map.set("id", parseInt(id));
- } else if(current_char == DEFINITION) {
- current_pos += 2;
- const param_list = [];
- let word = '';
- while(object_line[current_pos] != ',') {
- if(object_line[current_pos] == ' ') {
- param_list.push(Number(word));
- word = '';
- } else if(object_line[current_pos] == '@') {
- current_pos += 2
- while(1==1) {
- const char = object_line[current_pos];
- if(char == '@' && bject_line[current_pos + 1] == '|') {
- current_pos += 1;
- break;
- } else {
- word += object_line[current_pos];
- }
- current_pos += 1;
- }
- } else {
- word += object_line[current_pos];
- }
- current_pos += 1;
- }
- param_list.push(word);
- map.set("param", param_list);
- }
- if(current_char == '{' || current_char == ' ') {
- current_pos += 1;
- continue;
- } else {
- console.warn("Malformed object string: ",object_line);
- break;
- }
- }
- if(current_pos != object_line.length) {
- console.warn("Object string ",object_line, " parsing did not match its size: Processed: ",current_pos," Length: ", object_line.length);
- }
- return map;
- }
- }
|