4 $Id: spec.l 1381 2018-07-04 04:41:34Z coelho $
6 Copyright 1989-2016 MINES ParisTech
8 This file is part of NewGen.
10 NewGen is free software: you can redistribute it and/or modify it under the
11 terms of the GNU General Public License as published by the Free Software
12 Foundation, either version 3 of the License, or any later version.
14 NewGen is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 License for more details.
19 You should have received a copy of the GNU General Public License along with
20 NewGen. If not, see <http://www.gnu.org/licenses/>.
24 When a new keyword is added, update the KEYWORDS array in build.c
36 #define yylval genspec_lval
39 extern int Current_start;
43 /* We may parse strings or files...
45 static char * string_to_parse = (char*) 0; /* shared pointer! */
47 #define YY_INPUT(buffer, result, max_size) \
50 if (string_to_parse) /* we're parsing a string */ \
52 while(i < (int) max_size && *string_to_parse) \
53 buffer[i++]=*string_to_parse++; \
55 else /* it's a file */ \
58 while (i < (int) max_size && (c=getc(yyin))!=EOF) \
59 buffer[i++]=(char) c; \
61 result = i==0? YY_NULL: i; \
64 void genspec_set_string_to_parse(char *s) { string_to_parse = s;}
65 void genspec_reset_string_to_parse() { string_to_parse = (char *) 0;}
67 #else /* ATT/POSIX just seem to like parsing yyin... */
68 // I guess this code won't be use again anyway... RK
69 void genspec_set_string_to_parse(char *s)
71 if ((genspec_in = tmpfile()) == NULL) {
72 user( "Cannot create temp spec file in read/write mode\n" ) ;
75 fprintf( genspec_in , "%s", s ) ;
78 void genspec_reset_string_to_parse()
80 // The file created by tmpfile() is automatically deleted on close:
83 #endif // FLEX_SCANNER
85 /* cache parsed string instead of strduplicating them
86 * valid because those strign are never released
89 #ifndef DISABLE_NEWGEN_PARSER_CACHE
92 char* strcache(char *s)
94 static void* rootp = NULL;
96 if( !(res = tfind(s,&rootp,(int(*)(const void*,const void*))strcmp)) )
98 char * dup = strdup(s);
99 res = tsearch(dup,&rootp,(int(*)(const void*,const void*))strcmp);
107 #define strcache strdup
115 external {return GRAM_EXTERNAL ;}
116 import {return GRAM_IMPORT ;}
117 tabulated {return TABULATED ;}
119 persistant {return PERSISTANT ;}
120 persistent {return PERSISTANT ;}
123 \; {return SEMI_COLUMN ;}
126 "->" {return ARROW ;}
132 \"[_a-zA-Z0-9/\.~]+\" { ++yytext; yytext[+strlen(yytext)-1] = '\0';
133 yylval.name = strcache(yytext);
136 ^--NEWGEN-START\ [0-9]*\n {
138 atoi( yytext+strlen( "--NEWGEN-START " )) ;
140 ^--NEWGEN-INDEX\ [0-9]*\n ;
142 [_a-zA-Z][_a-zA-Z0-9]* { yylval.name = strcache(yytext); return IDENT ;}
143 [0-9]+ { yylval.val = atoi(yytext); return GRAM_INT ;}