5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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 THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * Keywords are multichar, punctuation lexing is a bit more complicated
32 * than keyword lexing.
34 static const char *const lex_keywords[] = {
35 "do", "else", "if", "while",
36 "break", "continue", "return", "goto",
47 struct lex_file *lex_open(FILE *fp) {
48 struct lex_file *lex = mem_a(sizeof(struct lex_file));
51 fseek(lex->file, 0, SEEK_END);
52 lex->length = ftell(lex->file);
53 lex->size = lex->length; /* copy, this is never changed */
54 fseek(lex->file, 0, SEEK_SET);
57 memset(lex->peek, 0, sizeof(lex->peek));
62 int lex_close(struct lex_file *file) {
65 ret = fclose(file->file);
71 static void lex_addch(int ch, struct lex_file *file) {
72 if (file->current < sizeof(file->lastok)-1)
73 file->lastok[file->current++] = (char)ch;
74 if (file->current == sizeof(file->lastok)-1)
75 file->lastok[file->current] = (char)'\0';
77 static inline void lex_clear(struct lex_file *file) {
82 * read in inget/unget character from a lexer stream.
83 * This doesn't play with file streams, the lexer has
84 * it's own internal state for this.
86 static int lex_inget(struct lex_file *file) {
89 return file->peek[--file->last];
90 return fgetc(file->file);
92 static void lex_unget(int ch, struct lex_file *file) {
93 if (file->last < sizeof(file->peek))
94 file->peek[file->last++] = ch;
99 * This is trigraph and digraph support, a feature not qc compiler
100 * supports. Moving up in this world!
102 static int lex_trigraph(struct lex_file *file) {
104 if ((ch = lex_inget(file)) != '?') {
109 ch = lex_inget(file);
111 case '(' : return '[' ;
112 case ')' : return ']' ;
113 case '/' : return '\\';
114 case '\'': return '^' ;
115 case '<' : return '{' ;
116 case '>' : return '}' ;
117 case '!' : return '|' ;
118 case '-' : return '~' ;
119 case '=' : return '#' ;
121 lex_unget('?', file);
122 lex_unget(ch , file);
127 static int lex_digraph(struct lex_file *file, int first) {
128 int ch = lex_inget(file);
131 if (ch == '%') return '{';
132 if (ch == ':') return '[';
135 if (ch == '>') return '}';
136 if (ch == ':') return '#';
139 if (ch == '>') return ']';
147 static int lex_getch(struct lex_file *file) {
148 int ch = lex_inget(file);
150 return lex_trigraph(file);
151 if (ch == '<' || ch == ':' || ch == '%')
152 return lex_digraph (file, ch);
157 static int lex_get(struct lex_file *file) {
159 if (!isspace(ch = lex_getch(file)))
162 /* skip over all spaces */
163 while (isspace(ch) && ch != '\n')
164 ch = lex_getch(file);
172 static int lex_skipchr(struct lex_file *file) {
177 lex_addch('\'', file);
179 for (it = 0; it < 2 && ((ch = lex_inget(file)) != '\''); it++) {
185 lex_addch(lex_getch(file), file);
187 lex_addch('\'', file);
188 lex_addch('\0', file);
196 static int lex_skipstr(struct lex_file *file) {
199 lex_addch('"', file);
201 while ((ch = lex_getch(file)) != '"') {
202 if (ch == '\n' || ch == EOF)
207 lex_addch(lex_inget(file), file);
210 lex_addch('"', file);
211 lex_addch('\0', file);
215 static int lex_skipcmt(struct lex_file *file) {
218 ch = lex_getch(file);
221 lex_addch('/', file);
222 lex_addch('/', file);
224 while ((ch = lex_getch(file)) != '\n') {
227 lex_addch(lex_getch(file), file);
232 lex_addch('\0', file);
241 lex_addch('/', file);
246 while ((ch = lex_getch(file)) != '*') {
248 return error(ERROR_LEX, "malformatted comment at line %d", file->line);
253 } while ((ch = lex_getch(file)) != '/');
255 lex_addch('/', file);
256 lex_addch('\0', file);
261 static int lex_getsource(struct lex_file *file) {
262 int ch = lex_get(file);
264 /* skip char/string/comment */
266 case '\'': return lex_skipchr(file);
267 case '"': return lex_skipstr(file);
268 case '/': return lex_skipcmt(file);
273 int lex_token(struct lex_file *file) {
274 int ch = lex_getsource(file);
277 /* valid identifier */
278 if (ch > 0 && (ch == '_' || isalpha(ch))) {
280 while (ch > 0 && (isalpha(ch) || ch == '_')) {
282 ch = lex_getsource(file);
285 lex_addch('\0', file);
287 /* look inside the table for a keyword .. */
288 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
289 if (!strncmp(file->lastok, lex_keywords[it], sizeof(lex_keywords[it])))
292 /* try the hashtable for typedefs? */
293 if (typedef_find(file->lastok))
294 for (it = 0; it < sizeof(lex_keywords)/sizeof(*lex_keywords); it++)
295 if (!strncmp(typedef_find(file->lastok)->name, lex_keywords[it], sizeof(lex_keywords[it])))
304 void lex_reset(struct lex_file *file) {
307 file->length = file->size;
308 fseek(file->file, 0, SEEK_SET);
310 memset(file->peek, 0, sizeof(file->peek ));
311 memset(file->lastok, 0, sizeof(file->lastok));