puppet.js 7.4 KB

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