aef9a62f6d2277b8249a131afb3e7794b9195c2e
[platform/upstream/libxkbcommon.git] / src / xkbcomp / scanner.c
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 "xkbcomp-priv.h"
25 #include "parser-priv.h"
26 #include "scanner-utils.h"
27
28 static void
29 scanner_log(enum xkb_log_level level, struct scanner *s, const char *msg)
30 {
31     xkb_log_cond_level(s->ctx, level, "%s:%d:%d: %s\n", s->file_name,
32                        s->token_line, s->token_column, msg);
33 }
34
35 int
36 scanner_error(struct scanner *s, const char *msg)
37 {
38     scanner_log(XKB_LOG_LEVEL_ERROR, s, msg);
39     return ERROR_TOK;
40 }
41
42 void
43 scanner_warn(struct scanner *s, const char *msg)
44 {
45     scanner_log(XKB_LOG_LEVEL_WARNING, s, msg);
46 }
47
48 static bool
49 number(struct scanner *s, int64_t *out, int *out_tok)
50 {
51     bool is_float = false, is_hex = false;
52     const char *start = s->s + s->pos;
53     char *end;
54
55     if (lit(s, "0x")) {
56         while (isxdigit(peek(s))) next(s);
57         is_hex = true;
58     }
59     else {
60         while (isdigit(peek(s))) next(s);
61         is_float = chr(s, '.');
62         while (isdigit(peek(s))) next(s);
63     }
64     if (s->s + s->pos == start)
65         return false;
66
67     errno = 0;
68     if (is_hex)
69         *out = strtoul(start, &end, 16);
70     else if (is_float)
71         *out = strtod(start, &end);
72     else
73         *out = strtoul(start, &end, 10);
74     if (errno != 0 || s->s + s->pos != end)
75         *out_tok = ERROR_TOK;
76     else
77         *out_tok = (is_float ? FLOAT : INTEGER);
78     return true;
79 }
80
81 int
82 _xkbcommon_lex(YYSTYPE *yylval, struct scanner *s)
83 {
84     int tok;
85
86 skip_more_whitespace_and_comments:
87     /* Skip spaces. */
88     while (isspace(peek(s))) next(s);
89
90     /* Skip comments. */
91     if (lit(s, "//") || chr(s, '#')) {
92         while (!eof(s) && !eol(s)) next(s);
93         goto skip_more_whitespace_and_comments;
94     }
95
96     /* See if we're done. */
97     if (eof(s)) return END_OF_FILE;
98
99     /* New token. */
100     s->token_line = s->line;
101     s->token_column = s->column;
102     s->buf_pos = 0;
103
104     /* String literal. */
105     if (chr(s, '\"')) {
106         while (!eof(s) && !eol(s) && peek(s) != '\"') {
107             if (chr(s, '\\')) {
108                 uint8_t o;
109                 if      (chr(s, '\\')) buf_append(s, '\\');
110                 else if (chr(s, 'n'))  buf_append(s, '\n');
111                 else if (chr(s, 't'))  buf_append(s, '\t');
112                 else if (chr(s, 'r'))  buf_append(s, '\r');
113                 else if (chr(s, 'b'))  buf_append(s, '\b');
114                 else if (chr(s, 'f'))  buf_append(s, '\f');
115                 else if (chr(s, 'v'))  buf_append(s, '\v');
116                 else if (chr(s, 'e'))  buf_append(s, '\033');
117                 else if (oct(s, &o))   buf_append(s, (char) o);
118                 else {
119                     scanner_warn(s, "unknown escape sequence in string literal");
120                     /* Ignore. */
121                 }
122             } else {
123                 buf_append(s, next(s));
124             }
125         }
126         if (!buf_append(s, '\0') || !chr(s, '\"'))
127             return scanner_error(s, "unterminated string literal");
128         yylval->str = strdup(s->buf);
129         if (!yylval->str)
130             return scanner_error(s, "scanner out of memory");
131         return STRING;
132     }
133
134     /* Key name literal. */
135     if (chr(s, '<')) {
136         while (isgraph(peek(s)) && peek(s) != '>')
137             buf_append(s, next(s));
138         if (!buf_append(s, '\0') || !chr(s, '>'))
139             return scanner_error(s, "unterminated key name literal");
140         /* Empty key name literals are allowed. */
141         yylval->sval = xkb_atom_intern(s->ctx, s->buf, s->buf_pos - 1);
142         return KEYNAME;
143     }
144
145     /* Operators and punctuation. */
146     if (chr(s, ';')) return SEMI;
147     if (chr(s, '{')) return OBRACE;
148     if (chr(s, '}')) return CBRACE;
149     if (chr(s, '=')) return EQUALS;
150     if (chr(s, '[')) return OBRACKET;
151     if (chr(s, ']')) return CBRACKET;
152     if (chr(s, '(')) return OPAREN;
153     if (chr(s, ')')) return CPAREN;
154     if (chr(s, '.')) return DOT;
155     if (chr(s, ',')) return COMMA;
156     if (chr(s, '+')) return PLUS;
157     if (chr(s, '-')) return MINUS;
158     if (chr(s, '*')) return TIMES;
159     if (chr(s, '!')) return EXCLAM;
160     if (chr(s, '~')) return INVERT;
161
162     /* Identifier. */
163     if (isalpha(peek(s)) || peek(s) == '_') {
164         s->buf_pos = 0;
165         while (isalnum(peek(s)) || peek(s) == '_')
166             buf_append(s, next(s));
167         if (!buf_append(s, '\0'))
168             return scanner_error(s, "identifier too long");
169
170         /* Keyword. */
171         tok = keyword_to_token(s->buf);
172         if ((int) tok != -1) return tok;
173
174         yylval->str = strdup(s->buf);
175         if (!yylval->str)
176             return scanner_error(s, "scanner out of memory");
177         return IDENT;
178     }
179
180     /* Number literal (hexadecimal / decimal / float). */
181     if (number(s, &yylval->num, &tok)) {
182         if (tok == ERROR_TOK)
183             return scanner_error(s, "malformed number literal");
184         return tok;
185     }
186
187     return scanner_error(s, "unrecognized token");
188 }
189
190 XkbFile *
191 XkbParseString(struct xkb_context *ctx, const char *string, size_t len,
192                const char *file_name, const char *map)
193 {
194     struct scanner scanner;
195     scanner_init(&scanner, ctx, string, len, file_name);
196     return parse(ctx, &scanner, map);
197 }
198
199 XkbFile *
200 XkbParseFile(struct xkb_context *ctx, FILE *file,
201              const char *file_name, const char *map)
202 {
203     bool ok;
204     XkbFile *xkb_file;
205     const char *string;
206     size_t size;
207
208     ok = map_file(file, &string, &size);
209     if (!ok) {
210         log_err(ctx, "Couldn't read XKB file %s: %s\n",
211                 file_name, strerror(errno));
212         return NULL;
213     }
214
215     xkb_file = XkbParseString(ctx, string, size, file_name, map);
216     unmap_file(string, size);
217     return xkb_file;
218 }