Lars Magne Ingebrigtsen writes:
Jamie Zawinski <jwz(a)jwz.org> writes:
> I suppose a bound variable around re-search-forward, to indicate the
> parsing style, would be the way to go. That, or a separate function
> for new-style, like PHP does.
Or, perhaps, a new reader syntax for the new-style regexps. Like:
(re-search-forward #"(\s+)" nil t)
-1.
These are just strings that happen to be in regexp syntax, that we
then feed to a regexp engine. It would be really ugly to do
CHECK_STRING_OR_ALTERNATE_REGEXP_SYNTAX(object);
everywhere, and it would break old code that does CHECK_STRING. We
could redefine CHECK_STRING, I suppose, but ... no, this is too hacky.
Besides, what do we do when we decide to introduce Unicode-conforming
regexps or XML regexps? More syntax, more object types?
AFAICS, Jamie's basic request for more elegant input can be satisfied
with a straightforward (though the corner cases are likely to be
tedious) string translation, which emulates PCRE syntax, and adds some
new operators to handle Emacs extensions like matching charsets and
Mule character classes.
I wouldn't be surprised if the needed functions already exist in
somebody's "regexp explorer" library.
Then you can write
(re-search-forward (pcre-to-emacs-re #r"\bstr(i|o)ng\b") ...)
to get the effect of
(re-search-forward "\\bstr\\(i\\|o\\)ng\\b" ...)
and
(defun re-search-forward-command (regexp)
(interactive "s")
(when re-search-commands-use-pcre
(setq regexp (pcre-to-emacs-re regexp)))
(re-search-forward regexp))
or
(defun re-search-forward-command (regexp)
(interactive "s")
(re-search-forward (cond ((eq re-search-command-syntax 'pcre)
(pcre-to-emacs-re regexp))
;; add new syntaxes here
((null re-search-command-syntax)
regexp)
(t (error 'invalid-state
"not a known regexp syntax"
re-search-command-syntax)))))
Finally, pcre-to-emacs-re can be a compiler macro that pretranslates
the regexp, so that there's no run-time cost for literal regexps in
compiled code. As long as you don't build up regexps in a non-
syntactic way, you can do this for computed regexps, too.
_______________________________________________
XEmacs-Beta mailing list
XEmacs-Beta(a)xemacs.org
http://lists.xemacs.org/mailman/listinfo/xemacs-beta