repair brokeness provided by a foolish Makefile
[manmower/obtheme.git] / obtheme.l
1 %{
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <glib.h>
7 #include "frame.h"
8 #include "misc.h"
9 #include "render.h"
10 #include "obtheme.h"
11 #include "obtheme.tab.h"
12
13 int yylex(YYSTYPE *yylval, struct parser_control *pc);
14
15 void obthemeerror(struct parser_control *pc, char *s)
16 {
17         printf("Parse error in file %s on line %d\n%s"
18                 , pc->currfile[pc->include_stack_ptr]
19                 , pc->currline[pc->include_stack_ptr] + 1
20                 , pc->error_buf);
21 }
22
23 extern int parserparse(void *);
24
25 int yywrap(void)
26 {
27         return 1;
28 }
29
30 int obtheme_parse(struct obthemedata *td, const char *filename)
31 {
32         FILE *input;
33         int ret;
34         struct parser_control *pc;
35 //illdebug = 1;
36         pc = parser_init(td);
37         input = fopen(filename, "r");
38         if (!input)
39                 return 2;
40         yyin = input;
41         yyrestart(input);
42         BEGIN 0;
43         pc->include_stack_ptr = 0;
44         pc->currline[pc->include_stack_ptr] = 0;
45         strncpy(pc->currfile[pc->include_stack_ptr], filename, 500);
46         ret = obthemeparse(pc);
47         if (ret != 0) {
48         //XXX I THINK I NEED TO CLOSE ALL INCLUDE FILES HERE
49         //probably also fclose input and call parser_finish?
50                 return ret;
51         }
52         fclose(input);
53         parser_finish(pc);
54         return ret;
55 }
56
57 %}
58 %x comment
59 %x incl
60 %%
61
62 include                 BEGIN(incl);
63 <incl>[ \t]*            /* eat the whitespace */
64 <incl>[^ \t\n]+         { /* got the include file name */
65         char *incfile;
66         if ( pc->include_stack_ptr >= MAX_INCLUDE_DEPTH) {
67                 fprintf( stderr, "Includes nested too deeply" );
68                 exit(1);
69         }
70
71         pc->include_stack[pc->include_stack_ptr++] = YY_CURRENT_BUFFER;
72         incfile = malloc(strlen(yytext) + strlen("include/")+ 1);
73         strcpy(incfile, "include/");
74         strcat(incfile, yytext);
75         yyin = fopen( incfile, "r" );
76         free(incfile);
77         strncpy(pc->currfile[pc->include_stack_ptr], yytext, 500);
78         pc->currline[pc->include_stack_ptr] = 0;
79         if ( ! yyin ) {
80                 printf("Could not find include file %s (%s)\n", yytext, incfile);
81                 exit(1);
82         }
83         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
84
85         BEGIN(INITIAL);
86         }
87
88 <<EOF>> {
89         if ( --(pc->include_stack_ptr) < 0 )
90             {
91                         yy_delete_buffer(YY_CURRENT_BUFFER);
92             yyterminate();
93             }
94
95         else
96             {
97             yy_delete_buffer( YY_CURRENT_BUFFER );
98                         fclose(yyin);
99                         yy_switch_to_buffer(
100                  pc->include_stack[pc->include_stack_ptr] );
101             }
102         }
103
104
105 "//"+[^\n]*             ;
106 "/*"                    BEGIN(comment);
107 <comment>[^*\n]*        ;
108 <comment>"*"+[^*/\n]*   ;
109 <comment>"*"+"/"        BEGIN(INITIAL);
110 <comment>\n             LINE++;
111
112 theme                   return THEME;
113 frame                   return FRAME;
114 decor                   return DECOR;
115 space                   return SPACE;
116 geometry                return GEOMETRY;
117 material                return MATERIAL;
118 gradient                return GRADIENT;
119 context                 return CONTEXT;
120 cursor                  return CURSOR;
121 style                   return STYLE;
122 up                      return UP;
123 anchor                  return ANCHOR;
124 opacity                 return OPACITY;
125 shapeof                 return SHAPEOF;
126 texture                 return TEXTURE;
127 image                   return IMAGE;
128 to                      return TO;
129 box                     return BOX;
130 NORTH                   return NORTH;
131 NORTHEAST               return NORTHEAST;
132 EAST                    return EAST;
133 SOUTHEAST               return SOUTHEAST;
134 SOUTH                   return SOUTH;
135 SOUTHWEST               return SOUTHWEST;
136 WEST                    return WEST;
137 NORTHWEST               return NORTHWEST;
138 NONE                    return NONE;
139 UNCHANGED               return UNCHANGED;
140
141 0x[0-9A-Z]+             yylval->integer = strtol(yytext, (char **)NULL, 16); return NUMBER;
142 [0-9]+                  yylval->integer = atoi(yytext); return NUMBER;
143 [a-zA-Z_][a-zA-Z0-9_]*  yylval->string = g_strdup(yytext); return ID;
144 \".*\" {
145                         yylval->string = g_strdup(yytext+1); 
146                         yylval->string[strlen(yylval->string)-1] = 0;
147                         return STRING;
148                 }
149 \`[^`]*\` {
150                         yylval->string = g_strdup(yytext+1); 
151                         yylval->string[strlen(yylval->string)-1] = 0;
152                         return STRING;
153                 }
154
155 \$[a-zA-Z_][a-zA-Z0-9_]*        yylval->string = g_strdup(yytext+1); return ID;
156 \$[0-9]*                yylval->integer = strtol(yytext + 1, (char **)NULL, 16); return SUBST;
157 \@                      return AT;
158 :                       return COLON;
159 ;                       return SEMICOLON;
160 "+"                     return PLUS;
161 "-"                     return MINUS;
162 "}"                     return RCB;
163 "{"                     return LCB;
164 ")"                     return RB;
165 "("                     return LB;
166 "<-"                    return LEFT_ARROW;
167 "->"                    return RIGHT_ARROW;
168 "<->"                   return DOUBLE_ARROW;
169 "."                     return DOT;
170 "*"                     return STAR;
171 "/"                     return SLASH;
172 ","                     return COMMA;
173 \n                      LINE++; /* zap EOL */
174 [ \t]+                  ; /* and whitespace */
175 .                       return 0;
176 %%