Skip to content

Commit 54d7c6f

Browse files
committed
Refactor php-create-regexp-for-method using rx package
Before the change leaks without matching the method.
1 parent 3367e28 commit 54d7c6f

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

php.el

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,9 @@ it is the character that will terminate the string, or t if the string should be
163163
"^\\s-*\\(?:\\(?:abstract\\|final\\|private\\|protected\\|public\\|static\\)\\s-+\\)*function\\s-+&?\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*("
164164
"Regular expression for a PHP function.")
165165

166-
(defun php-create-regexp-for-method (visibility)
167-
"Make a regular expression for methods with the given VISIBILITY.
166+
(eval-when-compile
167+
(defun php-create-regexp-for-method (&optional visibility)
168+
"Make a regular expression for methods with the given VISIBILITY.
168169
169170
VISIBILITY must be a string that names the visibility for a PHP
170171
method, e.g. 'public'. The parameter VISIBILITY can itself also
@@ -174,16 +175,24 @@ The regular expression this function returns will check for other
174175
keywords that can appear in method signatures, e.g. 'final' and
175176
'static'. The regular expression will have one capture group
176177
which will be the name of the method."
177-
(concat
178-
;; Initial space with possible 'abstract' or 'final' keywords
179-
"^\\s-*\\(?:\\(?:abstract\\|final\\)\\s-+\\)?"
180-
;; 'static' keyword may come either before or after visibility
181-
"\\(?:" visibility "\\(?:\\s-+static\\)?\\|\\(?:static\\s-+\\)?" visibility "\\)\\s-+"
182-
;; Make sure 'function' comes next with some space after
183-
"function\\s-+"
184-
;; Capture the name as the first group and the regexp and make sure
185-
;; by the end we see the opening parenthesis for the parameters.
186-
"\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*("))
178+
(rx-form `(: line-start
179+
(* (syntax whitespace))
180+
,@(if visibility
181+
`((* (or "abstract" "final" "static")
182+
(+ (syntax whitespace)))
183+
(or ,@visibility)
184+
(+ (syntax whitespace))
185+
(* (or "abstract" "final" "static")
186+
(+ (syntax whitespace))))
187+
'((* (* (or "abstract" "final" "static"
188+
"private" "protected" "public"))
189+
(+ (syntax whitespace)))))
190+
"function"
191+
(+ (syntax whitespace))
192+
(group (+ (or (syntax word) (syntax symbol))))
193+
(* (syntax whitespace))
194+
"(")))
195+
)
187196

188197
(defun php-create-regexp-for-classlike (type)
189198
"Accepts a `TYPE' of a 'classlike' object as a string, such as

0 commit comments

Comments
 (0)