Translate simple formal grammar to vim syntax - vim

I want to write bison syntax file for vim. I've managed to write syntax rule for definition section. Now I'm trying to compose syntax rule for bison rule. So, I have
rule : identifier ":" rightHandSidePart ( "|" rightHandSidePart )* ";"
where
rightHandSidePart : listOfIdentifiers "{" /* some C code here */ "}"
listOfIdentifiers : listOfIdentifiers identifier | /* nothing */
and identifier may be declared as [_a-zA-Z][_0-9a-zA-Z]* regular expression.
So the question is: how do I translate this grammar to vim syntax rules?

You might be able to use autohighlight to convert your grammar to vim's syntax regex.
Autohighlight generates vim and emacs syntax highlighting from a BNF grammar and a description of which terms should be highlighted which colors.

Related

String literal in ANTLR4

I'm using antlr4 C++ runtime and I'd like to create a string literal in my lexer definition file. How can I do this?
What I have so far:
V_STRING : '"' ~('\\' | '"')* '"';
I doesn't work with
printf("string literal\n");
but works with
printf("string literal\\n");
I don't want to explicitly escape the new line character.
my assumptions are that antlr interprets the new line character as a regular new line (when reading a file, for example).
Thanks in advance.
It's always a good idea to list out your token stream to see if your Lexer rules really do what you expect. (Look into the tokens option of the TestRig; also, some plugins will show you your tokens)
In your case your rule essentially says that a String is " a " followed by 0 or more characters that are not a \ or a " and then a "".
So, when the Lexer encounters your \, matches the ~('\\\\'|'")* part of the rule and then looks for a " (which it does not find, since the \ is followed by a n), so It won't recognize "string literal\n" as a V_STRING token (it also fails to match "string literal\\n" as well, here, so I'm not quite sure what's going on with the example that "works").
try:
V_STRING: '"' ~["]* '"';
Note: this is a very simple String rule, but it accepts your input. You probably want to examine grammars for other languages to see how you might want to handle strings in your language; there are several approaches (and many of them involve using Lexer modes). You can find examples here)
If you want the "\n" to be treated as a newline, just understand that the parser won't do that for you, you'll just see the characters "" and "n". It'll be up to you to handle encoding the escaped characters (and it's once you try to handle " that it'll get more complicated and you'll need to look into Lexer modes)

Can we escape types, codes, etc. in comments section so that spell check does not consider them as typo

Vim supports spell-check only in comments section already, however, if I have a type name or something not a regular word, it will consider it as a typo. For instance, in the following example, std::endl will be highlighted as typo.
// Don't use std::endl, it will flush unnecessarily
I wish we could use `` to escape them like following.
// Don't use `std::endl`, it will flush unnecessarily
Is there any tips or solution for this besides adding everything into dictionary?
I really don't want to disable spell-check due to this, so any help is greatly appreciated.
Thank you!
You can use this syntax rule to create a new group matching a `...` block and disable spelling inside those blocks:
syntax region cCommentNoSpell start=+`+ end=+`+
\ contained containedin=cComment,cCommentL transparent
\ contains=#NoSpell
To load this for cpp and c files, add this line (by itself) to a file ~/.vim/after/syntax/c.vim, so it is loaded after the system syntax files for C++ and C. (The cpp syntax rule includes all syntax for c so you'll get it on cpp too.)
The syntax rule uses ` as both start and ending delimiter.
It uses contained and containedin to only match inside comments. The cComment rule matches traditional multi-line /* ... */ comments and cCommentL matches single-line // ... comments. (Both are defined in the syntax file for C and C++ shipped with Vim.)
The transparent attribute instructs it not to use this syntax rule as a highlighting group, so it keeps the normal highlighting for comments in the parts matched by this rule.
Finally, contains=#NoSpell is what disables spelling on the regions that match this rule. See :help spell-syntax for more details on how spelling works together with syntax highlighting.

Vim: use different color for words with a pattern

Am a hardware engineer and I use Embedded Ruby Language to simplify writing my hardware verilog/system verilog code. In my *.sv and *.v files, i have a lot of ERB variables starting with "__" (double underscore). E.g. <% __MEM_DEPTH = 64 %>. Is there any way by which I can make vim display the words starting with the double underscore in a different color?
You can extend the built-in syntax highlighting. For example, put the following into ~/.vim/after/syntax/verilog.vim:
syntax match verilogErbVar "\<__\w\+\>"
hi link verilogErbVar Identifier
This assumes that the corresponding text fragments aren't yet matched by the original syntax (in my short test, they weren't). Else, you need to find the syntax groups that contain them and add a containedin=... to the :syntax command.
To find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.

How to add lua operators to vim syntax file?

I'm a Lua developer. My development environment is SSH into a central server and using VIM.
Vim comes with the following Lua syntax file.
What I noticed is the syntax file does not include Relational Operators (e.g.: =, >, etc) and instead treats relational operators as normal text syntax (color).
I tried adding the following to my .vim/after/syntax/lua.vim but it didn't work
syn keyword luaOperator < > <= >= == ~=
Any ideas how I can make relational operators a different syntax color in Lua?
Solved.
By reading this article, I figured out how to get VIM to syntax highlight the following Lua operators: <, >, <=, >=, ==, ~=, =.
Put the following in your .vim/after/syntax/lua.vim file
syntax match potionOperator "<"
syntax match potionOperator ">"
syntax match potionOperator "<="
syntax match potionOperator ">="
syntax match potionOperator "=="
syntax match potionOperator "\v\~\="
syntax match potionOperator "="
highlight link potionOperator Operator
Then to set the operator to whatever color you want, in your .vimrc file put:
hi potionOperator ctermfg=COLOR
where COLOR is whatever color you want (e.g. yellow, red, etc)

Convert Notepad++ syntax highlighting file to vim (or does anyone have a q/kdb+ vim syntax highlight file?)

I have a syntax highlighting file for the q/kdb+ language and I'd like to convert it to a vim compatible file so my q code won't look any more ugly than usual.
Are there utilities available to automatically convert notepad++ xml syntax highlighting files to vi versions? I had a look around but I couldn't find anything.
Alternatively does anyone have a vim q syntax highlighting file?
a q/kdb+ vim syntax highlight files:
https://github.com/simongarland/vim
The answer to both questions is no (I don't know of any converters and I don't have a q syntax highlighting file), but the Notepad++ syntax highlighting XML format looks extremely simple. I don't have the 'Q' one to hand, but I had a look at one of the ones from the website and the translation looks pretty trivial. In that case, you could do most of the work with:
" Remove all the lines that aren't lists of keywords
" (there doesn't seem to be anything much more complicated
" than that in the definition file)
:g!/<Keywords name=/d
" Convert the lines (fairly poor XML parsing here!)
:%s/\s*<Keywords name="\([^"]\+\)">\([[:alpha:]_ ]\{-}\)<\/Keywords>/syn keyword \1 \2/
This generates lots of lines that look like:
syn keyword Words1 case then do while
You'll have to tweak the syntax class (Words1 in this case) to be something that will be highlighted in Vim (or syn-link it to something that will be highlighted in Vim).
You could probably then deal with the symbols with a regexp, but it might be easier to just do them by hand, so convert:
<Keywords name="Operators">- ! " # $ & * , . ; ? # \ ^ { | } ~ + < = ></Keywords>
into:
syn match Operators /\<[-!"#$&*,.;?#\\^{|}~+<=>]/
(this is \< to mark a word boundary, followed by a character class [..] with all the symbols in it).
You would then just need to add:
if exists("b:current_syntax")
finish
endif
at the start and:
let b:current_syntax = "q"
at the end.
Of course, this doesn't get you all the way, but hopefully it will give you a lot of what you need to get the syntax file that you want. There is plenty of help available in:
:help syntax
and by looking at the examples in the syntax directory of the runtime folder.
Good luck!

Resources