| 1 | /* Declarations having to do with GNU Emacs syntax tables. |
| 2 | Copyright (C) 1985, 93, 94, 97, 1998 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 6 | GNU Emacs is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GNU Emacs is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Emacs; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | |
| 22 | extern Lisp_Object Qsyntax_table_p; |
| 23 | extern void update_syntax_table P_ ((int, int, int, Lisp_Object)); |
| 24 | |
| 25 | /* The standard syntax table is stored where it will automatically |
| 26 | be used in all new buffers. */ |
| 27 | #define Vstandard_syntax_table buffer_defaults.syntax_table |
| 28 | |
| 29 | /* A syntax table is a chartable whose elements are cons cells |
| 30 | (CODE+FLAGS . MATCHING-CHAR). MATCHING-CHAR can be nil if the char |
| 31 | is not a kind of parenthesis. |
| 32 | |
| 33 | The low 8 bits of CODE+FLAGS is a code, as follows: */ |
| 34 | |
| 35 | enum syntaxcode |
| 36 | { |
| 37 | Swhitespace, /* for a whitespace character */ |
| 38 | Spunct, /* for random punctuation characters */ |
| 39 | Sword, /* for a word constituent */ |
| 40 | Ssymbol, /* symbol constituent but not word constituent */ |
| 41 | Sopen, /* for a beginning delimiter */ |
| 42 | Sclose, /* for an ending delimiter */ |
| 43 | Squote, /* for a prefix character like Lisp ' */ |
| 44 | Sstring, /* for a string-grouping character like Lisp " */ |
| 45 | Smath, /* for delimiters like $ in Tex. */ |
| 46 | Sescape, /* for a character that begins a C-style escape */ |
| 47 | Scharquote, /* for a character that quotes the following character */ |
| 48 | Scomment, /* for a comment-starting character */ |
| 49 | Sendcomment, /* for a comment-ending character */ |
| 50 | Sinherit, /* use the standard syntax table for this character */ |
| 51 | Scomment_fence, /* Starts/ends comment which is delimited on the |
| 52 | other side by any char with the same syntaxcode. */ |
| 53 | Sstring_fence, /* Starts/ends string which is delimited on the |
| 54 | other side by any char with the same syntaxcode. */ |
| 55 | Smax /* Upper bound on codes that are meaningful */ |
| 56 | }; |
| 57 | |
| 58 | /* Set the syntax entry VAL for char C in table TABLE. */ |
| 59 | |
| 60 | #define SET_RAW_SYNTAX_ENTRY(table, c, val) \ |
| 61 | ((c) < CHAR_TABLE_SINGLE_BYTE_SLOTS \ |
| 62 | ? (XCHAR_TABLE (table)->contents[(unsigned char) (c)] = (val)) \ |
| 63 | : Faset ((table), make_number (c), (val))) |
| 64 | |
| 65 | /* Fetch the syntax entry for char C in syntax table TABLE. |
| 66 | This macro is called only when C is less than CHAR_TABLE_ORDINARY_SLOTS. |
| 67 | Do inheritance. */ |
| 68 | |
| 69 | #ifdef __GNUC__ |
| 70 | #define SYNTAX_ENTRY_FOLLOW_PARENT(table, c) \ |
| 71 | ({ Lisp_Object tbl = table; \ |
| 72 | Lisp_Object temp = XCHAR_TABLE (tbl)->contents[(c)]; \ |
| 73 | while (NILP (temp)) \ |
| 74 | { \ |
| 75 | tbl = XCHAR_TABLE (tbl)->parent; \ |
| 76 | if (NILP (tbl)) \ |
| 77 | break; \ |
| 78 | temp = XCHAR_TABLE (tbl)->contents[(c)]; \ |
| 79 | } \ |
| 80 | temp; }) |
| 81 | #else |
| 82 | extern Lisp_Object syntax_temp; |
| 83 | extern Lisp_Object syntax_parent_lookup P_ ((Lisp_Object, int)); |
| 84 | |
| 85 | #define SYNTAX_ENTRY_FOLLOW_PARENT(table, c) \ |
| 86 | (syntax_temp = XCHAR_TABLE (table)->contents[(c)], \ |
| 87 | (NILP (syntax_temp) \ |
| 88 | ? syntax_parent_lookup (table, (c)) \ |
| 89 | : syntax_temp)) |
| 90 | #endif |
| 91 | |
| 92 | /* SYNTAX_ENTRY fetches the information from the entry for character C |
| 93 | in syntax table TABLE, or from globally kept data (gl_state). |
| 94 | Does inheritance. */ |
| 95 | /* CURRENT_SYNTAX_TABLE gives the syntax table valid for current |
| 96 | position, it is either the buffer's syntax table, or syntax table |
| 97 | found in text properties. */ |
| 98 | |
| 99 | #ifdef SYNTAX_ENTRY_VIA_PROPERTY |
| 100 | # define SYNTAX_ENTRY(c) \ |
| 101 | (gl_state.use_global ? gl_state.global_code : SYNTAX_ENTRY_INT (c)) |
| 102 | # define CURRENT_SYNTAX_TABLE gl_state.current_syntax_table |
| 103 | #else |
| 104 | # define SYNTAX_ENTRY SYNTAX_ENTRY_INT |
| 105 | # define CURRENT_SYNTAX_TABLE current_buffer->syntax_table |
| 106 | #endif |
| 107 | |
| 108 | #define SYNTAX_ENTRY_INT(c) \ |
| 109 | ((c) < CHAR_TABLE_SINGLE_BYTE_SLOTS \ |
| 110 | ? SYNTAX_ENTRY_FOLLOW_PARENT (CURRENT_SYNTAX_TABLE, \ |
| 111 | (unsigned char) (c)) \ |
| 112 | : Faref (CURRENT_SYNTAX_TABLE, make_number ((c)))) |
| 113 | |
| 114 | /* Extract the information from the entry for character C |
| 115 | in the current syntax table. */ |
| 116 | |
| 117 | #ifdef __GNUC__ |
| 118 | #define SYNTAX(c) \ |
| 119 | ({ Lisp_Object temp; \ |
| 120 | temp = SYNTAX_ENTRY (c); \ |
| 121 | (CONSP (temp) \ |
| 122 | ? (enum syntaxcode) (XINT (XCONS (temp)->car) & 0xff) \ |
| 123 | : Swhitespace); }) |
| 124 | |
| 125 | #define SYNTAX_WITH_FLAGS(c) \ |
| 126 | ({ Lisp_Object temp; \ |
| 127 | temp = SYNTAX_ENTRY (c); \ |
| 128 | (CONSP (temp) \ |
| 129 | ? XINT (XCONS (temp)->car) \ |
| 130 | : (int) Swhitespace); }) |
| 131 | |
| 132 | #define SYNTAX_MATCH(c) \ |
| 133 | ({ Lisp_Object temp; \ |
| 134 | temp = SYNTAX_ENTRY (c); \ |
| 135 | (CONSP (temp) \ |
| 136 | ? XCONS (temp)->cdr \ |
| 137 | : Qnil); }) |
| 138 | #else |
| 139 | #define SYNTAX(c) \ |
| 140 | (syntax_temp = SYNTAX_ENTRY ((c)), \ |
| 141 | (CONSP (syntax_temp) \ |
| 142 | ? (enum syntaxcode) (XINT (XCONS (syntax_temp)->car) & 0xff) \ |
| 143 | : Swhitespace)) |
| 144 | |
| 145 | #define SYNTAX_WITH_FLAGS(c) \ |
| 146 | (syntax_temp = SYNTAX_ENTRY ((c)), \ |
| 147 | (CONSP (syntax_temp) \ |
| 148 | ? XINT (XCONS (syntax_temp)->car) \ |
| 149 | : (int) Swhitespace)) |
| 150 | |
| 151 | #define SYNTAX_MATCH(c) \ |
| 152 | (syntax_temp = SYNTAX_ENTRY ((c)), \ |
| 153 | (CONSP (syntax_temp) \ |
| 154 | ? XCONS (syntax_temp)->cdr \ |
| 155 | : Qnil)) |
| 156 | #endif |
| 157 | |
| 158 | /* Then there are six single-bit flags that have the following meanings: |
| 159 | 1. This character is the first of a two-character comment-start sequence. |
| 160 | 2. This character is the second of a two-character comment-start sequence. |
| 161 | 3. This character is the first of a two-character comment-end sequence. |
| 162 | 4. This character is the second of a two-character comment-end sequence. |
| 163 | 5. This character is a prefix, for backward-prefix-chars. |
| 164 | Note that any two-character sequence whose first character has flag 1 |
| 165 | and whose second character has flag 2 will be interpreted as a comment start. |
| 166 | |
| 167 | bit 6 is used to discriminate between two different comment styles. |
| 168 | Languages such as C++ allow two orthogonal syntax start/end pairs |
| 169 | and bit 6 is used to determine whether a comment-end or Scommentend |
| 170 | ends style a or b. Comment start sequences can start style a or b. |
| 171 | Style a is always the default. |
| 172 | */ |
| 173 | |
| 174 | /* These macros extract a particular flag for a given character. */ |
| 175 | |
| 176 | #define SYNTAX_COMSTART_FIRST(c) ((SYNTAX_WITH_FLAGS (c) >> 16) & 1) |
| 177 | |
| 178 | #define SYNTAX_COMSTART_SECOND(c) ((SYNTAX_WITH_FLAGS (c) >> 17) & 1) |
| 179 | |
| 180 | #define SYNTAX_COMEND_FIRST(c) ((SYNTAX_WITH_FLAGS (c) >> 18) & 1) |
| 181 | |
| 182 | #define SYNTAX_COMEND_SECOND(c) ((SYNTAX_WITH_FLAGS (c) >> 19) & 1) |
| 183 | |
| 184 | #define SYNTAX_PREFIX(c) ((SYNTAX_WITH_FLAGS (c) >> 20) & 1) |
| 185 | |
| 186 | #define SYNTAX_COMMENT_STYLE(c) ((SYNTAX_WITH_FLAGS (c) >> 21) & 1) |
| 187 | |
| 188 | /* These macros extract specific flags from an integer |
| 189 | that holds the syntax code and the flags. */ |
| 190 | |
| 191 | #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1) |
| 192 | |
| 193 | #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1) |
| 194 | |
| 195 | #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1) |
| 196 | |
| 197 | #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1) |
| 198 | |
| 199 | #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1) |
| 200 | |
| 201 | #define SYNTAX_FLAGS_COMMENT_STYLE(flags) (((flags) >> 21) & 1) |
| 202 | |
| 203 | /* This array, indexed by a character, contains the syntax code which that |
| 204 | character signifies (as a char). For example, |
| 205 | (enum syntaxcode) syntax_spec_code['w'] is Sword. */ |
| 206 | |
| 207 | extern unsigned char syntax_spec_code[0400]; |
| 208 | |
| 209 | /* Indexed by syntax code, give the letter that describes it. */ |
| 210 | |
| 211 | extern char syntax_code_spec[16]; |
| 212 | |
| 213 | /* Convert the byte offset BYTEPOS into a character position, |
| 214 | for the object recorded in gl_state with SETUP_SYNTAX_TABLE_FOR_OBJECT. |
| 215 | |
| 216 | The value is meant for use in the UPDATE_SYNTAX_TABLE... macros. |
| 217 | These macros do nothing when parse_sexp_lookup_properties is 0, |
| 218 | so we return 0 in that case, for speed. */ |
| 219 | |
| 220 | #define SYNTAX_TABLE_BYTE_TO_CHAR(bytepos) \ |
| 221 | (! parse_sexp_lookup_properties \ |
| 222 | ? 0 \ |
| 223 | : STRINGP (gl_state.object) \ |
| 224 | ? string_byte_to_char (gl_state.object, (bytepos)) \ |
| 225 | : BUFFERP (gl_state.object) \ |
| 226 | ? buf_bytepos_to_charpos (XBUFFER (gl_state.object), \ |
| 227 | (bytepos) + BUF_BEGV_BYTE (XBUFFER (gl_state.object)) - 1) - BUF_BEGV (XBUFFER (gl_state.object)) + 1 \ |
| 228 | : NILP (gl_state.object) \ |
| 229 | ? BYTE_TO_CHAR ((bytepos) + BEGV_BYTE - 1) - BEGV + 1 \ |
| 230 | : (bytepos)) |
| 231 | |
| 232 | /* Make syntax table state (gl_state) good for CHARPOS, assuming it is |
| 233 | currently good for a position before CHARPOS. */ |
| 234 | |
| 235 | #define UPDATE_SYNTAX_TABLE_FORWARD(charpos) \ |
| 236 | (parse_sexp_lookup_properties \ |
| 237 | && (charpos) >= gl_state.e_property \ |
| 238 | ? (update_syntax_table ((charpos) + gl_state.offset, 1, 0, \ |
| 239 | gl_state.object), \ |
| 240 | 1) \ |
| 241 | : 0) |
| 242 | |
| 243 | /* Make syntax table state (gl_state) good for CHARPOS, assuming it is |
| 244 | currently good for a position after CHARPOS. */ |
| 245 | |
| 246 | #define UPDATE_SYNTAX_TABLE_BACKWARD(charpos) \ |
| 247 | (parse_sexp_lookup_properties \ |
| 248 | && (charpos) <= gl_state.b_property \ |
| 249 | ? (update_syntax_table ((charpos) + gl_state.offset, -1, 0, \ |
| 250 | gl_state.object), \ |
| 251 | 1) \ |
| 252 | : 0) |
| 253 | |
| 254 | /* Make syntax table good for CHARPOS. */ |
| 255 | |
| 256 | #define UPDATE_SYNTAX_TABLE(charpos) \ |
| 257 | (parse_sexp_lookup_properties \ |
| 258 | && (charpos) <= gl_state.b_property \ |
| 259 | ? (update_syntax_table ((charpos) + gl_state.offset, -1, 0, \ |
| 260 | gl_state.object), \ |
| 261 | 1) \ |
| 262 | : (parse_sexp_lookup_properties \ |
| 263 | && (charpos) >= gl_state.e_property \ |
| 264 | ? (update_syntax_table ((charpos) + gl_state.offset, 1, 0,\ |
| 265 | gl_state.object), \ |
| 266 | 1) \ |
| 267 | : 0)) |
| 268 | |
| 269 | /* This macro should be called with FROM at the start of forward |
| 270 | search, or after the last position of the backward search. It |
| 271 | makes sure that the first char is picked up with correct table, so |
| 272 | one does not need to call UPDATE_SYNTAX_TABLE immediately after the |
| 273 | call. |
| 274 | Sign of COUNT gives the direction of the search. |
| 275 | */ |
| 276 | |
| 277 | #define SETUP_SYNTAX_TABLE(FROM, COUNT) \ |
| 278 | if (1) \ |
| 279 | { \ |
| 280 | gl_state.b_property = BEGV - 1; \ |
| 281 | gl_state.e_property = ZV + 1; \ |
| 282 | gl_state.object = Qnil; \ |
| 283 | gl_state.use_global = 0; \ |
| 284 | gl_state.offset = 0; \ |
| 285 | gl_state.current_syntax_table = current_buffer->syntax_table; \ |
| 286 | if (parse_sexp_lookup_properties) \ |
| 287 | if ((COUNT) > 0 || (FROM) > BEGV) \ |
| 288 | update_syntax_table ((COUNT) > 0 ? (FROM) : (FROM) - 1, (COUNT),\ |
| 289 | 1, Qnil); \ |
| 290 | } \ |
| 291 | else |
| 292 | |
| 293 | /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer. |
| 294 | If it is t, ignore properties altogether. |
| 295 | |
| 296 | This is meant for regex.c to use. For buffers, regex.c passes arguments |
| 297 | to the UPDATE_SYNTAX_TABLE macros which are relative to BEGV. |
| 298 | So if it is a buffer, we set the offset field to BEGV. */ |
| 299 | |
| 300 | #define SETUP_SYNTAX_TABLE_FOR_OBJECT(OBJECT, FROM, COUNT) \ |
| 301 | if (1) \ |
| 302 | { \ |
| 303 | gl_state.object = (OBJECT); \ |
| 304 | if (BUFFERP (gl_state.object)) \ |
| 305 | { \ |
| 306 | struct buffer *buf = XBUFFER (gl_state.object); \ |
| 307 | gl_state.b_property = 0; \ |
| 308 | gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1; \ |
| 309 | gl_state.offset = BUF_BEGV (buf) - 1; \ |
| 310 | } \ |
| 311 | else if (NILP (gl_state.object)) \ |
| 312 | { \ |
| 313 | gl_state.b_property = 0; \ |
| 314 | gl_state.e_property = ZV - BEGV + 1; \ |
| 315 | gl_state.offset = BEGV - 1; \ |
| 316 | } \ |
| 317 | else if (EQ (gl_state.object, Qt)) \ |
| 318 | { \ |
| 319 | gl_state.b_property = - 1; \ |
| 320 | gl_state.e_property = 1500000000; \ |
| 321 | gl_state.offset = 0; \ |
| 322 | } \ |
| 323 | else \ |
| 324 | { \ |
| 325 | gl_state.b_property = -1; \ |
| 326 | gl_state.e_property = 1 + XSTRING (gl_state.object)->size; \ |
| 327 | gl_state.offset = 0; \ |
| 328 | } \ |
| 329 | gl_state.use_global = 0; \ |
| 330 | gl_state.current_syntax_table = current_buffer->syntax_table; \ |
| 331 | if (parse_sexp_lookup_properties) \ |
| 332 | update_syntax_table (((FROM) + gl_state.offset \ |
| 333 | + (COUNT > 0 ? 0 : -1)), \ |
| 334 | COUNT, 1, gl_state.object); \ |
| 335 | } \ |
| 336 | else |
| 337 | |
| 338 | struct gl_state_s |
| 339 | { |
| 340 | Lisp_Object object; /* The object we are scanning. */ |
| 341 | int start; /* Where to stop. */ |
| 342 | int stop; /* Where to stop. */ |
| 343 | int use_global; /* Whether to use global_code |
| 344 | or c_s_t. */ |
| 345 | Lisp_Object global_code; /* Syntax code of current char. */ |
| 346 | Lisp_Object current_syntax_table; /* Syntax table for current pos. */ |
| 347 | Lisp_Object old_prop; /* Syntax-table prop at prev pos. */ |
| 348 | int b_property; /* Last index where c_s_t is |
| 349 | not valid. */ |
| 350 | int e_property; /* First index where c_s_t is |
| 351 | not valid. */ |
| 352 | INTERVAL forward_i; /* Where to start lookup on forward */ |
| 353 | INTERVAL backward_i; /* or backward movement. The |
| 354 | data in c_s_t is valid |
| 355 | between these intervals, |
| 356 | and possibly at the |
| 357 | intervals too, depending |
| 358 | on: */ |
| 359 | /* Offset for positions specified to UPDATE_SYNTAX_TABLE. */ |
| 360 | int offset; |
| 361 | char left_ok; |
| 362 | char right_ok; |
| 363 | }; |
| 364 | |
| 365 | extern struct gl_state_s gl_state; |
| 366 | extern int parse_sexp_lookup_properties; |
| 367 | extern INTERVAL interval_of P_ ((int, Lisp_Object)); |
| 368 | |
| 369 | extern int scan_words P_ ((int, int)); |