1 %option stack
2 
3 %x comment
4 %x api_entry
5 %x api_entry2
6 %x api_entry_param
7 %x var_type
8 
9 DIGIT    [0-9]
10 ID       [a-zA-Z_][a-zA-Z0-9_]*
11 
12     #include "spec.h"
13 
14    int num_lines = 0;
15 
16    VarType *currType = 0;
17 
18    ApiEntry apis[128];
19    int apiCount = 0;
20 
21    int typeNextState;
22 
checkPointerType()23    void checkPointerType() {
24        VarType *baseType = currType;
25        int curPtrLevel = 0;
26        while (curPtrLevel < baseType->ptrLevel) {
27            currType = &apis[apiCount].params[apis[apiCount].paramCount];
28            currType->type = 4;
29            currType->ptrLevel = curPtrLevel;
30            if (currType->ptrLevel > 0) {
31               currType->isConst = 1;
32            }
33            sprintf(currType->typeName, "%s", "size_t");
34            switch(baseType->ptrLevel - curPtrLevel) {
35            case 1:
36               sprintf(currType->name, "%s_length", baseType->name);
37               break;
38            case 2:
39               sprintf(currType->name, "%s_length_length", baseType->name);
40               break;
41            }
42            apis[apiCount].paramCount++;
43            curPtrLevel ++;
44        }
45    }
46 
47    int yylex();
48 
49 %%
50 
51 "/*"         BEGIN(comment);
52 <comment>[^*\n]*        /* eat anything that's not a '*' */
53 <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
54 <comment>\n             ++num_lines;
55 <comment>"*"+"/"        BEGIN(INITIAL);
56 
57 <*>" "   //printf("found ' '\n");
58 <*>"\t"   //printf("found ' '\n");
59 <*>"\n"  ++num_lines; //printf("found lf \n");
60 
61 {ID} {
62     memset(&apis[apiCount], 0, sizeof(ApiEntry));
63     memcpy(apis[apiCount].name, yytext, yyleng);
64     BEGIN(api_entry);
65     }
66 
67 <api_entry>"{" {
68     BEGIN(api_entry2);
69     }
70 
71 <api_entry2>"sync" {
72     apis[apiCount].sync = 1;
73     }
74 
75 <api_entry2>"handcodeApi" {
76     apis[apiCount].handcodeApi = 1;
77     }
78 
79 <api_entry2>"direct" {
80     apis[apiCount].direct = 1;
81     }
82 
83 <api_entry2>"nocontext" {
84     apis[apiCount].nocontext = 1;
85     }
86 
87 <api_entry2>"ret" {
88     currType = &apis[apiCount].ret;
89     typeNextState = api_entry2;
90     BEGIN(var_type);
91     }
92 
93 <api_entry2>"param" {
94     currType = &apis[apiCount].params[apis[apiCount].paramCount];
95     apis[apiCount].paramCount++;
96     typeNextState = api_entry_param;
97     BEGIN(var_type);
98     }
99 
100 <var_type>"const" {
101     currType->isConst = 1;
102     }
103 
104 <var_type>"i8" {
105     currType->type = 1;
106     currType->bits = 8;
107     BEGIN(typeNextState);
108     }
109 
110 <var_type>"i16" {
111     currType->type = 1;
112     currType->bits = 16;
113     BEGIN(typeNextState);
114     }
115 
116 <var_type>"i32" {
117     currType->type = 1;
118     currType->bits = 32;
119     BEGIN(typeNextState);
120     }
121 
122 <var_type>"i64" {
123     currType->type = 1;
124     currType->bits = 64;
125     BEGIN(typeNextState);
126     }
127 
128 <var_type>"u8" {
129     currType->type = 2;
130     currType->bits = 8;
131     BEGIN(typeNextState);
132     }
133 
134 <var_type>"u16" {
135     currType->type = 2;
136     currType->bits = 16;
137     BEGIN(typeNextState);
138     }
139 
140 <var_type>"u32" {
141     currType->type = 2;
142     currType->bits = 32;
143     BEGIN(typeNextState);
144     }
145 
146 <var_type>"u64" {
147     currType->type = 2;
148     currType->bits = 64;
149     BEGIN(typeNextState);
150     }
151 
152 <var_type>"f" {
153     currType->type = 3;
154     currType->bits = 32;
155     BEGIN(typeNextState);
156     }
157 
158 <var_type>"d" {
159     currType->type = 3;
160     currType->bits = 64;
161     BEGIN(typeNextState);
162     }
163 
164 <var_type>{ID} {
165     currType->type = 4;
166     currType->bits = 32;
167     memcpy(currType->typeName, yytext, yyleng);
168     BEGIN(typeNextState);
169     }
170 
171 <api_entry_param>"*" {
172     currType->ptrLevel ++;
173     }
174 
175 <api_entry_param>{ID} {
176     memcpy(currType->name, yytext, yyleng);
177     checkPointerType();
178     BEGIN(api_entry2);
179     }
180 
181 <api_entry2>"*" {
182     currType->ptrLevel ++;
183     }
184 
185 <api_entry2>"}" {
186     apiCount++;
187     BEGIN(INITIAL);
188     }
189 
190 <*>. {
191     fprintf(stderr, "error: unexpected character \'%c\' at line %d\n",
192             *yytext, num_lines + 1);
193     exit(1);
194     }
195 
196 %%
197 
198 
199 int yywrap()
200 {
201     return 1;
202 }
203 
204