scannerlexer.l 7.84 KB
Newer Older
1
/* -*- Mode: C -*-
2 3 4
/* GObject introspection: C lexer
 *
 * Copyright (c) 1997 Sandro Sigala  <ssigala@globalnet.it>
Jürg Billeter's avatar
Jürg Billeter committed
5
 * Copyright (c) 2007-2008 Jürg Billeter  <j@bitron.ch>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

%{
#include <ctype.h>
#include <stdio.h>

34 35
#include "scanner.h"
#include "scannerparser.h"
36
#include "grealpath.h"
37 38 39

int lineno;

40 41
extern int yylex (GIGenerator *igenerator);
#define YY_DECL int yylex (GIGenerator *igenerator)
42
static int yywrap (void);
43 44 45
static void parse_comment (GIGenerator *igenerator);
static void process_directive (GIGenerator *igenerator);
static int check_identifier (GIGenerator *igenerator, const char *);
46 47 48 49 50 51
%}

intsuffix				([uU][lL]?)|([lL][uU]?)
fracconst				([0-9]*\.[0-9]+)|([0-9]+\.)
exppart					[eE][-+]?[0-9]+
floatsuffix				[fFlL]
52 53
chartext				([^\'])|(\\.) 
stringtext				([^\"])|(\\.)
54 55 56

%%

57
"\n"					{ ++lineno; } /* " */
58 59
[\t\f\v\r ]+				{ /* Ignore whitespace. */ }

60
"/*"					{ parse_comment(igenerator); }
61 62 63 64 65
"//".*					{ }

"#define "[a-zA-Z_][a-zA-Z_0-9]*"("	{ yyless (yyleng - 1); return FUNCTION_MACRO; }
"#define "[a-zA-Z_][a-zA-Z_0-9]*	{ return OBJECT_MACRO; }

66
"#"					{ process_directive(igenerator); }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

"{"					{ return '{'; }
"<%"					{ return '{'; }
"}"					{ return '}'; }
"%>"					{ return '}'; }
"["					{ return '['; }
"<:"					{ return '['; }
"]"					{ return ']'; }
":>"					{ return ']'; }
"("					{ return '('; }
")"					{ return ')'; }
";"					{ return ';'; }
":"					{ return ':'; }
"..."					{ return ELLIPSIS; }
"?"					{ return '?'; }
"."					{ return '.'; }
"+"					{ return '+'; }
"-"					{ return '-'; }
"*"					{ return '*'; }
"/"					{ return '/'; }
"%"					{ return '%'; }
"^"					{ return '^'; }
"&"					{ return '&'; }
"|"					{ return '|'; }
"~"					{ return '~'; }
"!"					{ return '!'; }
"="					{ return '='; }
"<"					{ return '<'; }
">"					{ return '>'; }
"+="					{ return ADDEQ; }
"-="					{ return SUBEQ; }
"*="					{ return MULEQ; }
"/="					{ return DIVEQ; }
"%="					{ return MODEQ; }
"^="					{ return XOREQ; }
"&="					{ return ANDEQ; }
"|="					{ return OREQ; }
"<<"					{ return SL; }
">>"					{ return SR; }
"<<="					{ return SLEQ; }
">>="					{ return SREQ; }
"=="					{ return EQ; }
"!="					{ return NOTEQ; }
"<="					{ return LTEQ; }
">="					{ return GTEQ; }
"&&"					{ return ANDAND; }
"||"					{ return OROR; }
"++"					{ return PLUSPLUS; }
"--"					{ return MINUSMINUS; }
","					{ return ','; }
"->"					{ return ARROW; }

119
[a-zA-Z_][a-zA-Z_0-9]*			{ if (igenerator->macro_scan) return IDENTIFIER; else REJECT; }
120 121

"auto"					{ return AUTO; }
Jürg Billeter's avatar
Jürg Billeter committed
122
"_Bool"					{ return BOOL; }
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
"break"					{ return BREAK; }
"case"					{ return CASE; }
"char"					{ return CHAR; }
"const"					{ return CONST; }
"continue"				{ return CONTINUE; }
"default"				{ return DEFAULT; }
"do"					{ return DO; }
"double"				{ return DOUBLE; }
"else"					{ return ELSE; }
"enum"					{ return ENUM; }
"extern"				{ return EXTERN; }
"float"					{ return FLOAT; }
"for"					{ return FOR; }
"goto"					{ return GOTO; }
"if"					{ return IF; }
"inline"				{ return INLINE; }
"int"					{ return INT; }
"long"					{ return LONG; }
"register"				{ return REGISTER; }
"restrict"				{ return RESTRICT; }
"return"				{ return RETURN; }
"short"					{ return SHORT; }
"signed"				{ return SIGNED; }
"sizeof"				{ return SIZEOF; }
"static"				{ return STATIC; }
"struct"				{ return STRUCT; }
"switch"				{ return SWITCH; }
"typedef"				{ return TYPEDEF; }
"union"					{ return UNION; }
"unsigned"				{ return UNSIGNED; }
"void"					{ return VOID; }
"volatile"				{ return VOLATILE; }
"while"					{ return WHILE; }

