]> www.ginac.de Git - ginac.git/blob - ginsh/ginsh_lexer.ll
- final tweaks to spec file
[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-2000 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 read                    return T_READ;
72 write                   return T_WRITE;
73 time                    return T_TIME;
74 xyzzy                   return T_XYZZY;
75 inventory               return T_INVENTORY;
76 look                    return T_LOOK;
77 score                   return T_SCORE;
78
79                         /* comparison */
80 "=="                    return T_EQUAL;
81 "!="                    return T_NOTEQ;
82 "<="                    return T_LESSEQ;
83 ">="                    return T_GREATEREQ;
84
85                         /* last 1..3 expressions */
86 \"                      return T_QUOTE;
87 \"\"                    return T_QUOTE2;
88 \"\"\"                  return T_QUOTE3;
89
90                         /* matrix delimiters */
91 \[\[                    return T_MATRIX_BEGIN;
92 \]\]                    return T_MATRIX_END;
93
94                         /* numbers */
95 {D}+                    |
96 {D}+"."{D}*({E})?       |
97 {D}*"."{D}+({E})?       |
98 {D}+{E}                 yylval = numeric(yytext); return T_NUMBER;
99
100                         /* symbols */
101 {A}{AN}*                {
102                                 if (syms.find(yytext) == syms.end())
103                                         syms[yytext] = *(new symbol(yytext));
104                                 yylval = syms[yytext];
105                                 return T_SYMBOL;
106                         }
107
108                         /* everything else */
109 .                       return *yytext;
110
111 %%
112
113
114 /*
115  *  Routines
116  */
117
118 static int line_length = 0;
119 static char *line_read = NULL;
120 static char *line_ptr;
121
122 // Input function that uses libreadline for interactive input
123 static int ginsh_input(char *buf, int max_size)
124 {
125         int result;
126         if (yy_current_buffer->yy_is_interactive) {
127 #ifdef HAVE_LIBREADLINE
128                 // Do we need to read a new line?
129                 int actual;
130                 if (line_length == 0) {
131
132                         // Free old line
133                         if (line_read)
134                                 free(line_read);
135
136                         // Read new line, prompt "> "
137                         line_read = line_ptr = readline("> ");
138
139                         // EOF?
140                         if (!line_read) {
141                                 line_length = 0;
142                                 return YY_NULL;
143                         }
144
145                         // Add non-empty lines to history
146                         line_length = strlen(line_read) + 1;
147                         if (line_length > 1)
148                                 add_history(line_read);
149
150                         // Reappend trailing '\n' which is stripped by readline()
151                         line_read[line_length - 1] = '\n';
152                 }
153
154                 // Copy data to lex buffer
155                 actual = line_length > max_size ? max_size : line_length;
156                 memcpy(buf, line_ptr, actual);
157                 line_length -= actual;
158                 line_ptr += actual;
159                 result = actual;
160 #else
161                 printf("> "); fflush(stdout);
162                 int c = '*', n;
163                 for (n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n)
164                         buf[n] = (char)c;
165                 if (c == '\n')
166                         buf[n++] = (char)c;
167                 if (c == EOF && ferror(yyin))
168                         YY_FATAL_ERROR("input in flex scanner failed");
169                 result = n;
170 #endif
171         } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
172                 YY_FATAL_ERROR("input in flex scanner failed");
173
174         return result;
175 }
176
177 // List of input files to be processed
178 int num_files = 0;
179 char **file_list = NULL;
180
181 // EOF encountered, connect to next file. If this was the last file,
182 // connect to stdin. If this was stdin, terminate the scanner.
183 int yywrap()
184 {
185         if (yyin == stdin)
186                 return 1;
187
188         fclose(yyin);
189         if (num_files) {
190                 yyin = fopen(*file_list, "r");
191                 if (yyin == NULL) {
192                         cerr << "Can't open " << *file_list << endl;
193                         return 1;
194                 }
195                 num_files--;
196                 file_list++;
197         } else
198                 yyin = stdin;
199         return 0;
200 }