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