puppet.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineMode('puppet', function () {
  15. // Stores the words from the define method
  16. var words = {}
  17. // Taken, mostly, from the Puppet official variable standards regex
  18. var variable_regex = /({)?([a-z][a-z0-9_]*)?((::[a-z][a-z0-9_]*)*::)?[a-zA-Z0-9_]+(})?/
  19. // Takes a string of words separated by spaces and adds them as
  20. // keys with the value of the first argument 'style'
  21. function define(style, string) {
  22. var split = string.split(' ')
  23. for (var i = 0; i < split.length; i++) {
  24. words[split[i]] = style
  25. }
  26. }
  27. // Takes commonly known puppet types/words and classifies them to a style
  28. define('keyword', 'class define site node include import inherits')
  29. define('keyword', 'case if else in and elsif default or')
  30. define('atom', 'false true running present absent file directory undef')
  31. define(
  32. 'builtin',
  33. 'action augeas burst chain computer cron destination dport exec ' +
  34. 'file filebucket group host icmp iniface interface jump k5login limit log_level ' +
  35. 'log_prefix macauthorization mailalias maillist mcx mount nagios_command ' +
  36. 'nagios_contact nagios_contactgroup nagios_host nagios_hostdependency ' +
  37. 'nagios_hostescalation nagios_hostextinfo nagios_hostgroup nagios_service ' +
  38. 'nagios_servicedependency nagios_serviceescalation nagios_serviceextinfo ' +
  39. 'nagios_servicegroup nagios_timeperiod name notify outiface package proto reject ' +
  40. 'resources router schedule scheduled_task selboolean selmodule service source ' +
  41. 'sport ssh_authorized_key sshkey stage state table tidy todest toports tosource ' +
  42. 'user vlan yumrepo zfs zone zpool'
  43. )
  44. // After finding a start of a string ('|") this function attempts to find the end;
  45. // If a variable is encountered along the way, we display it differently when it
  46. // is encapsulated in a double-quoted string.
  47. function tokenString(stream, state) {
  48. var current,
  49. prev,
  50. found_var = false
  51. while (!stream.eol() && (current = stream.next()) != state.pending) {
  52. if (current === '$' && prev != '\\' && state.pending == '"') {
  53. found_var = true
  54. break
  55. }
  56. prev = current
  57. }
  58. if (found_var) {
  59. stream.backUp(1)
  60. }
  61. if (current == state.pending) {
  62. state.continueString = false
  63. } else {
  64. state.continueString = true
  65. }
  66. return 'string'
  67. }
  68. // Main function
  69. function tokenize(stream, state) {
  70. // Matches one whole word
  71. var word = stream.match(/[\w]+/, false)
  72. // Matches attributes (i.e. ensure => present ; 'ensure' would be matched)
  73. var attribute = stream.match(/(\s+)?\w+\s+=>.*/, false)
  74. // Matches non-builtin resource declarations
  75. // (i.e. "apache::vhost {" or "mycustomclasss {" would be matched)
  76. var resource = stream.match(/(\s+)?[\w:_]+(\s+)?{/, false)
  77. // Matches virtual and exported resources (i.e. @@user { ; and the like)
  78. var special_resource = stream.match(/(\s+)?[@]{1,2}[\w:_]+(\s+)?{/, false)
  79. // Finally advance the stream
  80. var ch = stream.next()
  81. // Have we found a variable?
  82. if (ch === '$') {
  83. if (stream.match(variable_regex)) {
  84. // If so, and its in a string, assign it a different color
  85. return state.continueString ? 'variable-2' : 'variable'
  86. }
  87. // Otherwise return an invalid variable
  88. return 'error'
  89. }
  90. // Should we still be looking for the end of a string?
  91. if (state.continueString) {
  92. // If so, go through the loop again
  93. stream.backUp(1)
  94. return tokenString(stream, state)
  95. }
  96. // Are we in a definition (class, node, define)?
  97. if (state.inDefinition) {
  98. // If so, return def (i.e. for 'class myclass {' ; 'myclass' would be matched)
  99. if (stream.match(/(\s+)?[\w:_]+(\s+)?/)) {
  100. return 'def'
  101. }
  102. // Match the rest it the next time around
  103. stream.match(/\s+{/)
  104. state.inDefinition = false
  105. }
  106. // Are we in an 'include' statement?
  107. if (state.inInclude) {
  108. // Match and return the included class
  109. stream.match(/(\s+)?\S+(\s+)?/)
  110. state.inInclude = false
  111. return 'def'
  112. }
  113. // Do we just have a function on our hands?
  114. // In 'ensure_resource("myclass")', 'ensure_resource' is matched
  115. if (stream.match(/(\s+)?\w+\(/)) {
  116. stream.backUp(1)
  117. return 'def'
  118. }
  119. // Have we matched the prior attribute regex?
  120. if (attribute) {
  121. stream.match(/(\s+)?\w+/)
  122. return 'tag'
  123. }
  124. // Do we have Puppet specific words?
  125. if (word && words.hasOwnProperty(word)) {
  126. // Negates the initial next()
  127. stream.backUp(1)
  128. // rs move the stream
  129. stream.match(/[\w]+/)
  130. // We want to process these words differently
  131. // do to the importance they have in Puppet
  132. if (stream.match(/\s+\S+\s+{/, false)) {
  133. state.inDefinition = true
  134. }
  135. if (word == 'include') {
  136. state.inInclude = true
  137. }
  138. // Returns their value as state in the prior define methods
  139. return words[word]
  140. }
  141. // Is there a match on a reference?
  142. if (/(^|\s+)[A-Z][\w:_]+/.test(word)) {
  143. // Negate the next()
  144. stream.backUp(1)
  145. // Match the full reference
  146. stream.match(/(^|\s+)[A-Z][\w:_]+/)
  147. return 'def'
  148. }
  149. // Have we matched the prior resource regex?
  150. if (resource) {
  151. stream.match(/(\s+)?[\w:_]+/)
  152. return 'def'
  153. }
  154. // Have we matched the prior special_resource regex?
  155. if (special_resource) {
  156. stream.match(/(\s+)?[@]{1,2}/)
  157. return 'special'
  158. }
  159. // Match all the comments. All of them.
  160. if (ch == '#') {
  161. stream.skipToEnd()
  162. return 'comment'
  163. }
  164. // Have we found a string?
  165. if (ch == "'" || ch == '"') {
  166. // Store the type (single or double)
  167. state.pending = ch
  168. // Perform the looping function to find the end
  169. return tokenString(stream, state)
  170. }
  171. // Match all the brackets
  172. if (ch == '{' || ch == '}') {
  173. return 'bracket'
  174. }
  175. // Match characters that we are going to assume
  176. // are trying to be regex
  177. if (ch == '/') {
  178. stream.match(/^[^\/]*\//)
  179. return 'variable-3'
  180. }
  181. // Match all the numbers
  182. if (ch.match(/[0-9]/)) {
  183. stream.eatWhile(/[0-9]+/)
  184. return 'number'
  185. }
  186. // Match the '=' and '=>' operators
  187. if (ch == '=') {
  188. if (stream.peek() == '>') {
  189. stream.next()
  190. }
  191. return 'operator'
  192. }
  193. // Keep advancing through all the rest
  194. stream.eatWhile(/[\w-]/)
  195. // Return a blank line for everything else
  196. return null
  197. }
  198. // Start it all
  199. return {
  200. startState: function () {
  201. var state = {}
  202. state.inDefinition = false
  203. state.inInclude = false
  204. state.continueString = false
  205. state.pending = false
  206. return state
  207. },
  208. token: function (stream, state) {
  209. // Strip the spaces, but regex will account for them eitherway
  210. if (stream.eatSpace()) return null
  211. // Go through the main process
  212. return tokenize(stream, state)
  213. },
  214. }
  215. })
  216. CodeMirror.defineMIME('text/x-puppet', 'puppet')
  217. })