]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.cpp
Finalize 1.8.2 release.
[ginac.git] / ginac / parser / parser.cpp
1 /** @file parser.cpp
2  *
3  *  Implementation of GiNaC's parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2022 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "parser.h"
24 #include "lst.h"
25 #include "lexer.h"
26 #include "debug.h"
27 #include "mul.h"
28 #include "constant.h"
29 #include "function.h"
30
31 #include <cstdint> // for uintptr_t
32 #include <sstream>
33 #include <stdexcept>
34
35 namespace GiNaC {
36
37 // <KLUDGE>
38 // Find out if ptr is a pointer to a function or a specially crafted integer.
39 // It's possible to distinguish between these because functions are aligned.
40 // Returns true if ptr is a pointer and false otherwise.
41 static bool decode_serial(unsigned& serial, const reader_func ptr)
42 {
43         uintptr_t u = (uintptr_t)(void *)ptr;
44         if (u & 1) {
45                 u >>= 1;
46                 serial = (unsigned)u;
47                 return false;
48         }
49         return true;
50 }
51
52 // Figures out if ptr is a pointer to function or a serial of GiNaC function.
53 // In the former case calls that function, in the latter case constructs
54 // GiNaC function with corresponding serial and arguments.
55 static ex dispatch_reader_fcn(const reader_func ptr, const exvector& args)
56 {
57         unsigned serial = 0; // dear gcc, could you please shut up?
58         bool is_ptr = decode_serial(serial, ptr);
59         if (is_ptr)
60                 return ptr(args);
61         else
62                 return function(serial, args);
63 }
64 // </KLUDGE>
65
66
67 /// identifier_expr:  identifier |  identifier '(' expression* ')'
68 ex parser::parse_identifier_expr()
69 {
70         std::string name = scanner->str;
71         get_next_tok();  // eat identifier.
72
73         if (token != '(') // symbol
74                 return find_or_insert_symbol(name, syms, strict);
75
76         // function/ctor call.
77         get_next_tok();  // eat (
78         exvector args;
79         if (token != ')') {
80                 while (true) {
81                         ex e = parse_expression();
82                         args.push_back(e);
83
84                         if (token == ')')
85                                 break;
86
87                         if (token != ',')
88                                 Parse_error("expected ')' or ',' in argument list");
89
90                         get_next_tok();
91                 }
92         }
93         // Eat the ')'.
94         get_next_tok();
95         prototype the_prototype = make_pair(name, args.size());
96         auto reader = funcs.find(the_prototype);
97         if (reader == funcs.end()) {
98                 Parse_error_("no function \"" << name << "\" with " <<
99                              args.size() << " arguments");
100         }
101         // reader->second might be a pointer to a C++ function or a specially
102         // crafted serial of a GiNaC::function.
103         ex ret = dispatch_reader_fcn(reader->second, args);
104         return ret;
105 }
106
107 /// paren_expr:  '(' expression ')'
108 ex parser::parse_paren_expr()
109 {
110         get_next_tok();  // eat (.
111         ex e = parse_expression();
112
113         if (token != ')')
114                 Parse_error("expected ')'");
115         get_next_tok();  // eat ).
116         return e;
117 }
118
119 /// lst_expr:  '{' expression { ',' expression } '}'
120 ex parser::parse_lst_expr()
121 {
122         get_next_tok();  // eat {.
123
124         lst list;
125         if (token != '}') {
126                 while (true) {
127                         ex e = parse_expression(); // expression();
128                         list.append(e);
129
130                         if (token == '}') {
131                                 break;
132                         }
133
134                         if (token != ',') {
135                                 Parse_error("expected '}'");
136                         }
137
138                         get_next_tok();  // eat ','.
139                 }
140         }
141         // Eat the '}'.
142         get_next_tok();
143
144         return list;
145 }
146
147 extern const ex _ex0;
148
149 /// unary_expr: [+-] expression
150 ex parser::parse_unary_expr()
151 {
152         // Unlike most other parse_* method this one does NOT consume
153         // current token so parse_binop_rhs() knows what kind of operator
154         // is being parsed.
155         
156         // There are different kinds of expressions which need to be handled:
157         // -a+b 
158         // -(a) 
159         // +a
160         // +(a)
161         // Delegate the work to parse_binop_rhs(), otherwise we end up
162         // duplicating it here. 
163         ex lhs = _ex0; // silly trick
164         ex e = parse_binop_rhs(0, lhs);
165         return e;
166 }
167
168 /// primary: identifier_expr | number_expr | paren_expr | unary_expr 
169 ex parser::parse_primary() 
170 {
171         switch (token) {
172                 case lexer::token_type::identifier:
173                          return parse_identifier_expr();
174                 case lexer::token_type::number:
175                          return parse_number_expr();
176                 case '(': 
177                          return parse_paren_expr();
178                 case '{': 
179                          return parse_lst_expr();
180                 case '-':
181                 case '+':
182                          return parse_unary_expr();
183                 case lexer::token_type::literal:
184                          return parse_literal_expr();
185                 case lexer::token_type::eof:
186                 default:
187                          Parse_error("unexpected token");
188         }
189 }
190
191 /// expression ::= primary binoprhs
192 ex parser::parse_expression() 
193 {
194         ex lhs = parse_primary();
195         ex res = parse_binop_rhs(0, lhs);
196         return res;
197 }
198
199 /// number_expr: number
200 ex parser::parse_number_expr()
201 {
202         ex n = numeric(scanner->str.c_str());
203         get_next_tok(); // consume the number
204         return n;
205 }
206
207 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
208 ex parser::parse_literal_expr()
209 {
210         get_next_tok(); // consume the literal
211         if (scanner->str == "I")
212                 return I;
213         else if (scanner->str == "Pi")
214                 return Pi;
215         else if (scanner->str == "Euler")
216                 return Euler;
217         else if (scanner->str == "Catalan")
218                 return Catalan;
219         bug("unknown literal: \"" << scanner->str << "\"");
220 }
221
222 ex parser::operator()(std::istream& input)
223 {
224         scanner->switch_input(&input);
225         get_next_tok();
226         ex ret = parse_expression();
227         // parse_expression() stops if it encounters an unknown token.
228         // This is not a bug: since the parser is recursive checking
229         // whether the next token is valid is responsibility of the caller.
230         // Hence make sure nothing is left in the stream:
231         if (token != lexer::token_type::eof)
232                 Parse_error("expected EOF");
233
234         return ret;
235 }
236
237 ex parser::operator()(const std::string& input)
238 {
239         std::istringstream is(input);
240         ex ret = operator()(is);
241         return ret;
242 }
243
244 int parser::get_next_tok()
245 {
246         token = scanner->gettok();
247         return token;
248 }
249
250 parser::parser(const symtab& syms_, const bool strict_,
251                const prototype_table& funcs_) : strict(strict_),
252         funcs(funcs_), syms(syms_)
253 {
254         scanner = new lexer();
255 }
256
257 parser::~parser()
258 {
259         delete scanner;
260 }
261
262 } // namespace GiNaC