1 /** @file ginsh_lexer.ll
3 * Lexical analyzer definition for ginsh.
4 * This file must be processed with flex. */
7 * GiNaC Copyright (C) 1999-2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "ginsh_parser.h"
37 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
39 // Table of all used symbols
42 // Type of symbols to generate (real or complex)
43 unsigned symboltype = domain::complex;
46 static int ginsh_input(char *buf, int max_size);
61 [ \t\n]+ /* skip whitespace */
62 \\$ /* skip line continuations */
63 "//".* /* skip comments starting with "//" */
64 ^"#".* /* skip lines starting with "#" */
65 ^"!".* system(yytext + 1); /* execute shell command */
68 Pi yylval = Pi; return T_LITERAL;
69 Euler yylval = Euler; return T_LITERAL;
70 Catalan yylval = Catalan; return T_LITERAL;
71 FAIL yylval = *new fail(); return T_LITERAL;
72 I yylval = I; return T_NUMBER;
73 Digits yylval = (long)Digits; return T_DIGITS;
76 quit|exit return T_QUIT;
77 warranty return T_WARRANTY;
79 iprint return T_IPRINT;
80 print_latex return T_PRINTLATEX;
81 print_csrc return T_PRINTCSRC;
84 inventory return T_INVENTORY;
87 complex_symbols return T_COMPLEX_SYMBOLS;
88 real_symbols return T_REAL_SYMBOLS;
94 ">=" return T_GREATEREQ;
96 /* last 1..3 expressions */
99 \%\%\% return T_QUOTE3;
109 {D}+{E} yylval = numeric(yytext); return T_NUMBER;
113 sym_tab::const_iterator i = syms.find(yytext);
114 if (i == syms.end()) {
115 if (symboltype == domain::complex) {
120 realsymbol tmp(yytext);
130 \${D}+ yylval = wild(atoi(yytext + 1)); return T_LITERAL;
132 /* everything else */
142 static int line_length = 0;
143 static char *line_read = NULL;
144 static char *line_ptr;
146 // Input function that uses libreadline for interactive input
147 static int ginsh_input(char *buf, int max_size)
150 #if defined(YY_CURRENT_BUFFER)
151 if (YY_CURRENT_BUFFER->yy_is_interactive) {
153 if (yy_current_buffer->yy_is_interactive) {
155 #ifdef HAVE_LIBREADLINE
156 // Do we need to read a new line?
158 if (line_length == 0) {
164 // Read new line, prompt "> "
165 line_read = line_ptr = readline("> ");
173 // Add non-empty lines to history
174 line_length = strlen(line_read) + 1;
176 add_history(line_read);
178 // Reappend trailing '\n' which is stripped by readline()
179 line_read[line_length - 1] = '\n';
182 // Copy data to lex buffer
183 actual = line_length > max_size ? max_size : line_length;
184 memcpy(buf, line_ptr, actual);
185 line_length -= actual;
189 printf("> "); fflush(stdout);
191 for (n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n)
195 if (c == EOF && ferror(yyin))
196 YY_FATAL_ERROR("input in flex scanner failed");
199 } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
200 YY_FATAL_ERROR("input in flex scanner failed");
205 // List of input files to be processed
207 char **file_list = NULL;
209 // EOF encountered, connect to next file. If this was the last file,
210 // connect to stdin. If this was stdin, terminate the scanner.
218 yyin = fopen(*file_list, "r");
220 cerr << "Can't open " << *file_list << endl;