Debian Codegraph @

tools/kate_lexer.l

raw
1 /* Copyright (C) 2008 Vincent Penquerc'h.
2 This file is part of the Kate codec library.
3 Written by Vincent Penquerc'h.
4
5 Use, distribution and reproduction of this library is governed
6 by a BSD style source license included with this source in the
7 file 'COPYING'. Please read these terms before distributing. */
8
9
10 %{
11
12 #define KATE_INTERNAL
13 #include "kate_internal.h"
14
15 #include <stdarg.h>
16 #include <stdlib.h>
17 #ifdef HAVE_STRING_H
18 #include <string.h>
19 #endif
20 #include "kate/kate.h"
21 #include "katedesc.h"
22 #include "kate_parser.h"
23
24 #define YY_DECL extern int katedesc_lex(YYSTYPE *lvalp)
25
26 static int previous_state=-1;
27
28 static size_t nmacros=0;
29 static char **macro_names=NULL;
30 static char **macro_bodies=NULL;
31
32 static void replace_macro(const char *name);
33 static const char *find_macro(const char *name);
34
35 static void process_nlines(const char *text);
36
37 static int freeze_nlines=0;
38
39 static YY_BUFFER_STATE main_buffer=NULL;
40
41 int nlines=1;
42
43 /* string [a-zA-Z ,\.;:!\?\-\(\)\'\`0-9#/\\:@%_]+ */
44
45 %}
46
47 %option nounput
48 %option yymore
49 %option never-interactive
50
51 %s NORMAL
52 %x BLOCK_COMMENT MACRO_1 MACRO_2
53
54 string [^\<\>\&]+
55 nqstring [^\<\>\&\"]+
56 /* single_quoted_string \'[^\<\>\&']*\' */
57 /* double_quoted_string \"[^\<\>\&"]*\" */
58 /* single_quoted_string \'[^']*\' */
59 /* double_quoted_string \"[^"\n]*\" */
60 double_quoted_string \"[^"]*\"
61
62 digit [0-9]
63 hexdigit [0-9a-fA-F]
64 fp1 [-+]?{digit}+\.?([eE][-+]?{digit}+)?
65 fp2 [-+]?{digit}*\.{digit}+([eE][-+]?{digit}+)?
66 fp {fp1}|{fp2}
67 unumber10 \+?{digit}+
68 unumber16 \+?0x{hexdigit}+
69 number10 [-+]?{digit}+
70 number16 [-+]?0x{hexdigit}+
71 color6 #{hexdigit}{6}
72 color8 #{hexdigit}{8}
73 identifier [a-zA-Z_][a-zA-Z_0-9]*
74 macro ${identifier}
75 entity &#{digit}+;
76
77 hash_comment #[^\n]*\n
78 cplusplus_comment \/\/[^\n]*\n
79 eol_comment {hash_comment}|{cplusplus_comment}
80
81 %%
82
83
84 <INITIAL>(\xff\xfe)|(\xfe\xff) {
85 fprintf(stderr,"This file seems to be encoded in UTF-16, Kate only supports UTF-8\n");
86 fprintf(stderr,"You will need to convert it to UTF-8 first (eg, use iconv)\n");
87 }
88 <INITIAL>\xef\xbb\xbf {BEGIN(NORMAL);}
89 <INITIAL>. {yyless(0);BEGIN(NORMAL);}
90
91 <NORMAL>"/*" {previous_state=NORMAL;BEGIN(BLOCK_COMMENT);}
92 <BLOCK_COMMENT>[^*\n]+ {}
93 <BLOCK_COMMENT>"*"+[^*/\n] {}
94 <BLOCK_COMMENT>[\r\n] {if (!freeze_nlines) nlines++;}
95 <BLOCK_COMMENT>"*"+"/" {BEGIN(previous_state);}
96
97 <MACRO_1>[ \t]+ {}
98 <MACRO_1>{identifier} {BEGIN(MACRO_2);lvalp->string=yytext;return IDENTIFIER;}
99
100 <MACRO_2>\\\n {yytext[yyleng-2]=' ';yymore();}
101 <MACRO_2>[^\n\\]+ {yymore();}
102 <MACRO_2>\n {BEGIN(previous_state);lvalp->string=yytext;process_nlines(yytext);return MACRO_BODY;}
103
104 {eol_comment} {if (!freeze_nlines) nlines++;}
105 [ \t]+ {}
106 [\r\n] {if (!freeze_nlines) nlines++;}
107
108 kate {return KATE;}
109
110 {macro} {replace_macro(katedesc_text+1);}
111
112 defs {return DEFS;}
113 event {return EVENT;}
114 language {return LANGUAGE;}
115 comment {return COMMENT;}
116 define {return DEFINE;}
117 style {return STYLE;}
118 region {return REGION;}
119 curve {return CURVE;}
120 text {return TEXT;}
121 background {return BACKGROUND;}
122 bg {return BACKGROUND;}
123 color {return COLOR;}
124 marker {return MARKER;}
125 position {return POSITION;}
126 size {return SIZE;}
127 halign {return HALIGN;}
128 valign {return VALIGN;}
129 default {return DEFAULT;}
130 points {return POINTS;}
131 hleft {return HLEFT;}
132 hcenter {return HCENTER;}
133 hright {return HRIGHT;}
134 vtop {return VTOP;}
135 vcenter {return VCENTER;}
136 vbottom {return VBOTTOM;}
137 starts {return STARTS;}
138 ends {return ENDS;}
139 at {return AT;}
140 start {return START;}
141 end {return END;}
142 time {return TIME;}
143 duration {return DURATION;}
144 --> {return ARROW;}
145 motion {return MOTION;}
146 mapping {return MAPPING;}
147 frame {return FRAME;}
148 none {return NONE;}
149 semantics {return SEMANTICS;}
150 external {return EXTERNAL;}
151 internal {return INTERNAL;}
152 alignment {return ALIGNMENT;}
153 rg {return RG;}
154 ba {return BA;}
155 for {return FOR;}
156 from {return FROM;}
157 to {return TO;}
158 macro {return MACRO;}
159 timebase {return TIMEBASE;}
160 morph {return MORPH;}
161 secondary {return SECONDARY;}
162 path {return PATH;}
163 section {return SECTION;}
164 directionality {return DIRECTIONALITY;}
165 palette {return PALETTE;}
166 bitmap {return BITMAP;}
167 colors {return COLORS;}
168 font {return FONT;}
169 range {return RANGE;}
170 first {return FIRST;}
171 last {return LAST;}
172 code {return CODE;}
173 point {return POINT;}
174 user {return USER;}
175 source {return SOURCE;}
176 draw {return DRAW;}
177 visible {return VISIBLE;}
178 category {return CATEGORY;}
179 id {return ID;}
180 bold {return BOLD;}
181 italics {return ITALICS;}
182 underline {return UNDERLINE;}
183 strike {return STRIKE;}
184 periodic {return PERIODIC;}
185 granule {return GRANULE;}
186 rate {return RATE;}
187 shift {return SHIFT;}
188 width {return WIDTH;}
189 height {return HEIGHT;}
190 left {return LEFT;}
191 top {return TOP;}
192 right {return RIGHT;}
193 bottom {return BOTTOM;}
194 margin {return MARGIN;}
195 margins {return MARGINS;}
196 horizontal {return HORIZONTAL;}
197 vertical {return VERTICAL;}
198 png {return PNG;}
199 justify {return JUSTIFY;}
200 clip {return CLIP;}
201 pre {return PRE;}
202 markup {return MARKUP;}
203 canvas {return CANVAS;}
204 local {return LOCAL;}
205 offset {return OFFSET;}
206 wrap {return WRAP;}
207 word {return WORD;}
208 meta {return META;}
209
210
211
212 left_to_right|l2r|ltr|ltr_ttb|l2r_t2b {return L2R_T2B;}
213 right_to_left|r2l|rtl|rtl_ttb|r2l_t2b {return R2L_T2B;}
214 ttb_rtl|t2b_r2l {return T2B_R2L;}
215 ttb_ltr|t2b_l2r {return T2B_L2R;}
216
217 pixels {lvalp->number=kate_pixel;return METRIC;}
218 pixel {lvalp->number=kate_pixel;return METRIC;}
219 percentage {lvalp->number=kate_percentage;return METRIC;}
220 percent {lvalp->number=kate_percentage;return METRIC;}
221 millionths {lvalp->number=kate_millionths;return METRIC;}
222
223 static {return STATIC;}
224 linear {return LINEAR;}
225 catmull_rom {return CATMULL_ROM;}
226 bezier_cubic {return BEZIER_CUBIC;}
227 bspline {return BSPLINE;}
228
229 simple_timed_glyph_marker {return SIMPLE_TIMED_GLYPH_MARKER;}
230 simple_timed_glyph_style_morph {return SIMPLE_TIMED_GLYPH_STYLE_MORPH;}
231 glyph {return GLYPH;}
232 pointer {return POINTER;}
233 in {return IN;}
234 pause {return PAUSE;}
235
236 {double_quoted_string} {
237 katedesc_text[strlen(katedesc_text)-1]=0;
238 lvalp->string=katedesc_text+1;
239 process_nlines(katedesc_text);
240 return STRING;
241 }
242 {unumber10} {lvalp->unumber=strtoul(katedesc_text,NULL,10);return UNUMBER;}
243 {unumber16} {lvalp->unumber=strtoul(katedesc_text,NULL,16);return UNUMBER;}
244 {number10} {lvalp->number=strtol(katedesc_text,NULL,10);return NUMBER;}
245 {number16} {lvalp->number=strtol(katedesc_text,NULL,16);return NUMBER;}
246 {fp} {lvalp->fp=(kate_float)atof(katedesc_text);return FLOAT;}
247 {color6} {lvalp->color=strtol(katedesc_text+1,NULL,16);return COLORSPEC;}
248 {color8} {lvalp->color=strtol(katedesc_text+1,NULL,16);return COLORSPEC;}
249
250
251 <<EOF>> {
252 katedesc_text="<EOF>";
253 if (freeze_nlines) --freeze_nlines;
254 yy_delete_buffer(YY_CURRENT_BUFFER);
255 if (main_buffer) {
256 yy_switch_to_buffer(main_buffer);
257 main_buffer=NULL;
258 }
259 else yyterminate();
260 }
261
262 . {return katedesc_text[0];}
263
264
265 %%
266
267 int katedesc_wrap()
268 {
269 return 1;
270 }
271
272 void set_macro_mode()
273 {
274 BEGIN(MACRO_1);
275 }
276
277 void unset_macro_mode()
278 {
279 BEGIN(NORMAL);
280 }
281
282 static void replace_macro(const char *name)
283 {
284 const char *body=find_macro(name);
285 if (body) {
286 ++freeze_nlines;
287 main_buffer=YY_CURRENT_BUFFER;
288 yy_scan_string(body);
289 }
290 }
291
292 void add_macro(const char *name,const char *body)
293 {
294 size_t nlen,blen;
295
296 if (!name || !body) {
297 katedesc_error("name and body cannot be null");
298 exit(-1);
299 }
300
301 ++nmacros;
302 macro_names=(char**)kate_realloc(macro_names,nmacros*sizeof(char*));
303 macro_bodies=(char**)kate_realloc(macro_bodies,nmacros*sizeof(char*));
304 if (!macro_names || !macro_bodies) {
305 katedesc_error("out of memory");
306 exit(-1);
307 }
308
309 nlen=strlen(name);
310 blen=strlen(body);
311 macro_names[nmacros-1]=kate_malloc(nlen+1);
312 macro_bodies[nmacros-1]=kate_malloc(blen+1);
313 if (!macro_names[nmacros-1] || !macro_bodies[nmacros-1]) {
314 katedesc_error("out of memory");
315 exit(-1);
316 }
317 strcpy(macro_names[nmacros-1],name);
318 strcpy(macro_bodies[nmacros-1],body);
319 }
320
321 static const char *find_macro(const char *name)
322 {
323 size_t n;
324 if (!name) {
325 katedesc_error("macro name cannot be null");
326 exit(-1);
327 }
328 for (n=0;n<nmacros;++n) {
329 if (!strcmp(macro_names[n],name)) return macro_bodies[n];
330 }
331 katedesc_error("undefined macro");
332 return NULL;
333 }
334
335 void free_macros(void)
336 {
337 size_t n;
338 for (n=0;n<nmacros;++n) {
339 kate_free(macro_names[n]);
340 kate_free(macro_bodies[n]);
341 }
342 kate_free(macro_names);
343 kate_free(macro_bodies);
344 }
345
346 static int get_nlines(const char *text)
347 {
348 int text_nlines=0;
349 if (!text) {
350 katedesc_error("null text");
351 return 0;
352 }
353 while (*text) if (*text++=='\n') ++text_nlines;
354 return text_nlines;
355 }
356
357 static void process_nlines(const char *text)
358 {
359 if (!freeze_nlines) {
360 nlines+=get_nlines(text);
361 }
362 }
363
364 void cleanup_lexer(void)
365 {
366 #ifdef FLEX_SCANNER
367 #if (YY_FLEX_MAJOR_VERSION+0)*0x10000+(YY_FLEX_MINOR_VERSION+0)*0x100+(YY_FLEX_SUBMINOR_VERSION+0)>=0x020509
368 katedesc_lex_destroy();
369 #endif
370 #endif
371 }
372