file-parser.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import * as ObjectClass from "./../enums/elements-class-enum";
  2. const TYPE = 0; // identifica se Ponto, Texto, Reta...
  3. const ID = 1; // identificador unico do objeto
  4. const DEFINITION = 2; // parametros dos quais o objeto depende (exemplo Reta depende de Ponto e Ponto => "2:IDPONTO1 IDPONTO2")
  5. const LIST = 3; // lista de identificadores de objetos dependentes do atual
  6. const LABEL = 4; // definicao do rotulo (valor/nome e se escondido ou nao)
  7. const DEFINED = 6; // 0 ou 1 (???)
  8. const COLOR = 5; // cor usando valor inteiro
  9. const HIDDEN = 7; // 0 (visivel) ou 1 (escondido)
  10. const PIXEL = 8; // LeScript usa (coordenada X e Y - inteiros)
  11. const FONT = 9; // 'Arial' T Tam: T = FONT.PLAIN, FONT.BOLD, FONT.ITALIC; Tam = 8, 9,...
  12. // PROIBIDO: "Arial" T Tam, pois em caso contrario nao sera lido como parametro
  13. // usa MARCAFNT = "\'" nao pode ser "\"" <[28/09/2006] agora vazio "">
  14. const LABEL_COLOR = 10; // cor de rotulo (tambem um inteiro) - inserido apos iGeom versao >= 2.9.9.31
  15. const OBJECT_LINE_REGEX = /^\{\w*(?:(?:\d+:@\|[^@]+@\|)|(?:\d+:[^,@|]+))\w*(?:,\w*(?:(?:\d+:@\|[^@]+@\|)|(?:\d+:[^,@\|]+)))+\w*\}!?$/.compile();
  16. export class FileParser {
  17. /**
  18. *
  19. * @param {string} content
  20. */
  21. constructor(content) {
  22. this.content = content;
  23. }
  24. parse () {
  25. const objects_list = []
  26. this.content.split('\n')
  27. .filter(str => !str.match(/^[#\[]/))
  28. .map(str => {
  29. if(!OBJECT_LINE_REGEX.test(str.trim())) {
  30. console.error("Malformed object string in igeom file: ",str);
  31. } else {
  32. const object = this.parseObjectLine(str.trim());
  33. objects_list.push(object);
  34. }
  35. });
  36. return objects_list;
  37. }
  38. /**
  39. *
  40. * @param {string} object_line
  41. */
  42. parseObjectLine (object_line) {
  43. let current_char = '';
  44. let current_pos = 0;
  45. const map = new Map();
  46. while(1==1) {
  47. current_char = object_line[current_pos];
  48. if(current_char == '}') {
  49. current_pos += 1;
  50. if(current_pos == object_line.length)
  51. break;
  52. current_char = object_line[current_pos]
  53. current_pos += 1;
  54. if(current_char != '!') {
  55. console.warn("Malformed object string: ",object_line);
  56. }
  57. break;
  58. }
  59. if(current_char == TYPE) {
  60. current_pos += 2;
  61. let type = '';
  62. while(object_line[current_pos] != ',') {
  63. type += object_line[current_pos];
  64. current_pos += 1;
  65. }
  66. map.set("type", ObjectClass.fromCodeToClass(parseInt(type)));
  67. } else if(current_char == ID) {
  68. current_pos += 2;
  69. let id = '';
  70. while(object_line[current_pos] != ',') {
  71. id += object_line[current_pos];
  72. current_pos += 1;
  73. }
  74. map.set("id", parseInt(id));
  75. } else if(current_char == DEFINITION) {
  76. current_pos += 2;
  77. const param_list = [];
  78. let word = '';
  79. while(object_line[current_pos] != ',') {
  80. if(object_line[current_pos] == ' ') {
  81. param_list.push(Number(word));
  82. word = '';
  83. } else if(object_line[current_pos] == '@') {
  84. current_pos += 2
  85. while(1==1) {
  86. const char = object_line[current_pos];
  87. if(char == '@' && bject_line[current_pos + 1] == '|') {
  88. current_pos += 1;
  89. break;
  90. } else {
  91. word += object_line[current_pos];
  92. }
  93. current_pos += 1;
  94. }
  95. } else {
  96. word += object_line[current_pos];
  97. }
  98. current_pos += 1;
  99. }
  100. param_list.push(word);
  101. map.set("param", param_list);
  102. }
  103. if(current_char == '{' || current_char == ' ') {
  104. current_pos += 1;
  105. continue;
  106. } else {
  107. console.warn("Malformed object string: ",object_line);
  108. break;
  109. }
  110. }
  111. if(current_pos != object_line.length) {
  112. console.warn("Object string ",object_line, " parsing did not match its size: Processed: ",current_pos," Length: ", object_line.length);
  113. }
  114. return map;
  115. }
  116. }