Solved

multi-line comments in syntax highlighting

Posted on 2001-08-07
4
157 Views
Last Modified: 2010-04-06
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
Comment
Question by:wizcool
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 200 total points
ID: 6362384
>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
 

Author Comment

by:wizcool
ID: 6374819
That sounds like a good solution.
I will implement it and then let you know (it shouldn't take long) :)
0
 

Author Comment

by:wizcool
ID: 6377555
It worked great, scrapdog.
Thanks
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6377751
That's great.  You're welcome.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question