Rename 'ctx' back to 'context' in external API
[platform/upstream/libxkbcommon.git] / src / xkbcomp / scanner.l
1 /************************************************************
2  Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3
4  Permission to use, copy, modify, and distribute this
5  software and its documentation for any purpose and without
6  fee is hereby granted, provided that the above copyright
7  notice appear in all copies and that both that copyright
8  notice and this permission notice appear in supporting
9  documentation, and that the name of Silicon Graphics not be
10  used in advertising or publicity pertaining to distribution
11  of the software without specific prior written permission.
12  Silicon Graphics makes no representation about the suitability
13  of this software for any purpose. It is provided "as is"
14  without any express or implied warranty.
15
16  SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25  ********************************************************/
26
27 %{
28 #include <stdio.h>
29
30 #include "xkbcomp-priv.h"
31 #include "parseutils.h"
32
33 extern int yyparse(struct parser_param *param);
34
35 #define YY_USER_ACTION {                \
36         yylloc->first_line = yylineno;  \
37         yylloc->last_line = yylineno;   \
38 }
39
40 #define APPEND_S(ch) do {                                               \
41     if (yyextra->s - yyextra->scanBuf >= sizeof(yyextra->scanBuf) - 1)  \
42         return ERROR_TOK;                                               \
43     *yyextra->s++ = ch;                                                 \
44 } while (0)
45 %}
46
47 %option reentrant
48 %option extra-type="struct scanner_extra *"
49 %option bison-bridge bison-locations
50 %option yylineno
51 %option nounistd noyywrap noinput nounput
52 %option never-interactive
53 %option case-insensitive
54
55 %x S_STR S_KEY
56
57 %%
58
59 "//"[^\n]*
60 "#"[^\n]*
61
62 \"                      yyextra->s = yyextra->scanBuf; BEGIN(S_STR);
63 \<                      yyextra->s = yyextra->scanBuf; BEGIN(S_KEY);
64
65 <S_STR>\" {
66                         BEGIN(INITIAL);
67                         *yyextra->s = '\0';
68                         yylval->str = strdup(yyextra->scanBuf);
69                         return STRING;
70                     }
71 <S_KEY>\> {
72                         BEGIN(INITIAL);
73                         *yyextra->s = '\0';
74                         yylval->str = strdup(yyextra->scanBuf);
75                         return KEYNAME;
76                     }
77
78 <S_STR,S_KEY>\\[0-7]{1,3} {
79                         /* octal escape sequence */
80                         unsigned int result;
81
82                         (void) sscanf( yytext + 1, "%o", &result );
83
84                         if (result > 0xff) {
85                             fprintf(stderr, "Illegal octal escape %s\n", yytext);
86                             return ERROR_TOK;
87                         }
88
89                         APPEND_S(result);
90                     }
91
92 <S_STR,S_KEY>\\[0-9]+ {
93                         fprintf(stderr, "Illegal octal escape %s\n", yytext);
94                         return ERROR_TOK;
95                     }
96
97 <S_STR,S_KEY>\\n        APPEND_S('\n');
98 <S_STR,S_KEY>\\t        APPEND_S('\t');
99 <S_STR,S_KEY>\\r        APPEND_S('\r');
100 <S_STR,S_KEY>\\b        APPEND_S('\b');
101 <S_STR,S_KEY>\\f        APPEND_S('\f');
102 <S_STR,S_KEY>\\v        APPEND_S('\v');
103 <S_STR,S_KEY>\\e        APPEND_S('\033');
104
105 <S_STR,S_KEY>.          APPEND_S(yytext[0]);
106
107 xkb_keymap              return XKB_KEYMAP;
108 xkb_keycodes            return XKB_KEYCODES;
109 xkb_types               return XKB_TYPES;
110 xkb_symbols             return XKB_SYMBOLS;
111 xkb_compat              return XKB_COMPATMAP;
112 xkb_compat_map          return XKB_COMPATMAP;
113 xkb_compatibility       return XKB_COMPATMAP;
114 xkb_compatibility_map   return XKB_COMPATMAP;
115 xkb_geometry            return XKB_GEOMETRY;
116 xkb_semantics           return XKB_SEMANTICS;
117 xkb_layout              return XKB_LAYOUT;
118 include                 return INCLUDE;
119 override                return OVERRIDE;
120 augment                 return AUGMENT;
121 replace                 return REPLACE;
122 alternate               return ALTERNATE;
123 partial                 return PARTIAL;
124 default                 return DEFAULT;
125 hidden                  return HIDDEN;
126 virtual_modifiers       return VIRTUAL_MODS;
127 type                    return TYPE;
128 interpret               return INTERPRET;
129 action                  return ACTION_TOK;
130 key                     return KEY;
131 alias                   return ALIAS;
132 group                   return GROUP;
133 modmap                  return MODIFIER_MAP;
134 mod_map                 return MODIFIER_MAP;
135 modifier_map            return MODIFIER_MAP;
136 indicator               return INDICATOR;
137 shape                   return SHAPE;
138 row                     return ROW;
139 keys                    return KEYS;
140 section                 return SECTION;
141 overlay                 return OVERLAY;
142 text                    return TEXT;
143 outline                 return OUTLINE;
144 solid                   return SOLID;
145 logo                    return LOGO;
146 virtual                 return VIRTUAL;
147 alphanumeric_keys       return ALPHANUMERIC_KEYS;
148 modifier_keys           return MODIFIER_KEYS;
149 keypad_keys             return KEYPAD_KEYS;
150 function_keys           return FUNCTION_KEYS;
151 alternate_group         return ALTERNATE_GROUP;
152
153 [a-zA-Z_][a-zA-Z_0-9]*  yylval->str = strdup(yytext); return IDENT;
154
155 0x[a-fA-F0-9]+          |
156 [0-9]+                  {
157                             char *end;
158                             yylval->num = strtoul(yytext, &end, 0);
159
160                             return INTEGER;
161                         }
162 [0-9]+\.[0-9]+ {
163                             char *end;
164                             yylval->num = strtod(yytext, &end) * XkbGeomPtsPerMM;
165
166                             return FLOAT;
167                         }
168
169 "="                     return EQUALS;
170 "+"                     return PLUS;
171 "-"                     return MINUS;
172 "/"                     return DIVIDE;
173 "*"                     return TIMES;
174 "{"                     return OBRACE;
175 "}"                     return CBRACE;
176 "("                     return OPAREN;
177 ")"                     return CPAREN;
178 "["                     return OBRACKET;
179 "]"                     return CBRACKET;
180 "."                     return DOT;
181 ","                     return COMMA;
182 ";"                     return SEMI;
183 "!"                     return EXCLAM;
184 "~"                     return INVERT;
185
186 [ \t\r\n\v]+            
187
188 <<EOF>>                 return END_OF_FILE;
189
190 .                       return ERROR_TOK;
191
192 %%
193
194 void
195 yyerror(struct YYLTYPE *loc, void *scanner, const char *msg)
196 {
197     struct scanner_extra *extra = yyget_extra(scanner);
198
199     if (warningLevel > 0) {
200         fprintf(stderr, "%s: line %d of %s\n", msg, loc->first_line,
201                 extra->scanFile ? extra->scanFile : "(unknown)");
202         if (warningLevel > 3)
203             fprintf(stderr, "last scanned symbol is: %s\n", extra->scanBuf);
204     }
205 }
206
207 bool
208 XKBParseString(struct xkb_context *ctx, const char *string,
209                const char *file_name, XkbFile **out)
210 {
211     int ret;
212     struct parser_param param;
213     struct scanner_extra extra;
214     YY_BUFFER_STATE state;
215
216     if (string == NULL)
217         return false;
218
219     param.ctx = ctx;
220
221     memset(&extra, 0, sizeof(extra));
222     ret = yylex_init_extra(&extra, &param.scanner);
223     if (ret != 0)
224         return false;
225
226     extra.scanFile = strdup(file_name);
227     if (!extra.scanFile)
228         return false;
229
230     state = yy_scan_string(string, param.scanner);
231     ret = yyparse(&param);
232     yy_delete_buffer(state, param.scanner);
233     yylex_destroy(param.scanner);
234     free(extra.scanFile);
235     if (ret != 0)
236         return false;
237
238     CheckDefaultMap(param.rtrn, file_name);
239     *out = param.rtrn;
240     return true;
241 }
242
243 bool
244 XKBParseFile(struct xkb_context *ctx, FILE *file,
245              const char *file_name, XkbFile **out)
246 {
247     int ret;
248     struct parser_param param;
249     struct scanner_extra extra;
250
251     if (!file)
252         return false;
253
254     param.ctx = ctx;
255
256     memset(&extra, 0, sizeof(extra));
257     ret = yylex_init_extra(&extra, &param.scanner);
258     if (ret != 0)
259         return false;
260
261     extra.scanFile = strdup(file_name);
262     if (!extra.scanFile)
263         return false;
264
265     yyset_in(file, param.scanner);
266     ret = yyparse(&param);
267     yylex_destroy(param.scanner);
268     free(extra.scanFile);
269     if (ret != 0)
270         return false;
271
272     CheckDefaultMap(param.rtrn, file_name);
273     *out = param.rtrn;
274     return true;
275 }