1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Reverse 2009-12-03T16:02:10Z!acm@muc.de "Enhance `c-parse-state' to run efficiently in

\"brace desserts\".".
This commit is contained in:
Alan Mackenzie 2010-04-12 18:58:52 +00:00
parent 7af4bf3aea
commit e42a330165
5 changed files with 305 additions and 1187 deletions

View file

@ -1145,117 +1145,23 @@ been put there by c-put-char-property. POINT remains unchanged."
(goto-char (point-max))))) (goto-char (point-max)))))
(defconst c-<-as-paren-syntax '(4 . ?>)) (defconst c-<-as-paren-syntax '(4 . ?>))
(put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
(defsubst c-mark-<-as-paren (pos) (defsubst c-mark-<-as-paren (pos)
;; Mark the "<" character at POS as a template opener using the ;; Mark the "<" character at POS as an sexp list opener using the
;; `syntax-table' property via the `category' property. ;; syntax-table property.
;; ;;
;; This function does a hidden buffer change. Note that we use ;; This function does a hidden buffer change.
;; indirection through the `category' text property. This allows us to (c-put-char-property pos 'syntax-table c-<-as-paren-syntax))
;; toggle the property in all template brackets simultaneously and
;; cheaply. We use this, for instance, in `c-parse-state'.
(c-put-char-property pos 'category 'c-<-as-paren-syntax))
(defconst c->-as-paren-syntax '(5 . ?<)) (defconst c->-as-paren-syntax '(5 . ?<))
(put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax)
(defsubst c-mark->-as-paren (pos) (defsubst c-mark->-as-paren (pos)
;; Mark the ">" character at POS as an sexp list closer using the ;; Mark the ">" character at POS as an sexp list closer using the
;; syntax-table property. ;; syntax-table property.
;; ;;
;; This function does a hidden buffer change. Note that we use ;; This function does a hidden buffer change.
;; indirection through the `category' text property. This allows us to (c-put-char-property pos 'syntax-table c->-as-paren-syntax))
;; toggle the property in all template brackets simultaneously and
;; cheaply. We use this, for instance, in `c-parse-state'.
(c-put-char-property pos 'category 'c->-as-paren-syntax))
(defsubst c-unmark-<->-as-paren (pos)
;; Unmark the "<" or "<" character at POS as an sexp list opener using
;; the syntax-table property indirectly through the `category' text
;; property.
;;
;; This function does a hidden buffer change. Note that we use
;; indirection through the `category' text property. This allows us to
;; toggle the property in all template brackets simultaneously and
;; cheaply. We use this, for instance, in `c-parse-state'.
(c-clear-char-property pos 'category))
(defsubst c-suppress-<->-as-parens ()
;; Suppress the syntactic effect of all marked < and > as parens. Note
;; that this effect is NOT buffer local. You should probably not use
;; this directly, but only through the macro
;; `c-with-<->-as-parens-suppressed'
(put 'c-<-as-paren-syntax 'syntax-table nil)
(put 'c->-as-paren-syntax 'syntax-table nil))
(defsubst c-restore-<->-as-parens ()
;; Restore the syntactic effect of all marked <s and >s as parens. This
;; has no effect on unmarked <s and >s
(put 'c-<-as-paren-syntax 'syntax-table c-<-as-paren-syntax)
(put 'c->-as-paren-syntax 'syntax-table c->-as-paren-syntax))
(defmacro c-with-<->-as-parens-suppressed (&rest forms)
;; Like progn, except that the paren property is suppressed on all
;; template brackets whilst they are running. This macro does a hidden
;; buffer change.
`(unwind-protect
(progn
(c-suppress-<->-as-parens)
,@forms)
(c-restore-<->-as-parens)))
;;;;;;;;;;;;;;;
(defconst c-cpp-delimiter '(14)) ; generic comment syntax
;; This is the value of the `category' text property placed on every #
;; which introduces a CPP construct and every EOL (or EOB, or character
;; preceding //, etc.) which terminates it. We can instantly "comment
;; out" all CPP constructs by giving `c-cpp-delimiter' a syntax-table
;; propery '(14) (generic comment delimiter).
(defmacro c-set-cpp-delimiters (beg end)
;; This macro does a hidden buffer change.
`(progn
(c-put-char-property ,beg 'category 'c-cpp-delimiter)
(if (< ,end (point-max))
(c-put-char-property ,end 'category 'c-cpp-delimiter))))
(defmacro c-clear-cpp-delimiters (beg end)
;; This macro does a hidden buffer change.
`(progn
(c-clear-char-property ,beg 'category)
(if (< ,end (point-max))
(c-clear-char-property ,end 'category))))
(defsubst c-comment-out-cpps ()
;; Render all preprocessor constructs syntactically commented out.
(put 'c-cpp-delimiter 'syntax-table c-cpp-delimiter))
(defsubst c-uncomment-out-cpps ()
;; Restore the syntactic visibility of preprocessor constructs.
(put 'c-cpp-delimiter 'syntax-table nil))
(defmacro c-with-cpps-commented-out (&rest forms)
;; Execute FORMS... whilst the syntactic effect of all characters in
;; all CPP regions is suppressed. In particular, this is to suppress
;; the syntactic significance of parens/braces/brackets to functions
;; such as `scan-lists' and `parse-partial-sexp'.
`(unwind-protect
(c-save-buffer-state ()
(c-comment-out-cpps)
,@forms)
(c-save-buffer-state ()
(c-uncomment-out-cpps))))
(defmacro c-with-all-but-one-cpps-commented-out (beg end &rest forms)
;; Execute FORMS... whilst the syntactic effect of all characters in
;; every CPP region APART FROM THE ONE BETWEEN BEG and END is
;; suppressed.
`(unwind-protect
(c-save-buffer-state ()
(c-clear-cpp-delimiters ,beg ,end)
,`(c-with-cpps-commented-out ,@forms))
(c-save-buffer-state ()
(c-set-cpp-delimiters ,beg ,end))))
(defsubst c-intersect-lists (list alist) (defsubst c-intersect-lists (list alist)
;; return the element of ALIST that matches the first element found ;; return the element of ALIST that matches the first element found
;; in LIST. Uses assq. ;; in LIST. Uses assq.

File diff suppressed because it is too large Load diff

View file

@ -426,8 +426,7 @@ stuff. Used on level 1 and higher."
(progn (progn
(c-mark-<-as-paren beg) (c-mark-<-as-paren beg)
(c-mark->-as-paren end)) (c-mark->-as-paren end))
;; (c-clear-char-property beg 'syntax-table) (c-clear-char-property beg 'syntax-table)))
(c-clear-char-property beg 'category)))
nil))))))) nil)))))))
;; #define. ;; #define.

View file

@ -461,7 +461,7 @@ The function is called even when font locking is disabled.
When the mode is initialized, this function is called with When the mode is initialized, this function is called with
parameters \(point-min), \(point-max) and <buffer size>." parameters \(point-min), \(point-max) and <buffer size>."
t nil t nil
(c c++ objc) 'c-neutralize-syntax-in-and-mark-CPP (c c++ objc) 'c-extend-and-neutralize-syntax-in-CPP
awk 'c-awk-extend-and-syntax-tablify-region) awk 'c-awk-extend-and-syntax-tablify-region)
(c-lang-defvar c-before-font-lock-function (c-lang-defvar c-before-font-lock-function
(c-lang-const c-before-font-lock-function)) (c-lang-const c-before-font-lock-function))

View file

@ -410,7 +410,7 @@ preferably use the `c-mode-menu' language constant directly."
;; temporary changes in some font lock support modes, causing extra ;; temporary changes in some font lock support modes, causing extra
;; unnecessary work and font lock glitches due to interactions between ;; unnecessary work and font lock glitches due to interactions between
;; various text properties. ;; various text properties.
;; ;;
;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any ;; (2007-02-12): The macro `combine-after-change-calls' ISN'T used any
;; more. ;; more.
@ -451,18 +451,18 @@ preferably use the `c-mode-menu' language constant directly."
end (point)))))))) end (point))))))))
;; c-maybe-stale-found-type records a place near the region being ;; c-maybe-stale-found-type records a place near the region being
;; changed where an element of `found-types' might become stale. It ;; changed where an element of `found-types' might become stale. It
;; is set in c-before-change and is either nil, or has the form: ;; is set in c-before-change and is either nil, or has the form:
;; ;;
;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where ;; (c-decl-id-start "foo" 97 107 " (* ooka) " "o"), where
;; ;;
;; o - `c-decl-id-start' is the c-type text property value at buffer ;; o - `c-decl-id-start' is the c-type text property value at buffer
;; pos 96. ;; pos 96.
;; ;;
;; o - 97 107 is the region potentially containing the stale type - ;; o - 97 107 is the region potentially containing the stale type -
;; this is delimited by a non-nil c-type text property at 96 and ;; this is delimited by a non-nil c-type text property at 96 and
;; either another one or a ";", "{", or "}" at 107. ;; either another one or a ";", "{", or "}" at 107.
;; ;;
;; o - " (* ooka) " is the (before change) buffer portion containing ;; o - " (* ooka) " is the (before change) buffer portion containing
;; the suspect type (here "ooka"). ;; the suspect type (here "ooka").
;; ;;
@ -517,9 +517,6 @@ that requires a literal mode spec at compile time."
(make-local-variable 'fill-paragraph-function) (make-local-variable 'fill-paragraph-function)
(setq fill-paragraph-function 'c-fill-paragraph) (setq fill-paragraph-function 'c-fill-paragraph)
;; Initialise the cache of brace pairs, and opening braces/brackets/parens.
(c-state-cache-init)
(when (or c-recognize-<>-arglists (when (or c-recognize-<>-arglists
(c-major-mode-is 'awk-mode) (c-major-mode-is 'awk-mode)
(c-major-mode-is '(c-mode c++-mode objc-mode))) (c-major-mode-is '(c-mode c++-mode objc-mode)))
@ -846,7 +843,7 @@ Note that the style variables are always made local to the buffer."
t) t)
(t nil))))))) (t nil)))))))
(defun c-neutralize-syntax-in-and-mark-CPP (begg endd old-len) (defun c-extend-and-neutralize-syntax-in-CPP (begg endd old-len)
;; (i) Extend the font lock region to cover all changed preprocessor ;; (i) Extend the font lock region to cover all changed preprocessor
;; regions; it does this by setting the variables `c-new-BEG' and ;; regions; it does this by setting the variables `c-new-BEG' and
;; `c-new-END' to the new boundaries. ;; `c-new-END' to the new boundaries.
@ -855,15 +852,10 @@ Note that the style variables are always made local to the buffer."
;; extended changed region. "Restore" lines which were CPP lines before the ;; extended changed region. "Restore" lines which were CPP lines before the
;; change and are no longer so; these can be located from the Buffer local ;; change and are no longer so; these can be located from the Buffer local
;; variables `c-old-BOM' and `c-old-EOM'. ;; variables `c-old-BOM' and `c-old-EOM'.
;; ;;
;; (iii) Mark every CPP construct by placing a `category' property value
;; `c-cpp-delimiter' at its start and end. The marked characters are the
;; opening # and usually the terminating EOL, but sometimes the character
;; before a comment/string delimiter.
;;
;; That is, set syntax-table properties on characters that would otherwise ;; That is, set syntax-table properties on characters that would otherwise
;; interact syntactically with those outside the CPP line(s). ;; interact syntactically with those outside the CPP line(s).
;; ;;
;; This function is called from an after-change function, BEGG ENDD and ;; This function is called from an after-change function, BEGG ENDD and
;; OLD-LEN being the standard parameters. It prepares the buffer for font ;; OLD-LEN being the standard parameters. It prepares the buffer for font
;; locking, hence must get called before `font-lock-after-change-function'. ;; locking, hence must get called before `font-lock-after-change-function'.
@ -874,34 +866,32 @@ Note that the style variables are always made local to the buffer."
;; This function is the C/C++/ObjC value of `c-before-font-lock-function'. ;; This function is the C/C++/ObjC value of `c-before-font-lock-function'.
;; ;;
;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!! ;; Note: SPEED _MATTERS_ IN THIS FUNCTION!!!
;; ;;
;; This function might make hidden buffer changes. ;; This function might make hidden buffer changes.
(c-save-buffer-state (limits) (c-save-buffer-state (limits mbeg+1)
;; First determine the region, (c-new-BEG c-new-END), which will get font ;; First determine the region, (c-new-BEG c-new-END), which will get font
;; locked. It might need "neutralizing". This region may not start ;; locked. It might need "neutralizing". This region may not start
;; inside a string, comment, or macro. ;; inside a string, comment, or macro.
(goto-char c-old-BOM) ; already set to old start of macro or begg. (goto-char c-old-BOM) ; already set to old start of macro or begg.
(setq c-new-BEG (setq c-new-BEG
(if (setq limits (c-state-literal-at (point))) (if (setq limits (c-literal-limits))
(cdr limits) ; go forward out of any string or comment. (cdr limits) ; go forward out of any string or comment.
(point))) (point)))
(goto-char endd) (goto-char endd)
(if (setq limits (c-state-literal-at (point))) (if (setq limits (c-literal-limits))
(goto-char (car limits))) ; go backward out of any string or comment. (goto-char (car limits))) ; go backward out of any string or comment.
(if (c-beginning-of-macro) (if (c-beginning-of-macro)
(c-end-of-macro)) (c-end-of-macro))
(setq c-new-END (max (+ (- c-old-EOM old-len) (- endd begg)) (setq c-new-END (max (+ (- c-old-EOM old-len) (- endd begg))
(point))) (point)))
;; Clear all old relevant properties. ;; Clear any existing punctuation properties.
(c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1)) (c-clear-char-property-with-value c-new-BEG c-new-END 'syntax-table '(1))
(c-clear-char-property-with-value c-new-BEG c-new-END 'category 'c-cpp-delimiter)
;; FIXME!!! What about the "<" and ">" category properties? 2009-11-16
;; Add needed properties to each CPP construct in the region. ;; Add needed properties to each CPP construct in the region.
(goto-char c-new-BEG) (goto-char c-new-BEG)
(let ((pps-position c-new-BEG) pps-state mbeg) (let ((pps-position c-new-BEG) pps-state)
(while (and (< (point) c-new-END) (while (and (< (point) c-new-END)
(search-forward-regexp c-anchored-cpp-prefix c-new-END t)) (search-forward-regexp c-anchored-cpp-prefix c-new-END t))
;; If we've found a "#" inside a string/comment, ignore it. ;; If we've found a "#" inside a string/comment, ignore it.
@ -910,16 +900,10 @@ Note that the style variables are always made local to the buffer."
pps-position (point)) pps-position (point))
(unless (or (nth 3 pps-state) ; in a string? (unless (or (nth 3 pps-state) ; in a string?
(nth 4 pps-state)) ; in a comment? (nth 4 pps-state)) ; in a comment?
(goto-char (match-beginning 0)) (setq mbeg+1 (point))
(setq mbeg (point)) (c-end-of-macro) ; Do we need to go forward 1 char here? No!
(if (> (c-syntactic-end-of-macro) mbeg) (c-neutralize-CPP-line mbeg+1 (point))
(progn (setq pps-position (point))))))) ; no need to update pps-state.
(c-neutralize-CPP-line mbeg (point))
(c-set-cpp-delimiters mbeg (point))
;(setq pps-position (point))
)
(forward-line)) ; no infinite loop with, e.g., "#//"
)))))
(defun c-before-change (beg end) (defun c-before-change (beg end)
;; Function to be put on `before-change-function'. Primarily, this calls ;; Function to be put on `before-change-function'. Primarily, this calls
@ -927,7 +911,7 @@ Note that the style variables are always made local to the buffer."
;; otherwise used only to remove stale entries from the `c-found-types' ;; otherwise used only to remove stale entries from the `c-found-types'
;; cache, and to record entries which a `c-after-change' function might ;; cache, and to record entries which a `c-after-change' function might
;; confirm as stale. ;; confirm as stale.
;; ;;
;; Note that this function must be FAST rather than accurate. Note ;; Note that this function must be FAST rather than accurate. Note
;; also that it only has any effect when font locking is enabled. ;; also that it only has any effect when font locking is enabled.
;; We exploit this by checking for font-lock-*-face instead of doing ;; We exploit this by checking for font-lock-*-face instead of doing