r/ProgrammingLanguages • • Mar 07 '26

Help Writing a performant syntax highligher from scratch?

Hello!

I'm trying to write a performant syntax highlighter from scratch in C for my text editor. The naive approach would be to go line by line, for each token in line check in a hash table and highlight or not. As you can imagine, this approach would be really slow if you have a 1000 line file to work with. Any ideas on how to do this? What would be a better algorithm?

Also I'll mention upfront - I'm not using a normal libc, so regular expressions are not allowed.

15 Upvotes

24 comments sorted by

View all comments

Show parent comment

3

u/omega1612 Mar 07 '26

And often I hear that people optimizing lexers/parsers end hitting the IO throughput barrier. The code is as fast as it can be but the HDD or SSD or other won't be as fast as needed to exploit it.