Переглянути джерело

Update 'js/ast/ivprogParser.js'

Fixes to solve problem to read 'ivph' file with comments (contribution of Edilson Cuambe)
leo 1 тиждень тому
батько
коміт
60b838740c
1 змінених файлів з 47 додано та 16 видалено
  1. 47 16
      js/ast/ivprogParser.js

+ 47 - 16
js/ast/ivprogParser.js

@@ -1,3 +1,6 @@
+// iVProg - www.usp.br/line/ivprog
+// LInE - Free Education, Private Data
+
 import * as Expressions from "./expressions";
 import * as Commands from "./commands";
 import * as Parsers from "../typeSystem/parsers";
@@ -10,7 +13,18 @@ import { LanguageDefinedFunction } from "../processor/definedFunctions";
 import { LanguageService } from "../services/languageService";
 
 export class IVProgParser {
+
   static createParser (input, fill = true) {
+    // Language Detection: this allow iVProg load without error when it
+    // is under language X, but ivph code is under another language Y
+    if (input!="" && input!=null) { // undefined
+      if (input.includes("programa") && input.includes("funcao")) {
+        LanguageService.setLang('pt');
+        }
+      else {
+        LanguageService.setLang('en');
+        }
+      }
     const lexer = LanguageService.getCurrentLexer();
     const parser = new IVProgParser(input, lexer);
     if (fill) {
@@ -87,7 +101,7 @@ export class IVProgParser {
       return (
         token.type !== this.ruleNames.WHITESPACE &&
         token.type !== this.ruleNames.COMMENTS
-      );
+        );
     });
   }
 
@@ -108,6 +122,10 @@ export class IVProgParser {
     return this.tokenStream[index];
   }
 
+  check (tokenType) {
+    return this.getToken().type === tokenType;
+  }
+
   insideScope (scope) {
     if (this.scope.length <= 0) {
       return IVProgParser.BASE === scope;
@@ -301,6 +319,24 @@ export class IVProgParser {
     return decl;
   }
 
+  parseComment () {
+    const token = this.getToken();
+    let text = token.text;
+    if (text.startsWith("//")) {
+      text = text.replace(/^\/\/\s*/, "").trim();
+    } else if (text.startsWith("/*")) {
+      text = text
+        .replace(/^\/\*\s*/, "")
+        .replace(/\s*\*\/$/, "")
+        .trim();
+    }
+    this.pos++;
+    const cmd = new Commands.Comment(text);
+    cmd.sourceInfo = SourceInfo.createSourceInfo(token);
+    return cmd;
+  }
+
+
   /*
    * Checks if the next token is PR_CONST. It's only available
    * at global variables declaration level
@@ -392,24 +428,13 @@ export class IVProgParser {
     }
   }
 
-  parseArrayDeclaration (
-    typeString,
-    isConst,
-    idString,
-    sourceInfo,
-    dimensions,
-    dim1,
-    dim2
-  ) {
+  parseArrayDeclaration (typeString, isConst, idString, sourceInfo, dimensions, dim1, dim2) {
     const assignmentToken = this.getToken();
     let n_lines = dim1;
     let n_columns = dim2;
     let initial = null;
     let dim_is_id = false;
-    if (
-      dim1 instanceof Expressions.VariableLiteral ||
-      dim2 instanceof Expressions.VariableLiteral
-    ) {
+    if (dim1 instanceof Expressions.VariableLiteral || dim2 instanceof Expressions.VariableLiteral) {
       dim_is_id = true;
       if (dimensions > 1 && (dim1 == null || dim2 == null)) {
         throw SyntaxErrorFactory.invalid_matrix_id_dimension(
@@ -914,6 +939,11 @@ export class IVProgParser {
 
   parseCommand () {
     const token = this.getToken();
+
+    if (token.type === this.ruleNames.COMMENTS) {
+      return this.parseComment();
+    }
+
     if (this.isVariableType(token)) {
       if (!this.insideScope(IVProgParser.FUNCTION)) {
         throw SyntaxErrorFactory.invalid_var_declaration(token);
@@ -924,7 +954,7 @@ export class IVProgParser {
       const cmd = this.parseDeclaration(varType);
       this.checkEOS();
       this.pos++;
-      return cmd;
+      return cmd; // Return array of declarations
     } else if (token.type === this.ruleNames.ID) {
       return this.parseIDCommand();
     } else if (token.type === this.ruleNames.DOT) {
@@ -1569,4 +1599,5 @@ export class IVProgParser {
       : this.variableTypes;
     return types.map((x) => this.lexer.literalNames[x]);
   }
-}
+
+} // parseIDCommand()