2 * ginsh.l - GiNaC Interactive Shell, lexical analyzer definition
4 * This file must be processed with flex
17 #include <readline/readline.h>
18 #include <readline/history.h>
22 #include <ginac/ginac.h>
25 #include "ginsh_parser.h"
27 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
29 // Table of all used symbols
33 static int ginsh_input(char *buf, int max_size);
48 [ \t\n]+ /* skip whitespace */
49 \\$ /* skip line continuations */
50 "#".* /* skip comments starting with "#" */
51 ^"!".* system(yytext + 1); /* execute shell command */
54 Pi yylval = Pi; return T_LITERAL;
55 EulerGamma yylval = EulerGamma; return T_LITERAL;
56 Catalan yylval = Catalan; return T_LITERAL;
57 FAIL yylval = *new fail(); return T_LITERAL;
58 I yylval = I; return T_NUMBER;
59 Digits yylval = (long)Digits; return T_DIGITS;
62 quit|exit return T_QUIT;
66 inventory return T_INVENTORY;
74 ">=" return T_GREATEREQ;
76 /* last 1..3 expressions */
79 \"\"\" return T_QUOTE3;
81 /* matrix delimiters */
82 \[\[ return T_MATRIX_BEGIN;
83 \]\] return T_MATRIX_END;
89 {D}+{E} yylval = numeric(yytext); return T_NUMBER;
93 if (syms.find(yytext) == syms.end())
94 syms[yytext] = *(new symbol(yytext));
95 yylval = syms[yytext];
109 static int line_length = 0;
110 static char *line_read = NULL;
111 static char *line_ptr;
113 // Input function that uses libreadline for interactive input
114 static int ginsh_input(char *buf, int max_size)
117 if (yy_current_buffer->yy_is_interactive) {
120 // Do we need to read a new line?
121 if (line_length == 0) {
127 // Read new line, prompt "> "
128 line_read = line_ptr = readline("> ");
136 // Add non-empty lines to history
137 line_length = strlen(line_read) + 1;
139 add_history(line_read);
141 // Reappend trailing '\n' which is stripped by readline()
142 line_read[line_length - 1] = '\n';
145 // Copy data to lex buffer
146 actual = line_length > max_size ? max_size : line_length;
147 memcpy(buf, line_ptr, actual);
148 line_length -= actual;
152 } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
153 YY_FATAL_ERROR("input in flex scanner failed");
158 // Scanner terminates on EOF