C syntax is bad for regex
Because it’s hard to grep what I want …
I find myself using grep (or regex in general) more often than before and I’ve been writing code without LSPs at all. And I found an unexpected drawback of C style syntax: it’s not ergonomic to find, say the definition/declaration of a function/struct/class, using grep.
The C syntax is that, you declare something in the same manner you use it. For example, you have a function:
uint64 *myfunc(int n);
You call the function myfunc
, with parameter n
, then you dereference the
result with a asterisk and you will get an integer, hence the syntax (also the
reason that the asterisk should attach to myfunc
not uint64
). This is very
expressive in what this function does but the problem is, there is no
dedicated identifier saying “I’m declaring/defining a function here”.
What if you want to find this definition from a massive codebase?
grep -ri "myfunc"
Obviously this gives you more than just declaration/definition. Well what about this?
grep -ri "uint64 *myfunc"
As it turns out, this is not convenient either, because 1) in many cases you
don’t know in advance or you don’t bother to figure out the exact return type of
a function and 2) even if you try you can’t really figure out the return type of
a function from the context you are reading – what about ptr = (void*) myfunc(42)
? There is no tell from the context.
On the other hand, structs may look better because they have the form
struct <name> {}
; However that’s only true if you don’t alias the struct like
struct lnode
{
//membbers
} my_node;
If you want grep my_node
hoping to find its definition, good luck!
In contrary, Rust for example, is much more ergonomic in this regard. Any function definition has exactly the form:
<pub> fn function_name(<parameters>) -> <return type>
{
<body>
}
If you want to find the definition of something you simply run
grep -ri "fn <name>"
grep -ri "struct <name>"
grep -ri "impl <struct name>"
I know there has always been remedy:
ctags, cscopes, and even treesitters, are able to figure out what is a function declaration/definition. But I then you are effectively using a LSP-alike thingy. I’m not arguing about whether we should use LSPs – they are good stuffs!
My conclusion is simply that, C syntax is not fit for the simplest tool – grep!
[+] click to leave a comment [+]
>> SEND COMMENT <<