names.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Utility functions for handling variables and procedure names.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Names');
  26. /**
  27. * Class for a database of entity names (variables, functions, etc).
  28. * @param {string} reservedWords A comma-separated string of words that are
  29. * illegal for use as names in a language (e.g. 'new,if,this,...').
  30. * @param {string=} opt_variablePrefix Some languages need a '$' or a namespace
  31. * before all variable names.
  32. * @constructor
  33. */
  34. Blockly.Names = function(reservedWords, opt_variablePrefix) {
  35. this.variablePrefix_ = opt_variablePrefix || '';
  36. this.reservedDict_ = Object.create(null);
  37. if (reservedWords) {
  38. var splitWords = reservedWords.split(',');
  39. for (var i = 0; i < splitWords.length; i++) {
  40. this.reservedDict_[splitWords[i]] = true;
  41. }
  42. }
  43. this.reset();
  44. };
  45. /**
  46. * When JavaScript (or most other languages) is generated, variable 'foo' and
  47. * procedure 'foo' would collide. However, Blockly has no such problems since
  48. * variable get 'foo' and procedure call 'foo' are unambiguous.
  49. * Therefore, Blockly keeps a separate type name to disambiguate.
  50. * getName('foo', 'variable') -> 'foo'
  51. * getName('foo', 'procedure') -> 'foo2'
  52. */
  53. /**
  54. * Empty the database and start from scratch. The reserved words are kept.
  55. */
  56. Blockly.Names.prototype.reset = function() {
  57. this.db_ = Object.create(null);
  58. this.dbReverse_ = Object.create(null);
  59. };
  60. /**
  61. * Convert a Blockly entity name to a legal exportable entity name.
  62. * @param {string} name The Blockly entity name (no constraints).
  63. * @param {string} type The type of entity in Blockly
  64. * ('VARIABLE', 'PROCEDURE', 'BUILTIN', etc...).
  65. * @return {string} An entity name legal for the exported language.
  66. */
  67. Blockly.Names.prototype.getName = function(name, type) {
  68. var normalized = name.toLowerCase() + '_' + type;
  69. var prefix = (type == Blockly.Variables.NAME_TYPE) ?
  70. this.variablePrefix_ : '';
  71. if (normalized in this.db_) {
  72. return prefix + this.db_[normalized];
  73. }
  74. var safeName = this.getDistinctName(name, type);
  75. this.db_[normalized] = safeName.substr(prefix.length);
  76. return safeName;
  77. };
  78. /**
  79. * Convert a Blockly entity name to a legal exportable entity name.
  80. * Ensure that this is a new name not overlapping any previously defined name.
  81. * Also check against list of reserved words for the current language and
  82. * ensure name doesn't collide.
  83. * @param {string} name The Blockly entity name (no constraints).
  84. * @param {string} type The type of entity in Blockly
  85. * ('VARIABLE', 'PROCEDURE', 'BUILTIN', etc...).
  86. * @return {string} An entity name legal for the exported language.
  87. */
  88. Blockly.Names.prototype.getDistinctName = function(name, type) {
  89. var safeName = this.safeName_(name);
  90. var i = '';
  91. while (this.dbReverse_[safeName + i] ||
  92. (safeName + i) in this.reservedDict_) {
  93. // Collision with existing name. Create a unique name.
  94. i = i ? i + 1 : 2;
  95. }
  96. safeName += i;
  97. this.dbReverse_[safeName] = true;
  98. var prefix = (type == Blockly.Variables.NAME_TYPE) ?
  99. this.variablePrefix_ : '';
  100. return prefix + safeName;
  101. };
  102. /**
  103. * Given a proposed entity name, generate a name that conforms to the
  104. * [_A-Za-z][_A-Za-z0-9]* format that most languages consider legal for
  105. * variables.
  106. * @param {string} name Potentially illegal entity name.
  107. * @return {string} Safe entity name.
  108. * @private
  109. */
  110. Blockly.Names.prototype.safeName_ = function(name) {
  111. if (!name) {
  112. name = 'unnamed';
  113. } else {
  114. // Unfortunately names in non-latin characters will look like
  115. // _E9_9F_B3_E4_B9_90 which is pretty meaningless.
  116. name = encodeURI(name.replace(/ /g, '_')).replace(/[^\w]/g, '_');
  117. // Most languages don't allow names with leading numbers.
  118. if ('0123456789'.indexOf(name[0]) != -1) {
  119. name = 'my_' + name;
  120. }
  121. }
  122. return name;
  123. };
  124. /**
  125. * Do the given two entity names refer to the same entity?
  126. * Blockly names are case-insensitive.
  127. * @param {string} name1 First name.
  128. * @param {string} name2 Second name.
  129. * @return {boolean} True if names are the same.
  130. */
  131. Blockly.Names.equals = function(name1, name2) {
  132. return name1.toLowerCase() == name2.toLowerCase();
  133. };