php.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2015 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 Helper functions for generating PHP for blocks.
  22. * @author daarond@gmail.com (Daaron Dwyer)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.PHP');
  26. goog.require('Blockly.Generator');
  27. /**
  28. * PHP code generator.
  29. * @type {!Blockly.Generator}
  30. */
  31. Blockly.PHP = new Blockly.Generator('PHP');
  32. /**
  33. * List of illegal variable names.
  34. * This is not intended to be a security feature. Blockly is 100% client-side,
  35. * so bypassing this list is trivial. This is intended to prevent users from
  36. * accidentally clobbering a built-in object or function.
  37. * @private
  38. */
  39. Blockly.PHP.addReservedWords(
  40. // http://php.net/manual/en/reserved.keywords.php
  41. '__halt_compiler,abstract,and,array,as,break,callable,case,catch,class,clone,const,continue,declare,default,die,do,echo,else,elseif,empty,enddeclare,endfor,endforeach,endif,endswitch,endwhile,eval,exit,extends,final,for,foreach,function,global,goto,if,implements,include,include_once,instanceof,insteadof,interface,isset,list,namespace,new,or,print,private,protected,public,require,require_once,return,static,switch,throw,trait,try,unset,use,var,while,xor,' +
  42. // http://php.net/manual/en/reserved.constants.php
  43. 'PHP_VERSION,PHP_MAJOR_VERSION,PHP_MINOR_VERSION,PHP_RELEASE_VERSION,PHP_VERSION_ID,PHP_EXTRA_VERSION,PHP_ZTS,PHP_DEBUG,PHP_MAXPATHLEN,PHP_OS,PHP_SAPI,PHP_EOL,PHP_INT_MAX,PHP_INT_SIZE,DEFAULT_INCLUDE_PATH,PEAR_INSTALL_DIR,PEAR_EXTENSION_DIR,PHP_EXTENSION_DIR,PHP_PREFIX,PHP_BINDIR,PHP_BINARY,PHP_MANDIR,PHP_LIBDIR,PHP_DATADIR,PHP_SYSCONFDIR,PHP_LOCALSTATEDIR,PHP_CONFIG_FILE_PATH,PHP_CONFIG_FILE_SCAN_DIR,PHP_SHLIB_SUFFIX,E_ERROR,E_WARNING,E_PARSE,E_NOTICE,E_CORE_ERROR,E_CORE_WARNING,E_COMPILE_ERROR,E_COMPILE_WARNING,E_USER_ERROR,E_USER_WARNING,E_USER_NOTICE,E_DEPRECATED,E_USER_DEPRECATED,E_ALL,E_STRICT,__COMPILER_HALT_OFFSET__,TRUE,FALSE,NULL,__CLASS__,__DIR__,__FILE__,__FUNCTION__,__LINE__,__METHOD__,__NAMESPACE__,__TRAIT__');
  44. /**
  45. * Order of operation ENUMs.
  46. * http://php.net/manual/en/language.operators.precedence.php
  47. */
  48. Blockly.PHP.ORDER_ATOMIC = 0; // 0 "" ...
  49. Blockly.PHP.ORDER_CLONE = 1; // clone
  50. Blockly.PHP.ORDER_NEW = 1; // new
  51. Blockly.PHP.ORDER_MEMBER = 2; // ()
  52. Blockly.PHP.ORDER_FUNCTION_CALL = 2; // ()
  53. Blockly.PHP.ORDER_INCREMENT = 3; // ++
  54. Blockly.PHP.ORDER_DECREMENT = 3; // --
  55. Blockly.PHP.ORDER_LOGICAL_NOT = 4; // !
  56. Blockly.PHP.ORDER_BITWISE_NOT = 4; // ~
  57. Blockly.PHP.ORDER_UNARY_PLUS = 4; // +
  58. Blockly.PHP.ORDER_UNARY_NEGATION = 4; // -
  59. Blockly.PHP.ORDER_MULTIPLICATION = 5; // *
  60. Blockly.PHP.ORDER_DIVISION = 5; // /
  61. Blockly.PHP.ORDER_MODULUS = 5; // %
  62. Blockly.PHP.ORDER_ADDITION = 6; // +
  63. Blockly.PHP.ORDER_SUBTRACTION = 6; // -
  64. Blockly.PHP.ORDER_BITWISE_SHIFT = 7; // << >> >>>
  65. Blockly.PHP.ORDER_RELATIONAL = 8; // < <= > >=
  66. Blockly.PHP.ORDER_IN = 8; // in
  67. Blockly.PHP.ORDER_INSTANCEOF = 8; // instanceof
  68. Blockly.PHP.ORDER_EQUALITY = 9; // == != === !==
  69. Blockly.PHP.ORDER_BITWISE_AND = 10; // &
  70. Blockly.PHP.ORDER_BITWISE_XOR = 11; // ^
  71. Blockly.PHP.ORDER_BITWISE_OR = 12; // |
  72. Blockly.PHP.ORDER_CONDITIONAL = 13; // ?:
  73. Blockly.PHP.ORDER_ASSIGNMENT = 14; // = += -= *= /= %= <<= >>= ...
  74. Blockly.PHP.ORDER_LOGICAL_AND = 15; // &&
  75. Blockly.PHP.ORDER_LOGICAL_OR = 16; // ||
  76. Blockly.PHP.ORDER_COMMA = 17; // ,
  77. Blockly.PHP.ORDER_NONE = 99; // (...)
  78. /**
  79. * Initialise the database of variable names.
  80. * @param {!Blockly.Workspace} workspace Workspace to generate code from.
  81. */
  82. Blockly.PHP.init = function(workspace) {
  83. // Create a dictionary of definitions to be printed before the code.
  84. Blockly.PHP.definitions_ = Object.create(null);
  85. // Create a dictionary mapping desired function names in definitions_
  86. // to actual function names (to avoid collisions with user functions).
  87. Blockly.PHP.functionNames_ = Object.create(null);
  88. if (!Blockly.PHP.variableDB_) {
  89. Blockly.PHP.variableDB_ =
  90. new Blockly.Names(Blockly.PHP.RESERVED_WORDS_, '$');
  91. } else {
  92. Blockly.PHP.variableDB_.reset();
  93. }
  94. var defvars = [];
  95. var variables = Blockly.Variables.allVariables(workspace);
  96. for (var i = 0; i < variables.length; i++) {
  97. defvars[i] = Blockly.PHP.variableDB_.getName(variables[i],
  98. Blockly.Variables.NAME_TYPE) + ';';
  99. }
  100. Blockly.PHP.definitions_['variables'] = defvars.join('\n');
  101. };
  102. /**
  103. * Prepend the generated code with the variable definitions.
  104. * @param {string} code Generated code.
  105. * @return {string} Completed code.
  106. */
  107. Blockly.PHP.finish = function(code) {
  108. // Convert the definitions dictionary into a list.
  109. var definitions = [];
  110. for (var name in Blockly.PHP.definitions_) {
  111. definitions.push(Blockly.PHP.definitions_[name]);
  112. }
  113. // Clean up temporary data.
  114. delete Blockly.PHP.definitions_;
  115. delete Blockly.PHP.functionNames_;
  116. Blockly.PHP.variableDB_.reset();
  117. return definitions.join('\n\n') + '\n\n\n' + code;
  118. };
  119. /**
  120. * Naked values are top-level blocks with outputs that aren't plugged into
  121. * anything. A trailing semicolon is needed to make this legal.
  122. * @param {string} line Line of generated code.
  123. * @return {string} Legal line of code.
  124. */
  125. Blockly.PHP.scrubNakedValue = function(line) {
  126. return line + ';\n';
  127. };
  128. /**
  129. * Encode a string as a properly escaped PHP string, complete with
  130. * quotes.
  131. * @param {string} string Text to encode.
  132. * @return {string} PHP string.
  133. * @private
  134. */
  135. Blockly.PHP.quote_ = function(string) {
  136. // TODO: This is a quick hack. Replace with goog.string.quote
  137. string = string.replace(/\\/g, '\\\\')
  138. .replace(/\n/g, '\\\n')
  139. .replace(/'/g, '\\\'');
  140. return '\'' + string + '\'';
  141. };
  142. /**
  143. * Common tasks for generating PHP from blocks.
  144. * Handles comments for the specified block and any connected value blocks.
  145. * Calls any statements following this block.
  146. * @param {!Blockly.Block} block The current block.
  147. * @param {string} code The PHP code created for this block.
  148. * @return {string} PHP code with comments and subsequent blocks added.
  149. * @private
  150. */
  151. Blockly.PHP.scrub_ = function(block, code) {
  152. var commentCode = '';
  153. // Only collect comments for blocks that aren't inline.
  154. if (!block.outputConnection || !block.outputConnection.targetConnection) {
  155. // Collect comment for this block.
  156. var comment = block.getCommentText();
  157. if (comment) {
  158. commentCode += Blockly.PHP.prefixLines(comment, '// ') + '\n';
  159. }
  160. // Collect comments for all value arguments.
  161. // Don't collect comments for nested statements.
  162. for (var x = 0; x < block.inputList.length; x++) {
  163. if (block.inputList[x].type == Blockly.INPUT_VALUE) {
  164. var childBlock = block.inputList[x].connection.targetBlock();
  165. if (childBlock) {
  166. var comment = Blockly.PHP.allNestedComments(childBlock);
  167. if (comment) {
  168. commentCode += Blockly.PHP.prefixLines(comment, '// ');
  169. }
  170. }
  171. }
  172. }
  173. }
  174. var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
  175. var nextCode = Blockly.PHP.blockToCode(nextBlock);
  176. return commentCode + code + nextCode;
  177. };