Implementing syntax highlighting can be a painful task. Lots of regular expressions and thinking may be required to master this.
On the search for an easy way to implement a syntax highlighting mechanism for NSTextView I found an easy and fast (regarding runtime) way to do this. By using flex (a tool with its roots in the 1970’s) defining the rules of what should be highlighted makes it a lot more easier and structured.
flex is the GNU version of lex, the lexical analyzer generator (Buzzword jackpot!) developed by Eric Schmidt and Mike Lesk in 1975. By defining some rules (mainly regular expressions) it generates a C program which allows us to scan through a text and do something with the text, e.g. divide it into tokens. Tokens are chunks of characters with a special meaning (as defined in our rules). Confused? Read on, this concept will become very clear soon.
In this article I want to show you how we can use this ancient but very powerful tool to implement a basic syntax highlighting within a NSTextView.
Continue reading ‘Syntax Highlighting for NSTextView with flex’

Latest Comments