MCPcopy
hub / github.com/designmodo/Flat-UI / createSimpleLexer

Function createSimpleLexer

docs/assets/js/prettify.js:662–788  ·  view source on GitHub ↗

Given triples of [style, pattern, context] returns a lexing function, * The lexing function interprets the patterns to find token boundaries and * returns a decoration list of the form * [index_0, style_0, index_1, style_1, ..., index_n, style_n] * where index_n is an index into the

(shortcutStylePatterns, fallthroughStylePatterns)

Source from the content-addressed store, hash-verified

660 * function that takes source code and returns a list of decorations.
661 */
662 function createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns) {
663 var shortcuts = {};
664 var tokenizer;
665 (function () {
666 var allPatterns = shortcutStylePatterns.concat(fallthroughStylePatterns);
667 var allRegexs = [];
668 var regexKeys = {};
669 for (var i = 0, n = allPatterns.length; i < n; ++i) {
670 var patternParts = allPatterns[i];
671 var shortcutChars = patternParts[3];
672 if (shortcutChars) {
673 for (var c = shortcutChars.length; --c >= 0;) {
674 shortcuts[shortcutChars.charAt(c)] = patternParts;
675 }
676 }
677 var regex = patternParts[1];
678 var k = '' + regex;
679 if (!regexKeys.hasOwnProperty(k)) {
680 allRegexs.push(regex);
681 regexKeys[k] = null;
682 }
683 }
684 allRegexs.push(/[\0-\uffff]/);
685 tokenizer = combinePrefixPatterns(allRegexs);
686 })();
687
688 var nPatterns = fallthroughStylePatterns.length;
689
690 /**
691 * Lexes job.sourceCode and produces an output array job.decorations of
692 * style classes preceded by the position at which they start in
693 * job.sourceCode in order.
694 *
695 * @param {Object} job an object like <pre>{
696 * sourceCode: {string} sourceText plain text,
697 * basePos: {int} position of job.sourceCode in the larger chunk of
698 * sourceCode.
699 * }</pre>
700 */
701 var decorate = function (job) {
702 var sourceCode = job.sourceCode, basePos = job.basePos;
703 /** Even entries are positions in source in ascending order. Odd enties
704 * are style markers (e.g., PR_COMMENT) that run from that position until
705 * the end.
706 * @type {Array.<number|string>}
707 */
708 var decorations = [basePos, PR_PLAIN];
709 var pos = 0; // index into sourceCode
710 var tokens = sourceCode.match(tokenizer) || [];
711 var styleCache = {};
712
713 for (var ti = 0, nTokens = tokens.length; ti < nTokens; ++ti) {
714 var token = tokens[ti];
715 var style = styleCache[token];
716 var match = void 0;
717
718 var isEmbedded;
719 if (typeof style === 'string') {

Callers 2

sourceDecoratorFunction · 0.85
prettify.jsFile · 0.85

Calls 1

combinePrefixPatternsFunction · 0.85

Tested by

no test coverage detected