Adding comment highlighting to custom grammar


#1

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?


#2

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.


#3

@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?


#4

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


#5

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


#6

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.


#7

@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?


#8

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


#9

@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?


#10

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


#11

@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?


#12

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.


#13

@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?


#14

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).


#15

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'


#16

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.


#17

Works perfectly! Thanks again :slight_smile: