Escaped quotes not handled properly with syntax colors

General questions about using TextPad

Moderators: AmigoJack, helios, bbadmin, Bob Hansen, MudGuard

Post Reply
vdawg
Posts: 19
Joined: Sun Oct 31, 2021 7:32 pm

Escaped quotes not handled properly with syntax colors

Post by vdawg »

I have a Perl script that uses the following substitution regex to replace double quotes with a backslash + double quote combo:

(Since the backslash is not showing up in the code sample below after posting, I'm going to use a vertical bar in place of where the real code has a backslash.)

$str =~ s/|"/||"/isg;

However, the editor gets confused by this and thinks everything after the second double quote is now a literal string and colors it in that defined color.

If I remove one of the backslashes prior to the second double quote, the remainder of the script's colors are displayed properly.

Is this something I can fix in a config or syntax file or is this an issue that requires a code change?

Thanks!
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

I don't think there is any way you can fix this. Perl syntax is complex; the only way an editor can parse it perfectly is by running Perl's own parser.

TextPad treats the first backslash as escaping the first double quote, so it treats it as not starting a string. It treats the second backslash as quoting the third backslash, leaving the second double quote unescaped, so it treats it as starting a string.

But you don't need the first backslash: just leave it out. Then TextPad will treat the text between the two double quotes as a string, but the string won't spill into what follows. (Also, /s means "." matches any character, including newline, and /i means match case-insensitively. Both of these are irrelevant in this regex replacement, so you can leave them out.)

Code: Select all

$str =~ s/"/\\"/g ;
If you can't avoid odd numbers of unescaped double quotes, you can put an extra one in a comment at the end of the line:

Code: Select all

$str =~ s/"//g ;  # "
(You can get backslashes into your posts by doubling them.)
vdawg
Posts: 19
Joined: Sun Oct 31, 2021 7:32 pm

Post by vdawg »

Thanks. My previous editor handled this with regard to syntax highlighting so I was hoping it was simply an issue that could be corrected. Unfortunately, that editor had other unrelated issues that made it unstable in recent versions, so here I am evaluating other editors...

Although removing the first backslash does resolve the issue in this case, that's only because the replacement pattern also happens to have a double quote in it. In a different instance I use a similar regex to *remove* the double quote character, so there isn't a second double quote in the replacement pattern to "reset" the syntax highlighting, and the rest of the code following the regex is remains incorrectly highlighted.

Yes, I can add a commented quote, but I'd really prefer not to have to write unnecessary code just to satisfy the editor. :(
Post Reply