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 gamma yylval = gamma; 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;
70 warranty return T_WARRANTY;
76 inventory return T_INVENTORY;
84 ">=" return T_GREATEREQ;
86 /* last 1..3 expressions */
89 \"\"\" return T_QUOTE3;
91 /* matrix delimiters */
92 \[\[ return T_MATRIX_BEGIN;
93 \]\] return T_MATRIX_END;
99 {D}+{E} yylval = numeric(yytext); return T_NUMBER;
103 if (syms.find(yytext) == syms.end())
104 syms[yytext] = *(new symbol(yytext));
105 yylval = syms[yytext];
109 /* everything else */
119 static int line_length = 0;
120 static char *line_read = NULL;
121 static char *line_ptr;
123 // Input function that uses libreadline for interactive input
124 static int ginsh_input(char *buf, int max_size)
127 if (yy_current_buffer->yy_is_interactive) {
128 #ifdef HAVE_LIBREADLINE
129 // Do we need to read a new line?
131 if (line_length == 0) {
137 // Read new line, prompt "> "
138 line_read = line_ptr = readline("> ");
146 // Add non-empty lines to history
147 line_length = strlen(line_read) + 1;
149 add_history(line_read);
151 // Reappend trailing '\n' which is stripped by readline()
152 line_read[line_length - 1] = '\n';
155 // Copy data to lex buffer
156 actual = line_length > max_size ? max_size : line_length;
157 memcpy(buf, line_ptr, actual);
158 line_length -= actual;
162 printf("> "); fflush(stdout);
164 for (n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n)
168 if (c == EOF && ferror(yyin))
169 YY_FATAL_ERROR("input in flex scanner failed");
172 } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
173 YY_FATAL_ERROR("input in flex scanner failed");
178 // List of input files to be processed
180 char **file_list = NULL;
182 // EOF encountered, connect to next file. If this was the last file,
183 // connect to stdin. If this was stdin, terminate the scanner.
191 yyin = fopen(*file_list, "r");
193 cerr << "Can't open " << *file_list << endl;