spec.l 3.8 KB

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