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