1 /** @file ginsh_lexer.ll
3 * Lexical analyzer definition for ginsh.
4 * This file must be processed with flex.
6 * GiNaC Copyright (C) 1999-2000 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
32 #include "ginsh_parser.h"
34 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
36 // Table of all used symbols
40 static int ginsh_input(char *buf, int max_size);
55 [ \t\n]+ /* skip whitespace */
56 \\$ /* skip line continuations */
57 "#".* /* skip comments starting with "#" */
58 ^"!".* system(yytext + 1); /* execute shell command */
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;
69 quit|exit return T_QUIT;
75 inventory return T_INVENTORY;
83 ">=" return T_GREATEREQ;
85 /* last 1..3 expressions */
88 \"\"\" return T_QUOTE3;
90 /* matrix delimiters */
91 \[\[ return T_MATRIX_BEGIN;
92 \]\] return T_MATRIX_END;
98 {D}+{E} yylval = numeric(yytext); return T_NUMBER;
102 if (syms.find(yytext) == syms.end())
103 syms[yytext] = *(new symbol(yytext));
104 yylval = syms[yytext];
108 /* everything else */
118 static int line_length = 0;
119 static char *line_read = NULL;
120 static char *line_ptr;
122 // Input function that uses libreadline for interactive input
123 static int ginsh_input(char *buf, int max_size)
126 if (yy_current_buffer->yy_is_interactive) {
127 #ifdef HAVE_LIBREADLINE
128 // Do we need to read a new line?
130 if (line_length == 0) {
136 // Read new line, prompt "> "
137 line_read = line_ptr = readline("> ");
145 // Add non-empty lines to history
146 line_length = strlen(line_read) + 1;
148 add_history(line_read);
150 // Reappend trailing '\n' which is stripped by readline()
151 line_read[line_length - 1] = '\n';
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;
161 printf("> "); fflush(stdout);
163 for (n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n)
167 if (c == EOF && ferror(yyin))
168 YY_FATAL_ERROR("input in flex scanner failed");
171 } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
172 YY_FATAL_ERROR("input in flex scanner failed");
177 // List of input files to be processed
179 char **file_list = NULL;
181 // EOF encountered, connect to next file. If this was the last file,
182 // connect to stdin. If this was stdin, terminate the scanner.
190 yyin = fopen(*file_list, "r");
192 cerr << "Can't open " << *file_list << endl;