cmake.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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")
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd)
  7. define(["../../lib/codemirror"], mod);
  8. else
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("cmake", function () {
  13. var variable_regex = /({)?[a-zA-Z0-9_]+(})?/;
  14. function tokenString(stream, state) {
  15. var current, prev, found_var = false;
  16. while (!stream.eol() && (current = stream.next()) != state.pending) {
  17. if (current === '$' && prev != '\\' && state.pending == '"') {
  18. found_var = true;
  19. break;
  20. }
  21. prev = current;
  22. }
  23. if (found_var) {
  24. stream.backUp(1);
  25. }
  26. if (current == state.pending) {
  27. state.continueString = false;
  28. } else {
  29. state.continueString = true;
  30. }
  31. return "string";
  32. }
  33. function tokenize(stream, state) {
  34. var ch = stream.next();
  35. // Have we found a variable?
  36. if (ch === '$') {
  37. if (stream.match(variable_regex)) {
  38. return 'variable-2';
  39. }
  40. return 'variable';
  41. }
  42. // Should we still be looking for the end of a string?
  43. if (state.continueString) {
  44. // If so, go through the loop again
  45. stream.backUp(1);
  46. return tokenString(stream, state);
  47. }
  48. // Do we just have a function on our hands?
  49. // In 'cmake_minimum_required (VERSION 2.8.8)', 'cmake_minimum_required' is matched
  50. if (stream.match(/(\s+)?\w+\(/) || stream.match(/(\s+)?\w+\ \(/)) {
  51. stream.backUp(1);
  52. return 'def';
  53. }
  54. if (ch == "#") {
  55. stream.skipToEnd();
  56. return "comment";
  57. }
  58. // Have we found a string?
  59. if (ch == "'" || ch == '"') {
  60. // Store the type (single or double)
  61. state.pending = ch;
  62. // Perform the looping function to find the end
  63. return tokenString(stream, state);
  64. }
  65. if (ch == '(' || ch == ')') {
  66. return 'bracket';
  67. }
  68. if (ch.match(/[0-9]/)) {
  69. return 'number';
  70. }
  71. stream.eatWhile(/[\w-]/);
  72. return null;
  73. }
  74. return {
  75. startState: function () {
  76. var state = {};
  77. state.inDefinition = false;
  78. state.inInclude = false;
  79. state.continueString = false;
  80. state.pending = false;
  81. return state;
  82. },
  83. token: function (stream, state) {
  84. if (stream.eatSpace()) return null;
  85. return tokenize(stream, state);
  86. }
  87. };
  88. });
  89. CodeMirror.defineMIME("text/x-cmake", "cmake");
  90. });