You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
16 KiB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("../javascript/javascript"), require("../css/css"), require("../htmlmixed/htmlmixed"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../javascript/javascript", "../css/css", "../htmlmixed/htmlmixed"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode('jade', function (config) {
  13. // token types
  14. var KEYWORD = 'keyword';
  15. var DOCTYPE = 'meta';
  16. var ID = 'builtin';
  17. var CLASS = 'qualifier';
  18. var ATTRS_NEST = {
  19. '{': '}',
  20. '(': ')',
  21. '[': ']'
  22. };
  23. var jsMode = CodeMirror.getMode(config, 'javascript');
  24. function State() {
  25. this.javaScriptLine = false;
  26. this.javaScriptLineExcludesColon = false;
  27. this.javaScriptArguments = false;
  28. this.javaScriptArgumentsDepth = 0;
  29. this.isInterpolating = false;
  30. this.interpolationNesting = 0;
  31. this.jsState = CodeMirror.startState(jsMode);
  32. this.restOfLine = '';
  33. this.isIncludeFiltered = false;
  34. this.isEach = false;
  35. this.lastTag = '';
  36. this.scriptType = '';
  37. // Attributes Mode
  38. this.isAttrs = false;
  39. this.attrsNest = [];
  40. this.inAttributeName = true;
  41. this.attributeIsType = false;
  42. this.attrValue = '';
  43. // Indented Mode
  44. this.indentOf = Infinity;
  45. this.indentToken = '';
  46. this.innerMode = null;
  47. this.innerState = null;
  48. this.innerModeForLine = false;
  49. }
  50. /**
  51. * Safely copy a state
  52. *
  53. * @return {State}
  54. */
  55. State.prototype.copy = function () {
  56. var res = new State();
  57. res.javaScriptLine = this.javaScriptLine;
  58. res.javaScriptLineExcludesColon = this.javaScriptLineExcludesColon;
  59. res.javaScriptArguments = this.javaScriptArguments;
  60. res.javaScriptArgumentsDepth = this.javaScriptArgumentsDepth;
  61. res.isInterpolating = this.isInterpolating;
  62. res.interpolationNesting = this.interpolationNesting;
  63. res.jsState = CodeMirror.copyState(jsMode, this.jsState);
  64. res.innerMode = this.innerMode;
  65. if (this.innerMode && this.innerState) {
  66. res.innerState = CodeMirror.copyState(this.innerMode, this.innerState);
  67. }
  68. res.restOfLine = this.restOfLine;
  69. res.isIncludeFiltered = this.isIncludeFiltered;
  70. res.isEach = this.isEach;
  71. res.lastTag = this.lastTag;
  72. res.scriptType = this.scriptType;
  73. res.isAttrs = this.isAttrs;
  74. res.attrsNest = this.attrsNest.slice();
  75. res.inAttributeName = this.inAttributeName;
  76. res.attributeIsType = this.attributeIsType;
  77. res.attrValue = this.attrValue;
  78. res.indentOf = this.indentOf;
  79. res.indentToken = this.indentToken;
  80. res.innerModeForLine = this.innerModeForLine;
  81. return res;
  82. };
  83. function javaScript(stream, state) {
  84. if (stream.sol()) {
  85. // if javaScriptLine was set at end of line, ignore it
  86. state.javaScriptLine = false;
  87. state.javaScriptLineExcludesColon = false;
  88. }
  89. if (state.javaScriptLine) {
  90. if (state.javaScriptLineExcludesColon && stream.peek() === ':') {
  91. state.javaScriptLine = false;
  92. state.javaScriptLineExcludesColon = false;
  93. return;
  94. }
  95. var tok = jsMode.token(stream, state.jsState);
  96. if (stream.eol()) state.javaScriptLine = false;
  97. return tok || true;
  98. }
  99. }
  100. function javaScriptArguments(stream, state) {
  101. if (state.javaScriptArguments) {
  102. if (state.javaScriptArgumentsDepth === 0 && stream.peek() !== '(') {
  103. state.javaScriptArguments = false;
  104. return;
  105. }
  106. if (stream.peek() === '(') {
  107. state.javaScriptArgumentsDepth++;
  108. } else if (stream.peek() === ')') {
  109. state.javaScriptArgumentsDepth--;
  110. }
  111. if (state.javaScriptArgumentsDepth === 0) {
  112. state.javaScriptArguments = false;
  113. return;
  114. }
  115. var tok = jsMode.token(stream, state.jsState);
  116. return tok || true;
  117. }
  118. }
  119. function yieldStatement(stream) {
  120. if (stream.match(/^yield\b/)) {
  121. return 'keyword';
  122. }
  123. }
  124. function doctype(stream) {
  125. if (stream.match(/^(?:doctype) *([^\n]+)?/)) {
  126. return DOCTYPE;
  127. }
  128. }
  129. function interpolation(stream, state) {
  130. if (stream.match('#{')) {
  131. state.isInterpolating = true;
  132. state.interpolationNesting = 0;
  133. return 'punctuation';
  134. }
  135. }
  136. function interpolationContinued(stream, state) {
  137. if (state.isInterpolating) {
  138. if (stream.peek() === '}') {
  139. state.interpolationNesting--;
  140. if (state.interpolationNesting < 0) {
  141. stream.next();
  142. state.isInterpolating = false;
  143. return 'punctuation';
  144. }
  145. } else if (stream.peek() === '{') {
  146. state.interpolationNesting++;
  147. }
  148. return jsMode.token(stream, state.jsState) || true;
  149. }
  150. }
  151. function caseStatement(stream, state) {
  152. if (stream.match(/^case\b/)) {
  153. state.javaScriptLine = true;
  154. return KEYWORD;
  155. }
  156. }
  157. function when(stream, state) {
  158. if (stream.match(/^when\b/)) {
  159. state.javaScriptLine = true;
  160. state.javaScriptLineExcludesColon = true;
  161. return KEYWORD;
  162. }
  163. }
  164. function defaultStatement(stream) {
  165. if (stream.match(/^default\b/)) {
  166. return KEYWORD;
  167. }
  168. }
  169. function extendsStatement(stream, state) {
  170. if (stream.match(/^extends?\b/)) {
  171. state.restOfLine = 'string';
  172. return KEYWORD;
  173. }
  174. }
  175. function append(stream, state) {
  176. if (stream.match(/^append\b/)) {
  177. state.restOfLine = 'variable';
  178. return KEYWORD;
  179. }
  180. }
  181. function prepend(stream, state) {
  182. if (stream.match(/^prepend\b/)) {
  183. state.restOfLine = 'variable';
  184. return KEYWORD;
  185. }
  186. }
  187. function block(stream, state) {
  188. if (stream.match(/^block\b *(?:(prepend|append)\b)?/)) {
  189. state.restOfLine = 'variable';
  190. return KEYWORD;
  191. }
  192. }
  193. function include(stream, state) {
  194. if (stream.match(/^include\b/)) {
  195. state.restOfLine = 'string';
  196. return KEYWORD;
  197. }
  198. }
  199. function includeFiltered(stream, state) {
  200. if (stream.match(/^include:([a-zA-Z0-9\-]+)/, false) && stream.match('include')) {
  201. state.isIncludeFiltered = true;
  202. return KEYWORD;
  203. }
  204. }
  205. function includeFilteredContinued(stream, state) {
  206. if (state.isIncludeFiltered) {
  207. var tok = filter(stream, state);
  208. state.isIncludeFiltered = false;
  209. state.restOfLine = 'string';
  210. return tok;
  211. }
  212. }
  213. function mixin(stream, state) {
  214. if (stream.match(/^mixin\b/)) {
  215. state.javaScriptLine = true;
  216. return KEYWORD;
  217. }
  218. }
  219. function call(stream, state) {
  220. if (stream.match(/^\+([-\w]+)/)) {
  221. if (!stream.match(/^\( *[-\w]+ *=/, false)) {
  222. state.javaScriptArguments = true;
  223. state.javaScriptArgumentsDepth = 0;
  224. }
  225. return 'variable';
  226. }
  227. if (stream.match(/^\+#{/, false)) {
  228. stream.next();
  229. state.mixinCallAfter = true;
  230. return interpolation(stream, state);
  231. }
  232. }
  233. function callArguments(stream, state) {
  234. if (state.mixinCallAfter) {
  235. state.mixinCallAfter = false;
  236. if (!stream.match(/^\( *[-\w]+ *=/, false)) {
  237. state.javaScriptArguments = true;
  238. state.javaScriptArgumentsDepth = 0;
  239. }
  240. return true;
  241. }
  242. }
  243. function conditional(stream, state) {
  244. if (stream.match(/^(if|unless|else if|else)\b/)) {
  245. state.javaScriptLine = true;
  246. return KEYWORD;
  247. }
  248. }
  249. function each(stream, state) {
  250. if (stream.match(/^(- *)?(each|for)\b/)) {
  251. state.isEach = true;
  252. return KEYWORD;
  253. }
  254. }
  255. function eachContinued(stream, state) {
  256. if (state.isEach) {
  257. if (stream.match(/^ in\b/)) {
  258. state.javaScriptLine = true;
  259. state.isEach = false;
  260. return KEYWORD;
  261. } else if (stream.sol() || stream.eol()) {
  262. state.isEach = false;
  263. } else if (stream.next()) {
  264. while (!stream.match(/^ in\b/, false) && stream.next());
  265. return 'variable';
  266. }
  267. }
  268. }
  269. function whileStatement(stream, state) {
  270. if (stream.match(/^while\b/)) {
  271. state.javaScriptLine = true;
  272. return KEYWORD;
  273. }
  274. }
  275. function tag(stream, state) {
  276. var captures;
  277. if (captures = stream.match(/^(\w(?:[-:\w]*\w)?)\/?/)) {
  278. state.lastTag = captures[1].toLowerCase();
  279. if (state.lastTag === 'script') {
  280. state.scriptType = 'application/javascript';
  281. }
  282. return 'tag';
  283. }
  284. }
  285. function filter(stream, state) {
  286. if (stream.match(/^:([\w\-]+)/)) {
  287. var innerMode;
  288. if (config && config.innerModes) {
  289. innerMode = config.innerModes(stream.current().substring(1));
  290. }
  291. if (!innerMode) {
  292. innerMode = stream.current().substring(1);
  293. }
  294. if (typeof innerMode === 'string') {
  295. innerMode = CodeMirror.getMode(config, innerMode);
  296. }
  297. setInnerMode(stream, state, innerMode);
  298. return 'atom';
  299. }
  300. }
  301. function code(stream, state) {
  302. if (stream.match(/^(!?=|-)/)) {
  303. state.javaScriptLine = true;
  304. return 'punctuation';
  305. }
  306. }
  307. function id(stream) {
  308. if (stream.match(/^#([\w-]+)/)) {
  309. return ID;
  310. }
  311. }
  312. function className(stream) {
  313. if (stream.match(/^\.([\w-]+)/)) {
  314. return CLASS;
  315. }
  316. }
  317. function attrs(stream, state) {
  318. if (stream.peek() == '(') {
  319. stream.next();
  320. state.isAttrs = true;
  321. state.attrsNest = [];
  322. state.inAttributeName = true;
  323. state.attrValue = '';
  324. state.attributeIsType = false;
  325. return 'punctuation';
  326. }
  327. }
  328. function attrsContinued(stream, state) {
  329. if (state.isAttrs) {
  330. if (ATTRS_NEST[stream.peek()]) {
  331. state.attrsNest.push(ATTRS_NEST[stream.peek()]);
  332. }
  333. if (state.attrsNest[state.attrsNest.length - 1] === stream.peek()) {
  334. state.attrsNest.pop();
  335. } else if (stream.eat(')')) {
  336. state.isAttrs = false;
  337. return 'punctuation';
  338. }
  339. if (state.inAttributeName && stream.match(/^[^=,\)!]+/)) {
  340. if (stream.peek() === '=' || stream.peek() === '!') {
  341. state.inAttributeName = false;
  342. state.jsState = CodeMirror.startState(jsMode);
  343. if (state.lastTag === 'script' && stream.current().trim().toLowerCase() === 'type') {
  344. state.attributeIsType = true;
  345. } else {
  346. state.attributeIsType = false;
  347. }
  348. }
  349. return 'attribute';
  350. }
  351. var tok = jsMode.token(stream, state.jsState);
  352. if (state.attributeIsType && tok === 'string') {
  353. state.scriptType = stream.current().toString();
  354. }
  355. if (state.attrsNest.length === 0 && (tok === 'string' || tok === 'variable' || tok === 'keyword')) {
  356. try {
  357. Function('', 'var x ' + state.attrValue.replace(/,\s*$/, '').replace(/^!/, ''));
  358. state.inAttributeName = true;
  359. state.attrValue = '';
  360. stream.backUp(stream.current().length);
  361. return attrsContinued(stream, state);
  362. } catch (ex) {
  363. //not the end of an attribute
  364. }
  365. }
  366. state.attrValue += stream.current();
  367. return tok || true;
  368. }
  369. }
  370. function attributesBlock(stream, state) {
  371. if (stream.match(/^&attributes\b/)) {
  372. state.javaScriptArguments = true;
  373. state.javaScriptArgumentsDepth = 0;
  374. return 'keyword';
  375. }
  376. }
  377. function indent(stream) {
  378. if (stream.sol() && stream.eatSpace()) {
  379. return 'indent';
  380. }
  381. }
  382. function comment(stream, state) {
  383. if (stream.match(/^ *\/\/(-)?([^\n]*)/)) {
  384. state.indentOf = stream.indentation();
  385. state.indentToken = 'comment';
  386. return 'comment';
  387. }
  388. }
  389. function colon(stream) {
  390. if (stream.match(/^: */)) {
  391. return 'colon';
  392. }
  393. }
  394. function text(stream, state) {
  395. if (stream.match(/^(?:\| ?| )([^\n]+)/)) {
  396. return 'string';
  397. }
  398. if (stream.match(/^(<[^\n]*)/, false)) {
  399. // html string
  400. setInnerMode(stream, state, 'htmlmixed');
  401. state.innerModeForLine = true;
  402. return innerMode(stream, state, true);
  403. }
  404. }
  405. function dot(stream, state) {
  406. if (stream.eat('.')) {
  407. var innerMode = null;
  408. if (state.lastTag === 'script' && state.scriptType.toLowerCase().indexOf('javascript') != -1) {
  409. innerMode = state.scriptType.toLowerCase().replace(/"|'/g, '');
  410. } else if (state.lastTag === 'style') {
  411. innerMode = 'css';
  412. }
  413. setInnerMode(stream, state, innerMode);
  414. return 'dot';
  415. }
  416. }
  417. function fail(stream) {
  418. stream.next();
  419. return null;
  420. }
  421. function setInnerMode(stream, state, mode) {
  422. mode = CodeMirror.mimeModes[mode] || mode;
  423. mode = config.innerModes ? config.innerModes(mode) || mode : mode;
  424. mode = CodeMirror.mimeModes[mode] || mode;
  425. mode = CodeMirror.getMode(config, mode);
  426. state.indentOf = stream.indentation();
  427. if (mode && mode.name !== 'null') {
  428. state.innerMode = mode;
  429. } else {
  430. state.indentToken = 'string';
  431. }
  432. }
  433. function innerMode(stream, state, force) {
  434. if (stream.indentation() > state.indentOf || (state.innerModeForLine && !stream.sol()) || force) {
  435. if (state.innerMode) {
  436. if (!state.innerState) {
  437. state.innerState = state.innerMode.startState ? CodeMirror.startState(state.innerMode, stream.indentation()) : {};
  438. }
  439. return stream.hideFirstChars(state.indentOf + 2, function () {
  440. return state.innerMode.token(stream, state.innerState) || true;
  441. });
  442. } else {
  443. stream.skipToEnd();
  444. return state.indentToken;
  445. }
  446. } else if (stream.sol()) {
  447. state.indentOf = Infinity;
  448. state.indentToken = null;
  449. state.innerMode = null;
  450. state.innerState = null;
  451. }
  452. }
  453. function restOfLine(stream, state) {
  454. if (stream.sol()) {
  455. // if restOfLine was set at end of line, ignore it
  456. state.restOfLine = '';
  457. }
  458. if (state.restOfLine) {
  459. stream.skipToEnd();
  460. var tok = state.restOfLine;
  461. state.restOfLine = '';
  462. return tok;
  463. }
  464. }
  465. function startState() {
  466. return new State();
  467. }
  468. function copyState(state) {
  469. return state.copy();
  470. }
  471. /**
  472. * Get the next token in the stream
  473. *
  474. * @param {Stream} stream
  475. * @param {State} state
  476. */
  477. function nextToken(stream, state) {
  478. var tok = innerMode(stream, state)
  479. || restOfLine(stream, state)
  480. || interpolationContinued(stream, state)
  481. || includeFilteredContinued(stream, state)
  482. || eachContinued(stream, state)
  483. || attrsContinued(stream, state)
  484. || javaScript(stream, state)
  485. || javaScriptArguments(stream, state)
  486. || callArguments(stream, state)
  487. || yieldStatement(stream, state)
  488. || doctype(stream, state)
  489. || interpolation(stream, state)
  490. || caseStatement(stream, state)
  491. || when(stream, state)
  492. || defaultStatement(stream, state)
  493. || extendsStatement(stream, state)
  494. || append(stream, state)
  495. || prepend(stream, state)
  496. || block(stream, state)
  497. || include(stream, state)
  498. || includeFiltered(stream, state)
  499. || mixin(stream, state)
  500. || call(stream, state)
  501. || conditional(stream, state)
  502. || each(stream, state)
  503. || whileStatement(stream, state)
  504. || tag(stream, state)
  505. || filter(stream, state)
  506. || code(stream, state)
  507. || id(stream, state)
  508. || className(stream, state)
  509. || attrs(stream, state)
  510. || attributesBlock(stream, state)
  511. || indent(stream, state)
  512. || text(stream, state)
  513. || comment(stream, state)
  514. || colon(stream, state)
  515. || dot(stream, state)
  516. || fail(stream, state);
  517. return tok === true ? null : tok;
  518. }
  519. return {
  520. startState: startState,
  521. copyState: copyState,
  522. token: nextToken
  523. };
  524. }, 'javascript', 'css', 'htmlmixed');
  525. CodeMirror.defineMIME('text/x-jade', 'jade');
  526. });