Parcourir la source

[FIX] Create consumeForSemiColon to prente a bug where a For could have no ';'

Create Test case for the bug above
Lucas de Souza il y a 6 ans
Parent
commit
26094d25c6
3 fichiers modifiés avec 39 ajouts et 5 suppressions
  1. 12 4
      js/ast/ivprogParser.js
  2. 1 1
      tests/test13.spec.js
  3. 26 0
      tests/test14.spec.js

+ 12 - 4
js/ast/ivprogParser.js

@@ -191,6 +191,15 @@ export class IVProgParser {
     return true;
   }
 
+  consumeForSemiColon () {
+    const eosToken = this.getToken();
+    if (eosToken.type === this.lexerClass.EOS && eosToken.text.match(';')) {
+      this.pos++;
+      return;  
+    }
+    throw SyntaxError.createError(';', eosToken);
+  }
+
   parseGlobalVariables () {
     const decl = this.parseMaybeConst();
     const eosToken = this.getToken();
@@ -631,6 +640,7 @@ export class IVProgParser {
       return new Commands.IfThenElse(logicalExpression, cmdBlocks, elseBlock);
     }
     this.popScope();
+
     return new Commands.IfThenElse(logicalExpression, cmdBlocks, null);
   }
 
@@ -643,8 +653,7 @@ export class IVProgParser {
     const attribution = this.parseForAssign();
     this.consumeNewLines();
     const condition = this.parseExpressionOR();
-    this.checkEOS();
-    this.pos++;
+    this.consumeForSemiColon();
     const increment = this.parseForAssign(true);
     this.checkCloseParenthesis()
     this.pos++;
@@ -721,8 +730,7 @@ export class IVProgParser {
     this.pos++
     const exp = this.parseExpressionOR();
     if(!isLast) {
-      this.checkEOS();
-      this.pos++;
+      this.consumeForSemiColon();
     }
     return new Commands.Assign(id, exp);
   }

+ 1 - 1
tests/test13.spec.js

@@ -25,7 +25,7 @@ describe('A complete program code', () => {
 
     it(`should not result in SyntaxError`, () => {
         const as = new IVProgParser(input, lexer);
-        const fun = as.parseFunction.bind(as);
+        const fun = as.parseTree.bind(as);
         expect(fun).not.toThrow(SyntaxError);
     });
 });

+ 26 - 0
tests/test14.spec.js

@@ -0,0 +1,26 @@
+import Lexers from './../grammar/';
+import {
+    IVProgParser
+} from './../js/ast/ivprogParser';
+import {
+    SyntaxError
+} from './../js/ast/SyntaxError';
+
+describe('For command', () => {
+    let input = `funcao inteiro test(real i) {
+      inteiro a = 5 + i
+      a = 5 + G[i][6]
+      para (a = 0
+        a < 5
+        a = a + 1) {
+         a = a
+      }
+    }`;
+    const lexer = Lexers['pt_br'];
+
+    it(`should result in SyntaxError`, () => {
+        const as = new IVProgParser(input, lexer);
+        const fun = as.parseFunction.bind(as);
+        expect(fun).toThrow();
+    });
+});