Merge branch 'master' of github.com:periscop/clay
[clay.git] / source / scanner.l
blobc9f1742ca90dd5f932ab6dffb1e4eaeb96a9183f
2    /*--------------------------------------------------------------------+
3     |                              Clay                                  |
4     |--------------------------------------------------------------------|
5     |                            scanner.l                               |
6     |--------------------------------------------------------------------|
7     |                    First version: 03/04/2012                       |
8     +--------------------------------------------------------------------+
10  +--------------------------------------------------------------------------+
11  |  / __)(  )    /__\ ( \/ )                                                |
12  | ( (__  )(__  /(__)\ \  /         Chunky Loop Alteration wizardrY         |
13  |  \___)(____)(__)(__)(__)                                                 |
14  +--------------------------------------------------------------------------+
15  | Copyright (C) 2012 University of Paris-Sud                               |
16  |                                                                          |
17  | This library is free software; you can redistribute it and/or modify it  |
18  | under the terms of the GNU Lesser General Public License as published by |
19  | the Free Software Foundation; either version 2.1 of the License, or      |
20  | (at your option) any later version.                                      |
21  |                                                                          |
22  | This library is distributed in the hope that it will be useful but       |
23  | WITHOUT ANY WARRANTY; without even the implied warranty of               |
24  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser  |
25  | General Public License for more details.                                 |
26  |                                                                          |
27  | You should have received a copy of the GNU Lesser General Public License |
28  | along with this software; if not, write to the Free Software Foundation, |
29  | Inc., 51 Franklin Street, Fifth Floor,                                   |
30  | Boston, MA  02110-1301  USA                                              |
31  |                                                                          |
32  | Clay, the Chunky Loop Alteration wizardrY                                |
33  | Written by Joel Poudroux, joel.poudroux@u-psud.fr                        |
34  +--------------------------------------------------------------------------*/
38   #include <ctype.h>
39   #include <parser.h>
40   #include <clay/array.h>
41   #include <clay/list.h>
42   #include <clay/macros.h>
44   void clay_scanner_free();
45   void clay_scanner_initialize();
46   
47   extern int is_in_a_list;
49   char* trim(char*);
52 //extern YYLTYPE clay_yylloc;
53 //extern YYSTYPE clay_yylval;;
58   int yycolumn = 1;
59   #define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno; \
60       yylloc.first_column = yycolumn; yylloc.last_column = yycolumn + yyleng - 1; \
61       yycolumn += yyleng; \
62       yylval.str = strdup(yytext);
63   */
64 //#define YY_USER_ACTION clay_yylloc.first_line = clay_yylloc.last_line = clay_yylineno;
66 //%option outfile="scanner.c" header-file="scanner.h"
69 %option yylineno
70 %option prefix="clay_yy"
71 %option noyywrap
72 %option noinput
73 %option nounput
74 SEPARATOR ([ \t]|\xc2\xa0)*
78 {SEPARATOR}#(.|{SEPARATOR})*\n { return COMMENT; }
80 -?[0-9]+           {
81                      clay_yylval.ival = atoi(clay_yytext);
82                      return INTEGER;
83                    }
85 true               {
86                      clay_yylval.ival = 1;
87                      return INTEGER;
88                    }
90 false              {
91                      clay_yylval.ival = 0;
92                      return INTEGER;
93                    }
95 [a-zA-Z_][a-zA-Z0-9_]*{SEPARATOR}\({SEPARATOR}\) {
96                      // remove the ')'
97                      clay_yytext[strlen(clay_yytext)-1] = '\0';
98                      char *tmp = trim(clay_yytext);
99                      // remove the '('
100                      tmp[strlen(tmp)-1] = '\0';
101                      clay_yylval.sval = strdup(trim(tmp));
102                      return IDENT_FUNCTION_NO_ARGS;
103                   }
105 [a-zA-Z_][a-zA-Z0-9_]*{SEPARATOR}\( {
106                      // remove the '('
107                      clay_yytext[strlen(clay_yytext)-1] = '\0';
108                      clay_yylval.sval = strdup(trim(clay_yytext));
109                      return IDENT_FUNCTION;
110                    }
112 [a-zA-Z_][a-zA-Z0-9_]* {
113                      clay_yylval.sval = strdup(clay_yytext);
114                      return IDENT;
115                    }
117 {SEPARATOR}        { ; }
119 \n                 { }
120 .                  { return clay_yytext[0]; }
126  * clay_scanner_free function:
127  * this function frees the memory allocated for the scanner. It frees
128  * flex's buffer (it supposes there is only one buffer) since flex does
129  * never free it itself.
130  * WARNING: this is probably *not* portable...
131  */
132 void clay_scanner_free() {
133   clay_yy_delete_buffer(YY_CURRENT_BUFFER);
134   free(yy_buffer_stack);
138  * clay_scanner_initialize function:
139  * this function initialises the scanner global variables with default values.
140  */
141 void clay_scanner_initialize() {
142   clay_yy_flush_buffer(YY_CURRENT_BUFFER);
145 char *trim(char *str) {
146   // taken here :
147   // http://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way
148   char *end;
150   // Trim leading space
151   while(isspace(*str)) str++;
153   if(*str == 0)  // All spaces?
154     return str;
156   // Trim trailing space
157   end = str + strlen(str) - 1;
158   while(end > str && isspace(*end)) end--;
160   // Write new null terminator
161   *(end+1) = 0;
163   return str;