"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/first-mate/lib/grammar-registry.js" (8 Mar 2017, 8294 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 CSON, Disposable, Emitter, EmitterMixin, Grammar, GrammarRegistry, Grim, NullGrammar, _, _ref;
    3 
    4   _ = require('underscore-plus');
    5 
    6   CSON = require('season');
    7 
    8   _ref = require('event-kit'), Emitter = _ref.Emitter, Disposable = _ref.Disposable;
    9 
   10   Grim = require('grim');
   11 
   12   Grammar = require('./grammar');
   13 
   14   NullGrammar = require('./null-grammar');
   15 
   16   module.exports = GrammarRegistry = (function() {
   17     function GrammarRegistry(options) {
   18       var _ref1;
   19       if (options == null) {
   20         options = {};
   21       }
   22       this.maxTokensPerLine = (_ref1 = options.maxTokensPerLine) != null ? _ref1 : Infinity;
   23       this.nullGrammar = new NullGrammar(this);
   24       this.clear();
   25     }
   26 
   27     GrammarRegistry.prototype.clear = function() {
   28       this.emitter = new Emitter;
   29       this.grammars = [];
   30       this.grammarsByScopeName = {};
   31       this.injectionGrammars = [];
   32       this.grammarOverridesByPath = {};
   33       this.scopeIdCounter = -1;
   34       this.idsByScope = {};
   35       this.scopesById = {};
   36       return this.addGrammar(this.nullGrammar);
   37     };
   38 
   39 
   40     /*
   41     Section: Event Subscription
   42      */
   43 
   44     GrammarRegistry.prototype.onDidAddGrammar = function(callback) {
   45       return this.emitter.on('did-add-grammar', callback);
   46     };
   47 
   48     GrammarRegistry.prototype.onDidUpdateGrammar = function(callback) {
   49       return this.emitter.on('did-update-grammar', callback);
   50     };
   51 
   52 
   53     /*
   54     Section: Managing Grammars
   55      */
   56 
   57     GrammarRegistry.prototype.getGrammars = function() {
   58       return _.clone(this.grammars);
   59     };
   60 
   61     GrammarRegistry.prototype.grammarForScopeName = function(scopeName) {
   62       return this.grammarsByScopeName[scopeName];
   63     };
   64 
   65     GrammarRegistry.prototype.addGrammar = function(grammar) {
   66       this.grammars.push(grammar);
   67       this.grammarsByScopeName[grammar.scopeName] = grammar;
   68       if (grammar.injectionSelector != null) {
   69         this.injectionGrammars.push(grammar);
   70       }
   71       this.grammarUpdated(grammar.scopeName);
   72       if (Grammar.includeDeprecatedAPIs) {
   73         this.emit('grammar-added', grammar);
   74       }
   75       this.emitter.emit('did-add-grammar', grammar);
   76       return new Disposable((function(_this) {
   77         return function() {
   78           return _this.removeGrammar(grammar);
   79         };
   80       })(this));
   81     };
   82 
   83     GrammarRegistry.prototype.removeGrammar = function(grammar) {
   84       _.remove(this.grammars, grammar);
   85       delete this.grammarsByScopeName[grammar.scopeName];
   86       _.remove(this.injectionGrammars, grammar);
   87       this.grammarUpdated(grammar.scopeName);
   88       return void 0;
   89     };
   90 
   91     GrammarRegistry.prototype.removeGrammarForScopeName = function(scopeName) {
   92       var grammar;
   93       grammar = this.grammarForScopeName(scopeName);
   94       if (grammar != null) {
   95         this.removeGrammar(grammar);
   96       }
   97       return grammar;
   98     };
   99 
  100     GrammarRegistry.prototype.readGrammarSync = function(grammarPath) {
  101       var grammar, _ref1;
  102       grammar = (_ref1 = CSON.readFileSync(grammarPath)) != null ? _ref1 : {};
  103       if (typeof grammar.scopeName === 'string' && grammar.scopeName.length > 0) {
  104         return this.createGrammar(grammarPath, grammar);
  105       } else {
  106         throw new Error("Grammar missing required scopeName property: " + grammarPath);
  107       }
  108     };
  109 
  110     GrammarRegistry.prototype.readGrammar = function(grammarPath, callback) {
  111       CSON.readFile(grammarPath, (function(_this) {
  112         return function(error, grammar) {
  113           if (grammar == null) {
  114             grammar = {};
  115           }
  116           if (error != null) {
  117             return typeof callback === "function" ? callback(error) : void 0;
  118           } else {
  119             if (typeof grammar.scopeName === 'string' && grammar.scopeName.length > 0) {
  120               return typeof callback === "function" ? callback(null, _this.createGrammar(grammarPath, grammar)) : void 0;
  121             } else {
  122               return typeof callback === "function" ? callback(new Error("Grammar missing required scopeName property: " + grammarPath)) : void 0;
  123             }
  124           }
  125         };
  126       })(this));
  127       return void 0;
  128     };
  129 
  130     GrammarRegistry.prototype.loadGrammarSync = function(grammarPath) {
  131       var grammar;
  132       grammar = this.readGrammarSync(grammarPath);
  133       this.addGrammar(grammar);
  134       return grammar;
  135     };
  136 
  137     GrammarRegistry.prototype.loadGrammar = function(grammarPath, callback) {
  138       this.readGrammar(grammarPath, (function(_this) {
  139         return function(error, grammar) {
  140           if (error != null) {
  141             return typeof callback === "function" ? callback(error) : void 0;
  142           } else {
  143             _this.addGrammar(grammar);
  144             return typeof callback === "function" ? callback(null, grammar) : void 0;
  145           }
  146         };
  147       })(this));
  148       return void 0;
  149     };
  150 
  151     GrammarRegistry.prototype.startIdForScope = function(scope) {
  152       var id;
  153       if (!(id = this.idsByScope[scope])) {
  154         id = this.scopeIdCounter;
  155         this.scopeIdCounter -= 2;
  156         this.idsByScope[scope] = id;
  157         this.scopesById[id] = scope;
  158       }
  159       return id;
  160     };
  161 
  162     GrammarRegistry.prototype.endIdForScope = function(scope) {
  163       return this.startIdForScope(scope) - 1;
  164     };
  165 
  166     GrammarRegistry.prototype.scopeForId = function(id) {
  167       if ((id % 2) === -1) {
  168         return this.scopesById[id];
  169       } else {
  170         return this.scopesById[id + 1];
  171       }
  172     };
  173 
  174     GrammarRegistry.prototype.grammarUpdated = function(scopeName) {
  175       var grammar, _i, _len, _ref1;
  176       _ref1 = this.grammars;
  177       for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  178         grammar = _ref1[_i];
  179         if (grammar.scopeName !== scopeName) {
  180           if (grammar.grammarUpdated(scopeName)) {
  181             if (Grammar.includeDeprecatedAPIs) {
  182               this.emit('grammar-updated', grammar);
  183             }
  184             this.emitter.emit('did-update-grammar', grammar);
  185           }
  186         }
  187       }
  188     };
  189 
  190     GrammarRegistry.prototype.createGrammar = function(grammarPath, object) {
  191       var grammar;
  192       if (object.maxTokensPerLine == null) {
  193         object.maxTokensPerLine = this.maxTokensPerLine;
  194       }
  195       grammar = new Grammar(this, object);
  196       grammar.path = grammarPath;
  197       return grammar;
  198     };
  199 
  200     GrammarRegistry.prototype.decodeTokens = function(lineText, tags, scopeTags, fn) {
  201       var expectedScopeName, index, offset, poppedScopeName, scopeNames, tag, token, tokens, _i, _len;
  202       if (scopeTags == null) {
  203         scopeTags = [];
  204       }
  205       offset = 0;
  206       scopeNames = scopeTags.map((function(_this) {
  207         return function(tag) {
  208           return _this.scopeForId(tag);
  209         };
  210       })(this));
  211       tokens = [];
  212       for (index = _i = 0, _len = tags.length; _i < _len; index = ++_i) {
  213         tag = tags[index];
  214         if (tag >= 0) {
  215           token = {
  216             value: lineText.substring(offset, offset + tag),
  217             scopes: scopeNames.slice()
  218           };
  219           if (fn != null) {
  220             token = fn(token, index);
  221           }
  222           tokens.push(token);
  223           offset += tag;
  224         } else if ((tag % 2) === -1) {
  225           scopeTags.push(tag);
  226           scopeNames.push(this.scopeForId(tag));
  227         } else {
  228           scopeTags.pop();
  229           expectedScopeName = this.scopeForId(tag + 1);
  230           poppedScopeName = scopeNames.pop();
  231           if (poppedScopeName !== expectedScopeName) {
  232             throw new Error("Expected popped scope to be " + expectedScopeName + ", but it was " + poppedScopeName);
  233           }
  234         }
  235       }
  236       return tokens;
  237     };
  238 
  239     return GrammarRegistry;
  240 
  241   })();
  242 
  243   if (Grim.includeDeprecatedAPIs) {
  244     EmitterMixin = require('emissary').Emitter;
  245     EmitterMixin.includeInto(GrammarRegistry);
  246     GrammarRegistry.prototype.on = function(eventName) {
  247       switch (eventName) {
  248         case 'grammar-added':
  249           Grim.deprecate("Call GrammarRegistry::onDidAddGrammar instead");
  250           break;
  251         case 'grammar-updated':
  252           Grim.deprecate("Call GrammarRegistry::onDidUpdateGrammar instead");
  253           break;
  254         default:
  255           Grim.deprecate("Call explicit event subscription methods instead");
  256       }
  257       return EmitterMixin.prototype.on.apply(this, arguments);
  258     };
  259   }
  260 
  261 }).call(this);