157
[a-zA-Z_][a-zA-Z_0-9]*			{ return check_identifier(igenerator, yytext); }
158 159 160 161 162 163 164 165 166 167 168 169 170 171

"0"[xX][0-9a-fA-F]+{intsuffix}?		{ return INTEGER; }
"0"[0-7]+{intsuffix}?			{ return INTEGER; }
[0-9]+{intsuffix}?			{ return INTEGER; }

{fracconst}{exppart}?{floatsuffix}?	{ return FLOATING; }
[0-9]+{exppart}{floatsuffix}?		{ return FLOATING; }

"'"{chartext}*"'"			{ return CHARACTER; }
"L'"{chartext}*"'"			{ return CHARACTER; }

"\""{stringtext}*"\""			{ return STRING; }
"L\""{stringtext}*"\""			{ return STRING; }

172
.					{ fprintf(stderr, "%s:%d: unexpected character `%c'\n", igenerator->current_filename, lineno, yytext[0]); }
173 174 175 176 177

%%

static int yywrap (void)
{
178
  return 1;
179 180
}

181
static void parse_gtkdoc (GIGenerator *igenerator, int *c1, int *c2)
182
{
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  gboolean isline = FALSE;
  gchar line[256];
  int i;
  gchar **parts;
  CDirective *directive;
  char *name, *value;

  i = 0;
  do {
      *c1 = *c2;
      if (*c1 == '\n')
	{
	  isline = TRUE;
	  break;
	}
      if (i >= 256)
	break;
      line[i++] = *c1;
      *c2 = input();

  } while (*c2 != EOF && !(*c1 == '*' && *c2 == '/'));
  
  if (!isline)
    return;

  line[i] = '\0';

  parts = g_strsplit (line, ": ", 2);

  if (g_strv_length (parts) == 2)
    {
      name = parts[0];
      value = parts[1];
    }
  else /* parts == 1 */
    {
      name = parts[0];
220
      value = NULL;
221 222 223 224 225 226 227 228
    }
  
  directive = cdirective_new (name, value);
  igenerator->directives = g_slist_prepend (igenerator->directives,
					    directive);

  g_strfreev (parts);
}
229

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
static void parse_comment (GIGenerator *igenerator)
{
  int c1, c2;
  
  c1 = input();
  c2 = input();

  while (c2 != EOF && !(c1 == '*' && c2 == '/'))
    {
      if (c1 == '\n')
	++lineno;
      c1 = c2;
      c2 = input();

      if (c1 == ' ' && c2 == '@')
	{
	  c1 = c2;
	  c2 = input();
	  parse_gtkdoc (igenerator, &c1, &c2);
249
	}
250
    }
251 252
}

253
static int check_identifier (GIGenerator *igenerator, const char *s)
254 255 256 257 258 259
{
	/*
	 * This function checks if `s' is a type name or an
	 * identifier.
	 */

260
	if (g_igenerator_is_typedef (igenerator, s)) {
261 262 263 264 265 266 267 268
		return TYPEDEF_NAME;
	} else if (strcmp (s, "__builtin_va_list") == 0) {
		return TYPEDEF_NAME;
	}

	return IDENTIFIER;
}

269
static void process_directive (GIGenerator *igenerator)
270 271 272
{
	/* extract current filename from #line directives */
	GString *filename_builder;
273
	gboolean in_string, found_filename;
274

275 276
	lineno = 0;
	found_filename = FALSE;
277 278 279 280 281 282 283 284
	in_string = FALSE;
	filename_builder = g_string_new ("");

	int c = input ();
	while (c != EOF && c != '\n') {
		if (!in_string) {
			if (c == '\"') {
				in_string = TRUE;
285 286 287 288 289
				found_filename = TRUE;
			} else if (c >= '0' && c <= '9') {
				if (!found_filename) {
					lineno = lineno * 10 + (c - '0');
				}
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
			}
		} else {
			if (c == '\"') {
				in_string = FALSE;
			} else if (c == '\\') {
				g_string_append_c (filename_builder, c);
				c = input ();
				g_string_append_c (filename_builder, c);
			} else {
				g_string_append_c (filename_builder, c);
			}
		}
		c = input ();
	}

	if (filename_builder->len > 0) {
		char *filename = g_strcompress (filename_builder->str);
307
		g_free (igenerator->current_filename);
308 309
		igenerator->current_filename = g_realpath(filename);
		g_free(filename);
310 311 312 313 314
	}

	g_string_free (filename_builder, TRUE);
}