"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/first-mate/lib/grammar.js" (7 Feb 2017, 12315 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 Emitter, EmitterMixin, Grammar, Grim, Injections, OnigRegExp, Pattern, Rule, ScopeSelector, TokenizeLineResult, fs, path, _;
    3 
    4   path = require('path');
    5 
    6   _ = require('underscore-plus');
    7 
    8   fs = require('fs-plus');
    9 
   10   OnigRegExp = require('oniguruma').OnigRegExp;
   11 
   12   Emitter = require('event-kit').Emitter;
   13 
   14   Grim = require('grim');
   15 
   16   Injections = require('./injections');
   17 
   18   Pattern = require('./pattern');
   19 
   20   Rule = require('./rule');
   21 
   22   ScopeSelector = require('./scope-selector');
   23 
   24   module.exports = Grammar = (function() {
   25     Grammar.prototype.registration = null;
   26 
   27     function Grammar(registry, options) {
   28       var firstLineMatch, injectionSelector, injections, patterns, repository;
   29       this.registry = registry;
   30       if (options == null) {
   31         options = {};
   32       }
   33       this.name = options.name, this.fileTypes = options.fileTypes, this.scopeName = options.scopeName, this.foldingStopMarker = options.foldingStopMarker, this.maxTokensPerLine = options.maxTokensPerLine;
   34       injections = options.injections, injectionSelector = options.injectionSelector, patterns = options.patterns, repository = options.repository, firstLineMatch = options.firstLineMatch;
   35       this.emitter = new Emitter;
   36       this.repository = null;
   37       this.initialRule = null;
   38       this.rawPatterns = patterns;
   39       this.rawRepository = repository;
   40       if (injectionSelector != null) {
   41         this.injectionSelector = new ScopeSelector(injectionSelector);
   42       } else {
   43         this.injectionSelector = null;
   44       }
   45       if (firstLineMatch) {
   46         this.firstLineRegex = new OnigRegExp(firstLineMatch);
   47       } else {
   48         this.firstLineRegex = null;
   49       }
   50       if (this.fileTypes == null) {
   51         this.fileTypes = [];
   52       }
   53       this.includedGrammarScopes = [];
   54       this.injections = new Injections(this, injections);
   55     }
   56 
   57 
   58     /*
   59     Section: Event Subscription
   60      */
   61 
   62     Grammar.prototype.onDidUpdate = function(callback) {
   63       return this.emitter.on('did-update', callback);
   64     };
   65 
   66 
   67     /*
   68     Section: Tokenizing
   69      */
   70 
   71     Grammar.prototype.tokenizeLines = function(text) {
   72       var line, lineNumber, lines, ruleStack, scopes, tags, _i, _len, _ref, _results;
   73       lines = text.split('\n');
   74       ruleStack = null;
   75       scopes = [];
   76       _results = [];
   77       for (lineNumber = _i = 0, _len = lines.length; _i < _len; lineNumber = ++_i) {
   78         line = lines[lineNumber];
   79         _ref = this.tokenizeLine(line, ruleStack, lineNumber === 0), tags = _ref.tags, ruleStack = _ref.ruleStack;
   80         _results.push(this.registry.decodeTokens(line, tags, scopes));
   81       }
   82       return _results;
   83     };
   84 
   85     Grammar.prototype.tokenizeLine = function(line, ruleStack, firstLine, compatibilityMode) {
   86       var contentScopeName, initialRule, initialRuleStackLength, lastRule, lastSymbol, match, nextTags, openScopeTags, penultimateRule, popStack, position, previousPosition, previousRuleStackLength, rule, scopeName, tag, tags, tagsEnd, tagsStart, tokenCount, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
   87       if (firstLine == null) {
   88         firstLine = false;
   89       }
   90       if (compatibilityMode == null) {
   91         compatibilityMode = true;
   92       }
   93       tags = [];
   94       if (ruleStack != null) {
   95         ruleStack = ruleStack.slice();
   96         if (compatibilityMode) {
   97           openScopeTags = [];
   98           for (_i = 0, _len = ruleStack.length; _i < _len; _i++) {
   99             _ref = ruleStack[_i], scopeName = _ref.scopeName, contentScopeName = _ref.contentScopeName;
  100             if (scopeName) {
  101               openScopeTags.push(this.registry.startIdForScope(scopeName));
  102             }
  103             if (contentScopeName) {
  104               openScopeTags.push(this.registry.startIdForScope(contentScopeName));
  105             }
  106           }
  107         }
  108       } else {
  109         if (compatibilityMode) {
  110           openScopeTags = [];
  111         }
  112         initialRule = this.getInitialRule();
  113         scopeName = initialRule.scopeName, contentScopeName = initialRule.contentScopeName;
  114         ruleStack = [
  115           {
  116             rule: initialRule,
  117             scopeName: scopeName,
  118             contentScopeName: contentScopeName
  119           }
  120         ];
  121         if (scopeName) {
  122           tags.push(this.startIdForScope(initialRule.scopeName));
  123         }
  124         if (contentScopeName) {
  125           tags.push(this.startIdForScope(initialRule.contentScopeName));
  126         }
  127       }
  128       initialRuleStackLength = ruleStack.length;
  129       position = 0;
  130       tokenCount = 0;
  131       while (true) {
  132         previousRuleStackLength = ruleStack.length;
  133         previousPosition = position;
  134         if (position === line.length + 1) {
  135           break;
  136         }
  137         if (tokenCount >= this.getMaxTokensPerLine() - 1) {
  138           tags.push(line.length - position);
  139           while (ruleStack.length > initialRuleStackLength) {
  140             _ref1 = ruleStack.pop(), scopeName = _ref1.scopeName, contentScopeName = _ref1.contentScopeName;
  141             if (contentScopeName) {
  142               tags.push(this.endIdForScope(contentScopeName));
  143             }
  144             if (scopeName) {
  145               tags.push(this.endIdForScope(scopeName));
  146             }
  147           }
  148           break;
  149         }
  150         if (match = _.last(ruleStack).rule.getNextTags(ruleStack, line, position, firstLine)) {
  151           nextTags = match.nextTags, tagsStart = match.tagsStart, tagsEnd = match.tagsEnd;
  152           if (position < tagsStart) {
  153             tags.push(tagsStart - position);
  154             tokenCount++;
  155           }
  156           tags.push.apply(tags, nextTags);
  157           for (_j = 0, _len1 = nextTags.length; _j < _len1; _j++) {
  158             tag = nextTags[_j];
  159             if (tag >= 0) {
  160               tokenCount++;
  161             }
  162           }
  163           position = tagsEnd;
  164         } else {
  165           if (position < line.length || line.length === 0) {
  166             tags.push(line.length - position);
  167           }
  168           break;
  169         }
  170         if (position === previousPosition) {
  171           if (ruleStack.length === previousRuleStackLength) {
  172             console.error("Popping rule because it loops at column " + position + " of line '" + line + "'", _.clone(ruleStack));
  173             if (ruleStack.length > 1) {
  174               _ref2 = ruleStack.pop(), scopeName = _ref2.scopeName, contentScopeName = _ref2.contentScopeName;
  175               if (contentScopeName) {
  176                 tags.push(this.endIdForScope(contentScopeName));
  177               }
  178               if (scopeName) {
  179                 tags.push(this.endIdForScope(scopeName));
  180               }
  181             } else {
  182               if (position < line.length || (line.length === 0 && tags.length === 0)) {
  183                 tags.push(line.length - position);
  184               }
  185               break;
  186             }
  187           } else if (ruleStack.length > previousRuleStackLength) {
  188             _ref3 = ruleStack.slice(-2), (_ref4 = _ref3[0], penultimateRule = _ref4.rule), (_ref5 = _ref3[1], lastRule = _ref5.rule);
  189             if ((lastRule != null) && lastRule === penultimateRule) {
  190               popStack = true;
  191             }
  192             if (((lastRule != null ? lastRule.scopeName : void 0) != null) && penultimateRule.scopeName === lastRule.scopeName) {
  193               popStack = true;
  194             }
  195             if (popStack) {
  196               ruleStack.pop();
  197               lastSymbol = _.last(tags);
  198               if (lastSymbol < 0 && lastSymbol === this.startIdForScope(lastRule.scopeName)) {
  199                 tags.pop();
  200               }
  201               tags.push(line.length - position);
  202               break;
  203             }
  204           }
  205         }
  206       }
  207       for (_k = 0, _len2 = ruleStack.length; _k < _len2; _k++) {
  208         rule = ruleStack[_k].rule;
  209         rule.clearAnchorPosition();
  210       }
  211       if (compatibilityMode) {
  212         return new TokenizeLineResult(line, openScopeTags, tags, ruleStack, this.registry);
  213       } else {
  214         return {
  215           line: line,
  216           tags: tags,
  217           ruleStack: ruleStack
  218         };
  219       }
  220     };
  221 
  222     Grammar.prototype.activate = function() {
  223       return this.registration = this.registry.addGrammar(this);
  224     };
  225 
  226     Grammar.prototype.deactivate = function() {
  227       var _ref;
  228       this.emitter = new Emitter;
  229       if ((_ref = this.registration) != null) {
  230         _ref.dispose();
  231       }
  232       return this.registration = null;
  233     };
  234 
  235     Grammar.prototype.clearRules = function() {
  236       this.initialRule = null;
  237       return this.repository = null;
  238     };
  239 
  240     Grammar.prototype.getInitialRule = function() {
  241       return this.initialRule != null ? this.initialRule : this.initialRule = this.createRule({
  242         scopeName: this.scopeName,
  243         patterns: this.rawPatterns
  244       });
  245     };
  246 
  247     Grammar.prototype.getRepository = function() {
  248       return this.repository != null ? this.repository : this.repository = (function(_this) {
  249         return function() {
  250           var data, name, repository, _ref;
  251           repository = {};
  252           _ref = _this.rawRepository;
  253           for (name in _ref) {
  254             data = _ref[name];
  255             if ((data.begin != null) || (data.match != null)) {
  256               data = {
  257                 patterns: [data],
  258                 tempName: name
  259               };
  260             }
  261             repository[name] = _this.createRule(data);
  262           }
  263           return repository;
  264         };
  265       })(this)();
  266     };
  267 
  268     Grammar.prototype.addIncludedGrammarScope = function(scope) {
  269       if (!_.include(this.includedGrammarScopes, scope)) {
  270         return this.includedGrammarScopes.push(scope);
  271       }
  272     };
  273 
  274     Grammar.prototype.grammarUpdated = function(scopeName) {
  275       if (!_.include(this.includedGrammarScopes, scopeName)) {
  276         return false;
  277       }
  278       this.clearRules();
  279       this.registry.grammarUpdated(this.scopeName);
  280       if (Grim.includeDeprecatedAPIs) {
  281         this.emit('grammar-updated');
  282       }
  283       this.emitter.emit('did-update');
  284       return true;
  285     };
  286 
  287     Grammar.prototype.startIdForScope = function(scope) {
  288       return this.registry.startIdForScope(scope);
  289     };
  290 
  291     Grammar.prototype.endIdForScope = function(scope) {
  292       return this.registry.endIdForScope(scope);
  293     };
  294 
  295     Grammar.prototype.scopeForId = function(id) {
  296       return this.registry.scopeForId(id);
  297     };
  298 
  299     Grammar.prototype.createRule = function(options) {
  300       return new Rule(this, this.registry, options);
  301     };
  302 
  303     Grammar.prototype.createPattern = function(options) {
  304       return new Pattern(this, this.registry, options);
  305     };
  306 
  307     Grammar.prototype.getMaxTokensPerLine = function() {
  308       return this.maxTokensPerLine;
  309     };
  310 
  311     Grammar.prototype.scopesFromStack = function(stack, rule, endPatternMatch) {
  312       var contentScopeName, scopeName, scopes, _i, _len, _ref;
  313       scopes = [];
  314       for (_i = 0, _len = stack.length; _i < _len; _i++) {
  315         _ref = stack[_i], scopeName = _ref.scopeName, contentScopeName = _ref.contentScopeName;
  316         if (scopeName) {
  317           scopes.push(scopeName);
  318         }
  319         if (contentScopeName) {
  320           scopes.push(contentScopeName);
  321         }
  322       }
  323       if (endPatternMatch && (rule != null ? rule.contentScopeName : void 0) && rule === stack[stack.length - 1]) {
  324         scopes.pop();
  325       }
  326       return scopes;
  327     };
  328 
  329     return Grammar;
  330 
  331   })();
  332 
  333   if (Grim.includeDeprecatedAPIs) {
  334     EmitterMixin = require('emissary').Emitter;
  335     EmitterMixin.includeInto(Grammar);
  336     Grammar.prototype.on = function(eventName) {
  337       if (eventName === 'did-update') {
  338         Grim.deprecate("Call Grammar::onDidUpdate instead");
  339       } else {
  340         Grim.deprecate("Call explicit event subscription methods instead");
  341       }
  342       return EmitterMixin.prototype.on.apply(this, arguments);
  343     };
  344   }
  345 
  346   TokenizeLineResult = (function() {
  347     function TokenizeLineResult(line, openScopeTags, tags, ruleStack, registry) {
  348       this.line = line;
  349       this.openScopeTags = openScopeTags;
  350       this.tags = tags;
  351       this.ruleStack = ruleStack;
  352       this.registry = registry;
  353     }
  354 
  355     Object.defineProperty(TokenizeLineResult.prototype, 'tokens', {
  356       get: function() {
  357         return this.registry.decodeTokens(this.line, this.tags, this.openScopeTags);
  358       }
  359     });
  360 
  361     return TokenizeLineResult;
  362 
  363   })();
  364 
  365 }).call(this);