36.7 Syntax Table Internals

Syntax tables are implemented as char-tables (see Char-Tables), but most Lisp programs don’t work directly with their elements. Syntax tables do not store syntax data as syntax descriptors (see Syntax Descriptors); they use an internal format, which is documented in this section. This internal format can also be assigned as syntax properties (see Syntax Properties).

Each entry in a syntax table is a raw syntax descriptor: a cons cell of the form (syntax-code . matching-char). syntax-code is an integer which encodes the syntax class and syntax flags, according to the table below. matching-char, if non-nil, specifies a matching character (similar to the second character in a syntax descriptor).

Use aref (see Functions that Operate on Arrays) to get the raw syntax descriptor of a character, e.g. (aref (syntax-table) ch).

Here are the syntax codes corresponding to the various syntax classes:

CodeClassCodeClass
0whitespace8paired delimiter
1punctuation9escape
2word10character quote
3symbol11comment-start
4open parenthesis12comment-end
5close parenthesis13inherit
6expression prefix14generic comment
7string quote15generic string

For example, in the standard syntax table, the entry for ‘(’ is (4 . 41). 41 is the character code for ‘)’.

Syntax flags are encoded in higher order bits, starting 16 bits from the least significant bit. This table gives the power of two which corresponds to each syntax flag.

PrefixFlagPrefixFlag
1(ash 1 16)p(ash 1 20)
2(ash 1 17)b(ash 1 21)
3(ash 1 18)n(ash 1 22)
4(ash 1 19)c(ash 1 23)
Function: string-to-syntax desc

Given a syntax descriptor desc (a string), this function returns the corresponding raw syntax descriptor.

Function: syntax-class-to-char syntax

Given a raw syntax descriptor syntax (an integer), this function returns the corresponding syntax descriptor (a character).

Function: syntax-after pos

This function returns the raw syntax descriptor for the character in the buffer after position pos, taking account of syntax properties as well as the syntax table. If pos is outside the buffer’s accessible portion (see accessible portion), the return value is nil.

Function: syntax-class syntax

This function returns the syntax code for the raw syntax descriptor syntax. More precisely, it takes the raw syntax descriptor’s syntax-code component, masks off the high 16 bits which record the syntax flags, and returns the resulting integer.

If syntax is nil, the return value is nil. This is so that the expression

(syntax-class (syntax-after pos))

evaluates to nil if pos is outside the buffer’s accessible portion, without throwing errors or returning an incorrect code.