cc-langs.el 121 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1
;;; cc-langs.el --- language specific settings for CC Mode
Richard M. Stallman's avatar
Richard M. Stallman committed
2

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1985, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
Glenn Morris's avatar
Glenn Morris committed
4
;;   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
Glenn Morris's avatar
Glenn Morris committed
5
;;   Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
6

Alan Mackenzie's avatar
Alan Mackenzie committed
7 8
;; Authors:    2002- Alan Mackenzie
;;             1998- Martin Stjernholm
9
;;             1992-1999 Barry A. Warsaw
10 11
;;             1987 Dave Detlefs
;;             1987 Stewart Clamen
Richard M. Stallman's avatar
Richard M. Stallman committed
12
;;             1985 Richard M. Stallman
Barry A. Warsaw's avatar
Barry A. Warsaw committed
13
;; Maintainer: bug-cc-mode@gnu.org
Richard M. Stallman's avatar
Richard M. Stallman committed
14
;; Created:    22-Apr-1997 (split from cc-mode.el)
Karl Heuer's avatar
Karl Heuer committed
15
;; Version:    See cc-mode.el
Richard M. Stallman's avatar
Richard M. Stallman committed
16 17 18 19
;; Keywords:   c languages oop

;; This file is part of GNU Emacs.

20
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
21
;; it under the terms of the GNU General Public License as published by
22 23
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26 27 28 29 30

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
31
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
32

33 34
;;; Commentary:

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
;; HACKERS NOTE: There's heavy macro magic here.  If you need to make
;; changes in this or other files containing `c-lang-defconst' but
;; don't want to read through the longer discussion below then read
;; this:
;;
;; o  A change in a `c-lang-defconst' or `c-lang-defvar' will not take
;;    effect if the file containing the mode init function (typically
;;    cc-mode.el) is byte compiled.
;; o  To make changes show in font locking you need to reevaluate the
;;    `*-font-lock-keywords-*' constants, which normally is easiest to
;;    do with M-x eval-buffer in cc-fonts.el.
;; o  In either case it's necessary to reinitialize the mode to make
;;    the changes show in an existing buffer.

;;; Introduction to the language dependent variable system:
;;
;; This file contains all the language dependent variables, except
;; those specific for font locking which reside in cc-fonts.el.  As
;; far as possible, all the differences between the languages that CC
;; Mode supports are described with these variables only, so that the
;; code can be shared.
;;
;; The language constant system (see cc-defs.el) is used to specify
;; various language dependent info at a high level, such as lists of
;; keywords, and then from them generate - at compile time - the
;; various regexps and other low-level structures actually employed in
;; the code at runtime.
;;
;; This system is also designed to make it easy for developers of
;; derived modes to customize the source constants for new language
;; variants, without having to keep up with the exact regexps etc that
;; are used in each CC Mode version.  It's possible from an external
;; package to add a new language by inheriting an existing one, and
;; then change specific constants as necessary for the new language.
;; The old values for those constants (and the values of all the other
;; high-level constants) may be used to build the new ones, and those
;; new values will in turn be used by the low-level definitions here
;; to build the runtime constants appropriately for the new language
;; in the current version of CC Mode.
;;
;; Like elsewhere in CC Mode, the existence of a doc string signifies
;; that a language constant is part of the external API, and that it
;; therefore can be used with a high confidence that it will continue
;; to work with future versions of CC Mode.  Even so, it's not
;; unlikely that such constants will change meaning slightly as this
;; system is refined further; a certain degree of dependence on the CC
;; Mode version is unavoidable when hooking in at this level.  Also
;; note that there's still work to be done to actually use these
;; constants everywhere inside CC Mode; there are still hardcoded
;; values in many places in the code.
;;
;; Separate packages will also benefit from the compile time
;; evaluation; the byte compiled file(s) for them will contain the
;; compiled runtime constants ready for use by (the byte compiled) CC
;; Mode, and the source definitions in this file don't have to be
;; loaded then.  However, if a byte compiled package is loaded that
;; has been compiled with a different version of CC Mode than the one
;; currently loaded, then the compiled-in values will be discarded and
;; new ones will be built when the mode is initialized.  That will
;; automatically trig a load of the file(s) containing the source
;; definitions (i.e. this file and/or cc-fonts.el) if necessary.
;;
;; A small example of a derived mode is available at
;; <http://cc-mode.sourceforge.net/derived-mode-ex.el>.  It also
;; contains some useful hints for derived mode developers.

;;; Using language variables:
;;
;; The `c-lang-defvar' forms in this file comprise the language
;; variables that CC Mode uses.  It does not work to use
;; `c-lang-defvar' anywhere else (which isn't much of a limitation
;; since these variables sole purpose is to interface with the CC Mode
;; core functions).  The values in these `c-lang-defvar's are not
;; evaluated right away but instead collected to a single large `setq'
;; that can be inserted for a particular language with the
;; `c-init-language-vars' macro.

;; This file is only required at compile time, or when not running
;; from byte compiled files, or when the source definitions for the
;; language constants are requested.

116 117
;;; Code:

118 119 120 121
;; For Emacs < 22.2.
(eval-and-compile
  (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))

