javaparser.l 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. D [0-9]
  2. N [1-9]
  3. L [a-zA-Z_$]
  4. H [a-fA-F0-9]
  5. OC [0-7]
  6. E [Ee][+-]?{D}+
  7. LS [fFdD]
  8. Escape \\[ntbrf\\'"]
  9. Escchar \\{D}({D}?{D})?
  10. Escunichar \\u{H}{H}{H}{H}
  11. %{
  12. int yylineno = 0;
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "Python.h"
  16. #define YYSTYPE void *
  17. #include "tokens.h"
  18. extern void *py_parser;
  19. extern void (*py_input)(PyObject *parser, char *buf, int *result, int max_size);
  20. #define returntoken(tok) /*printf("%d=%s\n", tok, yytext);*/ yylval = PyString_FromString(strdup(yytext)); return (tok);
  21. #define YY_INPUT(buf,result,max_size) { (*py_input)(py_parser, buf, &result, max_size); }
  22. #include "table.h"
  23. %}
  24. %%
  25. {N}{D}*(l|L)? { returntoken(INTEGER_LITERAL_TOKEN); }
  26. {N}{D}*(d|D)? { returntoken(INTEGER_LITERAL_TOKEN); }
  27. 0[xX]{H}+(l|L)? { returntoken(INTEGER_LITERAL_TOKEN); }
  28. 0{OC}*(l|L)? { returntoken(INTEGER_LITERAL_TOKEN); }
  29. {D}+"."{D}*({E})?{LS}? { returntoken(FLOATING_POINT_LITERAL_TOKEN); }
  30. "."{D}+({E})?{LS}? { returntoken(FLOATING_POINT_LITERAL_TOKEN); }
  31. {D}+{E}{LS}? { returntoken(FLOATING_POINT_LITERAL_TOKEN); }
  32. {D}{LS} { returntoken(FLOATING_POINT_LITERAL_TOKEN); }
  33. "true" { returntoken(BOOLEAN_LITERAL_TOKEN); }
  34. "false" { returntoken(BOOLEAN_LITERAL_TOKEN); }
  35. {L}({L}|{D})* { returntoken(Table_Lookup(KeywordTable));}
  36. '[^'\\]' { returntoken(CHARACTER_LITERAL_TOKEN);}
  37. '{Escape}' { returntoken(CHARACTER_LITERAL_TOKEN);}
  38. \"([^\"\\]|{Escape}|{Escchar}|{Escunichar})*\" { returntoken(STRING_LITERAL_TOKEN); }
  39. \/\/.*$ {}
  40. \/\* { comment();}
  41. "(" { returntoken(OPEN_PAREN_TOKEN); }
  42. ")" { returntoken(CLOSE_PAREN_TOKEN); }
  43. "{" { returntoken(OPEN_BRACE_TOKEN); }
  44. "}" { returntoken(CLOSE_BRACE_TOKEN); }
  45. "[" { returntoken(OPEN_BRACKET_TOKEN); }
  46. "]" { returntoken(CLOSE_BRACKET_TOKEN); }
  47. ";" { returntoken(SEMICOLON_TOKEN); }
  48. "," { returntoken(COMMA_TOKEN); }
  49. "." { returntoken(PERIOD_TOKEN); }
  50. "=" { returntoken(ASSIGNS_TOKEN); }
  51. ">" { returntoken(GREATER_TOKEN); }
  52. "<" { returntoken(LESS_TOKEN); }
  53. "!" { returntoken(NOT_TOKEN); }
  54. "~" { returntoken(TILDE_TOKEN); }
  55. "?" { returntoken(CONDITIONAL_TOKEN); }
  56. ":" { returntoken(COLON_TOKEN); }
  57. "==" { returntoken(EQ_TOKEN); }
  58. "<=" { returntoken(LE_TOKEN); }
  59. ">=" { returntoken(GE_TOKEN); }
  60. "!=" { returntoken(NE_OP_TOKEN); }
  61. "||" { returntoken(LOGICAL_OR_TOKEN); }
  62. "&&" { returntoken(LOGICAL_AND_TOKEN); }
  63. "++" { returntoken(INC_TOKEN); }
  64. "--" { returntoken(DEC_TOKEN); }
  65. "+" { returntoken(PLUS_TOKEN); }
  66. "-" { returntoken(MINUS_TOKEN); }
  67. "*" { returntoken(MUL_TOKEN); }
  68. "/" { returntoken(DIV_TOKEN); }
  69. "&" { returntoken(AND_TOKEN); }
  70. "|" { returntoken(OR_TOKEN); }
  71. "^" { returntoken(XOR_TOKEN); }
  72. "%" { returntoken(MOD_TOKEN); }
  73. "<<" { returntoken(SHL_TOKEN); }
  74. ">>" { returntoken(SAR_TOKEN); }
  75. ">>>" { returntoken(SHL_TOKEN); }
  76. "+=" { returntoken(ADD_ASSIGN_TOKEN); }
  77. "-=" { returntoken(SUB_ASSIGN_TOKEN); }
  78. "*=" { returntoken(MUL_ASSIGN_TOKEN); }
  79. "/=" { returntoken(DIV_ASSIGN_TOKEN); }
  80. "&=" { returntoken(AND_ASSIGN_TOKEN); }
  81. "|=" { returntoken(OR_ASSIGN_TOKEN); }
  82. "^=" { returntoken(XOR_ASSIGN_TOKEN); }
  83. "%=" { returntoken(MOD_ASSIGN_TOKEN); }
  84. "<<=" { returntoken(SHL_ASSIGN_TOKEN); }
  85. ">>=" { returntoken(SAR_ASSIGN_TOKEN); }
  86. ">>>=" { returntoken(SHR_ASSIGN_TOKEN); }
  87. [ \t\v\f] {}
  88. [\n] {yylineno++;}
  89. . { printf("unknown char %c ignored\n", yytext[0]); /* ignore bad chars */}
  90. %%
  91. yywrap() { return(1); }
  92. /* test_main()
  93. {
  94. int t;
  95. for(t = yylex(), t > 0, t = yylex())
  96. {
  97. printf("%s\t%d\n", yytext,t);
  98. }
  99. return 0;
  100. }
  101. */
  102. /* input: Table of tokens
  103. * returns TokenCode of keyword if matched or
  104. * ID_TOKEN if no match is found
  105. */
  106. int Table_Lookup(struct KeywordToken Table[])
  107. {
  108. struct KeywordToken *Curr;
  109. int i = 0;
  110. for (Curr = Table; Curr->Keyword != ""; Curr++)
  111. {
  112. //printf("Table_Lookup: yytext='%s', Curr->Keyword='%s', idx=%d\n", yytext, Curr->Keyword, i);
  113. if (strcmp(Curr->Keyword, yytext)==0)
  114. {
  115. //printf("Table_Lookup: '%s' => %d\n", yytext, Curr->TokenCode);
  116. return (Curr->TokenCode);
  117. }
  118. i++;
  119. }
  120. return ID_TOKEN;
  121. }
  122. commentold()
  123. {
  124. char c = -1, c1;
  125. while(c != 0)
  126. {
  127. for(c = input(); c != '*' && c!=0; c = input())
  128. ;
  129. /* now we have a star or no more chars */
  130. if(c == 0 || (c1 = input() == '/'))
  131. return;
  132. if (c1 == '*')
  133. unput(c1);
  134. }
  135. }
  136. comment()
  137. {
  138. int prev=-1, cur=-1;
  139. while (1)
  140. {
  141. cur = input();
  142. if (cur == '/' && prev == '*')
  143. return;
  144. else if (cur == 0)
  145. return;
  146. prev = cur;
  147. }
  148. }