Adding comment highlighting to custom grammar


I am trying to make my own grammar file. So far I got some basic highlighting which is fine for my purposes. However, I would like to add highlighting for C like comments, so single line comments starting with // and block comments /*...*/.

Could somebody point me to some documentation on how to achieve this?


You can see how language-c defines its comments here. I’ve made a grammar template with annotations for reference, and it might also help.


@DamnedScholar Thank you for your reply. I added both the comments and line_continuation_character parts of the c.cson file to my own cson file and started the first pattern with an include of comments. There is no error upon load of the package in Atom and my highlights of data types and numbers still work, but the comments are just plain text. Is there more I should include besides these two parts of the c.cson file?


Please share your grammar file. I can’t tell you what you’re missing if I can’t see it.


The repository can be found on my github, click here.


The issue was that you didn’t have a repository key for the includes. I fixed that, moved line_continuation_character into the repository, and tweaked everything a little to make it more readable (having the keys in quotes is unnecessary, even though I haven’t gotten around to changing that on my template yet). I’ve made a pull request on your repo with the changes.


@DamnedScholar It works! Thank you so much. I have some more things I would like to implement in my grammar such as C like function highlighting and some other stuff. Would it be best to create a new thread for it or continue in this one?


Keep it in this one for the sake of continuity and tidiness.


@DamnedScholar Alright. I am also trying to accomplish C like function highlighting. Would I be able to copy this from the language-c repository as well or are C files interpreted by the Atom highlighter?


Atom itself is completely ignorant when it comes to languages. All highlighting is done according to the grammar file that gets selected.


@DamnedScholar That is good to hear. I am trying to highlight a C like function call such as function(). I tried the c_function_call from the language-c package, without any luck. Any ideas on how to achieve this?


Works fine for me. My theme doesn’t have any styling for that particular selector, but inspecting the line in dev tools, I can see that the scope was applied correctly.


@DamnedScholar I feel rather stupid now. I used incorrect include, it works perfectly now. Thanks again!

Something else I would like to add is to highlight a declaration which looks for example like 8'b0. Here the 8 can be any positive number (but not zero). The b can also be an h and the 0 can be either 0 or 1. How would I go about writing this in my cson file?


The regular expression ([1-9]\\d*)\\'(b|h)(0|1) would do the job. That matches for any number that does not begin with 0 followed by an apostrophe and either b or h, then either 0 or 1.

I like to test out my regular expressions here. Keep in mind that escapes (\) in a normal regex need to be doubled in the CSON file (like the example in this post).


I tested your expression in the online tool and I get the syntax much better now. However, when I include it in my CSON file, Atom throws all kinds of errors. I think it is due to the ' in the expression.

I tried to add the following under the patterns section in the CSON file.
match: '\\b([1-9]\\d*)\\'(b|h)(0|1)\\b'
name: 'constant.numeric.tie'


My bad. You need to put it in double quotes, in which case the apostrophe doesn’t need to be escaped. So match: "\\b([1-9]\\d*)'(b|h)(0|1)\\b" is right.


Works perfectly! Thanks again :slight_smile: