Happy New Year!
[ginac.git] / ginsh / ginsh_lexer.lpp
1 /** @file ginsh_lexer.lpp
2  *
3  *  Lexical analyzer definition for ginsh.
4  *  This file must be processed with flex. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2019 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24
25 /*
26  *  Definitions
27  */
28
29 %option nounput
30
31 %pointer
32
33 %{
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include "ginsh.h"
39 #include "ginsh_parser.hpp"
40
41 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
42
43 // Table of all used symbols
44 sym_tab syms;
45
46 // Type of symbols to generate (real or complex)
47 unsigned symboltype = domain::complex;
48
49 // lex input function
50 static int ginsh_input(char *buf, int max_size);
51 %}
52
53         /* Abbreviations */
54 D       [0-9]
55 E       [elEL][-+]?{D}+
56 A       [a-zA-Z_]
57 AN      [0-9a-zA-Z_]
58
59
60 /*
61  *  Lexical rules
62  */
63
64 %%
65 [ \t\n]+                /* skip whitespace */
66 \\$                     /* skip line continuations */
67 "//".*                  /* skip comments starting with "//" */
68 ^"#".*                  /* skip lines starting with "#" */
69 ^"!".*                  system(yytext + 1);     /* execute shell command */
70
71                         /* special values */
72 Pi                      yylval = Pi; return T_LITERAL;
73 Euler                   yylval = Euler; return T_LITERAL;
74 Catalan                 yylval = Catalan; return T_LITERAL;
75 FAIL                    yylval = *new fail(); return T_LITERAL;
76 I                       yylval = I; return T_NUMBER;
77 Digits                  yylval = (long)Digits; return T_DIGITS;
78
79                         /* keywords */
80 quit|exit               return T_QUIT;
81 warranty                return T_WARRANTY;
82 print                   return T_PRINT;
83 iprint                  return T_IPRINT;
84 print_latex             return T_PRINTLATEX;
85 print_csrc              return T_PRINTCSRC;
86 time                    return T_TIME;
87 xyzzy                   return T_XYZZY;
88 inventory               return T_INVENTORY;
89 look                    return T_LOOK;
90 score                   return T_SCORE;
91 complex_symbols return T_COMPLEX_SYMBOLS;
92 real_symbols    return T_REAL_SYMBOLS;
93
94                         /* comparison */
95 "=="                    return T_EQUAL;
96 "!="                    return T_NOTEQ;
97 "<="                    return T_LESSEQ;
98 ">="                    return T_GREATEREQ;
99
100                         /* last 1..3 expressions */
101 \%                      return T_QUOTE;
102 \%\%                    return T_QUOTE2;
103 \%\%\%                  return T_QUOTE3;
104
105                         /* numbers */
106 {D}+                    |
107 "#"{D}+"R"{AN}+         |
108 "#b"([01])+             |
109 "#o"[0-7]+              |
110 "#x"[0-9a-fA-F]+        |
111 {D}+"."{D}*({E})?       |
112 {D}*"."{D}+({E})?       |
113 {D}+{E}                 yylval = numeric(yytext); return T_NUMBER;
114
115                         /* symbols */
116 {A}{AN}*                {
117                                 sym_tab::const_iterator i = syms.find(yytext);
118                                 if (i == syms.end()) {
119                                         if (symboltype == domain::complex) {
120                                                 symbol tmp(yytext);
121                                                 syms[yytext] = tmp;
122                                                 yylval = tmp;
123                                         } else {
124                                                 realsymbol tmp(yytext);
125                                                 syms[yytext] = tmp;
126                                                 yylval = tmp;
127                                         }
128                                 } else
129                                         yylval = i->second;
130                                 return T_SYMBOL;
131                         }
132
133                         /* wildcards */
134 \${D}+                  yylval = wild(atoi(yytext + 1)); return T_LITERAL;
135
136                         /* everything else */
137 .                       return *yytext;
138
139 %%
140
141
142 /*
143  *  Routines
144  */
145
146 static int line_length = 0;
147 static char *line_read = nullptr;
148 static char *line_ptr;
149
150 // Input function that uses libreadline for interactive input
151 static int ginsh_input(char *buf, int max_size)
152 {
153         int result;
154 #if defined(YY_CURRENT_BUFFER)
155         if (YY_CURRENT_BUFFER->yy_is_interactive) {
156 #else
157         if (yy_current_buffer->yy_is_interactive) {
158 #endif
159 #ifdef HAVE_LIBREADLINE
160                 // Do we need to read a new line?
161                 int actual;
162                 if (line_length == 0) {
163
164                         // Free old line
165                         if (line_read)
166                                 free(line_read);
167
168                         // Read new line, prompt "> "
169                         line_read = line_ptr = readline("> ");
170
171                         // EOF?
172                         if (!line_read) {
173                                 line_length = 0;
174                                 return YY_NULL;
175                         }
176
177                         // Add non-empty lines to history
178                         line_length = strlen(line_read) + 1;
179                         if (line_length > 1)
180                                 add_history(line_read);
181
182                         // Reappend trailing '\n' which is stripped by readline()
183                         line_read[line_length - 1] = '\n';
184                 }
185
186                 // Copy data to lex buffer
187                 actual = line_length > max_size ? max_size : line_length;
188                 memcpy(buf, line_ptr, actual);
189                 line_length -= actual;
190                 line_ptr += actual;
191                 result = actual;
192 #else
193                 printf("> "); fflush(stdout);
194                 int c = '*', n;
195                 for (n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n)
196                         buf[n] = (char)c;
197                 if (c == '\n')
198                         buf[n++] = (char)c;
199                 if (c == EOF && ferror(yyin))
200                         YY_FATAL_ERROR("input in flex scanner failed");
201                 result = n;
202 #endif
203         } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
204                 YY_FATAL_ERROR("input in flex scanner failed");
205
206         return result;
207 }
208
209 // List of input files to be processed
210 int num_files = 0;
211 char **file_list = nullptr;
212
213 // EOF encountered, connect to next file. If this was the last file,
214 // connect to stdin. If this was stdin, terminate the scanner.
215 int yywrap()
216 {
217         if (yyin == stdin)
218                 return 1;
219
220         fclose(yyin);
221         if (num_files) {
222                 yyin = fopen(*file_list, "r");
223                 if (yyin == nullptr) {
224                         cerr << "Can't open " << *file_list << endl;
225                         return 1;
226                 }
227                 num_files--;
228                 file_list++;
229         } else
230                 yyin = stdin;
231         return 0;
232 }