jstparser.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. Copyright 2008, mark turansky (www.markturansky.com)
  3. Copyright 2010, Andrew Kelley (superjoesoftware.com)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is furnished
  9. to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. The Software shall be used for Good, not Evil.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. (This is the license from www.json.org and I think it's awesome)
  21. */
  22. String.prototype.endsWith = function endsWith(c) {
  23. if (this.charAt(this.length - 1) == c) {
  24. return true;
  25. } else {
  26. return false;
  27. }
  28. };
  29. String.prototype.startsWith = function startsWith(c) {
  30. if (this.charAt(0) == c) {
  31. return true;
  32. } else {
  33. return false;
  34. }
  35. };
  36. RegExp.quote = function(str) {
  37. return str.replace(/([.?*+\^$\[\]\\(){}\-])/g, "\\$1");
  38. };
  39. String.prototype.replaceAll = function replaceAll(a, b) {
  40. return this.replace(new RegExp(RegExp.quote(a), 'g'), b);
  41. };
  42. var Jst = function () {
  43. // private variables:
  44. var that; // reference to the public object
  45. // all write and writeln functions eval'd by Jst
  46. // concatenate to this internal variable
  47. var html = "";
  48. // private functions
  49. function CharacterStack(str) {
  50. var i;
  51. this.characters = [];
  52. this.peek = function peek() {
  53. return this.characters[this.characters.length - 1];
  54. };
  55. this.pop = function pop() {
  56. return this.characters.pop();
  57. };
  58. this.push = function push(c) {
  59. this.characters.push(c);
  60. };
  61. this.hasMore = function hasMore() {
  62. if (this.characters.length > 0) {
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. };
  68. for (i = str.length; i >= 0; i -= 1) {
  69. this.characters.push(str.charAt(i));
  70. }
  71. }
  72. function StringWriter() {
  73. this.str = "";
  74. this.write = function write(s) {
  75. this.str += s;
  76. };
  77. this.toString = function toString() {
  78. return this.str;
  79. };
  80. }
  81. function parseScriptlet(stack) {
  82. var fragment = new StringWriter();
  83. var c; // character
  84. while (stack.hasMore()) {
  85. if (stack.peek() == '%') { //possible end delimiter
  86. c = stack.pop();
  87. if (stack.peek() == '>') { //end delimiter
  88. // pop > so that it is not available to main parse loop
  89. stack.pop();
  90. if (stack.peek() == '\n') {
  91. fragment.write(stack.pop());
  92. }
  93. break;
  94. } else {
  95. fragment.write(c);
  96. }
  97. } else {
  98. fragment.write(stack.pop());
  99. }
  100. }
  101. return fragment.toString();
  102. }
  103. function isOpeningDelimiter(c) {
  104. if (c == "<" || c == "%lt;") {
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. function isClosingDelimiter(c) {
  111. if (c == ">" || c == "%gt;") {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. function appendExpressionFragment(writer, fragment, jstWriter) {
  118. var i,j;
  119. var c;
  120. // check to be sure quotes are on both ends of a string literal
  121. if (fragment.startsWith("\"") && !fragment.endsWith("\"")) {
  122. //some scriptlets end with \n, especially if the script ends the file
  123. if (fragment.endsWith("\n") && fragment.charAt(fragment.length - 2) == '"') {
  124. //we're ok...
  125. } else {
  126. throw { "message":"'" + fragment + "' is not properly quoted"};
  127. }
  128. }
  129. if (!fragment.startsWith("\"") && fragment.endsWith("\"")) {
  130. throw { "message":"'" + fragment + "' is not properly quoted"};
  131. }
  132. // print or println?
  133. if (fragment.endsWith("\n")) {
  134. writer.write(jstWriter + "ln(");
  135. //strip the newline
  136. fragment = fragment.substring(0, fragment.length - 1);
  137. } else {
  138. writer.write(jstWriter + "(");
  139. }
  140. if (fragment.startsWith("\"") && fragment.endsWith("\"")) {
  141. //strip the quotes
  142. fragment = fragment.substring(1, fragment.length - 1);
  143. writer.write("\"");
  144. for (i = 0; i < fragment.length; i += 1) {
  145. c = fragment.charAt(i);
  146. if (c == '"') {
  147. writer.write("\\");
  148. writer.write(c);
  149. }
  150. }
  151. writer.write("\"");
  152. } else {
  153. for (j = 0; j < fragment.length; j += 1) {
  154. writer.write(fragment.charAt(j));
  155. }
  156. }
  157. writer.write(");");
  158. }
  159. function appendTextFragment(writer, fragment) {
  160. var i;
  161. var c;
  162. if (fragment.endsWith("\n")) {
  163. writer.write("writeln(\"");
  164. } else {
  165. writer.write("write(\"");
  166. }
  167. for (i = 0; i < fragment.length; i += 1) {
  168. c = fragment.charAt(i);
  169. if (c == '"') {
  170. writer.write("\\");
  171. }
  172. // we took care of the line break with print vs. println
  173. if (c != '\n' && c != '\r') {
  174. writer.write(c);
  175. }
  176. }
  177. writer.write("\");");
  178. }
  179. function parseExpression(stack) {
  180. var fragment = new StringWriter();
  181. var c;
  182. while (stack.hasMore()) {
  183. if (stack.peek() == '%') { //possible end delimiter
  184. c = stack.pop();
  185. if (isClosingDelimiter(stack.peek())) { //end delimiter
  186. //pop > so that it is not available to main parse loop
  187. stack.pop();
  188. if (stack.peek() == '\n') {
  189. fragment.write(stack.pop());
  190. }
  191. break;
  192. } else {
  193. fragment.write("%");
  194. }
  195. } else {
  196. fragment.write(stack.pop());
  197. }
  198. }
  199. return fragment.toString();
  200. }
  201. function parseText(stack) {
  202. var fragment = new StringWriter();
  203. var c,d;
  204. while (stack.hasMore()) {
  205. if (isOpeningDelimiter(stack.peek())) { //possible delimiter
  206. c = stack.pop();
  207. if (stack.peek() == '%') { // delimiter!
  208. // push c onto the stack to be used in main parse loop
  209. stack.push(c);
  210. break;
  211. } else {
  212. fragment.write(c);
  213. }
  214. } else {
  215. d = stack.pop();
  216. fragment.write(d);
  217. if (d == '\n') { //done with this fragment. println it.
  218. break;
  219. }
  220. }
  221. }
  222. return fragment.toString();
  223. }
  224. function safeWrite(s) {
  225. s = s.toString();
  226. s = s.replaceAll('&', '&amp;');
  227. s = s.replaceAll('"', '&quot;');
  228. s = s.replaceAll('<', '&lt;');
  229. s = s.replaceAll('>', '&gt;');
  230. html += s;
  231. }
  232. function safeWriteln(s) {
  233. safeWrite(s + "\n");
  234. }
  235. function write(s) {
  236. html += s;
  237. }
  238. function writeln(s) {
  239. write(s + "\n");
  240. }
  241. that = {
  242. // public methods:
  243. // pre-compile a template for quicker rendering. save the return value and
  244. // pass it to evaluate.
  245. compile: function (src) {
  246. var stack = new CharacterStack(src);
  247. var writer = new StringWriter();
  248. var c;
  249. var fragment;
  250. while (stack.hasMore()) {
  251. if (isOpeningDelimiter(stack.peek())) { //possible delimiter
  252. c = stack.pop();
  253. if (stack.peek() == '%') { //delimiter!
  254. c = stack.pop();
  255. if (stack.peek() == "=") {
  256. // expression, escape all html
  257. stack.pop();
  258. fragment = parseExpression(stack);
  259. appendExpressionFragment(writer, fragment,
  260. "safeWrite");
  261. } else if (stack.peek() == "+") {
  262. // expression, don't escape html
  263. stack.pop();
  264. fragment = parseExpression(stack);
  265. appendExpressionFragment(writer, fragment,
  266. "write");
  267. } else {
  268. fragment = parseScriptlet(stack);
  269. writer.write(fragment);
  270. }
  271. } else { //not a delimiter
  272. stack.push(c);
  273. fragment = parseText(stack);
  274. appendTextFragment(writer, fragment);
  275. }
  276. } else {
  277. fragment = parseText(stack);
  278. appendTextFragment(writer, fragment);
  279. }
  280. }
  281. return writer.toString();
  282. },
  283. // evaluate a pre-compiled script. recommended approach
  284. evaluate: function (script, args) {
  285. with(args) {
  286. html = "";
  287. eval(script);
  288. return html;
  289. }
  290. },
  291. // if you're lazy, you can use this
  292. evaluateSingleShot: function (src, args) {
  293. return this.evaluate(this.compile(src), args);
  294. }
  295. };
  296. return that;
  297. }();