1 |
|
2 |
|
3 |
|
4 | (function(mod) {
|
5 | if (typeof exports == "object" && typeof module == "object")
|
6 | mod(require("../../lib/codemirror"));
|
7 | else if (typeof define == "function" && define.amd)
|
8 | define(["../../lib/codemirror"], mod);
|
9 | else
|
10 | mod(CodeMirror);
|
11 | })(function(CodeMirror) {
|
12 | "use strict";
|
13 |
|
14 | CodeMirror.defineMode("puppet", function () {
|
15 |
|
16 | var words = {};
|
17 |
|
18 | var variable_regex = /({)?([a-z][a-z0-9_]*)?((::[a-z][a-z0-9_]*)*::)?[a-zA-Z0-9_]+(})?/;
|
19 |
|
20 |
|
21 |
|
22 | function define(style, string) {
|
23 | var split = string.split(' ');
|
24 | for (var i = 0; i < split.length; i++) {
|
25 | words[split[i]] = style;
|
26 | }
|
27 | }
|
28 |
|
29 |
|
30 | define('keyword', 'class define site node include import inherits');
|
31 | define('keyword', 'case if else in and elsif default or');
|
32 | define('atom', 'false true running present absent file directory undef');
|
33 | define('builtin', '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 |
|
45 |
|
46 |
|
47 | function tokenString(stream, state) {
|
48 | var current, prev, found_var = false;
|
49 | while (!stream.eol() && (current = stream.next()) != state.pending) {
|
50 | if (current === '$' && prev != '\\' && state.pending == '"') {
|
51 | found_var = true;
|
52 | break;
|
53 | }
|
54 | prev = current;
|
55 | }
|
56 | if (found_var) {
|
57 | stream.backUp(1);
|
58 | }
|
59 | if (current == state.pending) {
|
60 | state.continueString = false;
|
61 | } else {
|
62 | state.continueString = true;
|
63 | }
|
64 | return "string";
|
65 | }
|
66 |
|
67 |
|
68 | function tokenize(stream, state) {
|
69 |
|
70 | var word = stream.match(/[\w]+/, false);
|
71 |
|
72 | var attribute = stream.match(/(\s+)?\w+\s+=>.*/, false);
|
73 |
|
74 |
|
75 | var resource = stream.match(/(\s+)?[\w:_]+(\s+)?{/, false);
|
76 |
|
77 | var special_resource = stream.match(/(\s+)?[@]{1,2}[\w:_]+(\s+)?{/, false);
|
78 |
|
79 |
|
80 | var ch = stream.next();
|
81 |
|
82 |
|
83 | if (ch === '$') {
|
84 | if (stream.match(variable_regex)) {
|
85 |
|
86 | return state.continueString ? 'variable-2' : 'variable';
|
87 | }
|
88 |
|
89 | return "error";
|
90 | }
|
91 |
|
92 | if (state.continueString) {
|
93 |
|
94 | stream.backUp(1);
|
95 | return tokenString(stream, state);
|
96 | }
|
97 |
|
98 | if (state.inDefinition) {
|
99 |
|
100 | if (stream.match(/(\s+)?[\w:_]+(\s+)?/)) {
|
101 | return 'def';
|
102 | }
|
103 |
|
104 | stream.match(/\s+{/);
|
105 | state.inDefinition = false;
|
106 | }
|
107 |
|
108 | if (state.inInclude) {
|
109 |
|
110 | stream.match(/(\s+)?\S+(\s+)?/);
|
111 | state.inInclude = false;
|
112 | return 'def';
|
113 | }
|
114 |
|
115 |
|
116 | if (stream.match(/(\s+)?\w+\(/)) {
|
117 | stream.backUp(1);
|
118 | return 'def';
|
119 | }
|
120 |
|
121 | if (attribute) {
|
122 | stream.match(/(\s+)?\w+/);
|
123 | return 'tag';
|
124 | }
|
125 |
|
126 | if (word && words.hasOwnProperty(word)) {
|
127 |
|
128 | stream.backUp(1);
|
129 |
|
130 | stream.match(/[\w]+/);
|
131 |
|
132 |
|
133 | if (stream.match(/\s+\S+\s+{/, false)) {
|
134 | state.inDefinition = true;
|
135 | }
|
136 | if (word == 'include') {
|
137 | state.inInclude = true;
|
138 | }
|
139 |
|
140 | return words[word];
|
141 | }
|
142 |
|
143 | if (/(^|\s+)[A-Z][\w:_]+/.test(word)) {
|
144 |
|
145 | stream.backUp(1);
|
146 |
|
147 | stream.match(/(^|\s+)[A-Z][\w:_]+/);
|
148 | return 'def';
|
149 | }
|
150 |
|
151 | if (resource) {
|
152 | stream.match(/(\s+)?[\w:_]+/);
|
153 | return 'def';
|
154 | }
|
155 |
|
156 | if (special_resource) {
|
157 | stream.match(/(\s+)?[@]{1,2}/);
|
158 | return 'special';
|
159 | }
|
160 |
|
161 | if (ch == "#") {
|
162 | stream.skipToEnd();
|
163 | return "comment";
|
164 | }
|
165 |
|
166 | if (ch == "'" || ch == '"') {
|
167 |
|
168 | state.pending = ch;
|
169 |
|
170 | return tokenString(stream, state);
|
171 | }
|
172 |
|
173 | if (ch == '{' || ch == '}') {
|
174 | return 'bracket';
|
175 | }
|
176 |
|
177 |
|
178 | if (ch == '/') {
|
179 | stream.match(/.*?\//);
|
180 | return 'variable-3';
|
181 | }
|
182 |
|
183 | if (ch.match(/[0-9]/)) {
|
184 | stream.eatWhile(/[0-9]+/);
|
185 | return 'number';
|
186 | }
|
187 |
|
188 | if (ch == '=') {
|
189 | if (stream.peek() == '>') {
|
190 | stream.next();
|
191 | }
|
192 | return "operator";
|
193 | }
|
194 |
|
195 | stream.eatWhile(/[\w-]/);
|
196 |
|
197 | return null;
|
198 | }
|
199 |
|
200 | return {
|
201 | startState: function () {
|
202 | var state = {};
|
203 | state.inDefinition = false;
|
204 | state.inInclude = false;
|
205 | state.continueString = false;
|
206 | state.pending = false;
|
207 | return state;
|
208 | },
|
209 | token: function (stream, state) {
|
210 |
|
211 | if (stream.eatSpace()) return null;
|
212 |
|
213 | return tokenize(stream, state);
|
214 | }
|
215 | };
|
216 | });
|
217 |
|
218 | CodeMirror.defineMIME("text/x-puppet", "puppet");
|
219 |
|
220 | });
|