Solved

multi-line comments in syntax highlighting

Posted on 2001-08-07
4
155 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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