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