MCPcopy Index your code
hub / github.com/socketstream/socketstream / createSimpleLexer

Function createSimpleLexer

docs/js/google-code-prettify.js:656–782  ·  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

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

Callers 2

sourceDecoratorFunction · 0.85

Calls 1

combinePrefixPatternsFunction · 0.85

Tested by

no test coverage detected