puppet.js 7.3 KB

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