1 /** @file ginsh_lexer.ll
3 * Lexical analyzer definition for ginsh.
4 * This file must be processed with flex. */
7 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
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.
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.
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
33 #include "ginsh_parser.h"
35 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
37 // Table of all used symbols
40 // Type of symbols to generate (real or complex)
41 unsigned symboltype = domain::complex;
44 static int ginsh_input(char *buf, int max_size);
59 [ \t\n]+ /* skip whitespace */
60 \\$ /* skip line continuations */
61 "//".* /* skip comments starting with "//" */
62 ^"#".* /* skip lines starting with "#" */
63 ^"!".* system(yytext + 1); /* execute shell command */
66 Pi yylval = Pi; return T_LITERAL;
67 Euler yylval = Euler; return T_LITERAL;
68 Catalan yylval = Catalan; return T_LITERAL;
69 FAIL yylval = *new fail(); return T_LITERAL;
70 I yylval = I; return T_NUMBER;
71 Digits yylval = (long)Digits; return T_DIGITS;
74 quit|exit return T_QUIT;
75 warranty return T_WARRANTY;
77 iprint return T_IPRINT;
78 print_latex return T_PRINTLATEX;
79 print_csrc return T_PRINTCSRC;
82 inventory return T_INVENTORY;
85 complex_symbols return T_COMPLEX_SYMBOLS;
86 real_symbols return T_REAL_SYMBOLS;
92 ">=" return T_GREATEREQ;
94 /* last 1..3 expressions */
97 \%\%\% return T_QUOTE3;
107 {D}+{E} yylval = numeric(yytext); return T_NUMBER;
111 sym_tab::const_iterator i = syms.find(yytext);
112 if (i == syms.end()) {
113 if (symboltype == domain::complex) {
114 yylval = syms[yytext] = *(new symbol(yytext));
116 yylval = syms[yytext] = *(new symbol(yytext, domain::real));
124 \${D}+ yylval = wild(atoi(yytext + 1)); return T_LITERAL;
126 /* everything else */
136 static int line_length = 0;
137 static char *line_read = NULL;
138 static char *line_ptr;
140 // Input function that uses libreadline for interactive input
141 static int ginsh_input(char *buf, int max_size)
144 #if defined(YY_CURRENT_BUFFER)
145 if (YY_CURRENT_BUFFER->yy_is_interactive) {
147 if (yy_current_buffer->yy_is_interactive) {
149 #ifdef HAVE_LIBREADLINE
150 // Do we need to read a new line?
152 if (line_length == 0) {
158 // Read new line, prompt "> "
159 line_read = line_ptr = readline("> ");
167 // Add non-empty lines to history
168 line_length = strlen(line_read) + 1;
170 add_history(line_read);
172 // Reappend trailing '\n' which is stripped by readline()
173 line_read[line_length - 1] = '\n';
176 // Copy data to lex buffer
177 actual = line_length > max_size ? max_size : line_length;
178 memcpy(buf, line_ptr, actual);
179 line_length -= actual;
183 printf("> "); fflush(stdout);
185 for (n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n)
189 if (c == EOF && ferror(yyin))
190 YY_FATAL_ERROR("input in flex scanner failed");
193 } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
194 YY_FATAL_ERROR("input in flex scanner failed");
199 // List of input files to be processed
201 char **file_list = NULL;
203 // EOF encountered, connect to next file. If this was the last file,
204 // connect to stdin. If this was stdin, terminate the scanner.
212 yyin = fopen(*file_list, "r");
214 cerr << "Can't open " << *file_list << endl;