Reputation: 27200
Is it possible to build a regexp to match all keywords in a scala source file?
The best I came with so far:
"""(?<![\w`])(%s)(?![\w\_])""" format keyword
Is this good enough or I missed something?
Upvotes: 1
Views: 249
Reputation: 61007
The obvious solution is to build a regexp for matching all keywords, and you can do that by simply creating a big OR-ing of keywords (if|else|...)
. However, you'll need to exclude strings, comment and any text which may contain free keywords that aren't keywords.
When we think about regular expressions there are limits to what can be accomplished by regexp in a practical manner. That's to say that, regexp is not necessarily a good fit for your problem.
The reason for this is that you have to match a lot of invalid input and then discard it.
e.g. how will you handle typical C-style comments that terminate on a new line?
Ultimately, what you need is a tokenizer for Scala, and you could probably find that in the Scala distribution.
Upvotes: 4