improve CodeMirror integration
[racktables] / wwwroot / js / codemirror / rackcode.js
index a71e1b82ed2113ceff8f7218f3e64b7f3ddeea1d..505904972be3819ade10eaf6d56cf2f444f2cdeb 100644 (file)
@@ -7,52 +7,37 @@
     mod(CodeMirror);
 })(function(CodeMirror) {
 "use strict";
-CodeMirror.defineMode('rackcode', function() {
-  var allowkeywords = /^(allow)\b/i;
-  var denykeywords = /^(deny)\b/i;
-  var contextkeywords = /^(context|clear|insert|remove|on)\b/i;
-  var operatorkeywords = /^(define|and|or|not|true|false)\b/i;
-
-  return {
-    token: function(stream, state) {
-
-      if (stream.eatSpace())
-        return null;
-
-      var w;
-
-      if (stream.eatWhile(/\w/)) {
-        w = stream.current();
-
-
-               if (allowkeywords.test(w)) {
-            return 'positive';
-               } else if (denykeywords.test(w)) {
-            return 'negative';
-        } else if (operatorkeywords.test(w)) {
-            return 'operator';
-        } else if (contextkeywords.test(w)) {
-            return 'keyword';
-        }
-
-      } else if (stream.eat('#')) {
-        stream.skipToEnd();
-        return 'comment';
-      } else if (stream.eat('{')) {
-        while (w = stream.next()) {
-          if (w == '}')
-            break;
-
-          if (w == '\\')
-            stream.next();
-        }
-        return 'tag';
-      }  else {
-        stream.next();
-      }
-      return null;
-    }
-  };
+CodeMirror.defineMode('rackcode', function()
+{
+       return {
+               token: function (stream)
+               {
+                       const WORDS =
+                       {
+                               'allow':   'keyword positive',
+                               'deny':    'keyword negative',
+                               'define':  'keyword',
+                               'context': 'keyword',
+                               'clear':   'keyword',
+                               'insert':  'keyword',
+                               'remove':  'keyword',
+                               'on':      'keyword',
+                               'true':    'atom',
+                               'false':   'atom',
+                               'and':     'operator',
+                               'or':      'operator',
+                               'not':     'operator',
+                       };
+                       return stream.eatSpace() ? null :
+                               stream.eat ('(') ? 'bracket' :
+                               stream.eat (')') ? 'bracket' :
+                               stream.match (/^#.*$/) ? 'comment' :
+                               stream.match (/^{[^{}]+}/) ? 'variable' : // a tag
+                               stream.match (/^\[[^\[\]]+\]/) ? 'def' : // a predicate
+                               stream.eatWhile (/\S/) ? WORDS[stream.current()] :
+                               null;
+               }
+       };
 });
 
 CodeMirror.defineMIME("text/x-rackcode", "rackcode");