1 /*  Part of XPCE --- The SWI-Prolog GUI toolkit
2 
3     Author:        Jan Wielemaker and Anjo Anjewierden
4     E-mail:        jan@swi.psy.uva.nl
5     WWW:           http://www.swi.psy.uva.nl/projects/xpce/
6     Copyright (c)  1985-2017, University of Amsterdam
7     All rights reserved.
8 
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions
11     are met:
12 
13     1. Redistributions of source code must retain the above copyright
14        notice, this list of conditions and the following disclaimer.
15 
16     2. Redistributions in binary form must reproduce the above copyright
17        notice, this list of conditions and the following disclaimer in
18        the documentation and/or other materials provided with the
19        distribution.
20 
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25     COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32     POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #ifndef GLOBAL
36 #define GLOBAL extern
37 #endif
38 
39 NewClass(syntax_table)
40   Name		name;			/* Name of this table */
41   Int		size;			/* Size of the table (256) */
42   Regex		sentence_end;		/* End-Of-Sentence */
43   Regex		paragraph_end;		/* End-Of-Pargraph */
44   Name		qq_start;		/* Quasi quotation start */
45   Name		qq_end;			/* Quasi quotation end */
46   BoolObj	prolog;			/* Is Prolog syntax */
47   unsigned short *table;		/* Type-flags */
48   unsigned char  *context;		/* Context info */
49 End;
50 
51 #define makeCFlag(n)	(1 << (n-1))
52 
53 #define LC	makeCFlag(1)		/* Lower case letter */
54 #define UC	makeCFlag(2)		/* Upper case letter */
55 #define DI	makeCFlag(3)		/* Digit */
56 #define WS	makeCFlag(4)		/* Word separator (in symbol) */
57 #define SY	makeCFlag(5)		/* Other symbol-characters */
58 #define OB	makeCFlag(6)		/* Open Brace (context: close) */
59 #define CB	makeCFlag(7)		/* Close Brace (context: open) */
60 #define EL	makeCFlag(8)		/* Ends Line */
61 #define BL	makeCFlag(9)		/* Blank */
62 #define QT	makeCFlag(10)		/* String quote (context: escape) */
63 #define PU	makeCFlag(11)		/* Punctuation */
64 #define EB	makeCFlag(12)		/* End Buffer/string */
65 #define CS	makeCFlag(13)		/* Comment-start (context: 2nd char) */
66 #define CE	makeCFlag(14)		/* Comment-end (context: 2nd char) */
67 #define CT	makeCFlag(15)		/* Control character */
68 #define XD	makeCFlag(16)		/* Xdigit */
69 
70 #define AN	(LC|UC|DI|WS|SY)	/* General symbol-character */
71 
72 		/********************************
73 		*       CHARACTER TYPES		*
74 		********************************/
75 
76 #define streq(s, t)	((s) && (t) && (strcmp((s), (t)) == 0))
77 
78 #define META_OFFSET		(1L<<16)
79 
80 #define EOS	0			/* end of string */
81 #define ESC	27			/* char escape */
82 #define TAB	9			/* tab character */
83 #define DEL	127			/* delete character */
84 #define Control(x) (x & 037)
85 #define Meta(x)    (x + META_OFFSET)
86 
87 GLOBAL SyntaxTable DefaultSyntaxTable;	/* Systems default table */
88 extern unsigned short char_flags[];	/* Initial flags table */
89 extern unsigned short syntax_spec_code[]; /* Char --> syntax (for \sC regex) */
90 extern unsigned char  char_context[];	/* Initial context table */
91 
92 #define Is8char(c)		(((c) & ~0xff) == 0)
93 #define HasSyntax(c, f)		(Is8char(c) && \
94 				 (char_flags[(unsigned int)(c)] & (f)))
95 #define HasSyntax8(c, f)	((char_flags[(unsigned int)(c)] & (f)))
96 
97 #ifndef iscsym
98 #define iscsym(c)		(iswalnum(c) || c == '_')
99 #endif
100 #define isopenbrace(c)		HasSyntax((c), OB)
101 #define isclosebrace(c)		HasSyntax((c), CB)
102 #define isendsline(c)		HasSyntax((c), EL)
103 #define isquote(c)		HasSyntax((c), QT)
104 #define issymbol(c)		HasSyntax((c), SY)
105 #define iswordsep(c)		HasSyntax((c), WS)
106 
107 #define ischtype(c, tp)		HasSyntax((c), tp)
108 
109 #define ismatching(c1, c2)      (Is8char(c1) && \
110 				 (char_context[(unsigned int)(c1)] == (c2))
111 #define isstringescape(q, e)	(Is8char(q) && \
112 				 char_context[((unsigned int))(q)] == (e))
113 
114 #if !defined(_GNU_SOURCE) && !defined(isblank)
115 #define isblank(c)		((c) == ' ' || (c) == '\t')
116 #endif
117 
118 		/********************************
119 		*         TABLE VERSIONS	*
120 		********************************/
121 
122 #define THasSyntax(t, c, f)	(Is8char(c) && \
123 				 ((t)->table[(unsigned int)(c)] & (f)))
124 
125 #define tislower(t, c)		THasSyntax(t, c, LC)
126 #define tisupper(t, c)		THasSyntax(t, c, UC)
127 #define tisdigit(t, c)		THasSyntax(t, c, DI)
128 #define tisopenbrace(t, c)	THasSyntax(t, c, OB)
129 #define tisclosebrace(t, c)	THasSyntax(t, c, CB)
130 #define tisendsline(t, c)	THasSyntax(t, c, EL)
131 #define tisblank(t, c)		THasSyntax(t, c, BL)
132 #define tislayout(t, c)		THasSyntax(t, c, BL|EL)
133 #define tisquote(t, c)		THasSyntax(t, c, QT)
134 #define tissymbol(t, c)		THasSyntax(t, c, SY)
135 #define tiswordsep(t, c)	THasSyntax(t, c, WS)
136 #define tisprint(t, c)		!THasSyntax(t, c, CT)
137 
138 #define tisalnum(t, c)		THasSyntax(t, c, AN)
139 #define tisletter(t, c)		THasSyntax(t, c, LC|UC)
140 #define tischtype(t, c, tp)	THasSyntax(t, c, (tp))
141 
142 #define tismatching(t, c1, c2)  (Is8char(c1) && (t)->context[c1] == (c2))
143 #define tisstringescape(t,q,e)	(Is8char(q) && (t)->context[q] == (e))
144 
145 #define tiscommentstart(t, c)	(THasSyntax(t, c, CS) && \
146 				 !(t)->context[(unsigned int)(c)])
147 #define tiscommentend(t, c)	(THasSyntax(t, c, CE) && \
148 				 !(t)->context[(unsigned int)(c)])
149 #define tiscommentstart1(t, c)	(THasSyntax(t, c, CS) && \
150 				 ((t)->context[(unsigned int)(c)] & 1))
151 #define tiscommentend1(t, c)	(THasSyntax(t, c, CE) && \
152 				 ((t)->context[(unsigned int)(c)] & 4))
153 #define tiscommentstart2(t, c)	(THasSyntax(t, c, CS) && \
154 				 ((t)->context[(unsigned int)(c)] & 2))
155 #define tiscommentend2(t, c)	(THasSyntax(t, c, CE) && \
156 				 ((t)->context[(unsigned int)(c)] & 8))
157 
158 
159 		/********************************
160 		*     HOST-LANGUAGE SYMBOLS	*
161 		********************************/
162 
163 GLOBAL struct
164 { int	uppercase;			/* keywords mapped to uppercase */
165   char	word_separator;			/* current word separator */
166 } syntax;
167 
168