Re: hilit19 for Python; can handle unbalanced quotes in comments!

Michael Scharf (scharf@EMBL-Heidelberg.DE)
09 Jun 1994 00:27:03 GMT

Sorry, I detected a little bug :-(. I forgot the the following cases:

"... can't ..."
'"'

This one has no problems with:

# can't ....
string="...'..."
string='"'
# another unmatched quote "
string="""
...."""
string="\""
string="\\"
string="\\\""
string="\"\\\\"
string="\
"
string=''

However here the second string is not recognized

string="#" + "..."

Michael

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun hilit-python-string-find (qregex)
"looks for a string and returns (start . end) or NIL.
Finds strings delimited by QREGEX. QREGEX should match one character. It
handles the case correct when a unmatched QREGEX appears in a comment or a
string. The first QREGEX may not be preceded by a backslash and the closing
QREGEX may not be preceded by an odd number of backslashes."
(let (st en pp endchar)
(while (and (search-forward-regexp qregex nil t)
(or (save-excursion
(setq st (match-beginning 0))
;; remember the matching char - we'll search for it
(setq endchar (char-to-string (char-after st)))
(goto-char (point))
;; look for the line of the prevoius comment
(save-excursion
(skip-chars-backward "^#")
(skip-chars-backward "^\n")
(setq pp (point)))
;; in which line are we?
(skip-chars-backward "^\n")
;; is the prevoius comment in the same line as we are?
(eq pp (point)))
(eq ?\\ (char-after (1- st))))
))
(cond (endchar
(while (and (search-forward endchar nil t)
(save-excursion
(setq en (point))
(forward-char -1)
(skip-chars-backward "\\\\")
(forward-char 1)
(not (zerop (% (- en (point)) 2))))))))
(and en (cons st en))))

(hilit-set-mode-patterns
'python-mode
'(
(hilit-python-string-find "[\"']" string)
("\\s #.*$" nil comment)
("^#.*$" nil comment)
("\\<import\\>[A-Za-z_*, \t]+" nil include)
("\\<from[ \t]+\\w+" nil include)
("^[ \t]*\\(def\\|class\\)\\>[^:]*" nil defun)
("\\<\\(and\\|del\\|for\\|in\\|print\\|break\\|elif\\|is\\|raise\\|else\\|global\\|not\\|return\\|continue\\|except\\|if\\|or\\|try\\|finally\\|pass\\|while\\)\\>" nil keyword)
)
nil 'case-insensitive)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

--
         __________________________________________________________
   ****    ___   _  _   ___   _    
  ******  | __) | \/ | |   ) | |   Michael Scharf
 ******** | _)  |    | | -<  | |_  Tel: +49 6221 387 305 Fax: 517
  * ****  |___) |_||_| |___) |___) EMail: scharf@EMBL-Heidelberg.de
   ****   __________________________________________________________