LaravelTest
220 строк · 7.4 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14CodeMirror.defineMode("puppet", function () {15// Stores the words from the define method16var words = {};17// Taken, mostly, from the Puppet official variable standards regex18var variable_regex = /({)?([a-z][a-z0-9_]*)?((::[a-z][a-z0-9_]*)*::)?[a-zA-Z0-9_]+(})?/;19
20// Takes a string of words separated by spaces and adds them as21// keys with the value of the first argument 'style'22function define(style, string) {23var split = string.split(' ');24for (var i = 0; i < split.length; i++) {25words[split[i]] = style;26}27}28
29// Takes commonly known puppet types/words and classifies them to a style30define('keyword', 'class define site node include import inherits');31define('keyword', 'case if else in and elsif default or');32define('atom', 'false true running present absent file directory undef');33define('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// 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 it46// is encapsulated in a double-quoted string.47function tokenString(stream, state) {48var current, prev, found_var = false;49while (!stream.eol() && (current = stream.next()) != state.pending) {50if (current === '$' && prev != '\\' && state.pending == '"') {51found_var = true;52break;53}54prev = current;55}56if (found_var) {57stream.backUp(1);58}59if (current == state.pending) {60state.continueString = false;61} else {62state.continueString = true;63}64return "string";65}66
67// Main function68function tokenize(stream, state) {69// Matches one whole word70var word = stream.match(/[\w]+/, false);71// Matches attributes (i.e. ensure => present ; 'ensure' would be matched)72var attribute = stream.match(/(\s+)?\w+\s+=>.*/, false);73// Matches non-builtin resource declarations74// (i.e. "apache::vhost {" or "mycustomclasss {" would be matched)75var resource = stream.match(/(\s+)?[\w:_]+(\s+)?{/, false);76// Matches virtual and exported resources (i.e. @@user { ; and the like)77var special_resource = stream.match(/(\s+)?[@]{1,2}[\w:_]+(\s+)?{/, false);78
79// Finally advance the stream80var ch = stream.next();81
82// Have we found a variable?83if (ch === '$') {84if (stream.match(variable_regex)) {85// If so, and its in a string, assign it a different color86return state.continueString ? 'variable-2' : 'variable';87}88// Otherwise return an invalid variable89return "error";90}91// Should we still be looking for the end of a string?92if (state.continueString) {93// If so, go through the loop again94stream.backUp(1);95return tokenString(stream, state);96}97// Are we in a definition (class, node, define)?98if (state.inDefinition) {99// If so, return def (i.e. for 'class myclass {' ; 'myclass' would be matched)100if (stream.match(/(\s+)?[\w:_]+(\s+)?/)) {101return 'def';102}103// Match the rest it the next time around104stream.match(/\s+{/);105state.inDefinition = false;106}107// Are we in an 'include' statement?108if (state.inInclude) {109// Match and return the included class110stream.match(/(\s+)?\S+(\s+)?/);111state.inInclude = false;112return 'def';113}114// Do we just have a function on our hands?115// In 'ensure_resource("myclass")', 'ensure_resource' is matched116if (stream.match(/(\s+)?\w+\(/)) {117stream.backUp(1);118return 'def';119}120// Have we matched the prior attribute regex?121if (attribute) {122stream.match(/(\s+)?\w+/);123return 'tag';124}125// Do we have Puppet specific words?126if (word && words.hasOwnProperty(word)) {127// Negates the initial next()128stream.backUp(1);129// rs move the stream130stream.match(/[\w]+/);131// We want to process these words differently132// do to the importance they have in Puppet133if (stream.match(/\s+\S+\s+{/, false)) {134state.inDefinition = true;135}136if (word == 'include') {137state.inInclude = true;138}139// Returns their value as state in the prior define methods140return words[word];141}142// Is there a match on a reference?143if (/(^|\s+)[A-Z][\w:_]+/.test(word)) {144// Negate the next()145stream.backUp(1);146// Match the full reference147stream.match(/(^|\s+)[A-Z][\w:_]+/);148return 'def';149}150// Have we matched the prior resource regex?151if (resource) {152stream.match(/(\s+)?[\w:_]+/);153return 'def';154}155// Have we matched the prior special_resource regex?156if (special_resource) {157stream.match(/(\s+)?[@]{1,2}/);158return 'special';159}160// Match all the comments. All of them.161if (ch == "#") {162stream.skipToEnd();163return "comment";164}165// Have we found a string?166if (ch == "'" || ch == '"') {167// Store the type (single or double)168state.pending = ch;169// Perform the looping function to find the end170return tokenString(stream, state);171}172// Match all the brackets173if (ch == '{' || ch == '}') {174return 'bracket';175}176// Match characters that we are going to assume177// are trying to be regex178if (ch == '/') {179stream.match(/^[^\/]*\//);180return 'variable-3';181}182// Match all the numbers183if (ch.match(/[0-9]/)) {184stream.eatWhile(/[0-9]+/);185return 'number';186}187// Match the '=' and '=>' operators188if (ch == '=') {189if (stream.peek() == '>') {190stream.next();191}192return "operator";193}194// Keep advancing through all the rest195stream.eatWhile(/[\w-]/);196// Return a blank line for everything else197return null;198}199// Start it all200return {201startState: function () {202var state = {};203state.inDefinition = false;204state.inInclude = false;205state.continueString = false;206state.pending = false;207return state;208},209token: function (stream, state) {210// Strip the spaces, but regex will account for them eitherway211if (stream.eatSpace()) return null;212// Go through the main process213return tokenize(stream, state);214}215};216});217
218CodeMirror.defineMIME("text/x-puppet", "puppet");219
220});221