Possible defect in Atom's grammar engine



I’m having a hard time implementing nested patterns to create custom grammar highlights. I already spent days reading (and rereading, multiple times) TextMate’s original grammar webpage, and also checking other Atom language packages, but still can’t solve this problem.

Here is a very basic language snippet, for a fake language …

different items per line %%
more items %%

Here’s a very simple snippet from my language.cson file:

scopeName: 'source.MyLang'
name: 'MyLang'
fileTypes: [
patterns: [
    match: '([^\\n]+)(?=%%$)'
    comment: 'matches everything upto "%%" at end of line'
    name: 'keyword'
    patterns: [
        match: '(.*)'
        comment: 'this should find a match *INSIDE* the parent match'
        name: 'illegal.invalid'

So basically, the grammar detect a line in the first ‘pattern’. Then in the child ‘pattern’, it detects another match inside the parent match.

However, this is not working. Only the parent match is correctly found. Atom somehow appears to be ignoring the child pattern. This has nothing to do with regex not found because even a pattern of (.*), which should match any character, is not returning a result.

P.S. Ignore the names ‘keyword’ and ‘illegal.invalid’, these just show the matches in clear contrasting colors to help with debugging the highlighting.

Thanks …


Your problem is that match rules do not support a top-level patterns, only a top-level captures. Here’s how it should be written:

patterns: [
    match: '(.+)(?=%%$)'
    # matches everything upto "%%" at end of line
    name: 'keyword'
      '1': # Corresponding to the first capture group in your match
        # this should find a match *INSIDE* the parent match
        name: 'illegal.invalid'


Nesting patterns is for when you’re using begin and end to find a region, such as from <?php to ?>.


Thanks. Yes I’m aware of captures. The problem is this won’t allow matching repetitive patterns within a parent match. You do the match once; Atom won’t allow finding sub-patterns, as opposed to the original TextMate grammar.

This is not just specific to match. I also tried begin & beginCaptures with end & endCaptures, same behavior, no difference at all: can’t process the inner patterns.

I already tried begin & beginCaptures with end & endCaptures, same behavior, no difference at all.


Please try again and then post your CSON here. That’s how other language packages use nesting patterns.


Repeatedly matching within a parent match is definitely possible: see https://github.com/atom/language-php/blob/cf6db5974e74130bf2414d8c8f38ce307860704a/grammars/php.cson#L1702 for a very recent example.


Ok thank you for your help :thumbsup: … I’ll try the modifications you suggested and see how it goes …

Thank you