another checkpoint
[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 NORTH                   return NORTH;
128 NORTHEAST               return NORTHEAST;
129 EAST                    return EAST;
130 SOUTHEAST               return SOUTHEAST;
131 SOUTH                   return SOUTH;
132 SOUTHWEST               return SOUTHWEST;
133 WEST                    return WEST;
134 NORTHWEST               return NORTHWEST;
135 NONE                    return NONE;
136 UNCHANGED               return UNCHANGED;
137
138 0x[0-9A-Z]+             yylval->integer = strtol(yytext, (char **)NULL, 16); return NUMBER;
139 [0-9]+                  yylval->integer = atoi(yytext); return NUMBER;
140 [a-zA-Z_][a-zA-Z0-9_]*  yylval->string = g_strdup(yytext); return ID;
141 \".*\" {
142                         yylval->string = g_strdup(yytext+1); 
143                         yylval->string[strlen(yylval->string)-1] = 0;
144                         return STRING;
145                 }
146 \`[^`]*\` {
147                         yylval->string = g_strdup(yytext+1); 
148                         yylval->string[strlen(yylval->string)-1] = 0;
149                         return STRING;
150                 }
151
152 \$[a-zA-Z_][a-zA-Z0-9_]*        yylval->string = g_strdup(yytext+1); return ID;
153 \$[0-9]*                yylval->integer = strtol(yytext + 1, (char **)NULL, 16); return SUBST;
154 \@                      return AT;
155 :                       return COLON;
156 ;                       return SEMICOLON;
157 "+"                     return PLUS;
158 "-"                     return MINUS;
159 "}"                     return RCB;
160 "{"                     return LCB;
161 ")"                     return RB;
162 "("                     return LB;
163 "<-"                    return LEFT_ARROW;
164 "->"                    return RIGHT_ARROW;
165 "<->"                   return DOUBLE_ARROW;
166 \n                      LINE++; /* zap EOL */
167 [ \t]+                  ; /* and whitespace */
168 .                       return 0;
169 %%