Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CodeMirror pseudocode highlighter.
(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) {
"use strict";
CodeMirror.defineMode("pseudocode", function () {
var define1 = ["interface", "class", "abstract class"];
var define2 = ["implements", "extends"];
var define3 = ["abstract", "static", "private", "public", "protected", "method", "function", "constructor", "field"];
var keywords = ["override", "overload", "new",
"is", "return", "for", "foreach", "for each", "while", "if", "then", "else", "and", "or"];
var operator = /^[+\-*&%=<>!?|~^]/;
var bracket = /^[:;\(\)\[\]\{\}]/;
var atom = ["true", "false", "null", "this", "parent::", "static::"];
var number = /^(\d[+\-\*\/])?\d+(\.\d+)?/;
var word = /^\w+(?:'\w+)?/;
define1 = new RegExp("((" + define1.join(")|(") + "))\\b");
define2 = new RegExp("((" + define2.join(")|(") + "))\\b");
define3 = new RegExp("((" + define3.join(")|(") + "))\\b");
keywords = new RegExp("((" + keywords.join(")|(") + "))\\b", "i");
atom = new RegExp("((" + atom.join(")|(") + "))\\b");
function tokenBase(stream, state) {
function internalTokenBase(stream, state) {
var ch = stream.peek();
if (stream.eol()) {
state.define = false;
}
if (state.incomment) {
if (!stream.skipTo("*/")) {
stream.skipToEnd();
} else {
stream.eatWhile(/\*|\//);
state.incomment = false;
}
return "doc-multiline";
}
else if (state.instring) {
if (ch == state.instring) {
state.instring = false;
}
stream.next();
return "string";
}
else if (ch == "'" || ch == '"') {
state.instring = ch;
stream.next();
return "string";
}
else if (stream.eat("/")) {
if (stream.eat("*")) {
state.incomment = true;
if (!stream.skipTo("*/")) {
stream.skipToEnd();
} else {
stream.eatWhile(/\*|\//);
state.incomment = false;
}
return "doc-multiline";
}
else if (stream.eat("/")) {
if (stream.match(/\s*\.\.\.+\s*$/)) {
stream.skipToEnd();
return "comment";
}
stream.skipToEnd();
return "doc";
}
} else {
if (stream.eat(" ")) {
stream.eatSpace();
return;
}
else {
if (state.sol && stream.match(define1)) {
state.define = true;
state.def_mode = 1;
return "keyword";
}
if (stream.match(define2)) {
state.define = true;
state.def_mode = 2;
return "keyword";
}
if ((state.sol || state.def_mode == 3) && stream.match(define3)) {
state.define = true;
state.def_mode = 3;
return "keyword";
}
if (stream.match(keywords)) {
state.define = false;
state.def_mode = null;
return "keyword";
}
if (stream.match(atom)) {
return "atom";
}
if (stream.match(operator)) {
return "operator";
}
if (stream.match(bracket)) {
state.define = false;
state.def_mode = null;
return "bracket";
}
if (stream.match(number)) {
return "number";
}
if (stream.match(word)) {
if (state.define) {
var token = "def" + state.def_mode;
return token;
}
return "variable";
}
}
}
stream.next();
}
if (stream.sol()) {
state.sol = true;
}
var result = internalTokenBase(stream, state);
if (result) {
state.sol = false;
}
return result;
}
return {
startState: function () {
return {};
},
token: function (stream, state) {
return tokenBase(stream, state);
}
};
});
CodeMirror.defineMIME("text/x-pseudocode", "pseudocode");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment