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