For those of you interested, here's what my Source Editor uses to describe the grammar of CSS. The lexer is c++-based. It's still a work in progress, but you'll understand easily how it works. For perf reasons, the editor then creates a hashtable of all context nodes based on their name instead of doing multiple calls to getElementById().