PageRenderTime 72ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Parser.c

https://github.com/aoshun-alphameteor-infinity/Docmycode
C | 208 lines | 188 code | 13 blank | 7 comment | 52 complexity | efd278c9c9f5c16a93148e7eeab70b86 MD5 | raw file
  1. #include "Parser.h"
  2. /**
  3. \fn int ParseFile (FILE* file, Comment* result)
  4. \brief parse a file and put the documentation comments to result
  5. \param file the file for the parsing
  6. \param result the table which contains the documentation comments
  7. \return the number of documentation comments find during the parsing
  8. */
  9. int ParseFile (FILE* file, Comment** result){
  10. int count=0,test=0,bloc=0;
  11. char c,tmp;
  12. detail=0;
  13. while(!feof()){
  14. c=fgetc(file);
  15. if(test==0 && c=='/')
  16. test++;
  17. else
  18. if(bloc==1){
  19. bloc==0;
  20. count++;
  21. }
  22. if(test==1 && (c=='/' || c=='*')){
  23. tmp=c;
  24. test++;
  25. }
  26. else {
  27. test=0;
  28. if(bloc==1){
  29. bloc==0;
  30. count++;
  31. }
  32. }
  33. if(test==2 && (c=='/' || c=='*') && c=tmp){
  34. ParseComment(file,tmp,result[count]);
  35. bloc=1;
  36. test=0;
  37. }
  38. else{
  39. test=0;
  40. if(bloc==1){
  41. bloc==0;
  42. count++;
  43. }
  44. }
  45. }
  46. return count;
  47. }
  48. void ParseComment (FILE* file,char type,Comment* doc){
  49. char* line=NULL;
  50. StringList bloc=NULL;
  51. int end=0;
  52. if(doc==NULL)doc=newComment();
  53. if(doc==NULL)exit(EXIT_FAILURE);
  54. switch(type){
  55. case '/':
  56. line=GetLine(file);
  57. ParseLine(file,doc);
  58. free(line);
  59. break;
  60. case '*':
  61. bloc=newStringList();
  62. while(end==0){
  63. char* tmp=GetLine(file);
  64. int length=strlen(tmp);
  65. if(strstr(tmp,"*/")!=NULL)end=1;
  66. addtoStringList(bloc,tmp);
  67. }
  68. StringCell* tmp;
  69. int i;
  70. for(tmp=bloc.first, i=1;i<=bloc.size;i++, tmp=tmp->suivant){
  71. ParseLine(tmp->string,doc);
  72. }
  73. freeStringList(bloc);
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. void ParseLine (String* line,Comment* doc){
  80. int begin, end;
  81. char* finally;
  82. char* marker;
  83. if(strstr(line,"///")==line)begin=3;
  84. if(strstr(line,"/**")==line)begin=3;
  85. if((finally=strstr(line,"*/"))!=NULL){
  86. finally[0]='\n';
  87. finally[1]='\0';
  88. free(&finally[2]);
  89. }
  90. if(marker=strstr(line,"\\author")){
  91. detail=0;
  92. int i=strlen(field),max=strlen(marker);
  93. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  94. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  95. if(tmp==NULL)exit(EXIT_FAILURE);
  96. strcpy(tmp,&marker[i]);
  97. setComment(doc->author,tmp);
  98. }
  99. if(marker=strstr(line,"\\version")){
  100. detail=0;
  101. int i=strlen(field),max=strlen(marker);
  102. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  103. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  104. if(tmp==NULL)exit(EXIT_FAILURE);
  105. strcpy(tmp,&marker[i]);
  106. setComment(doc->version,tmp);
  107. }
  108. if(marker=strstr(line,"\\date")){
  109. detail=0;
  110. int i=strlen(field),max=strlen(marker);
  111. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  112. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  113. if(tmp==NULL)exit(EXIT_FAILURE);
  114. strcpy(tmp,&marker[i]);
  115. setComment(doc->date,tmp);
  116. }
  117. if(marker=strstr(line,"\\brief")){
  118. detail=0;
  119. int i=strlen(field),max=strlen(marker);
  120. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  121. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  122. if(tmp==NULL)exit(EXIT_FAILURE);
  123. strcpy(tmp,&marker[i]);
  124. setComment(doc->brief,tmp);
  125. }
  126. if(marker=strstr(line,"\\details")){
  127. detail=1;
  128. int i=strlen(field),max=strlen(marker);
  129. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  130. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  131. if(tmp==NULL)exit(EXIT_FAILURE);
  132. strcpy(tmp,&marker[i]);
  133. setComment(doc->details,tmp);
  134. }
  135. if(marker=strstr(line,"\\fn")){
  136. detail=0;
  137. int i=strlen(field),max=strlen(marker);
  138. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  139. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  140. if(tmp==NULL)exit(EXIT_FAILURE);
  141. strcpy(tmp,&marker[i]);
  142. setComment(doc->fn,tmp);
  143. }
  144. if(marker=strstr(line,"\\param")){
  145. detail=0;
  146. int i=strlen(field),max=strlen(marker);
  147. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  148. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  149. if(tmp==NULL)exit(EXIT_FAILURE);
  150. strcpy(tmp,&marker[i]);
  151. addtoStringList(doc->parameters,tmp);
  152. }
  153. if(marker=strstr(line,"\\return")){
  154. detail=0;
  155. int i=strlen(field),max=strlen(marker);
  156. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  157. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  158. if(tmp==NULL)exit(EXIT_FAILURE);
  159. strcpy(tmp,&marker[i]);
  160. setComment(doc->return_value,tmp);
  161. }
  162. if(marker=strstr(line,"\\bug")){
  163. detail=0;
  164. int i=strlen(field),max=strlen(marker);
  165. while((marker[i]==' ' || marker[i]=='\t')&& i<=max)i++;
  166. char* tmp=(char*)malloc(sizeof(char)*(1+strlen(&marker[i])));
  167. if(tmp==NULL)exit(EXIT_FAILURE);
  168. strcpy(tmp,&marker[i]);
  169. setComment(doc->bug,tmp);
  170. }
  171. if(detail==1){
  172. int length=1+strlen(doc->details)+strlen(line);
  173. char* tmp=(char*)malloc(length*sizeof(char));
  174. tmp[0]='\0';
  175. if(tmp==NULL)exit(EXIT_FAILURE);
  176. strcat(tmp,doc->details);
  177. strcat(tmp,line);
  178. free(doc->details);
  179. doc->details=tmp;
  180. }
  181. }
  182. void setComment(char* field, ,char* doc){
  183. if(field==NULL)field=doc;
  184. else{
  185. int length=1+strlen(field)+strlen(doc);
  186. char* tmp=(char*)malloc(length*sizeof(char));
  187. if(tmp==NULL)exit(EXIT_FAILURE);
  188. tmp[0]='\0';
  189. strcat(tmp,field);
  190. strcat(tmp,doc);
  191. free(field);
  192. free(doc);
  193. field=tmp;
  194. }
  195. }