"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/first-mate/lib/rule.js" (8 Mar 2017, 6977 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 Rule, Scanner, _,
    3     __slice = [].slice;
    4 
    5   _ = require('underscore-plus');
    6 
    7   Scanner = require('./scanner');
    8 
    9   module.exports = Rule = (function() {
   10     function Rule(grammar, registry, _arg) {
   11       var pattern, patterns, _i, _len, _ref, _ref1;
   12       this.grammar = grammar;
   13       this.registry = registry;
   14       _ref = _arg != null ? _arg : {}, this.scopeName = _ref.scopeName, this.contentScopeName = _ref.contentScopeName, patterns = _ref.patterns, this.endPattern = _ref.endPattern, this.applyEndPatternLast = _ref.applyEndPatternLast;
   15       this.patterns = [];
   16       _ref1 = patterns != null ? patterns : [];
   17       for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
   18         pattern = _ref1[_i];
   19         if (!pattern.disabled) {
   20           this.patterns.push(this.grammar.createPattern(pattern));
   21         }
   22       }
   23       if (this.endPattern && !this.endPattern.hasBackReferences) {
   24         if (this.applyEndPatternLast) {
   25           this.patterns.push(this.endPattern);
   26         } else {
   27           this.patterns.unshift(this.endPattern);
   28         }
   29       }
   30       this.scannersByBaseGrammarName = {};
   31       this.createEndPattern = null;
   32       this.anchorPosition = -1;
   33     }
   34 
   35     Rule.prototype.getIncludedPatterns = function(baseGrammar, included) {
   36       var allPatterns, pattern, _i, _len, _ref;
   37       if (included == null) {
   38         included = [];
   39       }
   40       if (_.include(included, this)) {
   41         return [];
   42       }
   43       included = included.concat([this]);
   44       allPatterns = [];
   45       _ref = this.patterns;
   46       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
   47         pattern = _ref[_i];
   48         allPatterns.push.apply(allPatterns, pattern.getIncludedPatterns(baseGrammar, included));
   49       }
   50       return allPatterns;
   51     };
   52 
   53     Rule.prototype.clearAnchorPosition = function() {
   54       return this.anchorPosition = -1;
   55     };
   56 
   57     Rule.prototype.getScanner = function(baseGrammar) {
   58       var patterns, scanner;
   59       if (scanner = this.scannersByBaseGrammarName[baseGrammar.name]) {
   60         return scanner;
   61       }
   62       patterns = this.getIncludedPatterns(baseGrammar);
   63       scanner = new Scanner(patterns);
   64       this.scannersByBaseGrammarName[baseGrammar.name] = scanner;
   65       return scanner;
   66     };
   67 
   68     Rule.prototype.scanInjections = function(ruleStack, line, position, firstLine) {
   69       var baseGrammar, injections, result, scanner, _i, _len, _ref;
   70       baseGrammar = ruleStack[0].rule.grammar;
   71       if (injections = baseGrammar.injections) {
   72         _ref = injections.getScanners(ruleStack);
   73         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
   74           scanner = _ref[_i];
   75           result = scanner.findNextMatch(line, firstLine, position, this.anchorPosition);
   76           if (result != null) {
   77             return result;
   78           }
   79         }
   80       }
   81     };
   82 
   83     Rule.prototype.normalizeCaptureIndices = function(line, captureIndices) {
   84       var capture, lineLength, _i, _len;
   85       lineLength = line.length;
   86       for (_i = 0, _len = captureIndices.length; _i < _len; _i++) {
   87         capture = captureIndices[_i];
   88         capture.end = Math.min(capture.end, lineLength);
   89         capture.start = Math.min(capture.start, lineLength);
   90       }
   91     };
   92 
   93     Rule.prototype.findNextMatch = function(ruleStack, line, position, firstLine) {
   94       var baseGrammar, injection, injectionGrammar, lineWithNewline, result, results, scanner, scopes, _i, _j, _len, _len1, _ref, _ref1;
   95       lineWithNewline = "" + line + "\n";
   96       baseGrammar = ruleStack[0].rule.grammar;
   97       results = [];
   98       scanner = this.getScanner(baseGrammar);
   99       if (result = scanner.findNextMatch(lineWithNewline, firstLine, position, this.anchorPosition)) {
  100         results.push(result);
  101       }
  102       if (result = this.scanInjections(ruleStack, lineWithNewline, position, firstLine)) {
  103         _ref = baseGrammar.injections.injections;
  104         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  105           injection = _ref[_i];
  106           if (injection.scanner === result.scanner) {
  107             if (injection.selector.getPrefix(this.grammar.scopesFromStack(ruleStack)) === 'L') {
  108               results.unshift(result);
  109             } else {
  110               results.push(result);
  111             }
  112           }
  113         }
  114       }
  115       scopes = null;
  116       _ref1 = this.registry.injectionGrammars;
  117       for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  118         injectionGrammar = _ref1[_j];
  119         if (injectionGrammar === this.grammar) {
  120           continue;
  121         }
  122         if (injectionGrammar === baseGrammar) {
  123           continue;
  124         }
  125         if (scopes == null) {
  126           scopes = this.grammar.scopesFromStack(ruleStack);
  127         }
  128         if (injectionGrammar.injectionSelector.matches(scopes)) {
  129           scanner = injectionGrammar.getInitialRule().getScanner(injectionGrammar, position, firstLine);
  130           if (result = scanner.findNextMatch(lineWithNewline, firstLine, position, this.anchorPosition)) {
  131             if (injectionGrammar.injectionSelector.getPrefix(scopes) === 'L') {
  132               results.unshift(result);
  133             } else {
  134               results.push(result);
  135             }
  136           }
  137         }
  138       }
  139       if (results.length > 1) {
  140         return _.min(results, (function(_this) {
  141           return function(result) {
  142             _this.normalizeCaptureIndices(lineWithNewline, result.captureIndices);
  143             return result.captureIndices[0].start;
  144           };
  145         })(this));
  146       } else if (results.length === 1) {
  147         result = results[0];
  148         this.normalizeCaptureIndices(lineWithNewline, result.captureIndices);
  149         return result;
  150       }
  151     };
  152 
  153     Rule.prototype.getNextTags = function(ruleStack, line, position, firstLine) {
  154       var captureIndices, endPatternMatch, firstCapture, index, nextTags, result, scanner;
  155       result = this.findNextMatch(ruleStack, line, position, firstLine);
  156       if (result == null) {
  157         return null;
  158       }
  159       index = result.index, captureIndices = result.captureIndices, scanner = result.scanner;
  160       firstCapture = captureIndices[0];
  161       endPatternMatch = this.endPattern === scanner.patterns[index];
  162       if (nextTags = scanner.handleMatch(result, ruleStack, line, this, endPatternMatch)) {
  163         return {
  164           nextTags: nextTags,
  165           tagsStart: firstCapture.start,
  166           tagsEnd: firstCapture.end
  167         };
  168       }
  169     };
  170 
  171     Rule.prototype.getRuleToPush = function(line, beginPatternCaptureIndices) {
  172       var rule;
  173       if (this.endPattern.hasBackReferences) {
  174         rule = this.grammar.createRule({
  175           scopeName: this.scopeName,
  176           contentScopeName: this.contentScopeName
  177         });
  178         rule.endPattern = this.endPattern.resolveBackReferences(line, beginPatternCaptureIndices);
  179         rule.patterns = [rule.endPattern].concat(__slice.call(this.patterns));
  180         return rule;
  181       } else {
  182         return this;
  183       }
  184     };
  185 
  186     return Rule;
  187 
  188   })();
  189 
  190 }).call(this);