Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Joe Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 12 | #include "spec.h" |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 13 | |
| 14 | int num_lines = 0; |
| 15 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 16 | VarType *currType = 0; |
| 17 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 18 | ApiEntry apis[128]; |
| 19 | int apiCount = 0; |
| 20 | |
| 21 | int typeNextState; |
| 22 | |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 23 | void checkPointerType() { |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 24 | VarType *baseType = currType; |
| 25 | int curPtrLevel = 0; |
| 26 | while (curPtrLevel < baseType->ptrLevel) { |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 27 | currType = &apis[apiCount].params[apis[apiCount].paramCount]; |
| 28 | currType->type = 4; |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 29 | currType->ptrLevel = curPtrLevel; |
| 30 | if (currType->ptrLevel > 0) { |
| 31 | currType->isConst = 1; |
| 32 | } |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 33 | sprintf(currType->typeName, "%s", "size_t"); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 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; |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 41 | } |
| 42 | apis[apiCount].paramCount++; |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 43 | curPtrLevel ++; |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Joe Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 47 | extern "C" int yylex(); |
| 48 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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"); |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 58 | <*>"\t" //printf("found ' '\n"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 75 | <api_entry2>"handcodeApi" { |
| 76 | apis[apiCount].handcodeApi = 1; |
| 77 | } |
| 78 | |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 79 | <api_entry2>"direct" { |
| 80 | apis[apiCount].direct = 1; |
| 81 | } |
| 82 | |
| 83 | <api_entry2>"nocontext" { |
| 84 | apis[apiCount].nocontext = 1; |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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; |
Joe Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 167 | memcpy(currType->typeName, yytext, yyleng); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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); |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 177 | checkPointerType(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 178 | BEGIN(api_entry2); |
| 179 | } |
| 180 | |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 181 | <api_entry2>"*" { |
| 182 | currType->ptrLevel ++; |
| 183 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 184 | |
| 185 | <api_entry2>"}" { |
| 186 | apiCount++; |
| 187 | BEGIN(INITIAL); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | %% |
| 192 | |
| 193 | |
| 194 | int yywrap() |
| 195 | { |
| 196 | return 1; |
| 197 | } |
| 198 | |