Backport 2012-05-02T11:33:49Z!lekktu@gmail.com from trunk
[bpt/emacs.git] / doc / lispref / syntax.texi
CommitLineData
b8d4c8d0
GM
1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
acaf905b 3@c Copyright (C) 1990-1995, 1998-1999, 2001-2012
d24880de 4@c Free Software Foundation, Inc.
b8d4c8d0 5@c See the file elisp.texi for copying conditions.
6336d8c3 6@setfilename ../../info/syntax
b8d4c8d0
GM
7@node Syntax Tables, Abbrevs, Searching and Matching, Top
8@chapter Syntax Tables
9@cindex parsing buffer text
10@cindex syntax table
11@cindex text parsing
12
4230351b
CY
13 A @dfn{syntax table} specifies the syntactic role of each character
14in a buffer. It can be used to determine where words, symbols, and
15other syntactic constructs begin and end. This information is used by
16many Emacs facilities, including Font Lock mode (@pxref{Font Lock
17Mode}) and the various complex movement commands (@pxref{Motion}).
b8d4c8d0
GM
18
19@menu
20* Basics: Syntax Basics. Basic concepts of syntax tables.
4230351b 21* Syntax Descriptors:: How characters are classified.
b8d4c8d0
GM
22* Syntax Table Functions:: How to create, examine and alter syntax tables.
23* Syntax Properties:: Overriding syntax with text properties.
d24880de 24* Motion and Syntax:: Moving over characters with certain syntaxes.
b8d4c8d0
GM
25* Parsing Expressions:: Parsing balanced expressions
26 using the syntax table.
27* Standard Syntax Tables:: Syntax tables used by various major modes.
28* Syntax Table Internals:: How syntax table information is stored.
29* Categories:: Another way of classifying character syntax.
30@end menu
31
32@node Syntax Basics
33@section Syntax Table Concepts
34
b8d4c8d0
GM
35 A syntax table is a char-table (@pxref{Char-Tables}). The element at
36index @var{c} describes the character with code @var{c}. The element's
37value should be a list that encodes the syntax of the character in
38question.
39
40 Syntax tables are used only for moving across text, not for the Emacs
41Lisp reader. Emacs Lisp uses built-in syntactic rules when reading Lisp
42expressions, and these rules cannot be changed. (Some Lisp systems
43provide ways to redefine the read syntax, but we decided to leave this
44feature out of Emacs Lisp for simplicity.)
45
46 Each buffer has its own major mode, and each major mode has its own
4230351b
CY
47idea of the syntactic class of various characters. For example, in
48Lisp mode, the character @samp{;} begins a comment, but in C mode, it
b8d4c8d0 49terminates a statement. To support these variations, Emacs makes the
4230351b
CY
50syntax table local to each buffer. Typically, each major mode has its
51own syntax table and installs that table in each buffer that uses that
52mode. Changing this table alters the syntax in all those buffers as
53well as in any buffers subsequently put in that mode. Occasionally
54several similar modes share one syntax table. @xref{Example Major
55Modes}, for an example of how to set up a syntax table.
b8d4c8d0
GM
56
57A syntax table can inherit the data for some characters from the
58standard syntax table, while specifying other characters itself. The
59``inherit'' syntax class means ``inherit this character's syntax from
16152b76 60the standard syntax table''. Just changing the standard syntax for a
b8d4c8d0
GM
61character affects all syntax tables that inherit from it.
62
63@defun syntax-table-p object
64This function returns @code{t} if @var{object} is a syntax table.
65@end defun
66
67@node Syntax Descriptors
68@section Syntax Descriptors
69@cindex syntax class
70
4230351b
CY
71 The syntactic role of a character is called its @dfn{syntax class}.
72Each syntax table specifies the syntax class of each character. There
b8d4c8d0
GM
73is no necessary relationship between the class of a character in one
74syntax table and its class in any other table.
75
4230351b
CY
76 Each syntax class is designated by a mnemonic character, which
77serves as the name of the class when you need to specify a class.
78Usually, this designator character is one that is often assigned that
79class; however, its meaning as a designator is unvarying and
80independent of what syntax that character currently has. Thus,
81@samp{\} as a designator character always means ``escape character''
82syntax, regardless of whether the @samp{\} character actually has that
83syntax in the current syntax table.
84@ifnottex
85@xref{Syntax Class Table}, for a list of syntax classes.
86@end ifnottex
b8d4c8d0
GM
87
88@cindex syntax descriptor
4230351b
CY
89 A @dfn{syntax descriptor} is a Lisp string that describes the syntax
90classes and other syntactic properties of a character. When you want
91to modify the syntax of a character, that is done by calling the
92function @code{modify-syntax-entry} and passing a syntax descriptor as
93one of its arguments (@pxref{Syntax Table Functions}).
94
95 The first character in a syntax descriptor designates the syntax
96class. The second character specifies a matching character (e.g.@: in
97Lisp, the matching character for @samp{(} is @samp{)}); if there is no
98matching character, put a space there. Then come the characters for
99any desired flags.
100
101 If no matching character or flags are needed, only one character
102(specifying the syntax class) is sufficient.
b8d4c8d0
GM
103
104 For example, the syntax descriptor for the character @samp{*} in C
ad21a12a 105mode is @code{". 23"} (i.e., punctuation, matching character slot
b8d4c8d0
GM
106unused, second character of a comment-starter, first character of a
107comment-ender), and the entry for @samp{/} is @samp{@w{. 14}} (i.e.,
108punctuation, matching character slot unused, first character of a
109comment-starter, second character of a comment-ender).
110
111@menu
112* Syntax Class Table:: Table of syntax classes.
113* Syntax Flags:: Additional flags each character can have.
114@end menu
115
116@node Syntax Class Table
117@subsection Table of Syntax Classes
118
4230351b
CY
119 Here is a table of syntax classes, the characters that designate
120them, their meanings, and examples of their use.
b8d4c8d0 121
4230351b
CY
122@table @asis
123@item Whitespace characters: @samp{@ } or @samp{-}
124Characters that separate symbols and words from each other.
125Typically, whitespace characters have no other syntactic significance,
126and multiple whitespace characters are syntactically equivalent to a
127single one. Space, tab, and formfeed are classified as whitespace in
128almost all major modes.
129
130This syntax class can be designated by either @w{@samp{@ }} or
131@samp{-}. Both designators are equivalent.
132
133@item Word constituents: @samp{w}
134Parts of words in human languages. These are typically used in
135variable and command names in programs. All upper- and lower-case
136letters, and the digits, are typically word constituents.
137
138@item Symbol constituents: @samp{_}
139Extra characters used in variable and command names along with word
140constituents. Examples include the characters @samp{$&*+-_<>} in Lisp
141mode, which may be part of a symbol name even though they are not part
142of English words. In standard C, the only non-word-constituent
b8d4c8d0 143character that is valid in symbols is underscore (@samp{_}).
b8d4c8d0 144
4230351b
CY
145@item Punctuation characters: @samp{.}
146Characters used as punctuation in a human language, or used in a
147programming language to separate symbols from one another. Some
148programming language modes, such as Emacs Lisp mode, have no
149characters in this class since the few characters that are not symbol
150or word constituents all have other uses. Other programming language
151modes, such as C mode, use punctuation syntax for operators.
152
153@item Open parenthesis characters: @samp{(}
154@itemx Close parenthesis characters: @samp{)}
155Characters used in dissimilar pairs to surround sentences or
156expressions. Such a grouping is begun with an open parenthesis
157character and terminated with a close. Each open parenthesis
158character matches a particular close parenthesis character, and vice
159versa. Normally, Emacs indicates momentarily the matching open
160parenthesis when you insert a close parenthesis. @xref{Blinking}.
161
162In human languages, and in C code, the parenthesis pairs are
163@samp{()}, @samp{[]}, and @samp{@{@}}. In Emacs Lisp, the delimiters
164for lists and vectors (@samp{()} and @samp{[]}) are classified as
165parenthesis characters.
166
167@item String quotes: @samp{"}
168Characters used to delimit string constants. The same string quote
169character appears at the beginning and the end of a string. Such
170quoted strings do not nest.
b8d4c8d0
GM
171
172The parsing facilities of Emacs consider a string as a single token.
173The usual syntactic meanings of the characters in the string are
174suppressed.
175
176The Lisp modes have two string quote characters: double-quote (@samp{"})
177and vertical bar (@samp{|}). @samp{|} is not used in Emacs Lisp, but it
178is used in Common Lisp. C also has two string quote characters:
179double-quote for strings, and single-quote (@samp{'}) for character
180constants.
181
4230351b
CY
182Human text has no string quote characters. We do not want quotation
183marks to turn off the usual syntactic properties of other characters
184in the quotation.
b8d4c8d0 185
4230351b
CY
186@item Escape-syntax characters: @samp{\}
187Characters that start an escape sequence, such as is used in string
188and character constants. The character @samp{\} belongs to this class
189in both C and Lisp. (In C, it is used thus only inside strings, but
190it turns out to cause no trouble to treat it this way throughout C
191code.)
b8d4c8d0
GM
192
193Characters in this class count as part of words if
194@code{words-include-escapes} is non-@code{nil}. @xref{Word Motion}.
b8d4c8d0 195
4230351b
CY
196@item Character quotes: @samp{/}
197Characters used to quote the following character so that it loses its
198normal syntactic meaning. This differs from an escape character in
199that only the character immediately following is ever affected.
b8d4c8d0
GM
200
201Characters in this class count as part of words if
202@code{words-include-escapes} is non-@code{nil}. @xref{Word Motion}.
203
204This class is used for backslash in @TeX{} mode.
b8d4c8d0 205
4230351b
CY
206@item Paired delimiters: @samp{$}
207Similar to string quote characters, except that the syntactic
208properties of the characters between the delimiters are not
209suppressed. Only @TeX{} mode uses a paired delimiter presently---the
210@samp{$} that both enters and leaves math mode.
211
212@item Expression prefixes: @samp{'}
213Characters used for syntactic operators that are considered as part of
214an expression if they appear next to one. In Lisp modes, these
215characters include the apostrophe, @samp{'} (used for quoting), the
216comma, @samp{,} (used in macros), and @samp{#} (used in the read
217syntax for certain data types).
218
219@item Comment starters: @samp{<}
220@itemx Comment enders: @samp{>}
b8d4c8d0 221@cindex comment syntax
4230351b
CY
222Characters used in various languages to delimit comments. Human text
223has no comment characters. In Lisp, the semicolon (@samp{;}) starts a
224comment and a newline or formfeed ends one.
b8d4c8d0 225
4230351b
CY
226@item Inherit standard syntax: @samp{@@}
227This syntax class does not specify a particular syntax. It says to
228look in the standard syntax table to find the syntax of this
229character.
b8d4c8d0 230
4230351b
CY
231@item Generic comment delimiters: @samp{!}
232Characters that start or end a special kind of comment. @emph{Any}
233generic comment delimiter matches @emph{any} generic comment
234delimiter, but they cannot match a comment starter or comment ender;
235generic comment delimiters can only match each other.
b8d4c8d0
GM
236
237This syntax class is primarily meant for use with the
4230351b
CY
238@code{syntax-table} text property (@pxref{Syntax Properties}). You
239can mark any range of characters as forming a comment, by giving the
240first and last characters of the range @code{syntax-table} properties
b8d4c8d0 241identifying them as generic comment delimiters.
b8d4c8d0 242
4230351b
CY
243@item Generic string delimiters: @samp{|}
244Characters that start or end a string. This class differs from the
245string quote class in that @emph{any} generic string delimiter can
246match any other generic string delimiter; but they do not match
247ordinary string quote characters.
b8d4c8d0
GM
248
249This syntax class is primarily meant for use with the
4230351b
CY
250@code{syntax-table} text property (@pxref{Syntax Properties}). You
251can mark any range of characters as forming a string constant, by
252giving the first and last characters of the range @code{syntax-table}
253properties identifying them as generic string delimiters.
254@end table
b8d4c8d0
GM
255
256@node Syntax Flags
257@subsection Syntax Flags
258@cindex syntax flags
259
260 In addition to the classes, entries for characters in a syntax table
7cf78aac
SM
261can specify flags. There are eight possible flags, represented by the
262characters @samp{1}, @samp{2}, @samp{3}, @samp{4}, @samp{b}, @samp{c},
263@samp{n}, and @samp{p}.
264
265 All the flags except @samp{p} are used to describe comment
266delimiters. The digit flags are used for comment delimiters made up
267of 2 characters. They indicate that a character can @emph{also} be
268part of a comment sequence, in addition to the syntactic properties
269associated with its character class. The flags are independent of the
270class and each other for the sake of characters such as @samp{*} in
271C mode, which is a punctuation character, @emph{and} the second
272character of a start-of-comment sequence (@samp{/*}), @emph{and} the
273first character of an end-of-comment sequence (@samp{*/}). The flags
274@samp{b}, @samp{c}, and @samp{n} are used to qualify the corresponding
275comment delimiter.
b8d4c8d0
GM
276
277 Here is a table of the possible flags for a character @var{c},
278and what they mean:
279
280@itemize @bullet
281@item
282@samp{1} means @var{c} is the start of a two-character comment-start
283sequence.
284
285@item
286@samp{2} means @var{c} is the second character of such a sequence.
287
288@item
289@samp{3} means @var{c} is the start of a two-character comment-end
290sequence.
291
292@item
293@samp{4} means @var{c} is the second character of such a sequence.
294
295@item
b8d4c8d0 296@samp{b} means that @var{c} as a comment delimiter belongs to the
7cf78aac
SM
297alternative ``b'' comment style. For a two-character comment starter,
298this flag is only significant on the second char, and for a 2-character
299comment ender it is only significant on the first char.
b8d4c8d0 300
7cf78aac
SM
301@item
302@samp{c} means that @var{c} as a comment delimiter belongs to the
303alternative ``c'' comment style. For a two-character comment
304delimiter, @samp{c} on either character makes it of style ``c''.
b8d4c8d0 305
7cf78aac
SM
306@item
307@samp{n} on a comment delimiter character specifies
308that this kind of comment can be nested. For a two-character
309comment delimiter, @samp{n} on either character makes it
310nestable.
b8d4c8d0 311
7cf78aac
SM
312Emacs supports several comment styles simultaneously in any one syntax
313table. A comment style is a set of flags @samp{b}, @samp{c}, and
314@samp{n}, so there can be up to 8 different comment styles.
315Each comment delimiter has a style and only matches comment delimiters
316of the same style. Thus if a comment starts with the comment-start
317sequence of style ``bn'', it will extend until the next matching
318comment-end sequence of style ``bn''.
b8d4c8d0 319
7cf78aac 320The appropriate comment syntax settings for C++ can be as follows:
b8d4c8d0
GM
321
322@table @asis
323@item @samp{/}
7cf78aac 324@samp{124}
b8d4c8d0 325@item @samp{*}
7cf78aac 326@samp{23b}
b8d4c8d0 327@item newline
7cf78aac 328@samp{>}
b8d4c8d0
GM
329@end table
330
331This defines four comment-delimiting sequences:
332
333@table @asis
334@item @samp{/*}
7cf78aac
SM
335This is a comment-start sequence for ``b'' style because the
336second character, @samp{*}, has the @samp{b} flag.
b8d4c8d0
GM
337
338@item @samp{//}
7cf78aac
SM
339This is a comment-start sequence for ``a'' style because the second
340character, @samp{/}, does not have the @samp{b} flag.
b8d4c8d0
GM
341
342@item @samp{*/}
7cf78aac 343This is a comment-end sequence for ``b'' style because the first
35a30759 344character, @samp{*}, has the @samp{b} flag.
b8d4c8d0
GM
345
346@item newline
7cf78aac
SM
347This is a comment-end sequence for ``a'' style, because the newline
348character does not have the @samp{b} flag.
b8d4c8d0
GM
349@end table
350
b8d4c8d0
GM
351@item
352@c Emacs 19 feature
353@samp{p} identifies an additional ``prefix character'' for Lisp syntax.
354These characters are treated as whitespace when they appear between
355expressions. When they appear within an expression, they are handled
356according to their usual syntax classes.
357
358The function @code{backward-prefix-chars} moves back over these
359characters, as well as over characters whose primary syntax class is
360prefix (@samp{'}). @xref{Motion and Syntax}.
361@end itemize
362
363@node Syntax Table Functions
364@section Syntax Table Functions
365
366 In this section we describe functions for creating, accessing and
367altering syntax tables.
368
369@defun make-syntax-table &optional table
370This function creates a new syntax table, with all values initialized
371to @code{nil}. If @var{table} is non-@code{nil}, it becomes the
372parent of the new syntax table, otherwise the standard syntax table is
373the parent. Like all char-tables, a syntax table inherits from its
374parent. Thus the original syntax of all characters in the returned
375syntax table is determined by the parent. @xref{Char-Tables}.
376
377Most major mode syntax tables are created in this way.
378@end defun
379
380@defun copy-syntax-table &optional table
381This function constructs a copy of @var{table} and returns it. If
382@var{table} is not supplied (or is @code{nil}), it returns a copy of the
383standard syntax table. Otherwise, an error is signaled if @var{table} is
384not a syntax table.
385@end defun
386
387@deffn Command modify-syntax-entry char syntax-descriptor &optional table
388This function sets the syntax entry for @var{char} according to
4230351b 389@var{syntax-descriptor}. @var{char} must be a character, or a cons
f147ff75
CY
390cell of the form @code{(@var{min} . @var{max})}; in the latter case,
391the function sets the syntax entries for all characters in the range
392between @var{min} and @var{max}, inclusive.
393
394The syntax is changed only for @var{table}, which defaults to the
4230351b
CY
395current buffer's syntax table, and not in any other syntax table.
396
397The argument @var{syntax-descriptor} is a syntax descriptor for the
398desired syntax (i.e.@: a string beginning with a class designator
399character, and optionally containing a matching character and syntax
400flags). An error is signaled if the first character is not one of the
401seventeen syntax class designators. @xref{Syntax Descriptors}.
b8d4c8d0
GM
402
403This function always returns @code{nil}. The old syntax information in
404the table for this character is discarded.
405
b8d4c8d0
GM
406@example
407@group
408@exdent @r{Examples:}
409
410;; @r{Put the space character in class whitespace.}
411(modify-syntax-entry ?\s " ")
412 @result{} nil
413@end group
414
415@group
416;; @r{Make @samp{$} an open parenthesis character,}
417;; @r{with @samp{^} as its matching close.}
418(modify-syntax-entry ?$ "(^")
419 @result{} nil
420@end group
421
422@group
423;; @r{Make @samp{^} a close parenthesis character,}
424;; @r{with @samp{$} as its matching open.}
425(modify-syntax-entry ?^ ")$")
426 @result{} nil
427@end group
428
429@group
430;; @r{Make @samp{/} a punctuation character,}
431;; @r{the first character of a start-comment sequence,}
432;; @r{and the second character of an end-comment sequence.}
433;; @r{This is used in C mode.}
434(modify-syntax-entry ?/ ". 14")
435 @result{} nil
436@end group
437@end example
438@end deffn
439
440@defun char-syntax character
441This function returns the syntax class of @var{character}, represented
442by its mnemonic designator character. This returns @emph{only} the
443class, not any matching parenthesis or flags.
444
445An error is signaled if @var{char} is not a character.
446
447The following examples apply to C mode. The first example shows that
448the syntax class of space is whitespace (represented by a space). The
449second example shows that the syntax of @samp{/} is punctuation. This
450does not show the fact that it is also part of comment-start and -end
451sequences. The third example shows that open parenthesis is in the class
452of open parentheses. This does not show the fact that it has a matching
453character, @samp{)}.
454
455@example
456@group
457(string (char-syntax ?\s))
458 @result{} " "
459@end group
460
461@group
462(string (char-syntax ?/))
463 @result{} "."
464@end group
465
466@group
467(string (char-syntax ?\())
468 @result{} "("
469@end group
470@end example
471
472We use @code{string} to make it easier to see the character returned by
473@code{char-syntax}.
474@end defun
475
476@defun set-syntax-table table
477This function makes @var{table} the syntax table for the current buffer.
478It returns @var{table}.
479@end defun
480
481@defun syntax-table
482This function returns the current syntax table, which is the table for
483the current buffer.
484@end defun
485
486@defmac with-syntax-table @var{table} @var{body}@dots{}
487This macro executes @var{body} using @var{table} as the current syntax
488table. It returns the value of the last form in @var{body}, after
489restoring the old current syntax table.
490
491Since each buffer has its own current syntax table, we should make that
492more precise: @code{with-syntax-table} temporarily alters the current
493syntax table of whichever buffer is current at the time the macro
494execution starts. Other buffers are not affected.
495@end defmac
496
497@node Syntax Properties
498@section Syntax Properties
499@kindex syntax-table @r{(text property)}
500
501When the syntax table is not flexible enough to specify the syntax of
4230351b
CY
502a language, you can override the syntax table for specific character
503occurrences in the buffer, by applying a @code{syntax-table} text
504property. @xref{Text Properties}, for how to apply text properties.
b8d4c8d0 505
4230351b 506 The valid values of @code{syntax-table} text property are:
b8d4c8d0
GM
507
508@table @asis
509@item @var{syntax-table}
510If the property value is a syntax table, that table is used instead of
4230351b
CY
511the current buffer's syntax table to determine the syntax for the
512underlying text character.
b8d4c8d0
GM
513
514@item @code{(@var{syntax-code} . @var{matching-char})}
4230351b
CY
515A cons cell of this format specifies the syntax for the underlying
516text character. (@pxref{Syntax Table Internals})
b8d4c8d0
GM
517
518@item @code{nil}
519If the property is @code{nil}, the character's syntax is determined from
520the current syntax table in the usual way.
521@end table
522
523@defvar parse-sexp-lookup-properties
4230351b
CY
524If this is non-@code{nil}, the syntax scanning functions, like
525@code{forward-sexp}, pay attention to syntax text properties.
526Otherwise they use only the current syntax table.
527@end defvar
528
529@defvar syntax-propertize-function
530This variable, if non-@code{nil}, should store a function for applying
531@code{syntax-table} properties to a specified stretch of text. It is
532intended to be used by major modes to install a function which applies
533@code{syntax-table} properties in some mode-appropriate way.
534
535The function is called by @code{syntax-ppss} (@pxref{Position Parse}),
536and by Font Lock mode during syntactic fontification (@pxref{Syntactic
537Font Lock}). It is called with two arguments, @var{start} and
538@var{end}, which are the starting and ending positions of the text on
539which it should act. It is allowed to call @code{syntax-ppss} on any
540position before @var{end}. However, it should not call
541@code{syntax-ppss-flush-cache}; so, it is not allowed to call
542@code{syntax-ppss} on some position and later modify the buffer at an
543earlier position.
544@end defvar
545
546@defvar syntax-propertize-extend-region-functions
547This abnormal hook is run by the syntax parsing code prior to calling
548@code{syntax-propertize-function}. Its role is to help locate safe
549starting and ending buffer positions for passing to
550@code{syntax-propertize-function}. For example, a major mode can add
551a function to this hook to identify multi-line syntactic constructs,
552and ensure that the boundaries do not fall in the middle of one.
553
554Each function in this hook should accept two arguments, @var{start}
555and @var{end}. It should return either a cons cell of two adjusted
556buffer positions, @code{(@var{new-start} . @var{new-end})}, or
557@code{nil} if no adjustment is necessary. The hook functions are run
558in turn, repeatedly, until they all return @code{nil}.
b8d4c8d0
GM
559@end defvar
560
561@node Motion and Syntax
562@section Motion and Syntax
563
564 This section describes functions for moving across characters that
565have certain syntax classes.
566
567@defun skip-syntax-forward syntaxes &optional limit
568This function moves point forward across characters having syntax
569classes mentioned in @var{syntaxes} (a string of syntax class
570characters). It stops when it encounters the end of the buffer, or
571position @var{limit} (if specified), or a character it is not supposed
572to skip.
573
574If @var{syntaxes} starts with @samp{^}, then the function skips
575characters whose syntax is @emph{not} in @var{syntaxes}.
576
577The return value is the distance traveled, which is a nonnegative
578integer.
579@end defun
580
581@defun skip-syntax-backward syntaxes &optional limit
582This function moves point backward across characters whose syntax
583classes are mentioned in @var{syntaxes}. It stops when it encounters
584the beginning of the buffer, or position @var{limit} (if specified), or
585a character it is not supposed to skip.
586
587If @var{syntaxes} starts with @samp{^}, then the function skips
588characters whose syntax is @emph{not} in @var{syntaxes}.
589
590The return value indicates the distance traveled. It is an integer that
591is zero or less.
592@end defun
593
594@defun backward-prefix-chars
595This function moves point backward over any number of characters with
596expression prefix syntax. This includes both characters in the
597expression prefix syntax class, and characters with the @samp{p} flag.
598@end defun
599
600@node Parsing Expressions
601@section Parsing Expressions
602
603 This section describes functions for parsing and scanning balanced
a037c171
CY
604expressions. We will refer to such expressions as @dfn{sexps},
605following the terminology of Lisp, even though these functions can act
606on languages other than Lisp. Basically, a sexp is either a balanced
607parenthetical grouping, a string, or a ``symbol'' (i.e.@: a sequence
608of characters whose syntax is either word constituent or symbol
4230351b
CY
609constituent). However, characters in the expression prefix syntax
610class (@pxref{Syntax Class Table}) are treated as part of the sexp if
611they appear next to it.
b8d4c8d0
GM
612
613 The syntax table controls the interpretation of characters, so these
614functions can be used for Lisp expressions when in Lisp mode and for C
615expressions when in C mode. @xref{List Motion}, for convenient
616higher-level functions for moving over balanced expressions.
617
618 A character's syntax controls how it changes the state of the
619parser, rather than describing the state itself. For example, a
620string delimiter character toggles the parser state between
16152b76 621``in-string'' and ``in-code'', but the syntax of characters does not
b8d4c8d0
GM
622directly say whether they are inside a string. For example (note that
62315 is the syntax code for generic string delimiters),
624
625@example
626(put-text-property 1 9 'syntax-table '(15 . nil))
627@end example
628
629@noindent
630does not tell Emacs that the first eight chars of the current buffer
631are a string, but rather that they are all string delimiters. As a
632result, Emacs treats them as four consecutive empty string constants.
633
634@menu
635* Motion via Parsing:: Motion functions that work by parsing.
636* Position Parse:: Determining the syntactic state of a position.
637* Parser State:: How Emacs represents a syntactic state.
638* Low-Level Parsing:: Parsing across a specified region.
639* Control Parsing:: Parameters that affect parsing.
640@end menu
641
642@node Motion via Parsing
643@subsection Motion Commands Based on Parsing
644
645 This section describes simple point-motion functions that operate
646based on parsing expressions.
647
648@defun scan-lists from count depth
7b926f3f
CY
649This function scans forward @var{count} balanced parenthetical
650groupings from position @var{from}. It returns the position where the
651scan stops. If @var{count} is negative, the scan moves backwards.
652
653If @var{depth} is nonzero, treat the starting position as being
654@var{depth} parentheses deep. The scanner moves forward or backward
655through the buffer until the depth changes to zero @var{count} times.
656Hence, a positive value for @var{depth} has the effect of moving out
657@var{depth} levels of parenthesis from the starting position, while a
658negative @var{depth} has the effect of moving deeper by @var{-depth}
659levels of parenthesis.
b8d4c8d0
GM
660
661Scanning ignores comments if @code{parse-sexp-ignore-comments} is
662non-@code{nil}.
663
7b926f3f
CY
664If the scan reaches the beginning or end of the accessible part of the
665buffer before it has scanned over @var{count} parenthetical groupings,
666the return value is @code{nil} if the depth at that point is zero; if
667the depth is non-zero, a @code{scan-error} error is signaled.
b8d4c8d0
GM
668@end defun
669
670@defun scan-sexps from count
671This function scans forward @var{count} sexps from position @var{from}.
672It returns the position where the scan stops. If @var{count} is
673negative, the scan moves backwards.
674
675Scanning ignores comments if @code{parse-sexp-ignore-comments} is
676non-@code{nil}.
677
678If the scan reaches the beginning or end of (the accessible part of) the
679buffer while in the middle of a parenthetical grouping, an error is
680signaled. If it reaches the beginning or end between groupings but
681before count is used up, @code{nil} is returned.
682@end defun
683
684@defun forward-comment count
685This function moves point forward across @var{count} complete comments
686 (that is, including the starting delimiter and the terminating
687delimiter if any), plus any whitespace encountered on the way. It
688moves backward if @var{count} is negative. If it encounters anything
689other than a comment or whitespace, it stops, leaving point at the
690place where it stopped. This includes (for instance) finding the end
691of a comment when moving forward and expecting the beginning of one.
692The function also stops immediately after moving over the specified
693number of complete comments. If @var{count} comments are found as
694expected, with nothing except whitespace between them, it returns
695@code{t}; otherwise it returns @code{nil}.
696
697This function cannot tell whether the ``comments'' it traverses are
698embedded within a string. If they look like comments, it treats them
699as comments.
b8d4c8d0
GM
700
701To move forward over all comments and whitespace following point, use
4230351b
CY
702@code{(forward-comment (buffer-size))}. @code{(buffer-size)} is a
703good argument to use, because the number of comments in the buffer
704cannot exceed that many.
705@end defun
b8d4c8d0
GM
706
707@node Position Parse
708@subsection Finding the Parse State for a Position
709
710 For syntactic analysis, such as in indentation, often the useful
711thing is to compute the syntactic state corresponding to a given buffer
712position. This function does that conveniently.
713
714@defun syntax-ppss &optional pos
4230351b
CY
715This function returns the parser state that the parser would reach at
716position @var{pos} starting from the beginning of the buffer.
717@iftex
718See the next section for
719@end iftex
720@ifnottex
721@xref{Parser State},
722@end ifnottex
723for a description of the parser state.
724
725The return value is the same as if you call the low-level parsing
726function @code{parse-partial-sexp} to parse from the beginning of the
727buffer to @var{pos} (@pxref{Low-Level Parsing}). However,
728@code{syntax-ppss} uses a cache to speed up the computation. Due to
729this optimization, the second value (previous complete subexpression)
730and sixth value (minimum parenthesis depth) in the returned parser
731state are not meaningful.
732
733This function has a side effect: it adds a buffer-local entry to
734@code{before-change-functions} (@pxref{Change Hooks}) for
735@code{syntax-ppss-flush-cache} (see below). This entry keeps the
736cache consistent as the buffer is modified. However, the cache might
737not be updated if @code{syntax-ppss} is called while
b8d4c8d0 738@code{before-change-functions} is temporarily let-bound, or if the
4230351b
CY
739buffer is modified without running the hook, such as when using
740@code{inhibit-modification-hooks}. In those cases, it is necessary to
741call @code{syntax-ppss-flush-cache} explicitly.
742@end defun
b8d4c8d0 743
106e6894
CY
744@defun syntax-ppss-flush-cache beg &rest ignored-args
745This function flushes the cache used by @code{syntax-ppss}, starting
746at position @var{beg}. The remaining arguments, @var{ignored-args},
747are ignored; this function accepts them so that it can be directly
748used on hooks such as @code{before-change-functions} (@pxref{Change
749Hooks}).
b8d4c8d0
GM
750@end defun
751
752 Major modes can make @code{syntax-ppss} run faster by specifying
753where it needs to start parsing.
754
755@defvar syntax-begin-function
756If this is non-@code{nil}, it should be a function that moves to an
757earlier buffer position where the parser state is equivalent to
758@code{nil}---in other words, a position outside of any comment,
759string, or parenthesis. @code{syntax-ppss} uses it to further
760optimize its computations, when the cache gives no help.
761@end defvar
762
763@node Parser State
764@subsection Parser State
765@cindex parser state
766
4230351b
CY
767 A @dfn{parser state} is a list of ten elements describing the state
768of the syntactic parser, after it parses the text between a specified
769starting point and a specified end point in the buffer. Parsing
770functions such as @code{syntax-ppss}
771@ifnottex
772(@pxref{Position Parse})
773@end ifnottex
774return a parser state as the value. Some parsing functions accept a
775parser state as an argument, for resuming parsing.
776
777 Here are the meanings of the elements of the parser state:
b8d4c8d0
GM
778
779@enumerate 0
780@item
781The depth in parentheses, counting from 0. @strong{Warning:} this can
782be negative if there are more close parens than open parens between
4230351b 783the parser's starting point and end point.
b8d4c8d0
GM
784
785@item
786@cindex innermost containing parentheses
787The character position of the start of the innermost parenthetical
788grouping containing the stopping point; @code{nil} if none.
789
790@item
791@cindex previous complete subexpression
792The character position of the start of the last complete subexpression
793terminated; @code{nil} if none.
794
795@item
796@cindex inside string
797Non-@code{nil} if inside a string. More precisely, this is the
798character that will terminate the string, or @code{t} if a generic
799string delimiter character should terminate it.
800
801@item
802@cindex inside comment
4230351b
CY
803@code{t} if inside a non-nestable comment (of any comment style;
804@pxref{Syntax Flags}); or the comment nesting level if inside a
805comment that can be nested.
b8d4c8d0
GM
806
807@item
808@cindex quote character
4230351b 809@code{t} if the end point is just after a quote character.
b8d4c8d0
GM
810
811@item
812The minimum parenthesis depth encountered during this scan.
813
814@item
4230351b
CY
815What kind of comment is active: @code{nil} if not in a comment or in a
816comment of style @samp{a}; 1 for a comment of style @samp{b}; 2 for a
817comment of style @samp{c}; and @code{syntax-table} for a comment that
818should be ended by a generic comment delimiter character.
b8d4c8d0
GM
819
820@item
821The string or comment start position. While inside a comment, this is
822the position where the comment began; while inside a string, this is the
823position where the string began. When outside of strings and comments,
824this element is @code{nil}.
825
826@item
827Internal data for continuing the parsing. The meaning of this
828data is subject to change; it is used if you pass this list
829as the @var{state} argument to another call.
830@end enumerate
831
832 Elements 1, 2, and 6 are ignored in a state which you pass as an
833argument to continue parsing, and elements 8 and 9 are used only in
4230351b
CY
834trivial cases. Those elements are mainly used internally by the
835parser code.
b8d4c8d0
GM
836
837 One additional piece of useful information is available from a
838parser state using this function:
839
840@defun syntax-ppss-toplevel-pos state
841This function extracts, from parser state @var{state}, the last
842position scanned in the parse which was at top level in grammatical
843structure. ``At top level'' means outside of any parentheses,
844comments, or strings.
845
846The value is @code{nil} if @var{state} represents a parse which has
847arrived at a top level position.
848@end defun
849
b8d4c8d0
GM
850@node Low-Level Parsing
851@subsection Low-Level Parsing
852
853 The most basic way to use the expression parser is to tell it
854to start at a given position with a certain state, and parse up to
855a specified end position.
856
857@defun parse-partial-sexp start limit &optional target-depth stop-before state stop-comment
858This function parses a sexp in the current buffer starting at
859@var{start}, not scanning past @var{limit}. It stops at position
860@var{limit} or when certain criteria described below are met, and sets
861point to the location where parsing stops. It returns a parser state
862describing the status of the parse at the point where it stops.
863
864@cindex parenthesis depth
865If the third argument @var{target-depth} is non-@code{nil}, parsing
866stops if the depth in parentheses becomes equal to @var{target-depth}.
867The depth starts at 0, or at whatever is given in @var{state}.
868
869If the fourth argument @var{stop-before} is non-@code{nil}, parsing
870stops when it comes to any character that starts a sexp. If
871@var{stop-comment} is non-@code{nil}, parsing stops when it comes to the
872start of a comment. If @var{stop-comment} is the symbol
873@code{syntax-table}, parsing stops after the start of a comment or a
874string, or the end of a comment or a string, whichever comes first.
875
876If @var{state} is @code{nil}, @var{start} is assumed to be at the top
877level of parenthesis structure, such as the beginning of a function
878definition. Alternatively, you might wish to resume parsing in the
879middle of the structure. To do this, you must provide a @var{state}
880argument that describes the initial status of parsing. The value
881returned by a previous call to @code{parse-partial-sexp} will do
882nicely.
883@end defun
884
885@node Control Parsing
886@subsection Parameters to Control Parsing
887
888@defvar multibyte-syntax-as-symbol
889If this variable is non-@code{nil}, @code{scan-sexps} treats all
890non-@acronym{ASCII} characters as symbol constituents regardless
891of what the syntax table says about them. (However, text properties
892can still override the syntax.)
893@end defvar
894
895@defopt parse-sexp-ignore-comments
896@cindex skipping comments
897If the value is non-@code{nil}, then comments are treated as
898whitespace by the functions in this section and by @code{forward-sexp},
899@code{scan-lists} and @code{scan-sexps}.
900@end defopt
901
902@vindex parse-sexp-lookup-properties
903The behavior of @code{parse-partial-sexp} is also affected by
904@code{parse-sexp-lookup-properties} (@pxref{Syntax Properties}).
905
906You can use @code{forward-comment} to move forward or backward over
907one comment or several comments.
908
909@node Standard Syntax Tables
910@section Some Standard Syntax Tables
911
912 Most of the major modes in Emacs have their own syntax tables. Here
913are several of them:
914
915@defun standard-syntax-table
916This function returns the standard syntax table, which is the syntax
917table used in Fundamental mode.
918@end defun
919
920@defvar text-mode-syntax-table
921The value of this variable is the syntax table used in Text mode.
922@end defvar
923
924@defvar c-mode-syntax-table
925The value of this variable is the syntax table for C-mode buffers.
926@end defvar
927
928@defvar emacs-lisp-mode-syntax-table
929The value of this variable is the syntax table used in Emacs Lisp mode
930by editing commands. (It has no effect on the Lisp @code{read}
931function.)
932@end defvar
933
934@node Syntax Table Internals
935@section Syntax Table Internals
936@cindex syntax table internals
937
938 Lisp programs don't usually work with the elements directly; the
939Lisp-level syntax table functions usually work with syntax descriptors
940(@pxref{Syntax Descriptors}). Nonetheless, here we document the
941internal format. This format is used mostly when manipulating
942syntax properties.
943
944 Each element of a syntax table is a cons cell of the form
945@code{(@var{syntax-code} . @var{matching-char})}. The @sc{car},
946@var{syntax-code}, is an integer that encodes the syntax class, and any
947flags. The @sc{cdr}, @var{matching-char}, is non-@code{nil} if
948a character to match was specified.
949
950 This table gives the value of @var{syntax-code} which corresponds
951to each syntactic type.
952
953@multitable @columnfractions .05 .3 .3 .31
954@item
955@tab
956@i{Integer} @i{Class}
957@tab
958@i{Integer} @i{Class}
959@tab
960@i{Integer} @i{Class}
961@item
962@tab
9630 @ @ whitespace
964@tab
9655 @ @ close parenthesis
966@tab
96710 @ @ character quote
968@item
969@tab
9701 @ @ punctuation
971@tab
9726 @ @ expression prefix
973@tab
97411 @ @ comment-start
975@item
976@tab
9772 @ @ word
978@tab
9797 @ @ string quote
980@tab
98112 @ @ comment-end
982@item
983@tab
9843 @ @ symbol
985@tab
9868 @ @ paired delimiter
987@tab
98813 @ @ inherit
989@item
990@tab
9914 @ @ open parenthesis
992@tab
9939 @ @ escape
994@tab
99514 @ @ generic comment
996@item
997@tab
99815 @ generic string
999@end multitable
1000
1001 For example, the usual syntax value for @samp{(} is @code{(4 . 41)}.
1002(41 is the character code for @samp{)}.)
1003
1004 The flags are encoded in higher order bits, starting 16 bits from the
1005least significant bit. This table gives the power of two which
1006corresponds to each syntax flag.
1007
1008@multitable @columnfractions .05 .3 .3 .3
1009@item
1010@tab
1011@i{Prefix} @i{Flag}
1012@tab
1013@i{Prefix} @i{Flag}
1014@tab
1015@i{Prefix} @i{Flag}
1016@item
1017@tab
1018@samp{1} @ @ @code{(lsh 1 16)}
1019@tab
1020@samp{4} @ @ @code{(lsh 1 19)}
1021@tab
1022@samp{b} @ @ @code{(lsh 1 21)}
1023@item
1024@tab
1025@samp{2} @ @ @code{(lsh 1 17)}
1026@tab
1027@samp{p} @ @ @code{(lsh 1 20)}
1028@tab
1029@samp{n} @ @ @code{(lsh 1 22)}
1030@item
1031@tab
1032@samp{3} @ @ @code{(lsh 1 18)}
1033@end multitable
1034
1035@defun string-to-syntax @var{desc}
1036This function returns the internal form corresponding to the syntax
1037descriptor @var{desc}, a cons cell @code{(@var{syntax-code}
1038. @var{matching-char})}.
1039@end defun
1040
1041@defun syntax-after pos
1042This function returns the syntax code of the character in the buffer
1043after position @var{pos}, taking account of syntax properties as well
1044as the syntax table. If @var{pos} is outside the buffer's accessible
1045portion (@pxref{Narrowing, accessible portion}), this function returns
1046@code{nil}.
1047@end defun
1048
1049@defun syntax-class syntax
1050This function returns the syntax class of the syntax code
1051@var{syntax}. (It masks off the high 16 bits that hold the flags
1052encoded in the syntax descriptor.) If @var{syntax} is @code{nil}, it
1053returns @code{nil}; this is so evaluating the expression
1054
1055@example
1056(syntax-class (syntax-after pos))
1057@end example
1058
1059@noindent
1060where @code{pos} is outside the buffer's accessible portion, will
1061yield @code{nil} without throwing errors or producing wrong syntax
1062class codes.
1063@end defun
1064
1065@node Categories
1066@section Categories
1067@cindex categories of characters
1068@cindex character categories
1069
1070 @dfn{Categories} provide an alternate way of classifying characters
1071syntactically. You can define several categories as needed, then
1072independently assign each character to one or more categories. Unlike
1073syntax classes, categories are not mutually exclusive; it is normal for
1074one character to belong to several categories.
1075
1076@cindex category table
1077 Each buffer has a @dfn{category table} which records which categories
1078are defined and also which characters belong to each category. Each
1079category table defines its own categories, but normally these are
1080initialized by copying from the standard categories table, so that the
1081standard categories are available in all modes.
1082
1083 Each category has a name, which is an @acronym{ASCII} printing character in
1084the range @w{@samp{ }} to @samp{~}. You specify the name of a category
1085when you define it with @code{define-category}.
1086
1087 The category table is actually a char-table (@pxref{Char-Tables}).
1088The element of the category table at index @var{c} is a @dfn{category
1089set}---a bool-vector---that indicates which categories character @var{c}
1090belongs to. In this category set, if the element at index @var{cat} is
1091@code{t}, that means category @var{cat} is a member of the set, and that
1092character @var{c} belongs to category @var{cat}.
1093
1094For the next three functions, the optional argument @var{table}
1095defaults to the current buffer's category table.
1096
1097@defun define-category char docstring &optional table
1098This function defines a new category, with name @var{char} and
1099documentation @var{docstring}, for the category table @var{table}.
1ea897d5
EZ
1100
1101Here's an example of defining a new category for characters that have
1102strong right-to-left directionality (@pxref{Bidirectional Display})
1103and using it in a special category table:
1104
1105@example
1106(defvar special-category-table-for-bidi
1107 (let ((category-table (make-category-table))
1108 (uniprop-table (unicode-property-table-internal 'bidi-class)))
1109 (define-category ?R "Characters of bidi-class R, AL, or RLO"
1110 category-table)
1111 (map-char-table
1112 #'(lambda (key val)
1113 (if (memq val '(R AL RLO))
1114 (modify-category-entry key ?R category-table)))
1115 uniprop-table)
1116 category-table))
1117@end example
b8d4c8d0
GM
1118@end defun
1119
1120@defun category-docstring category &optional table
1121This function returns the documentation string of category @var{category}
1122in category table @var{table}.
1123
1124@example
1125(category-docstring ?a)
1126 @result{} "ASCII"
1127(category-docstring ?l)
1128 @result{} "Latin"
1129@end example
1130@end defun
1131
1132@defun get-unused-category &optional table
1133This function returns a category name (a character) which is not
1134currently defined in @var{table}. If all possible categories are in use
1135in @var{table}, it returns @code{nil}.
1136@end defun
1137
1138@defun category-table
1139This function returns the current buffer's category table.
1140@end defun
1141
1142@defun category-table-p object
1143This function returns @code{t} if @var{object} is a category table,
1144otherwise @code{nil}.
1145@end defun
1146
1147@defun standard-category-table
1148This function returns the standard category table.
1149@end defun
1150
1151@defun copy-category-table &optional table
1152This function constructs a copy of @var{table} and returns it. If
1153@var{table} is not supplied (or is @code{nil}), it returns a copy of the
1154standard category table. Otherwise, an error is signaled if @var{table}
1155is not a category table.
1156@end defun
1157
1158@defun set-category-table table
1159This function makes @var{table} the category table for the current
1160buffer. It returns @var{table}.
1161@end defun
1162
1163@defun make-category-table
1164This creates and returns an empty category table. In an empty category
1165table, no categories have been allocated, and no characters belong to
1166any categories.
1167@end defun
1168
1169@defun make-category-set categories
1170This function returns a new category set---a bool-vector---whose initial
1171contents are the categories listed in the string @var{categories}. The
1172elements of @var{categories} should be category names; the new category
1173set has @code{t} for each of those categories, and @code{nil} for all
1174other categories.
1175
1176@example
1177(make-category-set "al")
1178 @result{} #&128"\0\0\0\0\0\0\0\0\0\0\0\0\2\20\0\0"
1179@end example
1180@end defun
1181
1182@defun char-category-set char
1183This function returns the category set for character @var{char} in the
1184current buffer's category table. This is the bool-vector which
1185records which categories the character @var{char} belongs to. The
1186function @code{char-category-set} does not allocate storage, because
1187it returns the same bool-vector that exists in the category table.
1188
1189@example
1190(char-category-set ?a)
1191 @result{} #&128"\0\0\0\0\0\0\0\0\0\0\0\0\2\20\0\0"
1192@end example
1193@end defun
1194
1195@defun category-set-mnemonics category-set
1196This function converts the category set @var{category-set} into a string
1197containing the characters that designate the categories that are members
1198of the set.
1199
1200@example
1201(category-set-mnemonics (char-category-set ?a))
1202 @result{} "al"
1203@end example
1204@end defun
1205
f147ff75
CY
1206@defun modify-category-entry char category &optional table reset
1207This function modifies the category set of @var{char} in category
b8d4c8d0 1208table @var{table} (which defaults to the current buffer's category
f147ff75
CY
1209table). @var{char} can be a character, or a cons cell of the form
1210@code{(@var{min} . @var{max})}; in the latter case, the function
1211modifies the category sets of all characters in the range between
1212@var{min} and @var{max}, inclusive.
b8d4c8d0 1213
f147ff75 1214Normally, it modifies a category set by adding @var{category} to it.
b8d4c8d0
GM
1215But if @var{reset} is non-@code{nil}, then it deletes @var{category}
1216instead.
1217@end defun
1218
1219@deffn Command describe-categories &optional buffer-or-name
1220This function describes the category specifications in the current
1221category table. It inserts the descriptions in a buffer, and then
1222displays that buffer. If @var{buffer-or-name} is non-@code{nil}, it
1223describes the category table of that buffer instead.
1224@end deffn