You are here: PSPad forum > Bug report / Hlášení chyb > Re: PHP Syntax highlighting error

Re: PHP Syntax highlighting error

Goto Page: Previous1 2

#11 Re: PHP Syntax highlighting error

Posted by: pspad | Date: 2015-07-11 11:58 | IP: IP Logged

html multihighlighter switches highlighters using regular expressions. expression for start and end of the script. give me correct expressions which will cover all alternatives.

Options: Reply | Quote | Up ^


#12 Re: PHP Syntax highlighting error

Posted by: viplex | Date: 2015-07-11 22:09 | IP: IP Logged

Regular expressions are bound to type 3 grammar (regular gramar) which makes it difficult to cover this topic, where the number of opening and closed tags and comments needs to be considered. PHP and a lot of other programming languages are of type 2 in chomsky hierarchy.

Maybe we could simplify it and disable syntax highlighting switch for <?php or <?= tags which are inside of comments.

I've attached a simple example of my idea ( regex101.com), but probably it's not worth the work to change that in pspad.

I'll try to find out how this is done in other editors.

Edit:
Maybe it's easier to iterate over the document and count the occurences of those special tags (like a pushdown automaton would). I'm not sure about performance in that case.

Edited 2 time(s). Last edit at 2015-07-11 22:19 by viplex.

Options: Reply | Quote | Up ^


#13 Re: PHP Syntax highlighting error

Posted by: pspad | Date: 2015-07-12 02:05 | IP: IP Logged

Syntax switching is doe before any code parsing. I dont know ifthere is any comment or something else. Its architecture of used editor component

You solved problem in case of PHP in comment, but not other cases - PHP starts in string, starts inside another script...

I tried it in the past but I was unsuccessful

Similar situation is with PHP script close tag.

Edited 1 time(s). Last edit at 2015-07-12 07:43 by pspad.

Options: Reply | Quote | Up ^


#14 Re: PHP Syntax highlighting error

Posted by: viplex | Date: 2015-07-20 12:30 | IP: IP Logged

Hi,
yes - you were unsuccessful because it's impossible.
PHP is a type 2 language and regex can only match type 3/regular languages.

Options: Reply | Quote | Up ^


#15 Re: PHP Syntax highlighting error

Posted by: Wirek | Date: 2015-07-31 15:43 | IP: IP Logged

The ?> 2 char sequence may also appear in a single or double quoted strings as for example a part of regular expression and it doesn't stand for PHP closing tag at all.
Here is an example as PHP code fragment:
<?php
/* ...some code above: (X)HTML, JavaScript, CSS... */
switch($format){
case 14:
return preg_replace('/^(\d{4})\D(\d{2})\D(\d{2})(?>\D+)(\d{2})\D(\d{2})\D(\d{2}).*$/', '\3-\2-\1 \4:\5:\6', $str);
break;
case 13:
return preg_replace('/^(\d{4})\D(\d{2})\D(\d{2})(?>\D+)(\d{2})\D(\d{2})\D(\d{2}).*$/', '\3.\2.\1 \4:\5:\6', $str);
break;
case 12:
return preg_replace('/^(\d{4})\D(\d{2})\D(\d{2})(?>\D+)(\d{2})\D(\d{2})\D(\d{2}).*$/', '\1.\2.\3 \4:\5:\6', $str);
break;
case 11: // 1 i 0 daja to samo
case 10:
return preg_replace('/^(\d{4})\D(\d{2})\D(\d{2})(?>\D+)(\d{2})\D(\d{2})\D(\d{2}).*$/', '\1-\2-\3 \4:\5:\6', $str);
break;
case 4:
return preg_replace('/^(\d{4})\D(\d{2})\D(\d{2}).*$/', '\3-\2-\1', $str);
break;
case 3:
return preg_replace('/^(\d{4})\D(\d{2})\D(\d{2}).*$/', '\3.\2.\1', $str);
break;
case 2:
return preg_replace('/^(\d{4})\D(\d{2})\D(\d{2}).*$/', '\1.\2.\3', $str);
break;
default: // 1 i 0 daja to samo
return preg_replace('/^(\d{4})\D(\d{2})\D(\d{2}).*$/', '\1-\2-\3', $str);
}
/* ...some code below: (X)HTML, JavaScript, CSS... */
?>

As you can see the '(?>\D+)' string fragments stand for "Once-only subpatterns" (read more: php.net) and "multihighlighter" for PHP files breaks on line #5 (case 14, first return value).

Dear pspad, since it isn't a trivial issue then maybe you should contact with the author of the original regular expressions utilised by the PHP multihighlighter in your editor and ask him/her for some help in the development of the expressions - the author should be well oriented in his/her own piece of code.

There are ways of dealing with "Recursive patterns" (read more: php.net) but I've never tried them. I wish you good luck! ...because now I have to break '?>' 2 char sequences into something similar to joining 2 separate strings like '?'.'>' to make it work around (which makes my code even less readable especially when various "Once-only subpatterns" are repeated several times in a single, quite long regular expression).

Options: Reply | Quote | Up ^


#16 Re: PHP Syntax highlighting error

Posted by: carbonize | Date: 2015-08-01 16:13 | IP: IP Logged

As has been said before switch to just PHP highlighting and not multihighlighting.

--
Carbonize

Options: Reply | Quote | Up ^


#17 Re: PHP Syntax highlighting error

Posted by: Wirek | Date: 2015-08-17 07:22 | IP: IP Logged

As it has been already said earlier: "...PHP files may also contain other (than PHP per se) source codes like: (X)HTML, JavaScript, CSS...".
PHP highlighting instead of multihighlighting is just not enough in these cases - it is simply useless.

Options: Reply | Quote | Up ^


#18 Re: PHP Syntax highlighting error

Posted by: carbonize | Date: 2015-08-17 08:56 | IP: IP Logged

When some of us started coding there were no such things as text editors with syntax highlighting. We used Notepad and had to look for errors the long way. Be grateful for what you have or you have two choices.

One - Tell Jan a better regex for the detection
Two - Use a different editor

--
Carbonize

Options: Reply | Quote | Up ^


#19 Re: PHP Syntax highlighting error

Posted by: viplex | Date: 2015-08-28 12:08 | IP: IP Logged

Hi,
as I already said, matching this with a regex is impossible.
There is no solution for that. It's not very difficult or the author lazy, it just does not work. (If you want to know why, ask here or read about chomsky hierarchy.

You will need to switch to php highlighter or pick another editor.

Options: Reply | Quote | Up ^


Goto Page: Previous1 2





Editor PSPad - freeware editor, © 2001 - 2024 Jan Fiala, Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák, Privacy policy and GDPR