[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

multi-line comments in syntax highlighting

Posted on 2001-08-07
4
Medium Priority
?
160 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 800 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

656 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