cmake.js 3.2 KB

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