• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "config.h"
25 
26 #include "xkbcomp-priv.h"
27 #include "parser-priv.h"
28 #include "scanner-utils.h"
29 
30 static bool
number(struct scanner * s,int64_t * out,int * out_tok)31 number(struct scanner *s, int64_t *out, int *out_tok)
32 {
33     bool is_float = false, is_hex = false;
34     const char *start = s->s + s->pos;
35     char *end;
36 
37     if (lit(s, "0x")) {
38         while (is_xdigit(peek(s))) next(s);
39         is_hex = true;
40     }
41     else {
42         while (is_digit(peek(s))) next(s);
43         is_float = chr(s, '.');
44         while (is_digit(peek(s))) next(s);
45     }
46     if (s->s + s->pos == start)
47         return false;
48 
49     errno = 0;
50     if (is_hex)
51         *out = strtoul(start, &end, 16);
52     else if (is_float)
53         /* The parser currently just ignores floats, so the cast is
54          * fine - the value doesn't matter. */
55         *out = strtod(start, &end);
56     else
57         *out = strtoul(start, &end, 10);
58     if (errno != 0 || s->s + s->pos != end)
59         *out_tok = ERROR_TOK;
60     else
61         *out_tok = (is_float ? FLOAT : INTEGER);
62     return true;
63 }
64 
65 int
_xkbcommon_lex(YYSTYPE * yylval,struct scanner * s)66 _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
67 {
68     int tok;
69 
70 skip_more_whitespace_and_comments:
71     /* Skip spaces. */
72     while (is_space(peek(s))) next(s);
73 
74     /* Skip comments. */
75     if (lit(s, "//") || chr(s, '#')) {
76         skip_to_eol(s);
77         goto skip_more_whitespace_and_comments;
78     }
79 
80     /* See if we're done. */
81     if (eof(s)) return END_OF_FILE;
82 
83     /* New token. */
84     s->token_line = s->line;
85     s->token_column = s->column;
86     s->buf_pos = 0;
87 
88     /* String literal. */
89     if (chr(s, '\"')) {
90         while (!eof(s) && !eol(s) && peek(s) != '\"') {
91             if (chr(s, '\\')) {
92                 uint8_t o;
93                 if      (chr(s, '\\')) buf_append(s, '\\');
94                 else if (chr(s, 'n'))  buf_append(s, '\n');
95                 else if (chr(s, 't'))  buf_append(s, '\t');
96                 else if (chr(s, 'r'))  buf_append(s, '\r');
97                 else if (chr(s, 'b'))  buf_append(s, '\b');
98                 else if (chr(s, 'f'))  buf_append(s, '\f');
99                 else if (chr(s, 'v'))  buf_append(s, '\v');
100                 else if (chr(s, 'e'))  buf_append(s, '\033');
101                 else if (oct(s, &o))   buf_append(s, (char) o);
102                 else {
103                     scanner_warn(s, "unknown escape sequence in string literal");
104                     /* Ignore. */
105                 }
106             } else {
107                 buf_append(s, next(s));
108             }
109         }
110         if (!buf_append(s, '\0') || !chr(s, '\"')) {
111             scanner_err(s, "unterminated string literal");
112             return ERROR_TOK;
113         }
114         yylval->str = strdup(s->buf);
115         if (!yylval->str)
116             return ERROR_TOK;
117         return STRING;
118     }
119 
120     /* Key name literal. */
121     if (chr(s, '<')) {
122         while (is_graph(peek(s)) && peek(s) != '>')
123             buf_append(s, next(s));
124         if (!buf_append(s, '\0') || !chr(s, '>')) {
125             scanner_err(s, "unterminated key name literal");
126             return ERROR_TOK;
127         }
128         /* Empty key name literals are allowed. */
129         yylval->atom = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
130         return KEYNAME;
131     }
132 
133     /* Operators and punctuation. */
134     if (chr(s, ';')) return SEMI;
135     if (chr(s, '{')) return OBRACE;
136     if (chr(s, '}')) return CBRACE;
137     if (chr(s, '=')) return EQUALS;
138     if (chr(s, '[')) return OBRACKET;
139     if (chr(s, ']')) return CBRACKET;
140     if (chr(s, '(')) return OPAREN;
141     if (chr(s, ')')) return CPAREN;
142     if (chr(s, '.')) return DOT;
143     if (chr(s, ',')) return COMMA;
144     if (chr(s, '+')) return PLUS;
145     if (chr(s, '-')) return MINUS;
146     if (chr(s, '*')) return TIMES;
147     if (chr(s, '/')) return DIVIDE;
148     if (chr(s, '!')) return EXCLAM;
149     if (chr(s, '~')) return INVERT;
150 
151     /* Identifier. */
152     if (is_alpha(peek(s)) || peek(s) == '_') {
153         s->buf_pos = 0;
154         while (is_alnum(peek(s)) || peek(s) == '_')
155             buf_append(s, next(s));
156         if (!buf_append(s, '\0')) {
157             scanner_err(s, "identifier too long");
158             return ERROR_TOK;
159         }
160 
161         /* Keyword. */
162         tok = keyword_to_token(s->buf, s->buf_pos - 1);
163         if (tok != -1) return tok;
164 
165         yylval->str = strdup(s->buf);
166         if (!yylval->str)
167             return ERROR_TOK;
168         return IDENT;
169     }
170 
171     /* Number literal (hexadecimal / decimal / float). */
172     if (number(s, &yylval->num, &tok)) {
173         if (tok == ERROR_TOK) {
174             scanner_err(s, "malformed number literal");
175             return ERROR_TOK;
176         }
177         return tok;
178     }
179 
180     scanner_err(s, "unrecognized token");
181     return ERROR_TOK;
182 }
183 
184 XkbFile *
XkbParseString(struct xkb_context * ctx,const char * string,size_t len,const char * file_name,const char * map)185 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
186                const char *file_name, const char *map)
187 {
188     struct scanner scanner;
189     scanner_init(&scanner, ctx, string, len, file_name, NULL);
190     return parse(ctx, &scanner, map);
191 }
192 
193 XkbFile *
XkbParseFile(struct xkb_context * ctx,FILE * file,const char * file_name,const char * map)194 XkbParseFile(struct xkb_context *ctx, FILE *file,
195              const char *file_name, const char *map)
196 {
197     bool ok;
198     XkbFile *xkb_file;
199     char *string;
200     size_t size;
201 
202     ok = map_file(file, &string, &size);
203     if (!ok) {
204         log_err(ctx, "Couldn't read XKB file %s: %s\n",
205                 file_name, strerror(errno));
206         return NULL;
207     }
208 
209     xkb_file = XkbParseString(ctx, string, size, file_name, map);
210     unmap_file(string, size);
211     return xkb_file;
212 }
213