]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.cpp
[BUGFIX] Fix crash in parser.
[ginac.git] / ginac / parser / parser.cpp
1 /** @file parser.cpp
2  *
3  *  Implementation of GiNaC's parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2024 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 #include "operators.h"
31
32 #include <cstdint> // for uintptr_t
33 #include <sstream>
34 #include <stdexcept>
35
36 namespace GiNaC {
37
38 ex reader_func::operator()(const exvector& args) const
39 {
40         switch (type) {
41         case FUNCTION_PTR:
42                 return func(args);
43         case GINAC_FUNCTION:
44                 return function(serial, args);
45         default:
46                 abort();
47         }
48 }
49
50 /// identifier_expr:  identifier |  identifier '(' expression* ')'
51 ex parser::parse_identifier_expr()
52 {
53         std::string name = scanner->str;
54         get_next_tok();  // eat identifier.
55
56         if (token != '(') // symbol
57                 return find_or_insert_symbol(name, syms, strict);
58
59         // function/ctor call.
60         get_next_tok();  // eat (
61         exvector args;
62         if (token != ')') {
63                 while (true) {
64                         ex e = parse_expression();
65                         args.push_back(e);
66
67                         if (token == ')')
68                                 break;
69
70                         if (token != ',')
71                                 Parse_error("expected ')' or ',' in argument list");
72
73                         get_next_tok();
74                 }
75         }
76         // Eat the ')'.
77         get_next_tok();
78         auto reader = funcs.find({name, args.size()});
79         if (reader == funcs.end()) {
80                 Parse_error_("no function \"" << name << "\" with " <<
81                              args.size() << " arguments");
82         }
83         // reader->second might be a pointer to a C++ function or a specially
84         // crafted serial of a GiNaC::function.
85         return reader->second(args);
86 }
87
88 /// paren_expr:  '(' expression ')'
89 ex parser::parse_paren_expr()
90 {
91         get_next_tok();  // eat (.
92         ex e = parse_expression();
93
94         if (token != ')')
95                 Parse_error("expected ')'");
96         get_next_tok();  // eat ).
97         return e;
98 }
99
100 /// lst_expr:  '{' expression { ',' expression } '}'
101 ex parser::parse_lst_expr()
102 {
103         get_next_tok();  // eat {.
104
105         lst list;
106         if (token != '}') {
107                 while (true) {
108                         ex e = parse_expression(); // expression();
109                         list.append(e);
110
111                         if (token == '}') {
112                                 break;
113                         }
114
115                         if (token != ',') {
116                                 Parse_error("expected '}'");
117                         }
118
119                         get_next_tok();  // eat ','.
120                 }
121         }
122         // Eat the '}'.
123         get_next_tok();
124
125         return list;
126 }
127
128 /// primary: identifier_expr | number_expr | paren_expr | unary_expr 
129 ex parser::parse_primary() 
130 {
131         switch (token) {
132                 case lexer::token_type::identifier:
133                          return parse_identifier_expr();
134                 case lexer::token_type::number:
135                          return parse_number_expr();
136                 case '(': 
137                          return parse_paren_expr();
138                 case '{': 
139                          return parse_lst_expr();
140                 case '-':
141                          return -parse_unary_expr();
142                 case '+':
143                          return parse_unary_expr();
144                 case lexer::token_type::literal:
145                          return parse_literal_expr();
146                 case lexer::token_type::eof:
147                 default:
148                          Parse_error("unexpected token");
149         }
150 }
151
152 /// expression ::= primary binoprhs
153 ex parser::parse_expression() 
154 {
155         ex lhs = parse_primary();
156         ex res = parse_binop_rhs(0, lhs);
157         return res;
158 }
159
160 /// number_expr: number
161 ex parser::parse_number_expr()
162 {
163         ex n = numeric(scanner->str.c_str());
164         get_next_tok(); // consume the number
165         return n;
166 }
167
168 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
169 ex parser::parse_literal_expr()
170 {
171         get_next_tok(); // consume the literal
172         if (scanner->str == "I")
173                 return I;
174         else if (scanner->str == "Pi")
175                 return Pi;
176         else if (scanner->str == "Euler")
177                 return Euler;
178         else if (scanner->str == "Catalan")
179                 return Catalan;
180         bug("unknown literal: \"" << scanner->str << "\"");
181 }
182
183 ex parser::operator()(std::istream& input)
184 {
185         scanner->switch_input(&input);
186         get_next_tok();
187         ex ret = parse_expression();
188         // parse_expression() stops if it encounters an unknown token.
189         // This is not a bug: since the parser is recursive, checking
190         // whether the next token is valid is responsibility of the caller.
191         // Hence make sure nothing is left in the stream:
192         if (token != lexer::token_type::eof)
193                 Parse_error("expected EOF");
194
195         return ret;
196 }
197
198 ex parser::operator()(const std::string& input)
199 {
200         std::istringstream is(input);
201         ex ret = operator()(is);
202         return ret;
203 }
204
205 int parser::get_next_tok()
206 {
207         token = scanner->gettok();
208         return token;
209 }
210
211 parser::parser(const symtab& syms_, const bool strict_,
212                const prototype_table& funcs_) : strict(strict_),
213         funcs(funcs_), syms(syms_)
214 {
215         scanner = new lexer();
216 }
217
218 parser::~parser()
219 {
220         delete scanner;
221 }
222
223 } // namespace GiNaC