Solved

multi-line comments in syntax highlighting

Posted on 2001-08-07
4
151 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
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 200 total points
Comment Utility
>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
Comment Utility
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
Comment Utility
It worked great, scrapdog.
Thanks
0
 
LVL 5

Expert Comment

by:scrapdog
Comment Utility
That's great.  You're welcome.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now