ソースを参照

Implement function and variables names checks against reserved words

Move all isValidIdentifier check to utils.js
Lucas de Souza 5 年 前
コミット
ffc952c460
4 ファイル変更59 行追加13 行削除
  1. 55 0
      js/util/utils.js
  2. 1 4
      js/visualUI/functions.js
  3. 1 4
      js/visualUI/globals.js
  4. 2 5
      js/visualUI/variables.js

+ 55 - 0
js/util/utils.js

@@ -1,3 +1,6 @@
+import { LanguageService } from "./../services/languageService";
+import { LocalizedStrings } from "./../services/localizedStringsService";
+import { Operators } from "./../ast/operators";
 
 /** 
  * source: https://pawelgrzybek.com/page-scroll-in-vanilla-javascript/ 
@@ -94,3 +97,55 @@ export function isElementInViewport (el) {
     rect.left < (window.innerWidth || document.documentElement.clientWidth) &&
     rect.top < (window.innerHeight || document.documentElement.clientHeight);
 }
+let cacheMainList = null;
+let cacheOp = null;
+export function isKeyword (text) {
+  fillCache();
+  for (let key = 0; key < cacheMainList.length; ++key) {
+    const keyword = cacheMainList[key];
+    if(keyword == text) {
+      return true;
+    }
+  }
+  // not in main list, check op
+  for (let op = 0; op < cacheOp.length; op++) {
+    const lOp = cacheOp[op];
+    if(lOp == text) {
+      return true;
+    }
+  }
+  return false;
+}
+
+export function isValidIdentifier (identifier_str) {
+  const validRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(identifier_str);
+  if(!validRegex) {
+    return false;
+  }
+	return !isKeyword(identifier_str);
+}
+
+function fillCache () {
+  if(cacheMainList == null) {
+    cacheMainList = [];
+    const mainList = ["RK_PROGRAM","RK_REAL","RK_VOID","RK_BOOLEAN","RK_STRING",
+      "RK_INTEGER","RK_CHARACTER","RK_SWITCH","RK_CASE","RK_DEFAULT","RK_CONST",
+      "RK_FUNCTION","RK_RETURN","RK_FOR","RK_BREAK","RK_DO","RK_WHILE","RK_IF",
+      "RK_ELSE","RK_FALSE","RK_TRUE"];
+    const lexerClass = LanguageService.getCurrentLexer();
+    const nullLexer = new lexerClass();
+    for (let key = 0; key < mainList.length; ++key) {
+      const word = mainList[key];
+      const keyword = nullLexer.literalNames[lexerClass[word]];
+      cacheMainList.push(keyword.substring(1, keyword.length-1));
+    }
+  }
+  if(cacheOp == null) {
+    cacheOp = []
+    const logicOpList = [Operators.AND.value, Operators.OR.value, Operators.NOT.value];
+    for (let op = 0; op < logicOpList.length; ++op) {
+      const lOp = `logic_operator_${logicOpList[op]}`;
+      cacheOp.push(LocalizedStrings.getUI(lOp))
+    }
+  }
+}

+ 1 - 4
js/visualUI/functions.js

@@ -15,6 +15,7 @@ import * as AlgorithmManagement from './algorithm';
 import * as Utils from './utils';
 import { registerUserEvent, ActionTypes } from "./../services/userLog";
 import VersionInfo from './../../.ima_version.json';
+import { isValidIdentifier } from "./../util/utils";
 
 var counter_new_functions = 0;
 var counter_new_parameters = 0;
@@ -1184,10 +1185,6 @@ function functionNameAlreadyExists (function_name) {
   return false;
 }
 
-function isValidIdentifier (identifier_str) {
-  return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(identifier_str);
-}
-
 var opened_name_parameter = false;
 var opened_input_parameter = null;
 function enableNameParameterUpdate (parameter_obj, parent_node, function_obj) {

+ 1 - 4
js/visualUI/globals.js

@@ -3,6 +3,7 @@ import * as Models from './ivprog_elements';
 import { LocalizedStrings } from './../services/localizedStringsService';
 import * as Utils from './utils';
 import { registerUserEvent, registerSystemEvent, ActionTypes } from "./../services/userLog";
+import { isValidIdentifier } from "./../util/utils";
 
 var counter_new_globals = 0;
 
@@ -55,10 +56,6 @@ function globalNameAlreadyExists (global_name) {
   return false;
 }
 
-function isValidIdentifier (identifier_str) {
-	return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(identifier_str);
-}
-
 function updateType (global_var, new_type, new_dimensions = 0) {
 	global_var.type = new_type;
 	global_var.dimensions = new_dimensions;

+ 2 - 5
js/visualUI/variables.js

@@ -3,6 +3,7 @@ import * as Models from './ivprog_elements';
 import { LocalizedStrings } from './../services/localizedStringsService';
 import * as Utils from './utils';
 import { registerUserEvent, registerSystemEvent, ActionTypes } from "./../services/userLog";
+import { isValidIdentifier } from "./../util/utils";
 
 var counter_new_variables = 0;
 
@@ -34,7 +35,7 @@ function updateName (variable_obj, new_name, variable_obj_dom, function_obj) {
 		if (variableNameAlreadyExists(new_name, function_obj)) {
 			Utils.renderErrorMessage(variable_obj_dom.find('.editing_name_var'), LocalizedStrings.getUI('inform_valid_variable_duplicated'));
 		} else {
-			registerUserEvent(function_obj.name, ActionTypes.REMOVE_FUNCTION_VAR, variable_obj.name, new_name);
+			registerUserEvent(function_obj.name, ActionTypes.RENAME_FUNCTION_VAR, variable_obj.name, new_name);
 			variable_obj.name = new_name;
 		}
 	} else {
@@ -63,10 +64,6 @@ function variableNameAlreadyExists (name_var, function_obj) {
 	return false;
 }
 
-function isValidIdentifier (identifier_str) {
-	return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(identifier_str);
-}
-
 function removeVariable (variable_obj, variable_container, function_name) {
 	var function_associated = variable_container.data('associatedFunction');
 	registerUserEvent(function_name, ActionTypes.REMOVE_FUNCTION_VAR, variable_obj.name);