Hello all,

I just want to throw this idea out there as a potential solution to the
problems I am having with the PHP syntax.  It would be helpful to be able to
name sequential 'nextgroups' for regions and matches, so that the syntax will
highlight an area using certain groups in specific order. I.e:

For example, the PHP function preg_replace(), takes 3 arguments like this:

  /* print 'Goodbye <name>' instead of 'Hello <name>' */
  $text = 'Hello Peter';
  print preg_replace('/Hello (\w+)/', "Goodbye \1", $text);

Preg_replace works just like Vim's substitute() function, except preg_replace
takes the subject as the last argument rather than the first.  There is
actually a major bug in that code sample which A) most people would not notice;
and B) the highlighting in most text editors would only confuse people even
more (I will explain further down).

For preg_replace, the first argument, '/Hello (\w+)/' is a perl-style regular
expression; the second argument is the replacement pattern which may contain
backreferences, and the third argument can be any variable, expression, etc.

Currently I am finding and highlighting the regular expression string like
this:

  syntax keyword pregFunction preg_replace nextgroup=pregOpenParent
  syntax match pregOpenParent /(/ nextgroup=pregString
  syntatx region pregString start=/'/ end=/'/ skip=...

I have a couple of problems with this; first of all, pregOpenParent matches a
lone opening '(', which means the parenthesis errors syntax items (which match
( and ) together to spot errors) will find another ')' seemingly all by itself
at the end of the call to preg_replace() and highlight it as an error.  I would
like to turn pregOpenParent into a region to take care of the closing ')', but
then how do I specify that the first argument (and only the first) to that
function call is a string with a PCRE pattern in it?

Also very important is that I am able to highlight the 2nd argument to
preg_replace using a specific group for Preg replacement strings, because
(!major bug explanation!) in the string "Goodbye \1", most people will see the
\1 and think 'backreference', and in fact the best text editors around might
highlight the \1 in a separate color from the rest of the string, and then most
would think 'definitely backreference!', when really it's octal (\x01) and not
a backreference at all.  So for the 2nd argument to preg-replace, I would like
to be able to highlight "\1" as an Error so that people might think "What's up
with that?" and hopefully they will work out that they need to use '\1' or
"\\1" instead; at any rate it would be obvious to them that if the
backreference doesn't seem to be working, the "\1" is definitely the problem
and they have a good idea where to start investigating.

My hope is that the new highlighting will save people from wasting hours on
PCRE regular expressions; even though I was fairly good with Vim REs when I
started using PHP's preg functions, a single line of code containing a
15-character preg RE would usually take me 10 or 15 minutes to write correctly
because A) I couldn't remember what needed to have a '\' before it and what
didn't, and B) if I had a misplaced '\', the syntax highlighting would not help
me to find it and I had to fiddle with the pattern endlessly to get it working.

So in order to make an effective PHP syntax, I need to be able to do something
like this in Vim:

  syntax keyword preg_replace nextgroup=pregReplaceParents
  syntax region pregReplaceParents matchgroup=Delimiter start=/(/ end=/)/
    \ first=pregPattern,phpIdentifier
    \ second=phpComma
    \ third=pregReplacement,phpIdentifier
    \ fourth=phpComma
    \ fifth=phpIdentifier,phpString

... so that inside the pregReplaceParents region, Vim would first try and match
a 'pregPattern' or a 'phpIdentifier', followed by a comma, followed by a
'pregReplacement' or a 'phpIdentifier', followed by a comma, followed by a
phpIdentifier or phpString.

An alternative to this which isn't as tidy but might be simpler to implement,
and might be useful elsewhere, is to allow the 'nextgroup' on a syntax cluster:

  syntax keyword preg_replace nextgroup=pregReplaceParents
  syntax region pregReplaceParents matchgroup=Delimiter start=/(/ end=/)/
    \ [EMAIL PROTECTED]
  syntax cluster pregReplaceFirst add=pregPattern,phpIdentifier
[EMAIL PROTECTED]
  syntax cluster pregReplaceSecond add=phpComma [EMAIL PROTECTED]
  syntax cluster pregReplaceThird add=pregReplacement,phpIdentifier
[EMAIL PROTECTED]
  syntax cluster pregReplaceFourth add=phpComma [EMAIL PROTECTED]
  syntax cluster pregReplaceFifth add=phpIdentifier,phpString

*end of suggestions*
==================================

Does anyone else think that one or both of these features might be useful?

Are there any specific plans for the future of Vim's highlighting?

Is there any possibility of these Feature Requests becoming reality, even if I
have to finish learning C and code them myself?  I would really like to push
the PHP syntax ahead so that it can highlight as many features and errors as
possible and Vim can remain one of the most productive editors for PHP, but it
seems that what I want to do is slightly beyond the capabilities of the syntax
commands.

regards,
Peter


Send instant messages to your online friends http://au.messenger.yahoo.com 

Reply via email to