Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

multi-line comments in syntax highlighting

I'm making a program that uses a richedit control and has to do syntax highlighting to the text. I figured out how to do the most part, but it remains a problem :
multi-line comments!

It's not difficult to highlight ML comments if I parse the whole text every time, but that is, obviously, incredibly slow. So I have to parse only the lines that were actually changed.
The problem is, when I have to parse a line, the parser needs to know if that line is to be highlighted normally or if is to be highlighted as a comment (if the line is inside a ML comment).

How can I know if a line is inside a ML comment ?

Note: I focused on the ML comments, but it can also exist multi-line strings in the same text.
0
wizcool
Asked:
wizcool
  • 2
  • 2
1 Solution
 
scrapdogCommented:
>It's not difficult to highlight ML comments if I parse
>the whole text every time, but that is, obviously,
>incredibly slow. So I have to parse only the lines that
>were actually changed.

You don't have to parse the whole text;  just parse the lines that have changed, as you have planned, with a few special cases.

Make a list of booleans, one that corresponds to each line.  This boolean represents whether or not the beginning of the line is inside a comment.

When parsing a line that has changed, check the comment flag:

If false:

If an opening delimiter for a comment is found when parsing a line that has changed, continue scanning lines until you find the matching closing delimiter (or the end if the file).  Set each line's comment flag (the list of booleans) to 'true' before you traverse it.  (Important:  Make sure you do NOT set the flag for the line with the opening delimiter).

If true:

You are currently inside a comment.  Continue scanning lines until the closing delimiter is found.  Starting with the line AFTER the line with closing delimiter, set the flag for each line to false UNTIL (and including) the next line that contains an opening delimiter.

Note that you will have to parse multiple lines, but rarely will you have to parse the whole text.  Also, you won't have to fully 'parse' each line anyway, you simply have to scan the lines for the appropriate delimiter (and perform logic to determine whether or not the delimiter is in a string or escaped, etc.)  This should be fairly fast because you do not have to care about anything that is not a comment or a string (that takes place elsewhere).

I have not tested this method but I am sure it would be quite an improvement in speed for your purpose.  Let me know if you need more explaining.

Note that if you plan to allow multi-LEVEL comments, you can use a list of counters instead of a list of booleans.  The counter would represent the comment depth at the beginning of the line.
0
 
wizcoolAuthor Commented:
That sounds like a good solution.
I will implement it and then let you know (it shouldn't take long) :)
0
 
wizcoolAuthor Commented:
It worked great, scrapdog.
Thanks
0
 
scrapdogCommented:
That's great.  You're welcome.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now