Subject: Re: bug#61436: Emacs Freezing With Java Files

From: Alan Mackenzie <[email-masked]>
Date: Wed, 17 Apr 2024 18:50:33 +0000

Hello again, Bob.

On Wed, Apr 17, 2024 at 13:06:50 +0000, Alan Mackenzie wrote:
> On Tue, Apr 16, 2024 at 21:35:59 -0400, Robert Weiner wrote:
> >    Hi Alan:
> >    I just re-read this whole thread and realized you resolved the problem
> >    for the Java defun-prompt-regexp but not the C++
> >    defun-prompt-regexpÂ in Hyperbole'sÂ hui-select.el:L404 (probably were
> >    just tired after all of that).
> >    Today, someone else reported that the C++ regexpÂ was hanging their
> >    Emacs.Â  Do you think you could pick this back up and rework the C++
> >    regexpÂ as you did the Java one?Â  It would be a big help; otherwise, I
> >    think we'll just have to disable that functionality in Hyperbole.
> >    Best regards,
> >    Bob
> >    On Sun, Oct 22, 2023 at 1:18â¯PM Mats Lidell
> >    <[1][email-masked]> wrote:

> Yes, I'll happily finish off that C++ regexp.  I made considerable
> progress with it back in October, getting smething basically working but
> with some rough edges.  One problem is that the regexp was ~1600
> characters long.  I don't know if this might make the program slow -
> possibly not.

> I've found the .el file I was working in, and located my notes from
> October.  It's going to take longer than a day or two, but hopefully
> less than a week or two.

It was rather easier than I'd anticipated.  There is my first attempt
below.  It should find most C++ defun starts, but not all.  In particlar
it won't recognise one with nested parens or nested template delimiters;
regexps cannot handle arbitrary nesting,and it didn't seem worth the
trouble to code in a small bounded degree of nesting, though this surely
could be done if I'm wrong, here.

The regexp is not small.  At the latest count it was 2,223 characters
long.  I hope this won't affect performance too much.

Please try out this regexp, and let me know how well it's working.
Thanks!

> [ .... ]

(defconst c++-defun-prompt-regexp
  (let*
      ((space* "[ \t\n\r\f]*")
       (space+ "[ \t\n\r\f]+")
       (ad-hoc-requires-clause
	(concat "\\(?:requires" space* "[][()<> \t\n\r\f_$a-zA-Z0-9&|\"'+=.,*:~-]+" space* "\\)?"))
       (id (concat "[_$~a-zA-Z][_$a-zA-Z0-9]*")
	   ;; (concat "\\(\\(~" space* "\\)?" "\\([_$a-zA-Z][_$a-zA-Z0-9]*\\)\\)")
	   )
       (template-brackets "\\(?:<[^;{}]*>\\)")
       (id-<> (concat id "\\(?:" space* template-brackets "\\)?"))
       (id-:: (concat id-<> "\\(?:" space* "::" space* id-<> "\\)*"))
       (paren-exp "([^{};]*)")
       (template-exp\? (concat "\\(?:template" space* template-brackets space* "\\)?"))
       (type-prefix-modifier* (concat "\\(?:\\(?:"
				      "\\(?:\\<extern" space+ "\"[^\"]+\"\\)"
				      "\\|"
				      (regexp-opt '("auto" "const" "explicit" "extern"
						    "friend" "inline" "mutable"
						    "noexcept" "overload"
						    "register" "static" "typedef"
						    "virtual" "volatile")
						  'words)
				      "\\)"
				      space+
				      "\\)*"))
       (type-exp (concat
		  "\\(?:\\(?:" template-brackets space* "\\)?"
		  type-prefix-modifier*
		  "\\(?:\\(?:decltype" space* paren-exp space* "\\)"
		  "\\|"
		  "\\(?:"
		  "\\(?:class\\|enum\\|struct\\|typename\\|union\\)"
		  "\\(?:" space* "\\.\\.\\.\\)?\\)"
		  space+ id space*
		  "\\(?::" id-:: space* "\\)?"
		  "\\|"
		  id-:: space*
		  "\\)"
		  "\\)\\{1,2\\}"))
       (type-mid-modifier* (concat "\\(?:"
				   (regexp-opt
				    '("auto" "consteval" "constexpr"
				      "constinit" "explicit"
				      "extern" "friend" "inline"
				      "mutable" "noexcept" "register"
				      "static" "template"
				      "thread_local" "throw"
				      "virtual" "volatile")
				    'words)
				   space+ "\\)*"))
       (operator-exp (concat "\\(?:operator\\>" space*
			     "\\(?:[][a-z_+*/%^?&|!~<>,:=-]+"
			     "\\|()\\|\"\""
			     "\\)" space*
			     "\\)"))

       (name-exp			; matches foo or (* foo), etc.
	(concat "\\(?:(" space* "[*&]+" space* id-:: space* "[][()]*" ")"
		"\\|\\(?:[*&]+" space* "\\)?" id-::
		"\\)" space*))
       (type-suffix-modifier* (concat "\\(?:"
				      (regexp-opt
				       '("auto" "const" "noexcept"
					 "requires" "throw" "volatile")
				       'words)
				      space+ "\\)*"))
       (post-paren-modifier* (concat "\\(?:"
				     (regexp-opt
				      '("const" "final" "override"
					"mutable")
				      'words)
				     space* "\\)*")))

    (concat template-exp\?
	    "\\(?:" ad-hoc-requires-clause "\\)?"
	    type-exp
	    type-mid-modifier*
	    "\\(?:" operator-exp "\\|" name-exp "\\)"
	    type-suffix-modifier*
	    paren-exp space*
	    "\\(?:->" space* type-exp "\\)?"
	    post-paren-modifier*
	    "{"))

-- 
Alan Mackenzie (Nuremberg, Germany).