Eliminated some problems resulting from last merge
[openstranded.git] / src / s2script / s2s.l
bloba7f0c5f73618d796c8d3dd7d0add7abbcb8b3b78
1 /*
2  * This is the lexical analyzer of openstranded's script parser
3  *
4  * Copyright (C) 2008-2009 Hermann Walth
5  *
6  * This file is part of OpenStranded
7  *
8  * OpenStranded is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * OpenStranded is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
20  */
23   #include "s2script.h"
24   #include "s2s.tab.h"
25   #define STRBUF_INITSIZE 16
28 IDENTIFIER    [a-zA-Z_][a-zA-Z0-9_]*
29 INTEGERNUM    [0-9]+
30 WHITESPACE    [\|\n \t]
31 ANYCHAR       [^\|\n \t]
33 %x QUOTE
34 %x BLOCKCOMMENT
35 %x LINECOMMENT
37         int strcount, strsize;
38         char* strbuf;
40 \${IDENTIFIER}          {
41                         yylval.val.string = (char*) malloc (strlen (yytext));
42                         strcpy (yylval.val.string, yytext + 1);
43                         return VAR;
44                         }
46 <INITIAL>\"             {
47                         BEGIN(QUOTE);
48                         strcount = 0;
49                         strsize = STRBUF_INITSIZE;
50                         strbuf = (char*) malloc (strsize);
51                         }
53 <QUOTE>\"               {
54                         BEGIN(INITIAL);
55                         yylval.type = S2S_STRING_T;
56                         yylval.val.string = realloc (strbuf, strcount + 1);
57                         yylval.val.string[strcount] = '\0';
58                         return STRING;
59                         }
61 <QUOTE>(.|\n)           {
62                         strbuf[strcount++] = yytext[0];
63                         if (strcount >= strsize)
64                           strbuf = realloc (strbuf, strsize += STRBUF_INITSIZE);
65                         }
67 <INITIAL>(\/\/)         {
68                         BEGIN(LINECOMMENT);
69                         }
71 <INITIAL>\/\*           {
72                         BEGIN(BLOCKCOMMENT);
73                         }
75 <LINECOMMENT>(\n|\|)    {
76                         BEGIN(INITIAL);
77                         }
79 <BLOCKCOMMENT>\*\/      {
80                         BEGIN(INITIAL);
81                         }
83 <LINECOMMENT,BLOCKCOMMENT>(.|\n) {}
85 (\&\&)|(and)            return AND;
86 (\|\|)|(or)             return OR;
87 (xor)                   return XOR;
89 (if)                    return IF;
90 (loop)          return LOOP;    
92 {IDENTIFIER}            {
93                         yylval.val.string = (char*) malloc (strlen (yytext) + 1);
94                         strcpy (yylval.val.string, yytext);
95                         return FUNCNAME;
96                         }
98 {INTEGERNUM}?\.{INTEGERNUM} {
99                         yylval.type = S2S_FLOAT_T;
100                         yylval.val.fnum = atof (yytext);
101                         return FLOAT;
102                         }
104 {INTEGERNUM}            {
105                         yylval.type = S2S_INT_T;
106                         yylval.val.num = atol (yytext);
107                         return NUM;
108                         }
110 (==)                    return EQ;
112 (\!=|\<\>)                      return NEQ;
114 \<                      return LT;
116 \>                      return GT;
118 (<=)|(=<)               return LE;
120 (>=)|(=>)               return GE;
123 {ANYCHAR}               {
124                         return yytext[0];
125                         }
127 {WHITESPACE}            {}