aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias EngdegÄrd2019-10-27 21:39:58 +0100
committerMattias EngdegÄrd2019-10-28 17:38:33 +0100
commit2aed0430c7cbcab793782c6e24623f9a0a23fafa (patch)
tree12ede4a58c16fd238931697346aa50d4f95c1a2a
parent0c3e3a82aa1e8339ad59eec833d2a4d93429d8d5 (diff)
downloademacs-2aed0430c7cbcab793782c6e24623f9a0a23fafa.tar.gz
emacs-2aed0430c7cbcab793782c6e24623f9a0a23fafa.zip
Use new-style rx extensions in python.el
* lisp/progmodes/python.el (python-rx): Use `rx-let' instead of `rx-constituents'. This allows for some slight redundancy reduction, since `rx-let' definitions are expanded inside `not' (bug#37849). Reorder some `or' forms for more efficient matching.
-rw-r--r--lisp/progmodes/python.el127
1 files changed, 59 insertions, 68 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 634c297957d..bdc0f1cd96f 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -388,80 +388,71 @@ It returns a file name which can be used directly as argument of
388 388
389;;; Python specialized rx 389;;; Python specialized rx
390 390
391(eval-and-compile 391(defmacro python-rx (&rest regexps)
392 (defconst python-rx-constituents 392 "Python mode specialized rx macro.
393 `((block-start . ,(rx symbol-start 393This variant of `rx' supports common Python named REGEXPS."
394 (or "def" "class" "if" "elif" "else" "try" 394 `(rx-let ((block-start (seq symbol-start
395 "except" "finally" "for" "while" "with" 395 (or "def" "class" "if" "elif" "else" "try"
396 ;; Python 3.5+ PEP492 396 "except" "finally" "for" "while" "with"
397 (and "async" (+ space) 397 ;; Python 3.5+ PEP492
398 (or "def" "for" "with"))) 398 (and "async" (+ space)
399 symbol-end)) 399 (or "def" "for" "with")))
400 (dedenter . ,(rx symbol-start 400 symbol-end))
401 (or "elif" "else" "except" "finally") 401 (dedenter (seq symbol-start
402 symbol-end)) 402 (or "elif" "else" "except" "finally")
403 (block-ender . ,(rx symbol-start 403 symbol-end))
404 (or 404 (block-ender (seq symbol-start
405 "break" "continue" "pass" "raise" "return") 405 (or
406 symbol-end)) 406 "break" "continue" "pass" "raise" "return")
407 (decorator . ,(rx line-start (* space) ?@ (any letter ?_) 407 symbol-end))
408 (* (any word ?_)))) 408 (decorator (seq line-start (* space) ?@ (any letter ?_)
409 (defun . ,(rx symbol-start 409 (* (any word ?_))))
410 (or "def" "class" 410 (defun (seq symbol-start
411 ;; Python 3.5+ PEP492 411 (or "def" "class"
412 (and "async" (+ space) "def")) 412 ;; Python 3.5+ PEP492
413 symbol-end)) 413 (and "async" (+ space) "def"))
414 (if-name-main . ,(rx line-start "if" (+ space) "__name__" 414 symbol-end))
415 (+ space) "==" (+ space) 415 (if-name-main (seq line-start "if" (+ space) "__name__"
416 (any ?' ?\") "__main__" (any ?' ?\") 416 (+ space) "==" (+ space)
417 (* space) ?:)) 417 (any ?' ?\") "__main__" (any ?' ?\")
418 (symbol-name . ,(rx (any letter ?_) (* (any word ?_)))) 418 (* space) ?:))
419 (open-paren . ,(rx (or "{" "[" "("))) 419 (symbol-name (seq (any letter ?_) (* (any word ?_))))
420 (close-paren . ,(rx (or "}" "]" ")"))) 420 (open-paren (or "{" "[" "("))
421 (simple-operator . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))) 421 (close-paren (or "}" "]" ")"))
422 ;; FIXME: rx should support (not simple-operator). 422 (simple-operator (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%))
423 (not-simple-operator . ,(rx 423 (not-simple-operator (not simple-operator))
424 (not 424 (operator (or "==" ">=" "is" "not"
425 (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))) 425 "**" "//" "<<" ">>" "<=" "!="
426 ;; FIXME: Use regexp-opt. 426 "+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
427 (operator . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">" 427 "=" "%"))
428 "=" "%" "**" "//" "<<" ">>" "<=" "!=" 428 (assignment-operator (or "+=" "-=" "*=" "/=" "//=" "%=" "**="
429 "==" ">=" "is" "not"))) 429 ">>=" "<<=" "&=" "^=" "|="
430 ;; FIXME: Use regexp-opt. 430 "="))
431 (assignment-operator . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**=" 431 (string-delimiter (seq
432 ">>=" "<<=" "&=" "^=" "|=")))
433 (string-delimiter . ,(rx (and
434 ;; Match even number of backslashes. 432 ;; Match even number of backslashes.
435 (or (not (any ?\\ ?\' ?\")) point 433 (or (not (any ?\\ ?\' ?\")) point
436 ;; Quotes might be preceded by an escaped quote. 434 ;; Quotes might be preceded by an
435 ;; escaped quote.
437 (and (or (not (any ?\\)) point) ?\\ 436 (and (or (not (any ?\\)) point) ?\\
438 (* ?\\ ?\\) (any ?\' ?\"))) 437 (* ?\\ ?\\) (any ?\' ?\")))
439 (* ?\\ ?\\) 438 (* ?\\ ?\\)
440 ;; Match single or triple quotes of any kind. 439 ;; Match single or triple quotes of any kind.
441 (group (or "\"\"\"" "\"" "'''" "'"))))) 440 (group (or "\"\"\"" "\"" "'''" "'"))))
442 (coding-cookie . ,(rx line-start ?# (* space) 441 (coding-cookie (seq line-start ?# (* space)
443 (or 442 (or
444 ;; # coding=<encoding name> 443 ;; # coding=<encoding name>
445 (: "coding" (or ?: ?=) (* space) (group-n 1 (+ (or word ?-)))) 444 (: "coding" (or ?: ?=) (* space)
446 ;; # -*- coding: <encoding name> -*- 445 (group-n 1 (+ (or word ?-))))
447 (: "-*-" (* space) "coding:" (* space) 446 ;; # -*- coding: <encoding name> -*-
448 (group-n 1 (+ (or word ?-))) (* space) "-*-") 447 (: "-*-" (* space) "coding:" (* space)
449 ;; # vim: set fileencoding=<encoding name> : 448 (group-n 1 (+ (or word ?-)))
450 (: "vim:" (* space) "set" (+ space) 449 (* space) "-*-")
451 "fileencoding" (* space) ?= (* space) 450 ;; # vim: set fileencoding=<encoding name> :
452 (group-n 1 (+ (or word ?-))) (* space) ":"))))) 451 (: "vim:" (* space) "set" (+ space)
453 "Additional Python specific sexps for `python-rx'") 452 "fileencoding" (* space) ?= (* space)
454 453 (group-n 1 (+ (or word ?-)))
455 (defmacro python-rx (&rest regexps) 454 (* space) ":")))))
456 "Python mode specialized rx macro. 455 (rx ,@regexps)))
457This variant of `rx' supports common Python named REGEXPS."
458 (let ((rx-constituents (append python-rx-constituents rx-constituents)))
459 (cond ((null regexps)
460 (error "No regexp"))
461 ((cdr regexps)
462 (rx-to-string `(and ,@regexps) t))
463 (t
464 (rx-to-string (car regexps) t))))))
465 456
466 457
467;;; Font-lock and syntax 458;;; Font-lock and syntax