UNPKG

ometa-highlighter

Version:

Code highlighter based on Ometa/JS (little inspired by Pygments)

832 lines 107 kB
var ometajs = require('ometajs'), OMeta = ometajs.OMeta, Parser = ometajs.Parser; var ometajs_ = require('ometajs').globals || global;var StringBuffer = ometajs_.StringBuffer; var objectThatDelegatesTo = ometajs_.objectThatDelegatesTo; var isImmutable = ometajs_.isImmutable; var digitValue = ometajs_.digitValue; var isSequenceable = ometajs_.isSequenceable; var escapeChar = ometajs_.escapeChar; var unescape = ometajs_.unescape; var getTag = ometajs_.getTag; var inspect = ometajs_.inspect; var lift = ometajs_.lift; var clone = ometajs_.clone; var Parser = ometajs_.Parser; var fail = ometajs_.fail; var OMeta = ometajs_.OMeta; var BSNullOptimization = ometajs_.BSNullOptimization; var BSAssociativeOptimization = ometajs_.BSAssociativeOptimization; var BSSeqInliner = ometajs_.BSSeqInliner; var BSJumpTableOptimization = ometajs_.BSJumpTableOptimization; var BSOMetaOptimizer = ometajs_.BSOMetaOptimizer; var BSOMetaParser = ometajs_.BSOMetaParser; var BSOMetaTranslator = ometajs_.BSOMetaTranslator; var BSJSParser = ometajs_.BSJSParser; var BSSemActionParser = ometajs_.BSSemActionParser; var BSJSIdentity = ometajs_.BSJSIdentity; var BSJSTranslator = ometajs_.BSJSTranslator; var BSOMetaJSParser = ometajs_.BSOMetaJSParser; var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator; if (global === ometajs_) { fail = (function(fail) { return function() { return fail }; })(fail); OMeta = require('ometajs').OMeta; }{ var OmetaHighlighterJs = exports.OmetaHighlighterJs = objectThatDelegatesTo(OMeta, { fromTo: function() { var $elf = this, _fromIdx = this.input.idx, x, y, s, c, e; return function() { x = this._apply("anything"); y = this._apply("anything"); s = this._applyWithArgs("seq", x); c = this._many(function() { return function() { this._not(function() { return this._applyWithArgs("seq", y); }); return this._apply("char"); }.call(this); }); e = this._applyWithArgs("seq", y); return [ s, c.join(""), e ]; }.call(this); }, nameFirst: function() { var $elf = this, _fromIdx = this.input.idx; return this._or(function() { return this._apply("letter"); }, function() { return function() { switch (this._apply("anything")) { case "$": return "$"; case "_": return "_"; default: throw fail(); } }.call(this); }); }, nameRest: function() { var $elf = this, _fromIdx = this.input.idx; return this._or(function() { return this._apply("nameFirst"); }, function() { return this._apply("digit"); }); }, name: function() { var $elf = this, _fromIdx = this.input.idx, r; return function() { r = this._applyWithArgs("firstAndRest", "nameFirst", "nameRest"); return [ "name", r.join("") ]; }.call(this); }, builtin: function() { var $elf = this, _fromIdx = this.input.idx, c; return function() { c = function() { switch (this._apply("anything")) { case "S": return function() { this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "g"); return "String"; }.call(this); case "N": return function() { this._applyWithArgs("exactly", "u"); this._applyWithArgs("exactly", "m"); this._applyWithArgs("exactly", "b"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "r"); return "Number"; }.call(this); case "d": return function() { switch (this._apply("anything")) { case "o": return function() { this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "u"); this._applyWithArgs("exactly", "m"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "t"); return "document"; }.call(this); case "e": return function() { switch (this._apply("anything")) { case "c": return function() { switch (this._apply("anything")) { case "o": return function() { switch (this._apply("anything")) { case "d": return function() { switch (this._apply("anything")) { case "e": return function() { switch (this._apply("anything")) { case "U": return function() { switch (this._apply("anything")) { case "R": return function() { switch (this._apply("anything")) { case "I": return this._or(function() { return "decodeURI"; }, function() { return function() { switch (this._apply("anything")) { case "C": return function() { this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "m"); this._applyWithArgs("exactly", "p"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "t"); return "decodeURIComponent"; }.call(this); default: throw fail(); } }.call(this); }); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); case "E": return function() { switch (this._apply("anything")) { case "r": return function() { switch (this._apply("anything")) { case "r": return function() { switch (this._apply("anything")) { case "o": return function() { switch (this._apply("anything")) { case "r": return this._or(function() { return "Error"; }, function() { return "Error"; }); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); case "t": return function() { this._applyWithArgs("exactly", "h"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "s"); return "this"; }.call(this); case "e": return function() { switch (this._apply("anything")) { case "n": return function() { switch (this._apply("anything")) { case "c": return function() { switch (this._apply("anything")) { case "o": return function() { switch (this._apply("anything")) { case "d": return function() { switch (this._apply("anything")) { case "e": return function() { switch (this._apply("anything")) { case "U": return function() { switch (this._apply("anything")) { case "R": return function() { switch (this._apply("anything")) { case "I": return this._or(function() { return "encodeURI"; }, function() { return function() { switch (this._apply("anything")) { case "C": return function() { this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "m"); this._applyWithArgs("exactly", "p"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "t"); return "encodeURIComponent"; }.call(this); default: throw fail(); } }.call(this); }); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); case "v": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "l"); return "eval"; }.call(this); default: throw fail(); } }.call(this); case "s": return function() { this._applyWithArgs("exactly", "u"); this._applyWithArgs("exactly", "n"); return "sun"; }.call(this); case "B": return function() { this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "l"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "n"); return "Boolean"; }.call(this); case "D": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "e"); return "Date"; }.call(this); case "F": return function() { this._applyWithArgs("exactly", "u"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "n"); return "Function"; }.call(this); case "w": return function() { this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "d"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "w"); return "window"; }.call(this); case "M": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "h"); return "Math"; }.call(this); case "i": return function() { switch (this._apply("anything")) { case "s": return function() { switch (this._apply("anything")) { case "N": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "N"); return "isNaN"; }.call(this); case "F": return function() { this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "e"); return "isFinite"; }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); case "A": return function() { this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "y"); return "Array"; }.call(this); case "O": return function() { this._applyWithArgs("exactly", "b"); this._applyWithArgs("exactly", "j"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "t"); return "Object"; }.call(this); case "n": return function() { this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "s"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "p"); this._applyWithArgs("exactly", "e"); return "netscape"; }.call(this); case "P": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "k"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "g"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "s"); return "Packages"; }.call(this); case "p": return function() { switch (this._apply("anything")) { case "a": return function() { switch (this._apply("anything")) { case "r": return function() { switch (this._apply("anything")) { case "s": return function() { switch (this._apply("anything")) { case "e": return function() { switch (this._apply("anything")) { case "I": return function() { this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "t"); return "parseInt"; }.call(this); case "F": return function() { this._applyWithArgs("exactly", "l"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "t"); return "parseFloat"; }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); case "R": return function() { this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "g"); this._applyWithArgs("exactly", "E"); this._applyWithArgs("exactly", "x"); this._applyWithArgs("exactly", "p"); return "RegExp"; }.call(this); default: throw fail(); } }.call(this); return [ "name.builtin", c ]; }.call(this); }, declaration: function() { var $elf = this, _fromIdx = this.input.idx, c; return function() { c = function() { switch (this._apply("anything")) { case "f": return function() { this._applyWithArgs("exactly", "u"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "n"); return "function"; }.call(this); case "w": return function() { this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "h"); return "with"; }.call(this); case "v": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "r"); return "var"; }.call(this); default: throw fail(); } }.call(this); return [ "keyword.declaration", c ]; }.call(this); }, reserved: function() { var $elf = this, _fromIdx = this.input.idx, c; return function() { c = function() { switch (this._apply("anything")) { case "p": return function() { switch (this._apply("anything")) { case "a": return function() { this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "k"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "g"); this._applyWithArgs("exactly", "e"); return "package"; }.call(this); case "r": return function() { switch (this._apply("anything")) { case "o": return function() { this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "d"); return "protected"; }.call(this); case "i": return function() { this._applyWithArgs("exactly", "v"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "e"); return "private"; }.call(this); default: throw fail(); } }.call(this); case "u": return function() { this._applyWithArgs("exactly", "b"); this._applyWithArgs("exactly", "l"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "c"); return "public"; }.call(this); default: throw fail(); } }.call(this); case "b": return function() { switch (this._apply("anything")) { case "y": return function() { this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "e"); return "byte"; }.call(this); case "o": return function() { this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "l"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "n"); return "boolean"; }.call(this); default: throw fail(); } }.call(this); case "f": return function() { switch (this._apply("anything")) { case "i": return function() { this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "l"); return "final"; }.call(this); case "l": return function() { this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "t"); return "float"; }.call(this); default: throw fail(); } }.call(this); case "t": return function() { switch (this._apply("anything")) { case "r": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "s"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "t"); return "transient"; }.call(this); case "h": return function() { this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "w"); this._applyWithArgs("exactly", "s"); return "throws"; }.call(this); default: throw fail(); } }.call(this); case "a": return function() { this._applyWithArgs("exactly", "b"); this._applyWithArgs("exactly", "s"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "t"); return "abstract"; }.call(this); case "e": return function() { switch (this._apply("anything")) { case "x": return function() { switch (this._apply("anything")) { case "p": return function() { this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "t"); return "export"; }.call(this); case "t": return function() { this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "d"); this._applyWithArgs("exactly", "s"); return "extends"; }.call(this); default: throw fail(); } }.call(this); case "n": return function() { this._applyWithArgs("exactly", "u"); this._applyWithArgs("exactly", "m"); return "enum"; }.call(this); default: throw fail(); } }.call(this); case "s": return function() { switch (this._apply("anything")) { case "y": return function() { this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "h"); this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "z"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "d"); return "synchronized"; }.call(this); case "t": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "c"); return "static"; }.call(this); case "h": return function() { this._applyWithArgs("exactly", "o"); this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "t"); return "short"; }.call(this); case "u": return function() { this._applyWithArgs("exactly", "p"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "r"); return "super"; }.call(this); default: throw fail(); } }.call(this); case "n": return function() { this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "i"); this._applyWithArgs("exactly", "v"); this._applyWithArgs("exactly", "e"); return "native"; }.call(this); case "i": return function() { switch (this._apply("anything")) { case "n": return function() { switch (this._apply("anything")) { case "t": return this._or(function() { return "int"; }, function() { return function() { switch (this._apply("anything")) { case "e": return function() { this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "f"); this._applyWithArgs("exactly", "a"); this._applyWithArgs("exactly", "c"); this._applyWithArgs("exactly", "e"); return "interface"; }.call(this); default: throw fail(); } }.call(this); }); default: throw fail(); } }.call(this); case "m": return function() { switch (this._apply("anything")) { case "p": return function() { switch (this._apply("anything")) { case "o": return function() { this._applyWithArgs("exactly", "r"); this._applyWithArgs("exactly", "t"); return "import"; }.call(this); case "l": return function() { this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "m"); this._applyWithArgs("exactly", "e"); this._applyWithArgs("exactly", "n"); this._applyWithArgs("exactly", "t"); this._applyWithArgs("exactly", "s"); return "implements"; }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); default: throw fail(); } }.call(this); case "d": return function() { switch (this._apply("anything")) {