Gerd Moellmann's avatar
Gerd Moellmann committed
122 123
(eval-when-compile
  (let ((load-path
Gerd Moellmann's avatar
Gerd Moellmann committed
124 125 126
	 (if (and (boundp 'byte-compile-dest-file)
		  (stringp byte-compile-dest-file))
	     (cons (file-name-directory byte-compile-dest-file) load-path)
Gerd Moellmann's avatar
Gerd Moellmann committed
127
	   load-path)))
128
    (load "cc-bytecomp" nil t)))
Gerd Moellmann's avatar
Gerd Moellmann committed
129

Gerd Moellmann's avatar
Gerd Moellmann committed
130 131
(cc-require 'cc-defs)
(cc-require 'cc-vars)
132

133

134 135 136
;; This file is not always loaded.  See note above.
(cc-external-require 'cl)

Richard M. Stallman's avatar
Richard M. Stallman committed
137

138
;;; Setup for the `c-lang-defvar' system.
139 140

(eval-and-compile
141
  ;; These are used to collect the init forms from the subsequent
142 143 144 145
  ;; `c-lang-defvar' and `c-lang-setvar'.  They are used to build the
  ;;  lambda in `c-make-init-lang-vars-fun' below, and to build `defvar's
  ;;  and `make-variable-buffer-local's in cc-engine and
  ;;  `make-local-variable's in `c-init-language-vars-for'.
146 147 148
  (defvar c-lang-variable-inits nil)
  (defvar c-lang-variable-inits-tail nil)
  (setq c-lang-variable-inits (list nil)
149 150 151 152 153
	c-lang-variable-inits-tail c-lang-variable-inits)
  (defvar c-emacs-variable-inits nil)
  (defvar c-emacs-variable-inits-tail nil)
  (setq c-emacs-variable-inits (list nil)
	c-emacs-variable-inits-tail c-emacs-variable-inits))
154 155

(defmacro c-lang-defvar (var val &optional doc)
156 157 158
  "Declares the buffer local variable VAR to get the value VAL.  VAL is
evaluated and assigned at mode initialization.  More precisely, VAL is
evaluated and bound to VAR when the result from the macro
159 160 161 162
`c-init-language-vars' is evaluated.

`c-lang-const' is typically used in VAL to get the right value for the
language being initialized, and such calls will be macro expanded to
163
the evaluated constant value at compile time."
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

  (when (and (not doc)
	     (eq (car-safe val) 'c-lang-const)
	     (eq (nth 1 val) var)
	     (not (nth 2 val)))
    ;; Special case: If there's no docstring and the value is a
    ;; simple (c-lang-const foo) where foo is the same name as VAR
    ;; then take the docstring from the language constant foo.
    (setq doc (get (intern (symbol-name (nth 1 val)) c-lang-constants)
		   'variable-documentation)))
  (or (stringp doc)
      (setq doc nil))

  (let ((elem (assq var (cdr c-lang-variable-inits))))
    (if elem
	(setcdr elem (list val doc))
      (setcdr c-lang-variable-inits-tail (list (list var val doc)))
      (setq c-lang-variable-inits-tail (cdr c-lang-variable-inits-tail))))

  ;; Return the symbol, like the other def* forms.
  `',var)

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
(defmacro c-lang-setvar (var val)
  "Causes the variable VAR to be made buffer local and to get set to the
value VAL.  VAL is evaluated and assigned at mode initialization.  More
precisely, VAL is evaluated and bound to VAR when the result from the
macro `c-init-language-vars' is evaluated.  VAR is typically a standard
Emacs variable like `comment-start'.

`c-lang-const' is typically used in VAL to get the right value for the
language being initialized, and such calls will be macro expanded to
the evaluated constant value at compile time."
  (let ((elem (assq var (cdr c-emacs-variable-inits))))
    (if elem
	(setcdr elem (list val)) ; Maybe remove "list", sometime. 2006-07-19
      (setcdr c-emacs-variable-inits-tail (list (list var val)))
      (setq c-emacs-variable-inits-tail (cdr c-emacs-variable-inits-tail))))

  ;; Return the symbol, like the other def* forms.
  `',var)

205
(put 'c-lang-defvar 'lisp-indent-function 'defun)
206 207 208 209
; (eval-after-load "edebug" ; 2006-07-09: def-edebug-spec is now in subr.el.
;  '
(def-edebug-spec c-lang-defvar
  (&define name def-form &optional stringp)) ;)
Richard M. Stallman's avatar
Richard M. Stallman committed
210

211 212 213 214 215 216
;; Suppress "might not be defined at runtime" warning.
;; This file is only used when compiling other cc files.
(declare-function delete-duplicates "cl-seq" (cl-seq &rest cl-keys))
(declare-function mapcan "cl-extra" (cl-func cl-seq &rest cl-rest))
(declare-function cl-macroexpand-all "cl-extra" (form &optional env))

217
(eval-and-compile
218 219 220
  ;; Some helper functions used when building the language constants.

  (defun c-filter-ops (ops opgroup-filter op-filter &optional xlate)
221
    ;; Extract a subset of the operators in the list OPS in a DWIM:ey
222 223
    ;; way.  The return value is a plain list of operators:
    ;;
224
    ;; OPS either has the structure of `c-operators', is a single
225
    ;; group in `c-operators', or is a plain list of operators.
226 227 228 229 230 231 232
    ;; 
    ;; OPGROUP-FILTER specifies how to select the operator groups.  It
    ;; can be t to choose all groups, a list of group type symbols
    ;; (such as 'prefix) to accept, or a function which will be called
    ;; with the group symbol for each group and should return non-nil
    ;; if that group is to be included.
    ;;
233 234 235 236
    ;; If XLATE is given, it's a function which is called for each
    ;; matching operator and its return value is collected instead.
    ;; If it returns a list, the elements are spliced directly into
    ;; the final result, which is returned as a list with duplicates
237 238 239 240
    ;; removed using `equal'.
    ;;
    ;; `c-mode-syntax-table' for the current mode is in effect during
    ;; the whole procedure.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    (unless (listp (car-safe ops))
      (setq ops (list ops)))
    (cond ((eq opgroup-filter t)
	   (setq opgroup-filter (lambda (opgroup) t)))
	  ((not (functionp opgroup-filter))
	   (setq opgroup-filter `(lambda (opgroup)
				   (memq opgroup ',opgroup-filter)))))
    (cond ((eq op-filter t)
	   (setq op-filter (lambda (op) t)))
	  ((stringp op-filter)
	   (setq op-filter `(lambda (op)
			      (string-match ,op-filter op)))))
    (unless xlate
      (setq xlate 'identity))
    (c-with-syntax-table (c-lang-const c-mode-syntax-table)
      (delete-duplicates
       (mapcan (lambda (opgroup)
		 (when (if (symbolp (car opgroup))
			   (when (funcall opgroup-filter (car opgroup))
			     (setq opgroup (cdr opgroup))
			     t)
			 t)
		   (mapcan (lambda (op)
			     (when (funcall op-filter op)
			       (let ((res (funcall xlate op)))
				 (if (listp res) res (list res)))))
			   opgroup)))
	       ops)
       :test 'equal))))

Gerd Moellmann's avatar
Gerd Moellmann committed
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
;;; Various mode specific values that aren't language related.

(c-lang-defconst c-mode-menu
  ;; The definition for the mode menu.  The menu title is prepended to
  ;; this before it's fed to `easy-menu-define'.
  t `(["Comment Out Region"     comment-region
       (c-fn-region-is-active-p)]
      ["Uncomment Region"       (comment-region (region-beginning)
						(region-end) '(4))
       (c-fn-region-is-active-p)]
      ["Indent Expression"      c-indent-exp
       (memq (char-after) '(?\( ?\[ ?\{))]
      ["Indent Line or Region"  c-indent-line-or-region t]
      ["Fill Comment Paragraph" c-fill-paragraph t]
      "----"
      ["Backward Statement"     c-beginning-of-statement t]
      ["Forward Statement"      c-end-of-statement t]
      ,@(when (c-lang-const c-opt-cpp-prefix)
	  ;; Only applicable if there's a cpp preprocessor.
	  `(["Up Conditional"         c-up-conditional t]
	    ["Backward Conditional"   c-backward-conditional t]
	    ["Forward Conditional"    c-forward-conditional t]
	    "----"
	    ["Macro Expand Region"    c-macro-expand
	     (c-fn-region-is-active-p)]
	    ["Backslashify"           c-backslash-region
	     (c-fn-region-is-active-p)]))
      "----"
      ("Toggle..."
Nick Roberts's avatar
Nick Roberts committed
301 302
       ["Syntactic indentation" c-toggle-syntactic-indentation
	:style toggle :selected c-syntactic-indentation]
303 304 305
       ["Electric mode"         c-toggle-electric-state
	:style toggle :selected c-electric-flag]
       ["Auto newline"          c-toggle-auto-newline
Nick Roberts's avatar
Nick Roberts committed
306
	:style toggle :selected c-auto-newline]
307 308
       ["Hungry delete"         c-toggle-hungry-state
	:style toggle :selected c-hungry-delete-key]
309 310 311
       ["Subword mode"          subword-mode
	:style toggle :selected (and (boundp 'subword-mode)
                                     subword-mode)])))
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

;;; Syntax tables.

(defun c-populate-syntax-table (table)
  "Populate the given syntax table as necessary for a C-like language.
This includes setting ' and \" as string delimiters, and setting up
the comment syntax to handle both line style \"//\" and block style
\"/*\" \"*/\" comments."

  (modify-syntax-entry ?_  "_"     table)
  (modify-syntax-entry ?\\ "\\"    table)
  (modify-syntax-entry ?+  "."     table)
  (modify-syntax-entry ?-  "."     table)
  (modify-syntax-entry ?=  "."     table)
  (modify-syntax-entry ?%  "."     table)
  (modify-syntax-entry ?<  "."     table)
  (modify-syntax-entry ?>  "."     table)
  (modify-syntax-entry ?&  "."     table)
  (modify-syntax-entry ?|  "."     table)
  (modify-syntax-entry ?\' "\""    table)
  (modify-syntax-entry ?\240 "."   table)

  ;; Set up block and line oriented comments.  The new C
  ;; standard mandates both comment styles even in C, so since
  ;; all languages now require dual comments, we make this the
  ;; default.
  (cond
   ;; XEmacs
   ((memq '8-bit c-emacs-features)
    (modify-syntax-entry ?/  ". 1456" table)
    (modify-syntax-entry ?*  ". 23"   table))
   ;; Emacs
   ((memq '1-bit c-emacs-features)
    (modify-syntax-entry ?/  ". 124b" table)
    (modify-syntax-entry ?*  ". 23"   table))
   ;; incompatible
   (t (error "CC Mode is incompatible with this version of Emacs")))

  (modify-syntax-entry ?\n "> b"  table)
  ;; Give CR the same syntax as newline, for selective-display
  (modify-syntax-entry ?\^m "> b" table))

(c-lang-defconst c-make-mode-syntax-table
  "Functions that generates the mode specific syntax tables.
The syntax tables aren't stored directly since they're quite large."
  t `(lambda ()
       (let ((table (make-syntax-table)))
	 (c-populate-syntax-table table)
	 ;; Mode specific syntaxes.
362
	 ,(cond ((or (c-major-mode-is 'objc-mode) (c-major-mode-is 'java-mode))
363 364 365 366
		 ;; Let '@' be part of symbols in ObjC to cope with
		 ;; its compiler directives as single keyword tokens.
		 ;; This is then necessary since it's assumed that
		 ;; every keyword is a single symbol.
367 368 369 370 371 372 373 374 375 376
		 `(modify-syntax-entry ?@ "_" table))
		((c-major-mode-is 'pike-mode)
		 `(modify-syntax-entry ?@ "." table)))
	 table)))

(c-lang-defconst c-mode-syntax-table
  ;; The syntax tables in evaluated form.  Only used temporarily when
  ;; the constants in this file are evaluated.
  t (funcall (c-lang-const c-make-mode-syntax-table)))

377
(c-lang-defconst c++-make-template-syntax-table
378 379 380 381 382 383 384
  ;; A variant of `c++-mode-syntax-table' that defines `<' and `>' as
  ;; parenthesis characters.  Used temporarily when template argument
  ;; lists are parsed.  Note that this encourages incorrect parsing of
  ;; templates since they might contain normal operators that uses the
  ;; '<' and '>' characters.  Therefore this syntax table might go
  ;; away when CC Mode handles templates correctly everywhere.
  t   nil
385
  (java c++) `(lambda ()
386 387 388 389 390
	 (let ((table (funcall ,(c-lang-const c-make-mode-syntax-table))))
	   (modify-syntax-entry ?< "(>" table)
	   (modify-syntax-entry ?> ")<" table)
	   table)))
(c-lang-defvar c++-template-syntax-table
391 392
  (and (c-lang-const c++-make-template-syntax-table)
       (funcall (c-lang-const c++-make-template-syntax-table))))
393

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
(c-lang-defconst c-no-parens-syntax-table
  ;; A variant of the standard syntax table which is used to find matching
  ;; "<"s and ">"s which have been marked as parens using syntax table
  ;; properties.  The other paren characters (e.g. "{", ")" "]") are given a
  ;; non-paren syntax here. so that the list commands will work on "< ... >"
  ;; even when there's unbalanced other parens inside them.
  ;;
  ;; This variable is nil for languages which don't have template stuff.
  t  `(lambda ()
	(if (c-lang-const c-recognize-<>-arglists)
	    (let ((table (funcall ,(c-lang-const c-make-mode-syntax-table))))
	      (modify-syntax-entry ?\( "." table)
	      (modify-syntax-entry ?\) "." table)
	      (modify-syntax-entry ?\[ "." table)
	      (modify-syntax-entry ?\] "." table)
	      (modify-syntax-entry ?\{ "." table)
	      (modify-syntax-entry ?\} "." table)
	      table))))
(c-lang-defvar c-no-parens-syntax-table
	       (funcall (c-lang-const c-no-parens-syntax-table)))

415 416 417 418 419 420 421 422 423 424 425 426 427
(c-lang-defconst c-identifier-syntax-modifications
  "A list that describes the modifications that should be done to the
mode syntax table to get a syntax table that matches all identifiers
and keywords as words.

The list is just like the one used in `font-lock-defaults': Each
element is a cons where the car is the character to modify and the cdr
the new syntax, as accepted by `modify-syntax-entry'."
  ;; The $ character is not allowed in most languages (one exception
  ;; is Java which allows it for legacy reasons) but we still classify
  ;; it as an indentifier character since it's often used in various
  ;; machine generated identifiers.
  t    '((?_ . "w") (?$ . "w"))
428
  (objc java) (append '((?@ . "w"))
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	       (c-lang-const c-identifier-syntax-modifications))
  awk  '((?_ . "w")))
(c-lang-defvar c-identifier-syntax-modifications
  (c-lang-const c-identifier-syntax-modifications))

(c-lang-defvar c-identifier-syntax-table
  (let ((table (copy-syntax-table (c-mode-var "mode-syntax-table")))
	(mods c-identifier-syntax-modifications)
	mod)
    (while mods
      (setq mod (car mods)
	    mods (cdr mods))
      (modify-syntax-entry (car mod) (cdr mod) table))
    table)
  "Syntax table built on the mode syntax table but additionally
classifies symbol constituents like '_' and '$' as word constituents,
so that all identifiers are recognized as words.")

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
(c-lang-defconst c-get-state-before-change-functions
  ;; For documentation see the following c-lang-defvar of the same name.
  ;; The value here may be a list of functions or a single function.
  t nil
  c++ '(c-extend-region-for-CPP c-before-change-check-<>-operators)
  (c objc) 'c-extend-region-for-CPP
  ;; java 'c-before-change-check-<>-operators
  awk 'c-awk-record-region-clear-NL)
(c-lang-defvar c-get-state-before-change-functions
	       (let ((fs (c-lang-const c-get-state-before-change-functions)))
		  (if (listp fs)
		      fs
		    (list fs)))
  "If non-nil, a list of functions called from c-before-change-hook.
Typically these will record enough state to allow
462 463 464 465
`c-before-font-lock-function' to extend the region to fontify,
and may do such things as removing text-properties which must be
recalculated.

466 467
These functions will be run in the order given.  Each of them
takes 2 parameters, the BEG and END supplied to every
468 469 470 471
before-change function; on entry, the buffer will have been
widened and match-data will have been saved; point is undefined
on both entry and exit; the return value is ignored.

472 473 474 475 476
The functions are called even when font locking isn't enabled.

When the mode is initialized, the functions are called with
parameters \(point-min) and \(point-max).")

477 478 479 480 481 482 483 484 485 486 487
(c-lang-defconst c-before-font-lock-function
  "If non-nil, a function called just before font locking.
Typically it will extend the region about to be fontified \(see
below) and will set `syntax-table' text properties on the region.

It takes 3 parameters, the BEG, END, and OLD-LEN supplied to
every after-change function; point is undefined on both entry and
exit; on entry, the buffer will have been widened and match-data
will have been saved; the return value is ignored.

The function may extend the region to be fontified by setting the
488
buffer local variables c-new-BEG and c-new-END.
489 490 491 492 493 494

The function is called even when font locking is disabled.

When the mode is initialized, this function is called with
parameters \(point-min), \(point-max) and <buffer size>."
  t nil
495
  (c c++ objc) 'c-neutralize-syntax-in-and-mark-CPP
496 497 498 499
  awk 'c-awk-extend-and-syntax-tablify-region)
(c-lang-defvar c-before-font-lock-function
	       (c-lang-const c-before-font-lock-function))

500 501 502 503 504

;;; Lexer-level syntax (identifiers, tokens etc).

(c-lang-defconst c-symbol-start
  "Regexp that matches the start of a symbol, i.e. any identifier or
505
keyword.  It's unspecified how far it matches.	Does not contain a \\|
506 507
operator at the top level."
  t    (concat "[" c-alpha "_]")
508
  java (concat "[" c-alpha "_@]")
509
  objc (concat "[" c-alpha "@]")
510 511 512 513 514 515 516 517 518 519
  pike (concat "[" c-alpha "_`]"))
(c-lang-defvar c-symbol-start (c-lang-const c-symbol-start))

(c-lang-defconst c-symbol-chars
  "Set of characters that can be part of a symbol.
This is on the form that fits inside [ ] in a regexp."
  ;; Pike note: With the backquote identifiers this would include most
  ;; operator chars too, but they are handled with other means instead.
  t    (concat c-alnum "_$")
  objc (concat c-alnum "_$@"))
520 521

(c-lang-defconst c-symbol-key
522 523
  "Regexp matching identifiers and keywords (with submatch 0).  Assumed
to match if `c-symbol-start' matches on the same position."
524 525 526 527 528 529 530 531 532 533 534 535 536
  t    (concat (c-lang-const c-symbol-start)
	       "[" (c-lang-const c-symbol-chars) "]*")
  pike (concat
	;; Use the value from C here since the operator backquote is
	;; covered by the other alternative.
	(c-lang-const c-symbol-key c)
	"\\|"
	(c-make-keywords-re nil
	  (c-lang-const c-overloadable-operators))))
(c-lang-defvar c-symbol-key (c-lang-const c-symbol-key))

(c-lang-defconst c-symbol-key-depth
  ;; Number of regexp grouping parens in `c-symbol-key'.
537
  t (regexp-opt-depth (c-lang-const c-symbol-key)))
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

(c-lang-defconst c-nonsymbol-chars
  "This is the set of chars that can't be part of a symbol, i.e. the
negation of `c-symbol-chars'."
  t (concat "^" (c-lang-const c-symbol-chars)))
(c-lang-defvar c-nonsymbol-chars (c-lang-const c-nonsymbol-chars))

(c-lang-defconst c-nonsymbol-key
  "Regexp that matches any character that can't be part of a symbol.
It's usually appended to other regexps to avoid matching a prefix.
It's assumed to not contain any submatchers."
  ;; The same thing regarding Unicode identifiers applies here as to
  ;; `c-symbol-key'.
  t (concat "[" (c-lang-const c-nonsymbol-chars) "]"))

553 554 555 556 557 558 559 560
(c-lang-defconst c-identifier-ops
  "The operators that make up fully qualified identifiers.  nil in
languages that don't have such things.  See `c-operators' for a
description of the format.  Binary operators can concatenate symbols,
e.g. \"::\" in \"A::B::C\".  Prefix operators can precede identifiers,
e.g. \"~\" in \"~A::B\".  Other types of operators aren't supported.

This value is by default merged into `c-operators'."
561
  t    nil
562 563 564
  c++  '((prefix "~" "??-" "compl")
	 (right-assoc "::")
	 (prefix "::"))
Martin Stjernholm's avatar
Martin Stjernholm committed
565 566 567 568 569 570
  ;; Java has "." to concatenate identifiers but it's also used for
  ;; normal indexing.  There's special code in the Java font lock
  ;; rules to fontify qualified identifiers based on the standard
  ;; naming conventions.  We still define "." here to make
  ;; `c-forward-name' move over as long names as possible which is
  ;; necessary to e.g. handle throws clauses correctly.
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
  java '((left-assoc "."))
  idl  '((left-assoc "::")
	 (prefix "::"))
  pike '((left-assoc "::")
	 (prefix "::")
	 (left-assoc ".")))

(c-lang-defconst c-opt-identifier-concat-key
  ;; Appendable adorned regexp matching the operators that join
  ;; symbols to fully qualified identifiers, or nil in languages that
  ;; don't have such things.
  ;;
  ;; This was a docstring constant in 5.30.  It still works but is now
  ;; considered internal - change `c-identifier-ops' instead.
  t (let ((ops (c-filter-ops (c-lang-const c-identifier-ops)
			     '(left-assoc right-assoc)
			     t)))
      (when ops
	(c-make-keywords-re 'appendable ops))))
590 591 592 593
(c-lang-defvar c-opt-identifier-concat-key
  (c-lang-const c-opt-identifier-concat-key)
  'dont-doc)

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
(c-lang-defconst c-opt-identifier-concat-key-depth
  ;; Number of regexp grouping parens in `c-opt-identifier-concat-key'.
  t (regexp-opt-depth (c-lang-const c-opt-identifier-concat-key)))

(c-lang-defconst c-opt-identifier-prefix-key
  ;; Appendable adorned regexp matching operators that might precede
  ;; an identifier and that are part of the identifier in that case.
  ;; nil in languages without such things.
  t (let ((ops (c-filter-ops (c-lang-const c-identifier-ops)
			     '(prefix)
			     t)))
      (when ops
	(c-make-keywords-re 'appendable ops))))

(c-lang-defconst c-after-id-concat-ops
  "Operators that can occur after a binary operator on `c-identifier-ops'
in identifiers.  nil in languages that don't have such things.

Operators here should also have appropriate entries in `c-operators' -
it's not taken care of by default."
  t    nil
  ;; '~' for destructors in C++, '*' for member pointers.
  c++  '("~" "*")
  ;; In Java we recognize '*' to deal with "foo.bar.*" that can occur
  ;; in import declarations.  (This will also match bogus things like
  ;; "foo.*bar" but we don't bother.)
  java '("*"))

622
(c-lang-defconst c-opt-after-id-concat-key
623 624 625 626 627 628 629 630 631 632 633 634 635
  ;; Regexp that must match the token after
  ;; `c-opt-identifier-concat-key' for it to be considered an
  ;; identifier concatenation operator (which e.g. causes the
  ;; preceding identifier to be fontified as a reference).  Assumed to
  ;; be a string if `c-opt-identifier-concat-key' is.
  ;;
  ;; This was a docstring constant in 5.30.  It still works but is now
  ;; considered internal - change `c-after-id-concat-ops' instead.
  t (concat (c-lang-const c-symbol-start)
	    (if (c-lang-const c-after-id-concat-ops)
		(concat "\\|" (c-make-keywords-re 'appendable
				(c-lang-const c-after-id-concat-ops)))
	      "")))
636 637

(c-lang-defconst c-identifier-start
638 639 640 641 642 643 644 645
  "Regexp that matches the start of an (optionally qualified) identifier.
It should also match all keywords.  It's unspecified how far it
matches."
  t (concat (c-lang-const c-symbol-start)
	    (if (c-lang-const c-opt-identifier-prefix-key)
		(concat "\\|"
			(c-lang-const c-opt-identifier-prefix-key))
	      "")))
646 647 648 649 650
(c-lang-defvar c-identifier-start (c-lang-const c-identifier-start))

(c-lang-defconst c-identifier-key
  "Regexp matching a fully qualified identifier, like \"A::B::c\" in
C++.  It does not recognize the full range of syntactic whitespace
651 652 653 654 655 656 657 658 659 660
between the tokens; `c-forward-name' has to be used for that.  It
should also not match identifiers containing parenthesis groupings,
e.g. identifiers with template arguments such as \"A<X,Y>\" in C++."
  ;; This regexp is more complex than strictly necessary to ensure
  ;; that it can be matched with a minimum of backtracking.
  t (concat (if (c-lang-const c-opt-identifier-prefix-key)
		(concat
		 "\\("
		 (c-lang-const c-opt-identifier-prefix-key)
		 (c-lang-const c-simple-ws) "*"
661
		 "\\)?")
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	      "")
	    "\\(" (c-lang-const c-symbol-key) "\\)"
	    (if (c-lang-const c-opt-identifier-concat-key)
		(concat
		 "\\("
		 (c-lang-const c-simple-ws) "*"
		 (c-lang-const c-opt-identifier-concat-key)
		 (c-lang-const c-simple-ws) "*"
		 (if (c-lang-const c-after-id-concat-ops)
		     (concat
		      "\\("
		       (c-make-keywords-re 'appendable
			 (c-lang-const c-after-id-concat-ops))
		      (concat
		       ;; For flexibility, consider the symbol match
		       ;; optional if we've hit a
		       ;; `c-after-id-concat-ops' operator.  This is
		       ;; also necessary to handle the "*" that can
		       ;; end import declaration identifiers in Java.
		       "\\("
		       (c-lang-const c-simple-ws) "*"
		       "\\(" (c-lang-const c-symbol-key) "\\)"
		       "\\)?")
		      "\\|"
686
		      "\\(" (c-lang-const c-symbol-key) "\\)"
687 688 689 690
		      "\\)")
		   (concat "\\(" (c-lang-const c-symbol-key) "\\)"))
		 "\\)*")
	      "")))
691 692 693
(c-lang-defvar c-identifier-key (c-lang-const c-identifier-key))

(c-lang-defconst c-identifier-last-sym-match
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
  ;; This was a docstring constant in 5.30 but it's no longer used.
  ;; It's only kept to avoid breaking third party code.
  ;;
  ;; Used to identify the submatch in `c-identifier-key' that
  ;; surrounds the last symbol in the qualified identifier.  It's a
  ;; list of submatch numbers, of which the first that has a match is
  ;; taken.  It's assumed that at least one does when the regexp has
  ;; matched.
  t nil)

(c-lang-defconst c-string-escaped-newlines
  "Set if the language support backslash escaped newlines inside string
literals."
  t nil
  (c c++ objc pike) t)
(c-lang-defvar c-string-escaped-newlines
  (c-lang-const c-string-escaped-newlines))

(c-lang-defconst c-multiline-string-start-char
  "Set if the language supports multiline string literals without escaped
newlines.  If t, all string literals are multiline.  If a character,
only literals where the open quote is immediately preceded by that
literal are multiline."
  t    nil
  pike ?#)
(c-lang-defvar c-multiline-string-start-char
  (c-lang-const c-multiline-string-start-char))
721 722 723 724 725

(c-lang-defconst c-opt-cpp-prefix
  "Regexp matching the prefix of a cpp directive in the languages that
normally use that macro preprocessor.  Tested at bol or at boi.
Assumed to not contain any submatches or \\| operators."
726 727
  ;; TODO (ACM, 2005-04-01).  Amend the following to recognise escaped NLs;
  ;; amend all uses of c-opt-cpp-prefix which count regexp-depth.
728 729 730 731
  t "\\s *#\\s *"
  (java awk) nil)
(c-lang-defvar c-opt-cpp-prefix (c-lang-const c-opt-cpp-prefix))

732 733 734 735 736 737 738
(c-lang-defconst c-anchored-cpp-prefix
  "Regexp matching the prefix of a cpp directive anchored to BOL,
in the languages that have a macro preprocessor."
  t (if (c-lang-const c-opt-cpp-prefix)
	(concat "^" (c-lang-const c-opt-cpp-prefix))))
(c-lang-defvar c-anchored-cpp-prefix (c-lang-const c-anchored-cpp-prefix))

739 740 741 742 743 744 745 746 747 748 749 750
(c-lang-defconst c-opt-cpp-start
  "Regexp matching the prefix of a cpp directive including the directive
name, or nil in languages without preprocessor support.  The first
submatch surrounds the directive name."
  t    (if (c-lang-const c-opt-cpp-prefix)
	   (concat (c-lang-const c-opt-cpp-prefix)
		   "\\([" c-alnum "]+\\)"))
  ;; Pike, being a scripting language, recognizes hash-bangs too.
  pike (concat (c-lang-const c-opt-cpp-prefix)
	       "\\([" c-alnum "]+\\|!\\)"))
(c-lang-defvar c-opt-cpp-start (c-lang-const c-opt-cpp-start))

751 752 753 754 755
(c-lang-defconst c-cpp-message-directives
  "List of cpp directives (without the prefix) that are followed by a
string message."
  t    (if (c-lang-const c-opt-cpp-prefix)
	   '("error"))
756
  (c c++ objc pike) '("error" "warning"))
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

(c-lang-defconst c-cpp-include-directives
  "List of cpp directives (without the prefix) that are followed by a
file name in angle brackets or quotes."
  t    (if (c-lang-const c-opt-cpp-prefix)
	   '("include"))
  objc '("include" "import"))

(c-lang-defconst c-opt-cpp-macro-define
  "Cpp directive (without the prefix) that is followed by a macro
definition, or nil if the language doesn't have any."
  t (if (c-lang-const c-opt-cpp-prefix)
	"define"))

(c-lang-defconst c-opt-cpp-macro-define-start
772 773 774
  ;; Regexp matching everything up to the macro body of a cpp define, or the
  ;; end of the logical line if there is none.  Submatch 1 is the name of the
  ;; macro.  Set if c-opt-cpp-macro-define is.
775 776 777
  t (if (c-lang-const c-opt-cpp-macro-define)
	(concat (c-lang-const c-opt-cpp-prefix)
		(c-lang-const c-opt-cpp-macro-define)
778 779
		"[ \t]+\\(\\(\\sw\\|_\\)+\\)\\(\([^\)]*\)\\)?"
		;;       ^                 ^ #defined name
780 781 782 783
		"\\([ \t]\\|\\\\\n\\)*")))
(c-lang-defvar c-opt-cpp-macro-define-start
  (c-lang-const c-opt-cpp-macro-define-start))

784 785 786 787 788 789 790 791 792 793
(c-lang-defconst c-opt-cpp-macro-define-id
  ;; Regexp matching everything up to the end of the identifier defined
  ;; by a cpp define.
  t (if (c-lang-const c-opt-cpp-macro-define)
	(concat (c-lang-const c-opt-cpp-prefix)	; #
		(c-lang-const c-opt-cpp-macro-define) ; define
		"[ \t]+\\(\\sw\\|_\\)+")))
(c-lang-defvar c-opt-cpp-macro-define-id
  (c-lang-const c-opt-cpp-macro-define-id))

794
(c-lang-defconst c-cpp-expr-directives
795
  "List of cpp directives (without the prefix) that are followed by an
796 797 798 799 800 801
expression."
  t (if (c-lang-const c-opt-cpp-prefix)
	'("if" "elif")))

(c-lang-defconst c-cpp-expr-functions
  "List of functions in cpp expressions."
802 803 804 805
  t    (if (c-lang-const c-opt-cpp-prefix)
	   '("defined"))
  pike '("defined" "efun" "constant"))

806 807 808 809 810 811
(c-lang-defconst c-assignment-operators
  "List of all assignment operators."
  t    '("=" "*=" "/=" "%=" "+=" "-=" ">>=" "<<=" "&=" "^=" "|=")
  java (append (c-lang-const c-assignment-operators)
	       '(">>>="))
  c++  (append (c-lang-const c-assignment-operators)
812
	       '("and_eq" "or_eq" "xor_eq" "??!=" "??'="))
813 814
  idl  nil)

815 816 817
(c-lang-defconst c-operators
  "List describing all operators, along with their precedence and
associativity.  The order in the list corresponds to the precedence of
818
the operators: The operators in each element are a group with the same
819
precedence, and the group has higher precedence than the groups in all
820 821 822
following elements.  The car of each element describes the type of the
operator group, and the cdr is a list of the operator tokens in it.
The operator group types are:
823 824 825

'prefix         Unary prefix operators.
'postfix        Unary postfix operators.
826 827 828
'postfix-if-paren
		Unary postfix operators if and only if the chars have
		parenthesis syntax.
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
'left-assoc     Binary left associative operators (i.e. a+b+c means (a+b)+c).
'right-assoc    Binary right associative operators (i.e. a=b=c means a=(b=c)).
'right-assoc-sequence
                Right associative operator that constitutes of a
                sequence of tokens that separate expressions.  All the
                tokens in the group are in this case taken as
                describing the sequence in one such operator, and the
                order between them is therefore significant.

Operators containing a character with paren syntax are taken to match
with a corresponding open/close paren somewhere else.  A postfix
operator with close paren syntax is taken to end a postfix expression
started somewhere earlier, rather than start a new one at point.  Vice
versa for prefix operators with open paren syntax.

Note that operators like \".\" and \"->\" which in language references
often are described as postfix operators are considered binary here,
since CC Mode treats every identifier as an expression."

  ;; There's currently no code in CC Mode that exploit all the info
  ;; in this variable; precedence, associativity etc are present as a
  ;; preparation for future work.

  t `(;; Preprocessor.
      ,@(when (c-lang-const c-opt-cpp-prefix)
	  `((prefix "#"
		    ,@(when (c-major-mode-is '(c-mode c++-mode))
			'("%:" "??=")))
	    (left-assoc "##"
			,@(when (c-major-mode-is '(c-mode c++-mode))
			    '("%:%:" "??=??=")))))

861 862
      ;; Primary.
      ,@(c-lang-const c-identifier-ops)
863
      ,@(cond ((or (c-major-mode-is 'c++-mode) (c-major-mode-is 'java-mode))
864
	       `((postfix-if-paren "<" ">"))) ; Templates.
865
	      ((c-major-mode-is 'pike-mode)
866
	       `((prefix "global" "predef")))
867
	      ((c-major-mode-is 'java-mode)
868
	       `((prefix "super"))))
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

      ;; Postfix.
      ,@(when (c-major-mode-is 'c++-mode)
	  ;; The following need special treatment.
	  `((prefix "dynamic_cast" "static_cast"
		    "reinterpret_cast" "const_cast" "typeid")))
      (left-assoc "."
		  ,@(unless (c-major-mode-is 'java-mode)
		      '("->")))
      (postfix "++" "--" "[" "]" "(" ")"
	       ,@(when (c-major-mode-is '(c-mode c++-mode))
		   '("<:" ":>" "??(" "??)")))

      ;; Unary.
      (prefix "++" "--" "+" "-" "!" "~"
	      ,@(when (c-major-mode-is 'c++-mode) '("not" "compl"))
	      ,@(when (c-major-mode-is '(c-mode c++-mode))
		  '("*" "&" "sizeof" "??-"))
	      ,@(when (c-major-mode-is 'objc-mode)
		  '("@selector" "@protocol" "@encode"))
	      ;; The following need special treatment.
	      ,@(cond ((c-major-mode-is 'c++-mode)
		       '("new" "delete"))
		      ((c-major-mode-is 'java-mode)
		       '("new"))
		      ((c-major-mode-is 'pike-mode)
		       '("class" "lambda" "catch" "throw" "gauge")))
	      "(" ")"			; Cast.
	      ,@(when (c-major-mode-is 'pike-mode)
		  '("[" "]")))		; Type cast.

      ;; Member selection.
      ,@(when (c-major-mode-is 'c++-mode)
	  `((left-assoc ".*" "->*")))

      ;; Multiplicative.
      (left-assoc "*" "/" "%")

      ;; Additive.
      (left-assoc "+" "-")

      ;; Shift.
      (left-assoc "<<" ">>"
		  ,@(when (c-major-mode-is 'java-mode)
		      '(">>>")))

      ;; Relational.
      (left-assoc "<" ">" "<=" ">="
		  ,@(when (c-major-mode-is 'java-mode)
		      '("instanceof")))

      ;; Equality.
      (left-assoc "==" "!="
		  ,@(when (c-major-mode-is 'c++-mode) '("not_eq")))

      ;; Bitwise and.
      (left-assoc "&"
		  ,@(when (c-major-mode-is 'c++-mode) '("bitand")))

      ;; Bitwise exclusive or.
      (left-assoc "^"
		  ,@(when (c-major-mode-is '(c-mode c++-mode))
		      '("??'"))
		  ,@(when (c-major-mode-is 'c++-mode) '("xor")))

      ;; Bitwise or.
      (left-assoc "|"
		  ,@(when (c-major-mode-is '(c-mode c++-mode))
		      '("??!"))
		  ,@(when (c-major-mode-is 'c++-mode) '("bitor")))

      ;; Logical and.
      (left-assoc "&&"
		  ,@(when (c-major-mode-is 'c++-mode) '("and")))

      ;; Logical or.
      (left-assoc "||"
		  ,@(when (c-major-mode-is '(c-mode c++-mode))
		      '("??!??!"))
		  ,@(when (c-major-mode-is 'c++-mode) '("or")))

      ;; Conditional.
      (right-assoc-sequence "?" ":")

      ;; Assignment.
954
      (right-assoc ,@(c-lang-const c-assignment-operators))
955 956 957 958 959 960 961 962 963 964 965 966 967

      ;; Exception.
      ,@(when (c-major-mode-is 'c++-mode)
	  '((prefix "throw")))

      ;; Sequence.
      (left-assoc ","))

  ;; IDL got its own definition since it has a much smaller operator
  ;; set than the other languages.
  idl `(;; Preprocessor.
	(prefix "#")
	(left-assoc "##")
968 969
	;; Primary.
	,@(c-lang-const c-identifier-ops)
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
	;; Unary.
	(prefix  "+" "-" "~")
	;; Multiplicative.
	(left-assoc "*" "/" "%")
	;; Additive.
	(left-assoc "+" "-")
	;; Shift.
	(left-assoc "<<" ">>")
	;; And.
	(left-assoc "&")
	;; Xor.
	(left-assoc "^")
	;; Or.
	(left-assoc "|")))

(c-lang-defconst c-operator-list
  ;; The operators as a flat list (without duplicates).
987
  t (c-filter-ops (c-lang-const c-operators) t t))
988 989

(c-lang-defconst c-overloadable-operators
990
  "List of the operators that are overloadable, in their \"identifier
991
form\".  See also `c-op-identifier-prefix'."
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  t    nil
  c++  '("new" "delete" ;; Can be followed by "[]" but we ignore that.
	 "+" "-" "*" "/" "%"
	 "^" "??'" "xor" "&" "bitand" "|" "??!" "bitor" "~" "??-" "compl"
	 "!" "=" "<" ">" "+=" "-=" "*=" "/=" "%=" "^="
	 "??'=" "xor_eq" "&=" "and_eq" "|=" "??!=" "or_eq"
	 "<<" ">>" ">>=" "<<=" "==" "!=" "not_eq" "<=" ">="
	 "&&" "and" "||" "??!??!" "or" "++" "--" "," "->*" "->"
	 "()" "[]" "<::>" "??(??)")
  ;; These work like identifiers in Pike.
  pike '("`+" "`-" "`&" "`|" "`^" "`<<" "`>>" "`*" "`/" "`%" "`~"
	 "`==" "`<" "`>" "`!" "`[]" "`[]=" "`->" "`->=" "`()" "``+"
	 "``-" "``&" "``|" "``^" "``<<" "``>>" "``*" "``/" "``%"
	 "`+="))

(c-lang-defconst c-overloadable-operators-regexp
  ;; Regexp tested after an "operator" token in C++.
  t   nil
  c++ (c-make-keywords-re nil (c-lang-const c-overloadable-operators)))
(c-lang-defvar c-overloadable-operators-regexp
  (c-lang-const c-overloadable-operators-regexp))

1014
(c-lang-defconst c-opt-op-identifier-prefix
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
  "Regexp matching the token before the ones in
`c-overloadable-operators' when operators are specified in their
\"identifier form\".  This typically matches \"operator\" in C++ where
operator functions are specified as e.g. \"operator +\".  It's nil in
languages without operator functions or where the complete operator
identifier is listed in `c-overloadable-operators'.

This regexp is assumed to not match any non-operator identifier."
  t   nil
  c++ (c-make-keywords-re t '("operator")))
1025 1026 1027 1028 1029 1030
(c-lang-defvar c-opt-op-identifier-prefix
  (c-lang-const c-opt-op-identifier-prefix))

;; Note: the following alias is an old name which was a mis-spelling.  It has
;; been corrected above and throughout cc-engine.el.  It will be removed at
;; some release very shortly in the future.  ACM, 2006-04-14.
1031
(defvaralias 'c-opt-op-identitier-prefix 'c-opt-op-identifier-prefix)
1032 1033
(make-obsolete-variable 'c-opt-op-identitier-prefix 'c-opt-op-identifier-prefix
			"CC Mode 5.31.4, 2006-04-14")
1034

1035 1036 1037 1038 1039 1040 1041 1042
(c-lang-defconst c-other-op-syntax-tokens
  "List of the tokens made up of characters in the punctuation or
parenthesis syntax classes that have uses other than as expression
operators."
  t '("{" "}" "(" ")" "[" "]" ";" ":" "," "=" "/*" "*/" "//")
  (c c++ pike) (append '("#" "##"	; Used by cpp.
			 "::" "...")
		       (c-lang-const c-other-op-syntax-tokens))
1043 1044 1045
  (c c++) (append '("*") (c-lang-const c-other-op-syntax-tokens))
  c++  (append '("&" "<%" "%>" "<:" ":>" "%:" "%:%:")
	       (c-lang-const c-other-op-syntax-tokens))
1046 1047 1048 1049 1050 1051 1052 1053 1054
  objc (append '("#" "##"		; Used by cpp.
		 "+" "-") (c-lang-const c-other-op-syntax-tokens))
  idl  (append '("#" "##")		; Used by cpp.
	       (c-lang-const c-other-op-syntax-tokens))
  pike (append '("..")
	       (c-lang-const c-other-op-syntax-tokens)
	       (c-lang-const c-overloadable-operators))
  awk '("{" "}" "(" ")" "[" "]" ";" "," "=" "/"))

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
(c-lang-defconst c-all-op-syntax-tokens
  ;; List of all tokens in the punctuation and parenthesis syntax
  ;; classes.
  t (delete-duplicates (append (c-lang-const c-other-op-syntax-tokens)
			       (c-lang-const c-operator-list))
		       :test 'string-equal))

(c-lang-defconst c-nonsymbol-token-char-list
  ;; List containing all chars not in the word, symbol or
  ;; syntactically irrelevant syntax classes, i.e. all punctuation,
  ;; parenthesis and string delimiter chars.
  t (c-with-syntax-table (c-lang-const c-mode-syntax-table)
      ;; Only go through the chars in the printable ASCII range.  No
      ;; language so far has 8-bit or widestring operators.
      (let (list (char 32))
	(while (< char 127)
	  (or (memq (char-syntax char) '(?w ?_ ?< ?> ?\ ))
	      (setq list (cons (c-int-to-char char) list)))
	  (setq char (1+ char)))
	list)))

1076 1077 1078 1079 1080
(c-lang-defconst c-nonsymbol-token-regexp
  ;; Regexp matching all tokens in the punctuation and parenthesis
  ;; syntax classes.  Note that this also matches ".", which can start
  ;; a float.
  t (c-make-keywords-re nil
1081 1082 1083
      (c-filter-ops (c-lang-const c-all-op-syntax-tokens)
		    t
		    "\\`\\(\\s.\\|\\s\(\\|\\s\)\\)+\\'")))
1084 1085 1086
(c-lang-defvar c-nonsymbol-token-regexp
  (c-lang-const c-nonsymbol-token-regexp))

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
(c-lang-defconst c-assignment-op-regexp
  ;; Regexp matching all assignment operators and only them.  The
  ;; beginning of the first submatch is used to detect the end of the
  ;; token, along with the end of the whole match.
  t (if (c-lang-const c-assignment-operators)
	(concat
	 ;; Need special case for "=" since it's a prefix of "==".
	 "=\\([^=]\\|$\\)"
	 "\\|"
	 (c-make-keywords-re nil
	   (set-difference (c-lang-const c-assignment-operators)
			   '("=")
			   :test 'string-equal)))
      "\\<\\>"))
(c-lang-defvar c-assignment-op-regexp
  (c-lang-const c-assignment-op-regexp))

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
(c-lang-defconst c-<>-multichar-token-regexp
  ;; Regexp matching all tokens containing "<" or ">" which are longer
  ;; than one char.
  t (c-make-keywords-re nil
      (c-filter-ops (c-lang-const c-all-op-syntax-tokens)
		    t
		    ".[<>]\\|[<>].")))
(c-lang-defvar c-<>-multichar-token-regexp
  (c-lang-const c-<>-multichar-token-regexp))

1114 1115 1116 1117
(c-lang-defconst c-<-op-cont-regexp
  ;; Regexp matching the second and subsequent characters of all
  ;; multicharacter tokens that begin with "<".
  t (c-make-keywords-re nil
1118 1119 1120 1121
      (c-filter-ops (c-lang-const c-all-op-syntax-tokens)
		    t
		    "\\`<."
		    (lambda (op) (substring op 1)))))
1122

1123 1124 1125 1126 1127 1128
(c-lang-defvar c-<-op-cont-regexp (c-lang-const c-<-op-cont-regexp))

(c-lang-defconst c->-op-cont-regexp
  ;; Regexp matching the second and subsequent characters of all
  ;; multicharacter tokens that begin with ">".
  t (c-make-keywords-re nil
1129 1130 1131
      (c-filter-ops (c-lang-const c-all-op-syntax-tokens)
		    t
		    "\\`>."
1132 1133 1134 1135 1136 1137 1138
		    (lambda (op) (substring op 1))))
  java (c-make-keywords-re nil
	 (c-filter-ops (c-lang-const c-all-op-syntax-tokens)
		       t
		       "\\`>[^>]\\|\\`>>[^>]"
		       (lambda (op) (substring op 1)))))

1139 1140 1141 1142 1143 1144 1145 1146
(c-lang-defvar c->-op-cont-regexp (c-lang-const c->-op-cont-regexp))

(c-lang-defconst c-stmt-delim-chars
  ;; The characters that should be considered to bound statements.  To
  ;; optimize `c-crosses-statement-barrier-p' somewhat, it's assumed to
  ;; begin with "^" to negate the set.  If ? : operators should be
  ;; detected then the string must end with "?:".
  t    "^;{}?:"
1147
  awk  "^;{}#\n\r?:") ; The newline chars gets special treatment.
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
(c-lang-defvar c-stmt-delim-chars (c-lang-const c-stmt-delim-chars))

(c-lang-defconst c-stmt-delim-chars-with-comma
  ;; Variant of `c-stmt-delim-chars' that additionally contains ','.
  t    "^;,{}?:"
  awk  "^;,{}\n\r?:") ; The newline chars gets special treatment.
(c-lang-defvar c-stmt-delim-chars-with-comma
  (c-lang-const c-stmt-delim-chars-with-comma))


;;; Syntactic whitespace.

1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
(c-lang-defconst c-simple-ws
  "Regexp matching an ordinary whitespace character.
Does not contain a \\| operator at the top level."
  ;; "\\s " is not enough since it doesn't match line breaks.
  t "\\(\\s \\|[\n\r]\\)")

(c-lang-defconst c-simple-ws-depth
  ;; Number of regexp grouping parens in `c-simple-ws'.
  t (regexp-opt-depth (c-lang-const c-simple-ws)))

(c-lang-defconst c-line-comment-starter
  "String that starts line comments, or nil if such don't exist.
Line comments are always terminated by newlines.  At least one of
`c-block-comment-starter' and this one is assumed to be set.

Note that it's currently not enough to set this to support a new
comment style.  Other stuff like the syntax table must also be set up
properly."
  t    "//"
  awk  "#")
(c-lang-defvar c-line-comment-starter (c-lang-const c-line-comment-starter))

(c-lang-defconst c-block-comment-starter
  "String that starts block comments, or nil if such don't exist.
Block comments are ended by `c-block-comment-ender', which is assumed
to be set if this is.  At least one of `c-line-comment-starter' and
this one is assumed to be set.

Note that it's currently not enough to set this to support a new
comment style.  Other stuff like the syntax table must also be set up
properly."
  t    "/*"
  awk  nil)

(c-lang-defconst c-block-comment-ender
  "String that ends block comments, or nil if such don't exist.

Note that it's currently not enough to set this to support a new
comment style.  Other stuff like the syntax table must also be set up
properly."
  t    "*/"
  awk  nil)

1203 1204
(c-lang-defconst c-comment-start-regexp
  ;; Regexp to match the start of any type of comment.
1205 1206 1207 1208 1209 1210
  t (let ((re (c-make-keywords-re nil
		(list (c-lang-const c-line-comment-starter)
		      (c-lang-const c-block-comment-starter)))))
      (if (memq 'gen-comment-delim c-emacs-features)
	  (concat re "\\|\\s!")
	re)))
1211 1212
(c-lang-defvar c-comment-start-regexp (c-lang-const c-comment-start-regexp))

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
;;;; Added by ACM, 2003/9/18.
(c-lang-defconst c-block-comment-start-regexp
  ;; Regexp which matches the start of a block comment (if such exists in the
  ;; language)
  t (if (c-lang-const c-block-comment-starter)
	(regexp-quote (c-lang-const c-block-comment-starter))
      "\\<\\>"))
(c-lang-defvar c-block-comment-start-regexp
  (c-lang-const c-block-comment-start-regexp))

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
(c-lang-defconst c-literal-start-regexp
  ;; Regexp to match the start of comments and string literals.
  t (concat (c-lang-const c-comment-start-regexp)
	    "\\|"
	    (if (memq 'gen-string-delim c-emacs-features)
		"\"|"
	      "\"")))
(c-lang-defvar c-literal-start-regexp (c-lang-const c-literal-start-regexp))

(c-lang-defconst c-doc-comment-start-regexp
  "Regexp to match the start of documentation comments."
  t    "\\<\\>"
  ;; From font-lock.el: `doxygen' uses /*! while others use /**.
  (c c++ objc) "/\\*[*!]"
  java "/\\*\\*"
  pike "/[/*]!")
(c-lang-defvar c-doc-comment-start-regexp
  (c-lang-const c-doc-comment-start-regexp))

(c-lang-defconst comment-start
  "String that starts comments inserted with M-; etc.
`comment-start' is initialized from this."
1245 1246 1247 1248 1249 1250 1251
  ;; Default: Prefer line comments to block comments, and pad with a space.
  t (concat (or (c-lang-const c-line-comment-starter)
		(c-lang-const c-block-comment-starter))
	    " ")
  ;; In C we still default to the block comment style since line
  ;; comments aren't entirely portable.
  c "/* ")
1252
(c-lang-setvar comment-start (c-lang-const comment-start))
1253 1254 1255 1256

(c-lang-defconst comment-end
  "String that ends comments inserted with M-; etc.
`comment-end' is initialized from this."
1257 1258 1259 1260 1261 1262 1263 1264
  ;; Default: Use block comment style if comment-start uses block
  ;; comments, and pad with a space in that case.
  t (if (string-match (concat "\\`\\("
			      (c-lang-const c-block-comment-start-regexp)
			      "\\)")
		      (c-lang-const comment-start))
	(concat " " (c-lang-const c-block-comment-ender))
      ""))
1265
(c-lang-setvar comment-end (c-lang-const comment-end))
1266 1267 1268 1269

(c-lang-defconst comment-start-skip
  "Regexp to match the start of a comment plus everything up to its body.
`comment-start-skip' is initialized from this."
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
  ;; Default: Allow the last char of the comment starter(s) to be
  ;; repeated, then allow any amount of horizontal whitespace.
  t (concat "\\("
	    (c-concat-separated
	     (mapcar (lambda (cs)
		       (when cs
			 (concat (regexp-quote cs) "+")))
		     (list (c-lang-const c-line-comment-starter)
			   (c-lang-const c-block-comment-starter)))
	     "\\|")
	    "\\)\\s *"))
1281
(c-lang-setvar comment-start-skip (c-lang-const comment-start-skip))
1282

1283
(c-lang-defconst c-syntactic-ws-start
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
  ;; Regexp matching any sequence that can start syntactic whitespace.
  ;; The only uncertain case is '#' when there are cpp directives.
  t (concat "\\s \\|"
	    (c-make-keywords-re nil
	      (append (list (c-lang-const c-line-comment-starter)
			    (c-lang-const c-block-comment-starter)
			    (when (c-lang-const c-opt-cpp-prefix)
			      "#"))
		      '("\n" "\r")))
	    "\\|\\\\[\n\r]"
	    (when (memq 'gen-comment-delim c-emacs-features)
	      "\\|\\s!")))
(c-lang-defvar c-syntactic-ws-start (c-lang-const c-syntactic-ws-start))
1297

1298
(c-lang-defconst c-syntactic-ws-end
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
  ;; Regexp matching any single character that might end syntactic whitespace.
  t (concat "\\s \\|"
	    (c-make-keywords-re nil
	      (append (when (c-lang-const c-block-comment-ender)
			(list
			 (string
			  (elt (c-lang-const c-block-comment-ender)
			       (1- (length
				    (c-lang-const c-block-comment-ender)))))))
		      '("\n" "\r")))
	    (when (memq 'gen-comment-delim c-emacs-features)
	      "\\|\\s!")))
(c-lang-defvar c-syntactic-ws-end (c-lang-const c-syntactic-ws-end))

(c-lang-defconst c-unterminated-block-comment-regexp
  ;; Regexp matching an unterminated block comment that doesn't
  ;; contain line breaks, or nil in languages without block comments.
  ;; Does not contain a \| operator at the top level.
  t (when (c-lang-const c-block-comment-starter)
      (concat
       (regexp-quote (c-lang-const c-block-comment-starter))
       ;; It's messy to cook together a regexp that matches anything
       ;; but c-block-comment-ender.
       (let ((end (c-lang-const c-block-comment-ender)))
	 (cond ((= (length end) 1)
		(concat "[^" end "\n\r]*"))
	       ((= (length end) 2)
		(concat "[^" (substring end 0 1) "\n\r]*"
			"\\("
			(regexp-quote (substring end 0 1)) "+"
			"[^"
			;; The quoting rules inside char classes are silly. :P
			(cond ((= (elt end 0) (elt end 1))
			       (concat (substring end 0 1) "\n\r"))
			      ((= (elt end 1) ?\])
			       (concat (substring end 1 2) "\n\r"
				       (substring end 0 1)))
			      (t
			       (concat (substring end 0 1) "\n\r"
				       (substring end 1 2))))
			"]"
			"[^" (substring end 0 1) "\n\r]*"
			"\\)*"))
	       (t
		(error "Can't handle a block comment ender of length %s"
		       (length end))))))))

(c-lang-defconst c-block-comment-regexp
  ;; Regexp matching a block comment that doesn't contain line breaks,
  ;; or nil in languages without block comments.  The reason we don't
  ;; allow line breaks is to avoid going very far and risk running out
  ;; of regexp stack; this regexp is intended to handle only short
  ;; comments that might be put in the middle of limited constructs
  ;; like declarations.  Does not contain a \| operator at the top
  ;; level.
  t (when (c-lang-const c-unterminated-block-comment-regexp)
      (concat
       (c-lang-const c-unterminated-block-comment-regexp)
       (let ((end (c-lang-const c-block-comment-ender)))
	 (cond ((= (length end) 1)
		(regexp-quote end))
	       ((= (length end) 2)
		(concat (regexp-quote (substring end 0 1)) "+"
			(regexp-quote (substring end 1 2))))
	       (t
		(error "Can't handle a block comment ender of length %s"
		       (length end))))))))
1366 1367 1368 1369 1370 1371

(c-lang-defconst c-nonwhite-syntactic-ws
  ;; Regexp matching a piece of syntactic whitespace that isn't a
  ;; sequence of simple whitespace characters.  As opposed to
  ;; `c-(forward|backward)-syntactic-ws', this doesn't regard cpp
  ;; directives as syntactic whitespace.
1372 1373 1374 1375 1376 1377 1378 1379 1380
  t (c-concat-separated
     (list (when (c-lang-const c-line-comment-starter)
	     (concat (regexp-quote (c-lang-const c-line-comment-starter))
		     "[^\n\r]*[\n\r]"))
	   (c-lang-const c-block-comment-regexp)
	   "\\\\[\n\r]"
	   (when (memq 'gen-comment-delim c-emacs-features)
	     "\\s!\\S!*\\s!"))
     "\\|"))
1381 1382 1383 1384 1385 1386

(c-lang-defconst c-syntactic-ws
  ;; Regexp matching syntactic whitespace, including possibly the
  ;; empty string.  As opposed to `c-(forward|backward)-syntactic-ws',
  ;; this doesn't regard cpp directives as syntactic whitespace.  Does
  ;; not contain a \| operator at the top level.
1387 1388 1389 1390 1391
  t (concat (c-lang-const c-simple-ws) "*"
	    "\\("
	    (concat "\\(" (c-lang-const c-nonwhite-syntactic-ws) "\\)"
		    (c-lang-const c-simple-ws) "*")
	    "\\)*"))
1392 1393 1394

(c-lang-defconst c-syntactic-ws-depth
  ;; Number of regexp grouping parens in `c-syntactic-ws'.
1395
  t (regexp-opt-depth (c-lang-const c-syntactic-ws)))
1396 1397 1398 1399 1400 1401

(c-lang-defconst c-nonempty-syntactic-ws
  ;; Regexp matching syntactic whitespace, which is at least one
  ;; character long.  As opposed to `c-(forward|backward)-syntactic-ws',
  ;; this doesn't regard cpp directives as syntactic whitespace.  Does
  ;; not contain a \| operator at the top level.
1402 1403 1404
  t (concat "\\("
	    (c-lang-const c-simple-ws)
	    "\\|"
1405 1406 1407 1408 1409
	    (c-lang-const c-nonwhite-syntactic-ws)
	    "\\)+"))

(c-lang-defconst c-nonempty-syntactic-ws-depth
  ;; Number of regexp grouping parens in `c-nonempty-syntactic-ws'.
1410
  t (regexp-opt-depth (c-lang-const c-nonempty-syntactic-ws)))
1411 1412 1413 1414 1415 1416

(c-lang-defconst c-single-line-syntactic-ws
  ;; Regexp matching syntactic whitespace without any line breaks.  As
  ;; opposed to `c-(forward|backward)-syntactic-ws', this doesn't
  ;; regard cpp directives as syntactic whitespace.  Does not contain
  ;; a \| operator at the top level.
1417 1418 1419 1420 1421
  t (if (c-lang-const c-block-comment-regexp)
	(concat "\\s *\\("
		(c-lang-const c-block-comment-regexp)
		"\\s *\\)*")
      "\\s *"))
1422 1423 1424

(c-lang-defconst c-single-line-syntactic-ws-depth
  ;; Number of regexp grouping parens in `c-single-line-syntactic-ws'.
1425
  t (regexp-opt-depth (c-lang-const c-single-line-syntactic-ws)))
1426

1427
(c-lang-defconst c-syntactic-eol
1428 1429 1430
  ;; Regexp that matches when there is no syntactically significant
  ;; text before eol.  Macros are regarded as syntactically
  ;; significant text here.
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
  t (concat (c-lang-const c-single-line-syntactic-ws)
	    ;; Match eol (possibly inside a block comment or preceded
	    ;; by a line continuation backslash), or the beginning of a
	    ;; line comment.  Note: This has to be modified for awk
	    ;; where line comments start with '#'.
	    "\\("
	    (c-concat-separated
	     (list (when (c-lang-const c-line-comment-starter)
		     (regexp-quote (c-lang-const c-line-comment-starter)))
		   (when (c-lang-const c-unterminated-block-comment-regexp)
		     (concat (c-lang-const c-unterminated-block-comment-regexp)
			     "$"))
		   "\\\\$"
1444
		   "$")
1445 1446 1447 1448 1449 1450 1451 1452 1453
	     "\\|")
	    "\\)"))
(c-lang-defvar c-syntactic-eol (c-lang-const c-syntactic-eol))


;;; Syntactic analysis ("virtual semicolons") for line-oriented languages (AWK).
(c-lang-defconst c-at-vsemi-p-fn
  "Contains a function \"Is there a virtual semicolon at POS or point?\".
Such a function takes one optional parameter, a buffer position (defaults to
1454
point), and returns nil or t.  This variable contains nil for languages which
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
don't have EOL terminated statements. "
  t nil
  awk 'c-awk-at-vsemi-p)
(c-lang-defvar c-at-vsemi-p-fn (c-lang-const c-at-vsemi-p-fn))

(c-lang-defconst c-vsemi-status-unknown-p-fn
  "Contains a function \"are we unsure whether there is a virtual semicolon on this line?\".
The (admittedly kludgey) purpose of such a function is to prevent an infinite
recursion in c-beginning-of-statement-1 when point starts at a `while' token.
The function MUST NOT UNDER ANY CIRCUMSTANCES call c-beginning-of-statement-1,
1465
even indirectly.  This variable contains nil for languages which don't have
1466 1467 1468 1469 1470
EOL terminated statements."
  t nil
  awk 'c-awk-vsemi-status-unknown-p)
(c-lang-defvar c-vsemi-status-unknown-p-fn
  (c-lang-const c-vsemi-status-unknown-p-fn))
1471