Simplified O_strcat().
[mpsl.git] / mpsl.l
blobcbf75c87a0514097c8211466f8ca20122733ef94
1 %{
2 /*
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
27 #include <stdio.h>
28 #include <wchar.h>
29 #include "mpdm.h"
31 #include <stdlib.h>
32 #include "y.tab.h"
34 void yyerror(char * s);
35 int yy_input_for_flex(char * buf, int max);
37 /* redefinition of input function for GNU Flex */
38 #undef YY_INPUT
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;
47 /* line number */
48 int mpsl_line = 0;
50 /* cached value MPSL.LC */
51 mpdm_t mpsl_lc = NULL;
53 /* dynamic string manipulation macros */
54 #ifndef ds_init
55 struct ds { wchar_t * d; int p; int s; };
56 #define ds_init(x) do { x.d = (wchar_t *)0; x.p = x.s = 0; } while(0)
57 #define ds_rewind(x) x.p = 0;
58 #define ds_free(x) do { if(x.d) free(x.d); ds_init(x); } while(0)
59 #define ds_redim(x) do { if(x.p >= x.s) x.d = realloc(x.d, ++x.s * sizeof(wchar_t)); } while(0)
60 #define ds_poke(x,c) do { ds_redim(x); x.d[x.p++] = c; } while(0)
61 #define ds_pokes(x,t) do { wchar_t *p = t; while(*p) ds_poke(x, *p++); } while(0)
62 #endif /* ds_init */
64 /* a dynamic string */
65 struct ds ds_v;
67 static wchar_t *s_mbstowcs(char *str)
68 /* converts from mbs to wcs, using a static buffer */
70         static wchar_t * wc = NULL;
71         static int wc_s = 0;
72         int n;
74         /* allocs wchar_t space, if needed */
75         if ((n = mbstowcs(NULL, str, 0)) > wc_s) {
76                 wc_s = n;
77                 wc = realloc(wc, (wc_s + 1) * sizeof(wchar_t));
78         }
80         /* really converts */
81         mbstowcs(wc, str, n);
82         wc[n] = L'\0';
84         return wc;
88 static mpdm_t literal_cache(char *ptr, wchar_t *wptr)
89 /* the cache of literal values */
91         mpdm_t v;
93         /* convert to wchar_t */
94         if (ptr != NULL)
95                 wptr = s_mbstowcs(ptr);
97         /* search in cache */
98         if ((v = mpdm_hget_s(mpsl_lc, wptr)) == NULL) {
99                 v = MPDM_S(wptr);
100                 mpdm_hset(mpsl_lc, v, v);
101         }
103         return v;
109 DIGIT           [0-9]
110 LETTER          [a-zA-Z_]
111 WSPACE          [ \t\r]+
112 OCTDIGIT        [0-7]
113 HEXDIGIT        [0-9a-fA-F]
114 BINDIGIT        [0-1]
115 HEXQUAD         {HEXDIGIT}{1,4}
117 DECINTEGER      {DIGIT}+
118 OCTINTEGER      0{OCTDIGIT}+
119 HEXINTEGER      0[xX]{HEXDIGIT}+
120 BININTEGER      0[bB]{BINDIGIT}+
122 REAL            {DIGIT}*[\.]?{DIGIT}+
123 SCIENT          {DIGIT}+(\.{DIGIT}+)?[eE][-+]?{DIGIT}+
124 SYMBOL          {LETTER}({LETTER}|{DIGIT})*
125 INTEGER         ({DECINTEGER}|{OCTINTEGER}|{HEXINTEGER}|{BININTEGER})
127 %x REM
128 %x STR
132 {INTEGER}       {
133                         /* integers */
134                         yylval.v = literal_cache(yytext, NULL);
135                         return INTEGER;
136                 }
138 ({REAL}|{SCIENT})       {
139                         /* real numbers */
140                         yylval.v = literal_cache(yytext, NULL);
141                         return REAL;
142                 }
144 \'[^']*\'       {
145                         /* single quoted string; return as is */
146                         yytext[yyleng - 1] = '\0';
147                         yylval.v = literal_cache(yytext + 1, NULL);
148                         return STRING;
149                 }
151 "NULL"          return NULLV;
152 "while"         return WHILE;
153 "if"            return IF;
154 "else"          return ELSE;
155 "sub"           return SUB;
156 "foreach"       return FOREACH;
157 "local"         return LOCAL;
158 "break"         return BREAK;
159 "return"        return RETURN;
161 "=="            return NUMEQ;
162 "!="            return NUMNE;
163 ">="            return NUMGE;
164 "<="            return NUMLE;
165 "&&"            return BOOLAND;
166 "||"            return BOOLOR;
167 "=>"            return HASHPAIR;
168 ".."            return RANGE;
169 "&"             return BITAND;
170 "|"             return BITOR;
171 "^"             return BITXOR;
172 "<<"    return SHL;
173 ">>"    return SHR;
174 "->"            return INVCALL;
176 "eq"            return STREQ;
177 "ne"            return STRNE;
178 "~"             return STRCAT;
180 "++"            return INC;
181 "--"            return DEC;
182 "+="            return IADD;
183 "-="            return ISUB;
184 "*="            return IMUL;
185 "/="            return IDIV;
186 "%"             return MOD;
187 "%="            return IMOD;
188 "&="            return IBITAND;
189 "|="            return IBITOR;
190 "^="            return IBITXOR;
191 "<<="           return ISHL;
192 ">>="           return ISHR;
193 "**"            return POW;
195 {SYMBOL}        {
196                         /* symbol name */
197                         yylval.v = literal_cache(yytext, NULL);
198                         return SYMBOL;
199                 }
201 {WSPACE}        ;       /* ignore spaces */
203 \n              { mpsl_line++; }
205 \/\*            { BEGIN REM; /* C-like comments */ }
206 <REM>\*\/       { BEGIN 0; }
207 <REM>\n         { mpsl_line++; }
208 <REM>.          ;
210 \"              { BEGIN STR; ds_rewind(ds_v); }
211 <STR>\n         { ds_poke(ds_v, L'\n'); mpsl_line++; }
212 <STR>\\n        { ds_poke(ds_v, L'\n'); }
213 <STR>\\t        { ds_poke(ds_v, L'\t'); }
214 <STR>\\r        { ds_poke(ds_v, L'\r'); }
215 <STR>\\e        { ds_poke(ds_v, 27); }
216 <STR>\\\"       { ds_poke(ds_v, L'\"'); }
217 <STR>\\\\       { ds_poke(ds_v, L'\\'); }
218 <STR>\"\\\n[ \t]+\"     ;
219 <STR>\"         {
220                         ds_poke(ds_v, L'\0');
221                         yylval.v = literal_cache(NULL, ds_v.d);
222                         BEGIN 0;
223                         return STRING;
224                 }
225 <STR>\\x\{{HEXQUAD}\} {
226                         int c;
228                         sscanf(yytext, "\\x{%x}", &c);
229                         ds_poke(ds_v, (wchar_t) c);
230                 }
231 <STR>.          { wchar_t wc; if (mbtowc(&wc, yytext, 1) > 0) ds_poke(ds_v, wc); }
233 .               { return *yytext; }
237 int yywrap(void) { return 1; }
239 int yy_input_for_flex(char *buf, int max)
241         int n = 0;
243         if (mpsl_file != NULL) {
244                 while (n < max) {
245                         int c;
247                         if ((c = fgetc(mpsl_file)) == EOF) {
248                                 mpsl_file = NULL;
249                                 break;
250                         }
252                         buf[n++] = c;
253                 }
254         }
255         else
256         if (mpsl_next_char != NULL) {
257                 for (;;) {
258                         char tmp[64];   /* really MB_CUR_MAX + 1 */
259                         int c, i;
261                         if (*mpsl_next_char == L'\0' ||
262                            (c = wctomb(tmp, *mpsl_next_char)) < 0) {
263                                 mpsl_next_char = NULL;
264                                 break;
265                         }
267                         /* no room? try next time */
268                         if (n + c >= max)
269                                 break;
271                         mpsl_next_char++;
273                         /* transfer */
274                         for (i = 0; i < c; i++)
275                                 buf[n++] = tmp[i];
276                 }
277         }
279         return n;