emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

rx: Adding custom sexps to work with.


From: Fabian Ezequiel Gallina
Subject: rx: Adding custom sexps to work with.
Date: Sun, 3 Oct 2010 07:49:46 -0300

Hello all,

I'm working on a new python.el and I'm using rx in most places where a
regexp is needed. I wanted to add some python specific named regexps
to rx-constituents so this way I'm able to write cleaner regular
expressions using the rx macro. The thing is that rx-constituents is a
constant and I'm not sure how to achieve what I'm looking for.

Here[0] I found this approach:

 (defmacro rx-extra (&rest body-forms)
   (let ((add-ins (list
                   `(file . ,(rx (+ (or alnum digit "." "/" "-" "_"))))
                   `(ws0 . ,(rx (0+ (any " " "\t"))))
                   `(ws+ . ,(rx (+ (any " " "\t"))))
                   `(int . ,(rx (+ digit))))))
     `(let ((rx-constituents (append ',add-ins rx-constituents)))
        ,@body-forms)))

And this is how I implemented it:

(defconst python-rx-constituents
  (list
   `(block-start          . ,(rx word-start
                               (or "def" "class" "if" "elif" "else" "try"
                                   "except" "finally" "for" "while" "with")
                               word-end))
   `(defun                . ,(rx word-start (or "def" "class") word-end))
   `(open-paren           . ,(rx (or "{" "[" "(")))
   `(close-paren          . ,(rx (or "}" "]" ")")))
   `(simple-operator      . ,(rx (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?< ?> ?= ?%)))
   `(not-simple-operator  . ,(rx (not (any ?+ ?- ?/ ?& ?^ ?~ ?| ?* ?<
?> ?= ?%))))
   `(operator             . ,(rx (or "+" "-" "/" "&" "^" "~" "|" "*" "<" ">"
                                     "=" "%" "**" "//" "<<" ">>" "<=" "!="
                                     "==" ">=" "is" "not")))
   `(assignment-operator  . ,(rx (or "=" "+=" "-=" "*=" "/=" "//=" "%=" "**="
                                     ">>=" "<<=" "&=" "^=" "|=")))))

(defmacro python-rx (&rest body-forms)
  "Python mode especialized rx macro which supports common python
named regexps."
  `(let ((rx-constituents (append ',python-rx-constituents rx-constituents)))
       (rx ,@body-forms)))

Which does the job and works but when I try to byte-compile the file,
it fails with the following message:

python.el:266:16:Error: Unknown rx form `assignment-operator'

Here's how that line reads:

        (let ((re (python-rx (group (+ (any word ?. ?_))) (* space)
                             assignment-operator)))

So here is the question: how can I extend rx supported sexps while
avoiding the bytecompiler to complain about it?



[0] http://www.emacswiki.org/emacs/rx

Thanks in advance,
Fabián E. Gallina



reply via email to

[Prev in Thread] Current Thread [Next in Thread]