Hello dear llist,
writing an interactive Lisp-editor I added a parenthesis matcher. I found a nice solution in Qt Quarterly issue 31/2009 at
http://doc.trolltech.com/qq/
Works well basically, but has a minor bug somewhere. Marking a left parenthesis always marks the corresponding right parenthesis correctly.
But vice versa - so marking a right parenthesis - won't work in some cases. Examples how marking the last right parenthesis will behave:
;; fails
(defun primep
((= 0 (mod x 2)) nil)
)
;; works
(defun primep
((= 0 (mod x 2)) nil))
;; works
(defun nextprime (x)
(loop for i from x
when (primep i) return i))
;; fails
(defun primep (x)
(cond
((not (integerp x)) nil)
((< x 2) nil)
((= x 2) 2)
((= 0 (mod x 2)) nil)
(t (loop for i from 3 to (isqrt x) by 2
never (= 0 (mod x i))))))
;; fails
(defun infix2prefix (infix-expression)
(list (second infix-expression)
(first infix-expression)
(third infix-expression)))
To try it out, paste it in the example code, which could be found at the link mentioned above or as attachment.
To give you a more detailed pointer and to spare your time: The hot spot is near the end of "textedit.cpp", where you find the three functions:
matchParentheses()
matchLeftParenthesis(...
matchRightParenthesis(...
matchLeftParenthesis always works regardless how the code is formatted or how parenthesis are nested. matchRightParenthesis will work mostly, but not in every case. Interestingly matchRightParenthesis is only a mirror image from the left-function. So one would expect, that it will work as well as the left counterpart.
Tried out a lot, but I am running out of ideas. Hope that some more clever people than I could give me a pointer.
Kind regards, Heinz
Bookmarks