Created
September 19, 2019 13:08
-
-
Save anderseknert/5cdc9e35086c82803ee863efd4613feb to your computer and use it in GitHub Desktop.
CodeMirror rego mode for syntax highlighting of rego policies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(mod) { | |
if (typeof exports === "object" && typeof module === "object") // CommonJS | |
mod(require("../../lib/codemirror")); | |
else if (typeof define === "function" && define.amd) // AMD | |
define(["../../lib/codemirror"], mod); | |
else // Plain browser env | |
mod(CodeMirror); | |
})(function(CodeMirror) { | |
function wordRegexp(words) { | |
return new RegExp("^((" + words.join(")|(") + "))\\b"); | |
} | |
var keywordsList = ["default", "not", "package", "import", "as", "with", "else", "some"]; | |
var constantsList = ["true", "false", "null"]; | |
var keywords = wordRegexp(keywordsList); | |
var constants = wordRegexp(constantsList); | |
CodeMirror.registerHelper("hintWords", "rego", keywordsList.concat(constantsList)); | |
CodeMirror.defineMode("rego", function(config) { | |
return { | |
token: function(stream, state) { | |
if (stream.eatSpace()) return null; | |
if (stream.match(/^#.*/)) return "comment"; | |
if (stream.match(/[{}\[\]\(\)]/)) return "bracket"; | |
if (stream.match(keywords)) return "keyword"; | |
if (stream.match(constants)) return "atom"; | |
if (stream.match(/[\=|\!\=|>|<|<\=|>\=|\+|-|\*|%|/|\||&|:\=]/)) return "operator"; | |
if (stream.match(/[\.,]/)) return "punctuation"; | |
if (stream.match(/\b[a-zA-Z_]+[a-zA-Z0-9_]*/)) return "variable"; | |
if (stream.match(/(-?(0|[1-9]\d*(\.\d+)?([eE][+-]?\d+)?))/)) return "number"; | |
if (stream.match(/"(\\["/bfnrt]|u[0-9a-fA-F]{4})*?[^"]*"/)) return "string"; | |
var ch = stream.next(); | |
return "error"; | |
}, | |
closeBrackets: "()[]{}''\"\"``", | |
lineComment: "#" | |
}; | |
}); | |
CodeMirror.defineMIME("text/x-rego", "rego"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment