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