"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/first-mate/lib/pattern.js" (8 Mar 2017, 12575 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 (function() {
    2   var AllCustomCaptureIndicesRegex, AllDigitsRegex, DigitRegex, Pattern, _,
    3     __slice = [].slice;
    4 
    5   _ = require('underscore-plus');
    6 
    7   AllCustomCaptureIndicesRegex = /\$(\d+)|\${(\d+):\/(downcase|upcase)}/g;
    8 
    9   AllDigitsRegex = /\\\d+/g;
   10 
   11   DigitRegex = /\\\d+/;
   12 
   13   module.exports = Pattern = (function() {
   14     function Pattern(grammar, registry, options) {
   15       var applyEndPatternLast, begin, beginCaptures, capture, captures, contentName, end, endCaptures, endPattern, group, match, name, patterns, _ref, _ref1;
   16       this.grammar = grammar;
   17       this.registry = registry;
   18       if (options == null) {
   19         options = {};
   20       }
   21       name = options.name, contentName = options.contentName, match = options.match, begin = options.begin, end = options.end, patterns = options.patterns;
   22       captures = options.captures, beginCaptures = options.beginCaptures, endCaptures = options.endCaptures, applyEndPatternLast = options.applyEndPatternLast;
   23       this.include = options.include, this.popRule = options.popRule, this.hasBackReferences = options.hasBackReferences;
   24       this.pushRule = null;
   25       this.backReferences = null;
   26       this.scopeName = name;
   27       this.contentScopeName = contentName;
   28       if (match) {
   29         if ((end || this.popRule) && (this.hasBackReferences != null ? this.hasBackReferences : this.hasBackReferences = DigitRegex.test(match))) {
   30           this.match = match;
   31         } else {
   32           this.regexSource = match;
   33         }
   34         this.captures = captures;
   35       } else if (begin) {
   36         this.regexSource = begin;
   37         this.captures = beginCaptures != null ? beginCaptures : captures;
   38         endPattern = this.grammar.createPattern({
   39           match: end,
   40           captures: endCaptures != null ? endCaptures : captures,
   41           popRule: true
   42         });
   43         this.pushRule = this.grammar.createRule({
   44           scopeName: this.scopeName,
   45           contentScopeName: this.contentScopeName,
   46           patterns: patterns,
   47           endPattern: endPattern,
   48           applyEndPatternLast: applyEndPatternLast
   49         });
   50       }
   51       if (this.captures != null) {
   52         _ref = this.captures;
   53         for (group in _ref) {
   54           capture = _ref[group];
   55           if (((_ref1 = capture.patterns) != null ? _ref1.length : void 0) > 0 && !capture.rule) {
   56             capture.scopeName = this.scopeName;
   57             capture.rule = this.grammar.createRule(capture);
   58           }
   59         }
   60       }
   61       this.anchored = this.hasAnchor();
   62     }
   63 
   64     Pattern.prototype.getRegex = function(firstLine, position, anchorPosition) {
   65       if (this.anchored) {
   66         return this.replaceAnchor(firstLine, position, anchorPosition);
   67       } else {
   68         return this.regexSource;
   69       }
   70     };
   71 
   72     Pattern.prototype.hasAnchor = function() {
   73       var character, escape, _i, _len, _ref;
   74       if (!this.regexSource) {
   75         return false;
   76       }
   77       escape = false;
   78       _ref = this.regexSource;
   79       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
   80         character = _ref[_i];
   81         if (escape && (character === 'A' || character === 'G' || character === 'z')) {
   82           return true;
   83         }
   84         escape = !escape && character === '\\';
   85       }
   86       return false;
   87     };
   88 
   89     Pattern.prototype.replaceAnchor = function(firstLine, offset, anchor) {
   90       var character, escape, escaped, placeholder, _i, _len, _ref;
   91       escaped = [];
   92       placeholder = '\uFFFF';
   93       escape = false;
   94       _ref = this.regexSource;
   95       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
   96         character = _ref[_i];
   97         if (escape) {
   98           switch (character) {
   99             case 'A':
  100               if (firstLine) {
  101                 escaped.push("\\" + character);
  102               } else {
  103                 escaped.push(placeholder);
  104               }
  105               break;
  106             case 'G':
  107               if (offset === anchor) {
  108                 escaped.push("\\" + character);
  109               } else {
  110                 escaped.push(placeholder);
  111               }
  112               break;
  113             case 'z':
  114               escaped.push('$(?!\n)(?<!\n)');
  115               break;
  116             default:
  117               escaped.push("\\" + character);
  118           }
  119           escape = false;
  120         } else if (character === '\\') {
  121           escape = true;
  122         } else {
  123           escaped.push(character);
  124         }
  125       }
  126       return escaped.join('');
  127     };
  128 
  129     Pattern.prototype.resolveBackReferences = function(line, beginCaptureIndices) {
  130       var beginCaptures, end, resolvedMatch, start, _i, _len, _ref;
  131       beginCaptures = [];
  132       for (_i = 0, _len = beginCaptureIndices.length; _i < _len; _i++) {
  133         _ref = beginCaptureIndices[_i], start = _ref.start, end = _ref.end;
  134         beginCaptures.push(line.slice(start, end));
  135       }
  136       resolvedMatch = this.match.replace(AllDigitsRegex, function(match) {
  137         var index;
  138         index = parseInt(match.slice(1));
  139         if (beginCaptures[index] != null) {
  140           return _.escapeRegExp(beginCaptures[index]);
  141         } else {
  142           return "\\" + index;
  143         }
  144       });
  145       return this.grammar.createPattern({
  146         hasBackReferences: false,
  147         match: resolvedMatch,
  148         captures: this.captures,
  149         popRule: this.popRule
  150       });
  151     };
  152 
  153     Pattern.prototype.ruleForInclude = function(baseGrammar, name) {
  154       var grammarName, hashIndex, ruleName, _ref, _ref1;
  155       hashIndex = name.indexOf('#');
  156       if (hashIndex === 0) {
  157         return this.grammar.getRepository()[name.slice(1)];
  158       } else if (hashIndex >= 1) {
  159         grammarName = name.slice(0, +(hashIndex - 1) + 1 || 9e9);
  160         ruleName = name.slice(hashIndex + 1);
  161         this.grammar.addIncludedGrammarScope(grammarName);
  162         return (_ref = this.registry.grammarForScopeName(grammarName)) != null ? _ref.getRepository()[ruleName] : void 0;
  163       } else if (name === '$self') {
  164         return this.grammar.getInitialRule();
  165       } else if (name === '$base') {
  166         return baseGrammar.getInitialRule();
  167       } else {
  168         this.grammar.addIncludedGrammarScope(name);
  169         return (_ref1 = this.registry.grammarForScopeName(name)) != null ? _ref1.getInitialRule() : void 0;
  170       }
  171     };
  172 
  173     Pattern.prototype.getIncludedPatterns = function(baseGrammar, included) {
  174       var rule, _ref;
  175       if (this.include) {
  176         rule = this.ruleForInclude(baseGrammar, this.include);
  177         return (_ref = rule != null ? rule.getIncludedPatterns(baseGrammar, included) : void 0) != null ? _ref : [];
  178       } else {
  179         return [this];
  180       }
  181     };
  182 
  183     Pattern.prototype.resolveScopeName = function(scopeName, line, captureIndices) {
  184       var resolvedScopeName;
  185       return resolvedScopeName = scopeName.replace(AllCustomCaptureIndicesRegex, function(match, index, commandIndex, command) {
  186         var capture, replacement;
  187         capture = captureIndices[parseInt(index != null ? index : commandIndex)];
  188         if (capture != null) {
  189           replacement = line.substring(capture.start, capture.end);
  190           while (replacement[0] === '.') {
  191             replacement = replacement.substring(1);
  192           }
  193           switch (command) {
  194             case 'downcase':
  195               return replacement.toLowerCase();
  196             case 'upcase':
  197               return replacement.toUpperCase();
  198             default:
  199               return replacement;
  200           }
  201         } else {
  202           return match;
  203         }
  204       });
  205     };
  206 
  207     Pattern.prototype.handleMatch = function(stack, line, captureIndices, rule, endPatternMatch) {
  208       var contentScopeName, end, ruleToPush, scopeName, start, tags, zeroWidthMatch, _ref;
  209       tags = [];
  210       zeroWidthMatch = captureIndices[0].start === captureIndices[0].end;
  211       if (this.popRule) {
  212         if (zeroWidthMatch && _.last(stack).zeroWidthMatch && _.last(stack).rule.anchorPosition === captureIndices[0].end) {
  213           return false;
  214         }
  215         contentScopeName = _.last(stack).contentScopeName;
  216         if (contentScopeName) {
  217           tags.push(this.grammar.endIdForScope(contentScopeName));
  218         }
  219       } else if (this.scopeName) {
  220         scopeName = this.resolveScopeName(this.scopeName, line, captureIndices);
  221         tags.push(this.grammar.startIdForScope(scopeName));
  222       }
  223       if (this.captures) {
  224         tags.push.apply(tags, this.tagsForCaptureIndices(line, _.clone(captureIndices), captureIndices, stack));
  225       } else {
  226         _ref = captureIndices[0], start = _ref.start, end = _ref.end;
  227         if (end !== start) {
  228           tags.push(end - start);
  229         }
  230       }
  231       if (this.pushRule) {
  232         ruleToPush = this.pushRule.getRuleToPush(line, captureIndices);
  233         ruleToPush.anchorPosition = captureIndices[0].end;
  234         contentScopeName = ruleToPush.contentScopeName;
  235         stack.push({
  236           rule: ruleToPush,
  237           scopeName: scopeName,
  238           contentScopeName: contentScopeName,
  239           zeroWidthMatch: zeroWidthMatch
  240         });
  241         if (contentScopeName) {
  242           tags.push(this.grammar.startIdForScope(contentScopeName));
  243         }
  244       } else {
  245         if (this.popRule) {
  246           scopeName = stack.pop().scopeName;
  247         }
  248         if (scopeName) {
  249           tags.push(this.grammar.endIdForScope(scopeName));
  250         }
  251       }
  252       return tags;
  253     };
  254 
  255     Pattern.prototype.tagsForCaptureRule = function(rule, line, captureStart, captureEnd, stack) {
  256       var captureTags, captureText, offset, openScopes, tag, tags, _i, _len;
  257       captureText = line.substring(captureStart, captureEnd);
  258       tags = rule.grammar.tokenizeLine(captureText, __slice.call(stack).concat([{
  259           rule: rule
  260         }])).tags;
  261       openScopes = [];
  262       captureTags = [];
  263       offset = 0;
  264       for (_i = 0, _len = tags.length; _i < _len; _i++) {
  265         tag = tags[_i];
  266         if (!(tag < 0 || (tag > 0 && offset < captureEnd))) {
  267           continue;
  268         }
  269         captureTags.push(tag);
  270         if (tag >= 0) {
  271           offset += tag;
  272         } else {
  273           if (tag % 2 === 0) {
  274             openScopes.pop();
  275           } else {
  276             openScopes.push(tag);
  277           }
  278         }
  279       }
  280       while (openScopes.length > 0) {
  281         captureTags.push(openScopes.pop() - 1);
  282       }
  283       return captureTags;
  284     };
  285 
  286     Pattern.prototype.tagsForCaptureIndices = function(line, currentCaptureIndices, allCaptureIndices, stack) {
  287       var captureHasNoScope, captureRule, captureTags, childCapture, emptyCapture, parentCapture, parentCaptureScope, previousChildCaptureEnd, scope, tags, _ref, _ref1;
  288       parentCapture = currentCaptureIndices.shift();
  289       tags = [];
  290       if (scope = (_ref = this.captures[parentCapture.index]) != null ? _ref.name : void 0) {
  291         parentCaptureScope = this.resolveScopeName(scope, line, allCaptureIndices);
  292         tags.push(this.grammar.startIdForScope(parentCaptureScope));
  293       }
  294       if (captureRule = (_ref1 = this.captures[parentCapture.index]) != null ? _ref1.rule : void 0) {
  295         captureTags = this.tagsForCaptureRule(captureRule, line, parentCapture.start, parentCapture.end, stack);
  296         tags.push.apply(tags, captureTags);
  297         while (currentCaptureIndices.length && currentCaptureIndices[0].start < parentCapture.end) {
  298           currentCaptureIndices.shift();
  299         }
  300       } else {
  301         previousChildCaptureEnd = parentCapture.start;
  302         while (currentCaptureIndices.length && currentCaptureIndices[0].start < parentCapture.end) {
  303           childCapture = currentCaptureIndices[0];
  304           emptyCapture = childCapture.end - childCapture.start === 0;
  305           captureHasNoScope = !this.captures[childCapture.index];
  306           if (emptyCapture || captureHasNoScope) {
  307             currentCaptureIndices.shift();
  308             continue;
  309           }
  310           if (childCapture.start > previousChildCaptureEnd) {
  311             tags.push(childCapture.start - previousChildCaptureEnd);
  312           }
  313           captureTags = this.tagsForCaptureIndices(line, currentCaptureIndices, allCaptureIndices, stack);
  314           tags.push.apply(tags, captureTags);
  315           previousChildCaptureEnd = childCapture.end;
  316         }
  317         if (parentCapture.end > previousChildCaptureEnd) {
  318           tags.push(parentCapture.end - previousChildCaptureEnd);
  319         }
  320       }
  321       if (parentCaptureScope) {
  322         if (tags.length > 1) {
  323           tags.push(this.grammar.endIdForScope(parentCaptureScope));
  324         } else {
  325           tags.pop();
  326         }
  327       }
  328       return tags;
  329     };
  330 
  331     return Pattern;
  332 
  333   })();
  334 
  335 }).call(this);