php.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var TextMode = require("./text").Mode;
  34. var PhpHighlightRules = require("./php_highlight_rules").PhpHighlightRules;
  35. var PhpLangHighlightRules = require("./php_highlight_rules").PhpLangHighlightRules;
  36. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  37. var Range = require("../range").Range;
  38. var WorkerClient = require("../worker/worker_client").WorkerClient;
  39. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  40. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  41. var unicode = require("../unicode");
  42. var HtmlMode = require("./html").Mode;
  43. var JavaScriptMode = require("./javascript").Mode;
  44. var CssMode = require("./css").Mode;
  45. var PhpMode = function(opts) {
  46. this.HighlightRules = PhpLangHighlightRules;
  47. this.$outdent = new MatchingBraceOutdent();
  48. this.$behaviour = new CstyleBehaviour();
  49. this.foldingRules = new CStyleFoldMode();
  50. };
  51. oop.inherits(PhpMode, TextMode);
  52. (function() {
  53. this.tokenRe = new RegExp("^["
  54. + unicode.packages.L
  55. + unicode.packages.Mn + unicode.packages.Mc
  56. + unicode.packages.Nd
  57. + unicode.packages.Pc + "\_]+", "g"
  58. );
  59. this.nonTokenRe = new RegExp("^(?:[^"
  60. + unicode.packages.L
  61. + unicode.packages.Mn + unicode.packages.Mc
  62. + unicode.packages.Nd
  63. + unicode.packages.Pc + "\_]|\s])+", "g"
  64. );
  65. this.lineCommentStart = ["//", "#"];
  66. this.blockComment = {start: "/*", end: "*/"};
  67. this.getNextLineIndent = function(state, line, tab) {
  68. var indent = this.$getIndent(line);
  69. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  70. var tokens = tokenizedLine.tokens;
  71. var endState = tokenizedLine.state;
  72. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  73. return indent;
  74. }
  75. if (state == "start") {
  76. var match = line.match(/^.*[\{\(\[\:]\s*$/);
  77. if (match) {
  78. indent += tab;
  79. }
  80. } else if (state == "doc-start") {
  81. if (endState != "doc-start") {
  82. return "";
  83. }
  84. var match = line.match(/^\s*(\/?)\*/);
  85. if (match) {
  86. if (match[1]) {
  87. indent += " ";
  88. }
  89. indent += "* ";
  90. }
  91. }
  92. return indent;
  93. };
  94. this.checkOutdent = function(state, line, input) {
  95. return this.$outdent.checkOutdent(line, input);
  96. };
  97. this.autoOutdent = function(state, doc, row) {
  98. this.$outdent.autoOutdent(doc, row);
  99. };
  100. this.$id = "ace/mode/php-inline";
  101. }).call(PhpMode.prototype);
  102. var Mode = function(opts) {
  103. if (opts && opts.inline) {
  104. var mode = new PhpMode();
  105. mode.createWorker = this.createWorker;
  106. mode.inlinePhp = true;
  107. return mode;
  108. }
  109. HtmlMode.call(this);
  110. this.HighlightRules = PhpHighlightRules;
  111. this.createModeDelegates({
  112. "js-": JavaScriptMode,
  113. "css-": CssMode,
  114. "php-": PhpMode
  115. });
  116. this.foldingRules.subModes["php-"] = new CStyleFoldMode();
  117. };
  118. oop.inherits(Mode, HtmlMode);
  119. (function() {
  120. this.createWorker = function(session) {
  121. var worker = new WorkerClient(["ace"], "ace/mode/php_worker", "PhpWorker");
  122. worker.attachToDocument(session.getDocument());
  123. if (this.inlinePhp)
  124. worker.call("setOptions", [{inline: true}]);
  125. worker.on("annotate", function(e) {
  126. session.setAnnotations(e.data);
  127. });
  128. worker.on("terminate", function() {
  129. session.clearAnnotations();
  130. });
  131. return worker;
  132. };
  133. this.$id = "ace/mode/php";
  134. }).call(Mode.prototype);
  135. exports.Mode = Mode;
  136. });