4 MPSL - Minimum Profit Scripting Language
5 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
7 mpsl.l - Minimum Profit Scripting Language [F]lexer
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
34 void yyerror(char *s);
35 int yy_input_for_flex(char *buf, int max);
37 /* redefinition of input function for GNU Flex */
39 #define YY_INPUT(b,r,m) (r = yy_input_for_flex(b,m))
41 /* internal pointer to next character in code */
42 wchar_t *mpsl_next_char = NULL;
44 /* file stream for compiling from file */
45 FILE *mpsl_file = NULL;
50 /* cached value MPSL.LC */
51 mpdm_t mpsl_lc = NULL;
53 /* dynamic string manipulation macros */
60 #define ds_init(x) do { x.d = (wchar_t *)0; x.p = x.s = 0; } while(0)
61 #define ds_rewind(x) x.p = 0;
62 #define ds_free(x) do { if(x.d) free(x.d); ds_init(x); } while(0)
63 #define ds_redim(x) do { if(x.p >= x.s) x.d = realloc(x.d, ++x.s * sizeof(wchar_t)); } while(0)
64 #define ds_poke(x,c) do { ds_redim(x); x.d[x.p++] = c; } while(0)
65 #define ds_pokes(x,t) do { wchar_t *p = t; while(*p) ds_poke(x, *p++); } while(0)
68 /* a dynamic string */
71 static wchar_t *s_mbstowcs(char *str)
72 /* converts from mbs to wcs, using a static buffer */
74 static wchar_t *wc = NULL;
78 /* allocs wchar_t space, if needed */
79 if ((n = mbstowcs(NULL, str, 0)) > wc_s) {
81 wc = realloc(wc, (wc_s + 1) * sizeof(wchar_t));
92 static mpdm_t literal_cache(char *ptr, wchar_t * wptr)
93 /* the cache of literal values */
97 /* convert to wchar_t */
99 wptr = s_mbstowcs(ptr);
101 /* search in cache */
102 if ((v = mpdm_hget_s(mpsl_lc, wptr)) == NULL) {
104 mpdm_hset(mpsl_lc, v, v);
119 HEXQUAD {HEXDIGIT}{1,4}
122 OCTINTEGER 0{OCTDIGIT}+
123 HEXINTEGER 0[xX]{HEXDIGIT}+
124 BININTEGER 0[bB]{BINDIGIT}+
126 REAL {DIGIT}*[\.]?{DIGIT}+
127 SCIENT {DIGIT}+(\.{DIGIT}+)?[eE][-+]?{DIGIT}+
128 SYMBOL {LETTER}({LETTER}|{DIGIT})*
129 INTEGER ({DECINTEGER}|{OCTINTEGER}|{HEXINTEGER}|{BININTEGER})
138 yylval.v = literal_cache(yytext, NULL);
144 yylval.v = literal_cache(yytext, NULL);
149 /* single quoted string; return as is */
150 yytext[yyleng - 1] = '\0';
151 yylval.v = literal_cache(yytext + 1, NULL);
156 "while" return WHILE;
161 "foreach" return FOREACH;
162 "local" return LOCAL;
163 "break" return BREAK;
164 "return" return RETURN;
174 "&" return AMPERSAND;
202 yylval.v = literal_cache(yytext, NULL);
206 {WSPACE} ; /* ignore spaces */
210 \/\* { BEGIN REM; /* C-like comments */ }
211 <REM>\*\/ { BEGIN 0; }
212 <REM>\n { mpsl_line++; }
215 \" { BEGIN STR; ds_rewind(ds_v); }
216 <STR>\n { ds_poke(ds_v, L'\n'); mpsl_line++; }
217 <STR>\\n { ds_poke(ds_v, L'\n'); }
218 <STR>\\t { ds_poke(ds_v, L'\t'); }
219 <STR>\\r { ds_poke(ds_v, L'\r'); }
220 <STR>\\e { ds_poke(ds_v, 27); }
221 <STR>\\\" { ds_poke(ds_v, L'\"'); }
222 <STR>\\\\ { ds_poke(ds_v, L'\\'); }
223 <STR>\"\\\n[ \t]+\" ;
225 ds_poke(ds_v, L'\0');
226 yylval.v = literal_cache(NULL, ds_v.d);
230 <STR>\\x\{{HEXQUAD}\} {
233 sscanf(yytext, "\\x{%x}", &c);
234 ds_poke(ds_v, (wchar_t) c);
236 <STR>. { wchar_t wc; if (mbtowc(&wc, yytext, 1) > 0) ds_poke(ds_v, wc); }
238 . { return *yytext; }
247 int yy_input_for_flex(char *buf, int max)
251 if (mpsl_file != NULL) {
255 if ((c = fgetc(mpsl_file)) == EOF) {
263 else if (mpsl_next_char != NULL) {
265 char tmp[64]; /* really MB_CUR_MAX + 1 */
268 if (*mpsl_next_char == L'\0' ||
269 (c = wctomb(tmp, *mpsl_next_char)) < 0) {
270 mpsl_next_char = NULL;
274 /* no room? try next time */
281 for (i = 0; i < c; i++)