Dynamic Grammers


#1

Is there an easy or known or allowed way to build a grammar on the fly? My module contains all the keywords and highlighted syntax terminology for my grammar in JavaScript arrays. This is to allow easy command line completion (my next feature to add). However it would be really nice if I could build parts of the grammar file from these values so as to not have them in more than one place.

Basically, I’d like to be able to programmatically add a pattern object in the activate call of my plugin. Bonus points for letting me know if there is a way to remove the set that I’ve added.

Context:
I have defined a grammar for GECK scripting (Fallout 3 and Fallout New Vegas). Both of these games use the same core language but have different functions available to them. Right now I am highlighting all the keywords for both games and both script extenders (FOSE and NVSE), but in an ideal world the game and script extenders to highlight could be configurable.


Ok to use grammar.cson for just file assoc…?
#2

This has been a recurring topic:


#3

@leedohm
This is how I made it work:

activate: function(state) {
  if (!this.grammarsAdded) {
    // Supplement grammer
    var grammar = atom.grammars.grammarsByScopeName['source.geck'];
    var patterns = grammar.rawPatterns;

    // Add Blocktypes
    patterns[patterns.length] = {
      match: language.getSet(language.keywords),
      name: 'keyword.geck-keyword'
    };

    patterns[patterns.length] = {
      match: language.getSet(language.blocktypes, true),
      name: 'keyword.control.geck.blocktype'
    };
    
    this.grammarsAdded = true;
  }

Where language.getSet() returns a regular expression string.


#4

I have finally adjusted this to be more abstract and reusable (within my package) but it could be applied to others outside. I ended up adding a third property that I could use to identify which ones I had added dynamically and remove/re-add or adjust at will.

Take a look here to see what I ended up doing
language-geck-main.js on GitHub around line 118 (updateGrammars)
GECK Language Syntax on GitHub

The syntax file defines the syntax by plugins (relevant to the game the syntax highlighting is for) in a manner that lets me disable or enable parts of the highlighting through configuration. I’m using a lot of ES6 syntax in the JS files, so if you’re not familiar with it then parts may be confusing.