expr.l 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. %option prefix="expr_"
  2. %option reentrant
  3. %option bison-bridge
  4. %{
  5. #include <linux/compiler.h>
  6. #include "expr.h"
  7. #include "expr-bison.h"
  8. char *expr_get_text(yyscan_t yyscanner);
  9. YYSTYPE *expr_get_lval(yyscan_t yyscanner);
  10. static double __value(YYSTYPE *yylval, char *str, int token)
  11. {
  12. double num;
  13. errno = 0;
  14. num = strtod(str, NULL);
  15. if (errno)
  16. return EXPR_ERROR;
  17. yylval->num = num;
  18. return token;
  19. }
  20. static int value(yyscan_t scanner)
  21. {
  22. YYSTYPE *yylval = expr_get_lval(scanner);
  23. char *text = expr_get_text(scanner);
  24. return __value(yylval, text, NUMBER);
  25. }
  26. /*
  27. * Allow @ instead of / to be able to specify pmu/event/ without
  28. * conflicts with normal division.
  29. */
  30. static char *normalize(char *str, int runtime)
  31. {
  32. char *ret = str;
  33. char *dst = str;
  34. while (*str) {
  35. if (*str == '@')
  36. *dst++ = '/';
  37. else if (*str == '\\')
  38. *dst++ = *++str;
  39. else if (*str == '?') {
  40. char *paramval;
  41. int i = 0;
  42. int size = asprintf(&paramval, "%d", runtime);
  43. if (size < 0)
  44. *dst++ = '0';
  45. else {
  46. while (i < size)
  47. *dst++ = paramval[i++];
  48. free(paramval);
  49. }
  50. }
  51. else
  52. *dst++ = *str;
  53. str++;
  54. }
  55. *dst = 0x0;
  56. return ret;
  57. }
  58. static int str(yyscan_t scanner, int token, int runtime)
  59. {
  60. YYSTYPE *yylval = expr_get_lval(scanner);
  61. char *text = expr_get_text(scanner);
  62. yylval->str = normalize(strdup(text), runtime);
  63. if (!yylval->str)
  64. return EXPR_ERROR;
  65. yylval->str = normalize(yylval->str, runtime);
  66. return token;
  67. }
  68. %}
  69. number ([0-9]+\.?[0-9]*|[0-9]*\.?[0-9]+)
  70. sch [-,=]
  71. spec \\{sch}
  72. sym [0-9a-zA-Z_\.:@?]+
  73. symbol ({spec}|{sym})+
  74. %%
  75. struct expr_scanner_ctx *sctx = expr_get_extra(yyscanner);
  76. {
  77. int start_token = sctx->start_token;
  78. if (sctx->start_token) {
  79. sctx->start_token = 0;
  80. return start_token;
  81. }
  82. }
  83. d_ratio { return D_RATIO; }
  84. max { return MAX; }
  85. min { return MIN; }
  86. if { return IF; }
  87. else { return ELSE; }
  88. #smt_on { return SMT_ON; }
  89. {number} { return value(yyscanner); }
  90. {symbol} { return str(yyscanner, ID, sctx->runtime); }
  91. "|" { return '|'; }
  92. "^" { return '^'; }
  93. "&" { return '&'; }
  94. "<" { return '<'; }
  95. ">" { return '>'; }
  96. "-" { return '-'; }
  97. "+" { return '+'; }
  98. "*" { return '*'; }
  99. "/" { return '/'; }
  100. "%" { return '%'; }
  101. "(" { return '('; }
  102. ")" { return ')'; }
  103. "," { return ','; }
  104. . { }
  105. %%
  106. int expr_wrap(void *scanner __maybe_unused)
  107. {
  108. return 1;
  109. }