aboutsummaryrefslogtreecommitdiff
path: root/src/build.l
blob: 7a77bfc0a6966f7e140fa0ed340de945dca423ba (plain)
1
2
3
4
5
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
119
120
121
122
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
%{
  #include "buildparser.h"
  #include "bu/string.h"

char *fstrdup( const Bu::String &s )
{
	char *sRet = new char[s.getSize()+1];
	memcpy( sRet, s.getStr(), s.getSize()+1 );
	return sRet;
}

char *rstrdup( const char *sIn )
{
	char *sRet = new char[strlen(sIn)+1];
	strcpy( sRet, sIn );
	return sRet;
}
void build_error( YYLTYPE *locp, yyscan_t yyscanner, BuildParser &bld, const char *msg );

Bu::String sBuf;
int iStrDepth = 0;
%}

%{
  #define YY_USER_ACTION	yylloc->last_column += yyleng;
%}

%x strdq
%x BlockComment
%option noyywrap nounput batch debug bison-bridge bison-locations reentrant
%option prefix="build_"
%option outfile="src/build.yy.c"
%option header-file="src/build.yy.h"
%%
\n+									{
	yylloc->last_line += yyleng;
	yylloc->first_line = yylloc->last_line;
	yylloc->first_column = yylloc->last_column = 0;
}
[ \t\r]+							{
	yylloc->first_line = yylloc->last_line;
	yylloc->first_column = yylloc->last_column+1;
}
"#".*								/* eol comment */
"//".*								/* eol comment */

"/*"											{
	BEGIN( BlockComment );
}

<BlockComment>"*/"								{
	BEGIN( INITIAL );
}

<BlockComment>\n+								{
	yylloc->last_column = -yyleng;
	yylloc->last_line += yyleng;
}
<BlockComment>.									{	}
	/*
<BlockComment>[^*\n/]+							{	}
<BlockComment>"*"[^/\n]+						{	}
<BlockComment>"*"|"/"							{	}
	*/

[(){}[\],.;=<>!+*/-]				return yytext[0];
"+="								return OP_ADDSETP;
"<<"								return OP_ADDSETR;
"=="								return OP_CMPEQUAL;
"!="								return OP_INEQUAL;
"<="								return OP_LTEQUAL;
">="								return OP_GTEQUAL;
"target"							return TOK_TARGET;
"input"								return TOK_INPUT;
"output"							return TOK_OUTPUT;
"unset"								return TOK_UNSET;
"set"								return TOK_SET;
"condition"							return TOK_CONDITION;
"requires"							return TOK_REQUIRES;
"auto"								return TOK_AUTO;
"config"							return TOK_CONFIG;
"display"							return TOK_DISPLAY;
"type"								return TOK_TYPE;
"int"								return TOK_INT;
"float"								return TOK_FLOAT;
"bool"								return TOK_BOOL;
"version"							return TOK_VERSION;
"string"							return TOK_STRING;
"default"							return TOK_DEFAULT;
"allow"								return TOK_ALLOW;
"rule"								return TOK_RULE;
"action"							return TOK_ACTION;
"profile"							return TOK_PROFILE;
"if"								return TOK_IF;
"then"								return TOK_THEN;
"else"								return TOK_ELSE;
"include"							return TOK_INCLUDE;
"warning"							return TOK_WARNING;
"error"								return TOK_ERROR;
"notice"							return TOK_NOTICE;
"cache"								return TOK_CACHE;
"always"							return TOK_ALWAYS;
"never"								return TOK_NEVER;
"global"							return TOK_GLOBAL;
"local"								return TOK_LOCAL;
"for"								return TOK_FOR;
"in"								return TOK_IN;
"do"								return TOK_DO;
"return"							return TOK_RETURN;
"function"							return TOK_FUNCTION;
"continue"							return TOK_CONTINUE;
"break"								return TOK_BREAK;
"value"								return TOK_VALUE;
"all"								return TOK_ALL;
"export"							return TOK_EXPORT;
"tag"								return TOK_TAG;
"null"								return TOK_NULL;

"true"								{
	yylval->bVal = true;
	return LTR_BOOL;
}
"false"								{
	yylval->bVal = false;
	return LTR_BOOL;
}

[a-zA-Z_][a-zA-Z0-9_]*:				{
	yytext[yyleng-1] = '\0';
	yylval->sVal = rstrdup( yytext );
	return LTR_PROFILE;
}

[a-zA-Z_][a-zA-Z0-9_]*				{
	yylval->sVal = rstrdup( yytext );
	if( b.isKeyword( yylval->sVal ) )
		return LTR_KEYWORD;
	else if( b.isCond( yylval->sVal ) )
		return LTR_CONDITION;
	return LTR_UNDEF;
}

([1-9][0-9]*)|(0)					{
	yylval->iVal = strtol( yytext, NULL, 10 );
	return LTR_INT;
}

(0\.0+)|((([1-9][0-9]*)|(0))\.[0-9]*)		{
	yylval->fVal = strtof( yytext, NULL );
	return LTR_FLOAT;
}

\"									{
	BEGIN( strdq );
	sBuf.clear();
	iStrDepth = 0;
}
<strdq>[^\\\n\"$()]+				{
	sBuf += yytext;
}
<strdq>\$\$							{
	sBuf += "$$";
}
<strdq>\$\(							{
	iStrDepth++; // TODO:  Should this really count depth?  I dunno...
	sBuf += "$(";
}
<strdq>\\\)							sBuf += "\\)";
<strdq>\)							{
	if( iStrDepth > 0 )
		iStrDepth--;
	sBuf += ")";
}
<strdq>[$(]							{
	sBuf += yytext;
}
<strdq>\n							{
	build_error( yylloc, yyscanner, b, "newline encountered in string");
}
<strdq>\\n							sBuf += "\n";
<strdq>\\t							sBuf += "\t";
<strdq>\\r							sBuf += "\r";
<strdq>\\b							sBuf += "\b";
<strdq>\\f							sBuf += "\f";
<strdq>\\\\							sBuf += "\\";
<strdq>\\\"							sBuf += "\"";
<strdq>\\\'							sBuf += "\'";
<strdq>\\\(							sBuf += "(";
<strdq>\\\`							sBuf += "`";
<strdq>\\.							printf("Invalid escape sequence.\n");
<strdq>\"[ \t\r\n]*\"				{/* Ignore spaces between strings. */}
<strdq>\"							{
	if( iStrDepth > 0 )
	{
		sBuf += "\"";
	}
	else
	{
		BEGIN( INITIAL );
		yylval->sVal = fstrdup( sBuf );
		return LTR_STRING;
	}
}

<<EOF>>								{
	build_pop_buffer_state( yyscanner );
	if( !YY_CURRENT_BUFFER )
		yyterminate();
	else
		b.endInclude( yylloc );
}

.									{
	build_error( yylloc, yyscanner, b, "invalid character");
}
%%