Browse Source

Implement file parser options: Type, Id and Definition

Lucas de Souza 5 năm trước cách đây
mục cha
commit
26d0b236d8
2 tập tin đã thay đổi với 140 bổ sung0 xóa
  1. 15 0
      src/app/core/enums/elements-class-enum.js
  2. 125 0
      src/app/core/parser/file-parser.js

+ 15 - 0
src/app/core/enums/elements-class-enum.js

@@ -2,3 +2,18 @@ export const ELEMENTS_CLASS = {
   POINT: 0,
   LINE_SEGMENT: 1
 };
+
+/**
+ * 
+ * @param {number} code 
+ */
+export function fromCodeToClass (code) {
+  switch(code) {
+    case 0:
+      return ELEMENTS_CLASS.POINT;
+    case 1:
+      return ELEMENTS_CLASS.LINE_SEGMENT;
+    default:
+      return ELEMENTS_CLASS.POINT;
+  }
+}

+ 125 - 0
src/app/core/parser/file-parser.js

@@ -0,0 +1,125 @@
+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;
+  }
+
+}