scanner.l 7.73 KB
Newer Older
1 2
/* scanner.l
 *
3
 * Copyright (C) 2006-2008  Jürg Billeter, Raffaele Sandrini
4 5 6 7
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 *	Raffaele Sandrini <rasa@gmx.ch>
 */

%{
#include "vala.h"
#include "parser.h"

#define YY_DECL int yylex (YYSTYPE *yylval_param, YYLTYPE *yylloc_param, ValaParser *parser)

30
#define uploc	{ yylloc->first_column = yylloc->last_column + 1; yylloc->last_column += yyleng; }
31 32

static gboolean file_comment = FALSE;
33 34 35 36 37 38 39 40 41 42
%}
 
%option yylineno
%option bison-bridge
%option bison-locations
%option noyywrap
%option nounput

%x IN_COMMENT

43 44 45
space				[ \t\n]*
ident				[[:alnum:]_]+
decimal_integer_literal		(0|[1-9][[:digit:]]*)
46
real_literal			[[:digit:]]+"."[[:digit:]]*{exponent_part}?{real_suffix}?
Jürg Billeter's avatar
Jürg Billeter committed
47
hex_digit			[[:digit:]A-Fa-f]
48 49 50
octal_digit			[0-7]
octal_integer_literal		0{octal_digit}+
hexadecimal_integer_literal	0x{hex_digit}+
51 52
exponent_part			(e|E){sign}?[[:digit:]]+
sign				"+"|"-"
53 54
integer_suffix			L|LL|U|UL|ULL
real_suffix			F
55 56
single_character		[^\'\\]
single_string_literal_character	[^\"\\]
57
simple_escape_sequence		\\[\'\"\?\\abfnrtv0]
58 59 60
hexadecimal_escape_sequence	\\x{hex_digit}{hex_digit}?{hex_digit}?{hex_digit}?
character			({single_character}|{simple_escape_sequence})
string_literal_character	({single_string_literal_character}|{simple_escape_sequence})
61
character_literal		\'{character}+\'
62
string_literal			\"{string_literal_character}*\"
63
integer_literal			({decimal_integer_literal}|{hexadecimal_integer_literal}|{octal_integer_literal}){integer_suffix}?
64
literal				({integer_literal}|{real_literal}|{character_literal}|{string_literal})
65

66 67 68
type_name			("weak"{space})?({ident}".")?{ident}
generic_type			{type_name}("<"{space}{type_name}("?"|"*"+)?(","{space}{type_name}("?"|"*"+)?)*">")?("?"|"*"+)?

69 70
%%

71
"/*"				{ uploc; file_comment = (yylineno == 1); BEGIN (IN_COMMENT); }
72
<IN_COMMENT>"*/"		{ uploc; BEGIN (INITIAL); yytext[yyleng - 2] = '\0'; vala_parser_push_comment (parser, yytext, file_comment); }
73 74
<IN_COMMENT>[^*\n]+		{ uploc; yymore (); }
<IN_COMMENT>"*"			{ uploc; yymore (); }
75 76
<IN_COMMENT>\n			{ yylloc->first_line = yylloc->last_line = yylineno; yylloc->first_column = 1; yylloc->last_column = 0; yymore (); }

77
"//".*				{ uploc; vala_parser_push_comment (parser, g_strdup (yytext + 2), FALSE); }
78 79 80

"{"		{ uploc; return OPEN_BRACE; }
"}"		{ uploc; return CLOSE_BRACE; }
81
"("({space}"weak")?{space}{ident}("."{ident})?("<"({ident}".")?{ident}(","({ident}".")?{ident})*">")?("["{space}"]")*{space}")"{space}("("|{ident}|{literal})	{ yyless (1); uploc; return OPEN_CAST_PARENS; }
82 83
"("		{ uploc; return OPEN_PARENS; }
")"		{ uploc; return CLOSE_PARENS; }
84
"["{space}(","{space})*"]"	{ yyless (1); uploc; return OPEN_ARRAY_TYPE_BRACKET; }
85 86
"["		{ uploc; return OPEN_BRACKET; }
"]"		{ uploc; return CLOSE_BRACKET; }
87
"..."		{ uploc; return ELLIPSIS; }
88 89 90 91 92
"."		{ uploc; return DOT; }
":"		{ uploc; return COLON; }
","		{ uploc; return COMMA; }
";"		{ uploc; return SEMICOLON; }
"#"		{ uploc; return HASH; }
93
"?"{space}({ident}{space}("="|";")|"[")	{ yyless (1); uploc; return NULLABLE_INTERR; }
94 95
"?"		{ uploc; return INTERR; }

96 97 98 99 100 101 102 103 104 105 106
"|="		{ uploc; return ASSIGN_BITWISE_OR; }
"&="		{ uploc; return ASSIGN_BITWISE_AND; }
"^="		{ uploc; return ASSIGN_BITWISE_XOR; }
"+="		{ uploc; return ASSIGN_ADD; }
"-="		{ uploc; return ASSIGN_SUB; }
"*="		{ uploc; return ASSIGN_MUL; }
"/="		{ uploc; return ASSIGN_DIV; }
"%="		{ uploc; return ASSIGN_PERCENT; }
"<<="		{ uploc; return ASSIGN_SHIFT_LEFT; }
">>="		{ uploc; return ASSIGN_SHIFT_RIGHT; }

107 108 109 110 111 112 113 114
"++"		{ uploc; return OP_INC; }
"--"		{ uploc; return OP_DEC; }
"=="		{ uploc; return OP_EQ; }
"!="		{ uploc; return OP_NE; }
"<<"		{ uploc; return OP_SHIFT_LEFT; }
">>"		{ uploc; return OP_SHIFT_RIGHT; }
"<="		{ uploc; return OP_LE; }
">="		{ uploc; return OP_GE; }
115
"=>"		{ uploc; return LAMBDA; }
116
"<"{space}{generic_type}{space}(","{space}{generic_type}{space})*">"	{ yyless (1); uploc; return GENERIC_LT; }
117 118 119 120 121 122 123 124
"<"		{ uploc; return OP_LT; }
">"		{ uploc; return OP_GT; }
"!"		{ uploc; return OP_NEG; }
"||"		{ uploc; return OP_OR; }
"|"		{ uploc; return BITWISE_OR; }
"&&"		{ uploc; return OP_AND; }
"&"		{ uploc; return BITWISE_AND; }
"^"		{ uploc; return CARRET; }
125
"~"		{ uploc; return TILDE; }
126

127 128
"->"		{ uploc; return OP_PTR; }

129 130 131 132 133 134 135
"="		{ uploc; return ASSIGN; }
"+"		{ uploc; return PLUS; }
"-"		{ uploc; return MINUS; }
"*"		{ uploc; return STAR; }
"/"		{ uploc; return DIV; }
"%"		{ uploc; return PERCENT; }

136
"@"[[:alnum:]_]+	{ uploc; yylval->str = g_strdup (yytext + 1); return IDENTIFIER; }
137 138

"abstract"	{ uploc; return ABSTRACT; }
139
"as"		{ uploc; return AS; }
140
"base"		{ uploc; return BASE; }
141
"break"		{ uploc; return BREAK; }
142
"case"		{ uploc; return CASE; }
143
"catch"		{ uploc; return CATCH; }
144 145 146
"class"		{ uploc; return CLASS; }
"const"		{ uploc; return CONST; }
"construct"	{ uploc; return CONSTRUCT; }
147
"continue"	{ uploc; return CONTINUE; }
148
"default"	{ uploc; return DEFAULT; }
149
"delegate"	{ uploc; return DELEGATE; }
150
"delete"	{ uploc; return DELETE; }
151
"do"		{ uploc; return DO; }
152
"else"		{ uploc; return ELSE; }
153
"ensures"	{ uploc; return ENSURES; }
154
"enum"		{ uploc; return ENUM; }
155
"errordomain"	{ uploc; return ERRORDOMAIN; }
156
"false"		{ uploc; return VALA_FALSE; }
157
"finally"	{ uploc; return FINALLY; }
158 159 160 161 162
"for"		{ uploc; return FOR; }
"foreach"	{ uploc; return FOREACH; }
"get"		{ uploc; return GET; }
"if"		{ uploc; return IF; }
"in"		{ uploc; return IN; }
163
"inline"	{ uploc; return INLINE; }
164
"interface"	{ uploc; return INTERFACE; }
165
"is"		{ uploc; return IS; }
166
"lock"		{ uploc; return LOCK; }
167 168 169 170 171 172
"namespace"	{ uploc; return NAMESPACE; }
"new"		{ uploc; return NEW; }
"null"		{ uploc; return VALA_NULL; }
"out"		{ uploc; return OUT; }
"override"	{ uploc; return OVERRIDE; }
"private"	{ uploc; return PRIVATE; }
173 174
"protected"	{ uploc; return PROTECTED; }
"public"	{ uploc; return PUBLIC; }
175
"ref"		{ uploc; return REF; }
176
"requires"	{ uploc; return REQUIRES; }
177
"set"		{ uploc; return SET; }
178
"signal"	{ uploc; return SIGNAL; }
179
"sizeof"	{ uploc; return SIZEOF; }
180 181
"static"	{ uploc; return STATIC; }
"struct"	{ uploc; return STRUCT; }
182
"switch"	{ uploc; return SWITCH; }
183
"return"	{ uploc; return RETURN; }
184
"this"		{ uploc; return THIS; }
185 186
"throw"		{ uploc; return THROW; }
"throws"	{ uploc; return THROWS; }
187
"true"		{ uploc; return VALA_TRUE; }
188
"try"		{ uploc; return TRY; }
189
"typeof"	{ uploc; return TYPEOF; }
190 191 192
"using"		{ uploc; return USING; }
"var"		{ uploc; return VAR; }
"virtual"	{ uploc; return VIRTUAL; }
Jürg Billeter's avatar
Jürg Billeter committed
193
"void"		{ uploc; return VOID; }
194
"volatile"	{ uploc; return VOLATILE; }
195
"weak"		{ uploc; return WEAK; }
196
"while"		{ uploc; return WHILE; }
197

198 199
{real_literal}		{ uploc; yylval->str = g_strdup (yytext); return REAL_LITERAL; }
{integer_literal}	{ uploc; yylval->str = g_strdup (yytext); return INTEGER_LITERAL; }
200

201 202
{character_literal}	{ uploc; yylval->str = g_strdup (yytext); return CHARACTER_LITERAL; }
{string_literal}	{ uploc; yylval->str = g_strdup (yytext); return STRING_LITERAL; }
203

204
{ident}			{ uploc; yylval->str = g_strdup (yytext); return IDENTIFIER; }
205

206
[ \t\r]+	{ uploc; /* eat up whitespace */ }
207 208 209
[\n]+		{ yylloc->first_line = yylloc->last_line = yylineno; yylloc->first_column = 1; yylloc->last_column = 0; }

.	{ uploc; fprintf (stderr, "%d: syntax error: unexpected character ´%s´\n", yylloc->first_line, yytext); }