Explorar o código

SintaxError factory method

Lucas de Souza %!s(int64=6) %!d(string=hai) anos
pai
achega
f2f30e4ca6
Modificáronse 2 ficheiros con 34 adicións e 19 borrados
  1. 12 2
      js/asa/SintaxError.js
  2. 22 17
      js/asa/analisadorSintatico.js

+ 12 - 2
js/asa/SintaxError.js

@@ -1,5 +1,15 @@
-export class SintaxError extends Error{
-  constructor(...msg) {
+export class SintaxError extends Error {
+
+  static createError (msg, token) {
+    const val = SintaxError.getErrorString(msg, token);
+    return new SintaxError(val);
+  }
+
+  static getErrorString (symbol, token) {
+    return `Sintax error! Expecting '${symbol}' but found ${token.text} at line:${token.line}, column:${token.column}`;
+  }
+
+  constructor (...msg) {
     super(...msg);
     Error.captureStackTrace(this, SintaxError);
   }

+ 22 - 17
js/asa/analisadorSintatico.js

@@ -88,11 +88,16 @@ class AnalisadorSintatico {
   * @returns Declararion(const, type, id, initVal?)
   **/
   parseHasConst () {
-    const token = this.getToken();
-    if(token.type === this.lexer.PR_CONST) {
-      this.pos++
-      return parseDeclararion(true);
-    } else if(isVariableType(token)) {
+    const constToken = this.getToken();
+    if(constToken.type === this.lexer.PR_CONST) {
+      this.pos++;
+      const typeToken = this.getToken();
+      if(!this.isVariableType(typeToken)) {
+        throw SintaxError.createError(this.getCommaTypeString(), typeToken);
+      }
+      this.pos++;;
+      return parseDeclararion(true, typeToken);
+    } else if(isVariableType(constToken)) {
       return parseDeclararion();
     } else {
       return null;
@@ -107,18 +112,12 @@ class AnalisadorSintatico {
   parseDeclararion (isConst = false) {
     const typeToken = this.getToken();
     if(!this.isVariableType(typeToken)) {
-      throw new SintaxError(this.variableTypes.map( x => this.lexer.literalNames[x])
-        .reduce((o, n) => {
-        if (o.length <= 0)
-          return n;
-        else
-          return o + ", " + n;
-      }, ''), typeToken);
+      throw SintaxError.createError(this.getCommaTypeString(), typeToken);
     }
     this.pos++;
     const idToken = this.getToken();
     if(idToken.type !== this.lexer.ID) {
-      throw new SintaxError('ID', idToken);
+      throw SintaxError.createError('ID', idToken);
     }
     this.pos++;
     const equalsToken = this.getToken();
@@ -137,11 +136,17 @@ class AnalisadorSintatico {
     }
   }
 
-  getErrorString (symbol, token) {
-    return `Sintax error! Expecting '${symbol}' but found ${token.text} at line:${token.line}, column:${token.column}`;
-  }
-
   isVariableType (token) {
     return this.variableTypes.find(v => v === token.type);
   }
+
+  getCommaTypeString () {
+    return this.variableTypes.map( x => this.lexer.literalNames[x])
+        .reduce((o, n) => {
+        if (o.length <= 0)
+          return n;
+        else
+          return o + ", " + n;
+      }, '');
+  }
 }