%option stack %x comment %x api_entry %x api_entry2 %x api_entry_param %x var_type DIGIT [0-9] ID [a-zA-Z_][a-zA-Z0-9_]* #include "spec.h" int num_lines = 0; VarType *currType = 0; ApiEntry apis[128]; int apiCount = 0; int typeNextState; void checkPointerType() { VarType *baseType = currType; int curPtrLevel = 0; while (curPtrLevel < baseType->ptrLevel) { currType = &apis[apiCount].params[apis[apiCount].paramCount]; currType->type = 4; currType->ptrLevel = curPtrLevel; if (currType->ptrLevel > 0) { currType->isConst = 1; } sprintf(currType->typeName, "%s", "size_t"); switch(baseType->ptrLevel - curPtrLevel) { case 1: sprintf(currType->name, "%s_length", baseType->name); break; case 2: sprintf(currType->name, "%s_length_length", baseType->name); break; } apis[apiCount].paramCount++; curPtrLevel ++; } } extern "C" int yylex(); %% "/*" BEGIN(comment); [^*\n]* /* eat anything that's not a '*' */ "*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ \n ++num_lines; "*"+"/" BEGIN(INITIAL); <*>" " //printf("found ' '\n"); <*>"\t" //printf("found ' '\n"); <*>"\n" ++num_lines; //printf("found lf \n"); {ID} { memset(&apis[apiCount], 0, sizeof(ApiEntry)); memcpy(apis[apiCount].name, yytext, yyleng); BEGIN(api_entry); } "{" { BEGIN(api_entry2); } "sync" { apis[apiCount].sync = 1; } "handcodeApi" { apis[apiCount].handcodeApi = 1; } "direct" { apis[apiCount].direct = 1; } "nocontext" { apis[apiCount].nocontext = 1; } "ret" { currType = &apis[apiCount].ret; typeNextState = api_entry2; BEGIN(var_type); } "param" { currType = &apis[apiCount].params[apis[apiCount].paramCount]; apis[apiCount].paramCount++; typeNextState = api_entry_param; BEGIN(var_type); } "const" { currType->isConst = 1; } "i8" { currType->type = 1; currType->bits = 8; BEGIN(typeNextState); } "i16" { currType->type = 1; currType->bits = 16; BEGIN(typeNextState); } "i32" { currType->type = 1; currType->bits = 32; BEGIN(typeNextState); } "i64" { currType->type = 1; currType->bits = 64; BEGIN(typeNextState); } "u8" { currType->type = 2; currType->bits = 8; BEGIN(typeNextState); } "u16" { currType->type = 2; currType->bits = 16; BEGIN(typeNextState); } "u32" { currType->type = 2; currType->bits = 32; BEGIN(typeNextState); } "u64" { currType->type = 2; currType->bits = 64; BEGIN(typeNextState); } "f" { currType->type = 3; currType->bits = 32; BEGIN(typeNextState); } "d" { currType->type = 3; currType->bits = 64; BEGIN(typeNextState); } {ID} { currType->type = 4; currType->bits = 32; memcpy(currType->typeName, yytext, yyleng); BEGIN(typeNextState); } "*" { currType->ptrLevel ++; } {ID} { memcpy(currType->name, yytext, yyleng); checkPointerType(); BEGIN(api_entry2); } "*" { currType->ptrLevel ++; } "}" { apiCount++; BEGIN(INITIAL); } <*>. { fprintf(stderr, "error: unexpected character \'%c\' at line %d\n", *yytext, num_lines + 1); exit(1); } %% int yywrap() { return 1; }