mapcalc.l 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. %{
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <grass/config.h>
  6. #ifdef HAVE_READLINE_READLINE_H
  7. #include <readline/readline.h>
  8. #include <readline/history.h>
  9. #endif
  10. #include <grass/gis.h>
  11. #include "mapcalc.h"
  12. #include "mapcalc.tab.h"
  13. #ifndef YY_NULL
  14. #define YY_NULL 0
  15. #endif
  16. #ifndef yyterminate
  17. #define yyterminate() return YY_NULL
  18. #endif
  19. static const char *input_string;
  20. static int input_length;
  21. static int input_offset;
  22. static FILE *input_stream;
  23. static int get_input_string(char *buf, int max_size)
  24. {
  25. const char *next = input_string + input_offset;
  26. int left = input_length - input_offset;
  27. int result;
  28. if (left <= 0)
  29. return YY_NULL;
  30. result = (left > max_size) ? max_size : left;
  31. memcpy(buf, next, result);
  32. input_offset += result;
  33. return result;
  34. }
  35. static int get_input_stream(char *buf, int max_size)
  36. {
  37. if (feof(input_stream))
  38. return YY_NULL;
  39. #ifdef HAVE_READLINE_READLINE_H
  40. if (isatty(fileno(input_stream)))
  41. {
  42. char *line_read;
  43. line_read = readline("mapcalc> ");
  44. if (!line_read)
  45. return YY_NULL;
  46. if (strlen(line_read) > max_size - 2)
  47. G_fatal_error("input line too long");
  48. strcpy(buf, line_read);
  49. strcat(buf, "\n");
  50. free(line_read);
  51. if (!*buf)
  52. return YY_NULL;
  53. add_history(buf);
  54. }
  55. else
  56. {
  57. if (!fgets(buf, max_size, input_stream))
  58. return YY_NULL;
  59. }
  60. #else
  61. if (isatty(fileno(input_stream)))
  62. fputs("mapcalc> ", stderr);
  63. if (!fgets(buf, max_size, input_stream))
  64. return YY_NULL;
  65. #endif
  66. return strlen(buf);
  67. }
  68. #define YY_INPUT(buf,result,max_size) \
  69. { \
  70. result = input_string \
  71. ? get_input_string(buf, max_size) \
  72. : get_input_stream(buf, max_size); \
  73. }
  74. %}
  75. W [ \t\r]+
  76. N [^ ^#@,"'\000-\037()\[\]+\-*/%><!=&|?:;~]+
  77. I [0-9]+
  78. X [0][xX][0-9a-fA-F]+
  79. E [eE][-+]?[0-9]+
  80. F [fF]
  81. %%
  82. {W} ; /* ignore white space */
  83. "\\"{W}?"\n" ; /* ignore backslash-newline */
  84. {I}"."{I}?{E}?{F} |
  85. "."{I}{E}?{F} {
  86. yylval.fval = atof(yytext);
  87. return FLOAT;
  88. }
  89. {I}"."{I}?{E}? |
  90. "."{I}{E}? {
  91. yylval.fval = atof(yytext);
  92. return DOUBLE;
  93. }
  94. {I}"."{I}?[eE] |
  95. {I}"."{I}?[eE][-+] |
  96. "."{I}?[eE] |
  97. "."{I}?[eE][-+] {
  98. fprintf(stderr, "unterminated FP constant\n");
  99. yyterminate();
  100. }
  101. {I} {
  102. yylval.ival = atoi(yytext);
  103. return INTEGER;
  104. }
  105. {X} {
  106. yylval.ival = (int) strtoul(yytext, NULL, 16);
  107. return INTEGER;
  108. }
  109. {N} {
  110. yylval.sval = strdup(yytext);
  111. return is_var(yytext) ? VARNAME : NAME;
  112. }
  113. \"[^"]+\" |
  114. \'[^']+\' {
  115. yylval.sval = strdup(yytext + 1);
  116. yylval.sval[yyleng - 2] = '\0';
  117. return is_var(yytext) ? VARSTRING : STRING;
  118. }
  119. \"[^"]+ |
  120. \'[^']+ {
  121. fprintf(stderr, "unterminated string\n");
  122. yyterminate();
  123. }
  124. "+" { return '+'; }
  125. "-" { return '-'; }
  126. "*" { return '*'; }
  127. "/" { return '/'; }
  128. "^" { return '^'; }
  129. "%" { return '%'; }
  130. "!" { return '!'; }
  131. "~" { return '~'; }
  132. "&&&" { return LOGAND2; }
  133. "&&" { return LOGAND; }
  134. "&" { return BITAND; }
  135. "|||" { return LOGOR2; }
  136. "||" { return LOGOR; }
  137. "|" { return BITOR; }
  138. "<<" { return LSH; }
  139. ">>>" { return RSHU; }
  140. ">>" { return RSH; }
  141. ">" { return GT; }
  142. ">=" { return GE; }
  143. "<" { return LT; }
  144. "<=" { return LE; }
  145. "==" { return EQ; }
  146. "!=" { return NE; }
  147. "?" { return '?'; }
  148. ":" { return ':'; }
  149. [rR]"#" { return 'r'; }
  150. [gG]"#" { return 'g'; }
  151. [bB]"#" { return 'b'; }
  152. "#" { return '#'; }
  153. [yY]"#" { return 'y'; }
  154. [iI]"#" { return 'i'; }
  155. "@" { return '@'; }
  156. "(" { return '('; }
  157. ")" { return ')'; }
  158. "[" { return '['; }
  159. "]" { return ']'; }
  160. "=" { return '='; }
  161. "," { return ','; }
  162. ^{W}*\n { yyterminate(); }
  163. ^{W}*"end"{W}*\n { yyterminate(); }
  164. ^{W}*"exit"{W}*\n { yyterminate(); }
  165. ";" |
  166. "\n" { return ';'; }
  167. %%
  168. int yywrap(void)
  169. {
  170. return 1;
  171. }
  172. void initialize_scanner_string(const char *s)
  173. {
  174. input_string = s;
  175. input_length = strlen(s);
  176. input_offset = 0;
  177. }
  178. void initialize_scanner_stream(FILE *fp)
  179. {
  180. input_stream = fp;
  181. setbuf(fp, NULL);
  182. }