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.

97 lines
2.5 KiB

2 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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. });