Poor man's syntax highlighting
Before getting to the more complex scenarios of creating professional DSL, I wanted to start by showing how you can easily create your own syntax highlighting.
This was the result:
And this is the actual code that makes this happens:
private void codeTextBox_TextChanged(object sender, EventArgs e) { int prevSelectionStart = codeTextBox.SelectionStart; int prevSelectionLength = codeTextBox.SelectionLength; codeTextBox.SelectionStart = 0; codeTextBox.SelectionLength = codeTextBox.TextLength; codeTextBox.SelectionColor = DefaultForeColor; var keyWords = new[] { "specification", "requires", "users_per_machine", "same_machine_as" }; foreach (string keyWord in keyWords) { MatchCollection matches = Regex.Matches(codeTextBox.Text, keyWord); foreach (Match match in matches) { codeTextBox.SelectionStart = match.Index; codeTextBox.SelectionLength = match.Length; codeTextBox.SelectionColor = Color.DarkOrchid; } } foreach (Match match in Regex.Matches(codeTextBox.Text, @"@[\w\d_]+")) { codeTextBox.SelectionStart = match.Index; codeTextBox.SelectionLength = match.Length; codeTextBox.SelectionColor = Color.DarkSeaGreen; } foreach (Match match in Regex.Matches(codeTextBox.Text, @" \d+")) { codeTextBox.SelectionStart = match.Index; codeTextBox.SelectionLength = match.Length; codeTextBox.SelectionColor = Color.DarkRed; } codeTextBox.SelectionStart = prevSelectionStart; codeTextBox.SelectionLength = prevSelectionLength; }
The code in the book comes with the following warning:
The code suffers from multiple bugs, issues and is generally not suited for anything but the simplest scenarios.
Comments
I think a better solution will be using language servies. Check out this link: http://msdn.microsoft.com/en-us/bb985513.aspx
You can easily (in relative terms) have intellisence, highlighting and full VS support.
Sometimes I get the idea you really suffer from NIH syndrome. I mean, get Actipro's SyntaxEditor control, write the language in a couple of minutes and have a ball. If you want with intellisense.
Writing your own syntax highlighting without using the same parser as the DSL parser used to parse the text inside the editor is a mistake btw. You can easily create syntax highlighting with the parser of the DSL, without having to worry about any errors, simply because the DSL parser system already gives you the tokenstream so it's very easy to do. And with the AST, you can position the caret in the editor box inside the AST and immediately know where you are and what you can see, so intellisense is doable as well.
Eyal,
I wouldn't can VS language services "easy" any day of the week.
@Frans,
Did you see the last statement?
I agree with everything you say about the stupidity of trying to handle this manually, but this is the first example in how to build the tooling experience, and I needed to show what the simplest option was, so I can show why that is bad.
Gotta love it. The post title effectively makes a disclaimer. The first sentence of the post effectively makes a disclaimer (e.g. "before getting to the..."). The last sentence of the post makes one final disclaimer. Idiots still come out of the woodwork. Okay, okay, perhaps not idiots. Just people with extremely poor reading comprehension. ;)
Jeremy: you don't get the sentiments of my post. I don't care if there's a disclaimer, the whole point of doing it yourself is silly to begin with. So instead of investing time to get something up and running with major flaws, one should invest time into what is more efficient, namely the code to write for the client/customer. Here's why:
The disclaimer says it's not suitable for anything else but the simplest scenario's :). Though there's a golden rule in software: There's nothing more permanent as a temporary solution.
The thing is that once you have hacked up this poor mans highlighting, your buddy Joe who's sitting next to you needs something similar too, and asks you for the code."Sure go ahead, but it's not really mature!", "oh don't worry, it's for something simple", and before you know it, the code ends up in bigger and bigger parts of the app, people try to add shabby shades of code to it etc. etc.
So before getting the URGE to get this running, stop. please, for all of mankind and beyond ;).
I'm therefore pleased to see that Oren has posted a better alternative in a follow up post :D
@Oren: agreed with the statement that you need for the BOOK an example what NOT to do :) Forgot to add that to my previous post ;)
Frans,
In isolation, it might have given the wrong impression.
I am showing the reader how to build a tooling experience, and I started with the Hello World equivalent of this, then building onward from this.
Frans, we can't help your our Joe if he is an idiot. If he is willing to add this kind of code to a real app and the senior dev let him then dev team have much bigger problem to deal with in the beginning.
@Frans
Not all developers have the piles of money required to buy all the assemblies/controls/code required to get rid of the need to "re-invent" the wheel.
Also, for what it's worth, if people didn't "re-invent" the wheel (i.e. suffer from NIH syndrome), there wouldn't be any progress and there wouldn't be any choices. Just because it's been done before, doesn't mean that someone can't come along and do it better. With the mindset of "NIH syndrome == BAD" that never happens.
Comment preview