This is generally more complex than it looks because of escaped quotation marks and that sort of thing. Indeed, there are many cases that you have to deal with in such situations. You might want to check out Text::Balanced, which could help.
| [reply] |
Since you are using the Tk::Text (or some inherited) API, it is simpler to encapsulate the text into logical tags, then parse contents in these tags differently according to tag type. This is the way VIM implements its syntax hilighting. It would be a lot nicer and powerful with Perl's regular expressions.
For example, given the code:
my $foo = "foo's $bar";
first, look for a string opening token (like qw/" ' qq. qr. q. qx./):
my $foo = "foo's $bar";
^
Then look for a corresponding close token (in this case /(?<[^\\])"/ to find the other end.
my $foo = "foo's $bar";
^---------->
Mark that part as a string. On a second pass, you might want to look for variables inside each string tag using $text->tagNextrange. (or the ones that evaluate "", qq//, etc)
At the end, you end up with something like the following (ignoring whitespace):
<perlcode>
<reserved>my</reserved>
<variable>$foo</variable>
<operator>=</operator>
<string>"foo's <variable>$bar</variable>"</string>;
</perlcode>
I hope you got the idea. | [reply] [d/l] [select] |
Using existing modules is always the best solution, because they handle complex cases and have had more error testing than you will invest for a few lines of code.
split /(")/ will return an array where each element is either unquoted text, a quote mark, or text froma quotation. If the first element is a quote, the array starts with a quotation, otherwise it starts with unquoted text. Check the last character of each string for a backslash, to determine whether the quotation mark is a real quote, or has been escaped. But if you find a backslash, make sure there is an odd number of backslashes; an even number simply puts literal backslashes into the string, but does not modify the quote. So it isn't simply, "odd block, unquoted; even block, quoted", but rather a block by block state machine.
Of course, if you allow both single quotes and double quotes to delimit strings, things become much more complicated. You could split on /('")/, but processing is stressfull. If you see a single quote within a double quote, is it an inner quote or a contraction/possessive?
John said, "Don't do it!".
Mary said, "I told you, 'Stop!'".
Paul said, "Stephen's watch is missing."
Assuming grammatically correct text, a single single quote within quoted text is a contraction or possessive. but if there are more than one, does that indicate an inner string, or multiple constractions and possessives?
I've seen many cases where people thought it too expensive to load a module just to handle some simple situation, like detecting balanced strings, or doing simple date calculations, when all it needs is a couple lines of code. This works fine until months or years later, when one of your data feeds starts sending data which includes that impossible special case your code doesn't handle. After hours or days of debugging, some poor sucker maintenance programmer me curses you for taking the easy wrong solution.
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] |
What is the best way to ignore a string or a commented statement and ignore these only, during the parsing? The best way is to use a parser that knows about the language it is parsing rather than rely on regular expressions to figure out what needs highlighting or not.
| [reply] |