I hope not, but I can't explain the behavior of the code other than by stating there's a bug.
Context: I am writing a custom highlighter for a new programming language. I only mention this to explain why I am not using one of the available highlighters.
The program is raising a 201 inside FindMatchingBracketLogical.
I've carefully traced the code and tried to understand what this code is doing. Without comments, it's a bit tricky. I assumed the error was in my highlighter -- and perhaps it is -- but I can't explain the following behavior:
Here's the trace:
First: The code I am writing in my editor consists of the following two lines
That's it. There's nothing else in the SynEdit editor. I've made sure there is no return after the '}'
Now, before the main form becomes visible, the Next function of the highlighter is called 7 times and the problem occurs on the seventh. The function Next returns '}' so it has grabbed the symbol from the second line.
As the stack unwinds, we go back to SetLine, then to TSynCustomHighlighter.StartAtLineIndex. From there, we go back to line 8566 inside the SynEdit unit. We're in a sub-function called FindMatchingBracketLogical.
Line 8566:
{8566} fHighlighter.StartAtLineIndex(PosY - 1); // PosY = 2, so we're starting at line 1.
{8567} TokenListCnt := 0;
Here, the code is asking the highlighter to begin scanning one line above the current one. So far so good.
Then, on line 8570: I've added my own comment to help along.
{8570} fHighlighter.Next; //We're getting the next token. No issues
{8571} i := TokenListCnt; //Now i is set to 0. That's the critical issue.
{Skip a few lines..}
{8577} TokenPosList[i].X := fHighlighter.GetTokenPos + 1;
{The token is the right curly brace at column 1, so X should be set to 2, which is exactly what happens}
{8579} If TokenPosList[i].X > PosX then begin //TokenPosList[i {0}].X = 2 from line 8577 and XPos = 1 so the condition is true.
{8580} TokenListCnt := i+1; //TokenListCnt = 1 now since i = 0
{8581} MaxKnownTokenPos := TokenPosList[i].X // So MaxKnownTokenPos = 2.
{8582} Result := TokenPosList[i-1].Attr := BracketKind // <=== offending line
That last line is trying to access position -1 of TokenPosList, which does not exist.
So, is the code I wrote in the highlighter leading me to land in the wrong place or is there a missing condition here?
Your help is appreciated.