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