1 /** @file ginsh_lexer.ll
3 * Lexical analyzer definition for ginsh.
4 * This file must be processed with flex.
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
33 #include <readline/readline.h>
34 #include <readline/history.h>
38 #include <ginac/ginac.h>
41 #include "ginsh_parser.h"
43 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
45 // Table of all used symbols
49 static int ginsh_input(char *buf, int max_size);
64 [ \t\n]+ /* skip whitespace */
65 \\$ /* skip line continuations */
66 "#".* /* skip comments starting with "#" */
67 ^"!".* system(yytext + 1); /* execute shell command */
70 Pi yylval = Pi; return T_LITERAL;
71 EulerGamma yylval = EulerGamma; return T_LITERAL;
72 Catalan yylval = Catalan; return T_LITERAL;
73 FAIL yylval = *new fail(); return T_LITERAL;
74 I yylval = I; return T_NUMBER;
75 Digits yylval = (long)Digits; return T_DIGITS;
78 quit|exit return T_QUIT;
82 inventory return T_INVENTORY;
90 ">=" return T_GREATEREQ;
92 /* last 1..3 expressions */
95 \"\"\" return T_QUOTE3;
97 /* matrix delimiters */
98 \[\[ return T_MATRIX_BEGIN;
99 \]\] return T_MATRIX_END;
105 {D}+{E} yylval = numeric(yytext); return T_NUMBER;
109 if (syms.find(yytext) == syms.end())
110 syms[yytext] = *(new symbol(yytext));
111 yylval = syms[yytext];
115 /* everything else */
125 static int line_length = 0;
126 static char *line_read = NULL;
127 static char *line_ptr;
129 // Input function that uses libreadline for interactive input
130 static int ginsh_input(char *buf, int max_size)
133 if (yy_current_buffer->yy_is_interactive) {
136 // Do we need to read a new line?
137 if (line_length == 0) {
143 // Read new line, prompt "> "
144 line_read = line_ptr = readline("> ");
152 // Add non-empty lines to history
153 line_length = strlen(line_read) + 1;
155 add_history(line_read);
157 // Reappend trailing '\n' which is stripped by readline()
158 line_read[line_length - 1] = '\n';
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;
168 } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
169 YY_FATAL_ERROR("input in flex scanner failed");
174 // List of input files to be processed
176 char **file_list = NULL;
178 // EOF encountered, connect to next file. If this was the last file,
179 // connect to stdin. If this was stdin, terminate the scanner.
187 yyin = fopen(*file_list, "r");
189 cerr << "Can't open " << *file_list << endl;