3 * Implementation of GiNaC's parser. */
6 * GiNaC Copyright (C) 1999-2024 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "operators.h"
32 #include <cstdint> // for uintptr_t
38 ex reader_func::operator()(const exvector& args) const
44 return function(serial, args);
50 /// identifier_expr: identifier | identifier '(' expression* ')'
51 ex parser::parse_identifier_expr()
53 std::string name = scanner->str;
54 get_next_tok(); // eat identifier.
56 if (token != '(') // symbol
57 return find_or_insert_symbol(name, syms, strict);
59 // function/ctor call.
60 get_next_tok(); // eat (
64 ex e = parse_expression();
71 Parse_error("expected ')' or ',' in argument list");
78 auto reader = funcs.find({name, args.size()});
79 if (reader == funcs.end()) {
80 Parse_error_("no function \"" << name << "\" with " <<
81 args.size() << " arguments");
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);
88 /// paren_expr: '(' expression ')'
89 ex parser::parse_paren_expr()
91 get_next_tok(); // eat (.
92 ex e = parse_expression();
95 Parse_error("expected ')'");
96 get_next_tok(); // eat ).
100 /// lst_expr: '{' expression { ',' expression } '}'
101 ex parser::parse_lst_expr()
103 get_next_tok(); // eat {.
108 ex e = parse_expression(); // expression();
116 Parse_error("expected '}'");
119 get_next_tok(); // eat ','.
128 /// primary: identifier_expr | number_expr | paren_expr | unary_expr
129 ex parser::parse_primary()
132 case lexer::token_type::identifier:
133 return parse_identifier_expr();
134 case lexer::token_type::number:
135 return parse_number_expr();
137 return parse_paren_expr();
139 return parse_lst_expr();
141 return -parse_unary_expr();
143 return parse_unary_expr();
144 case lexer::token_type::literal:
145 return parse_literal_expr();
146 case lexer::token_type::eof:
148 Parse_error("unexpected token");
152 /// expression ::= primary binoprhs
153 ex parser::parse_expression()
155 ex lhs = parse_primary();
156 ex res = parse_binop_rhs(0, lhs);
160 /// number_expr: number
161 ex parser::parse_number_expr()
163 ex n = numeric(scanner->str.c_str());
164 get_next_tok(); // consume the number
168 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
169 ex parser::parse_literal_expr()
171 get_next_tok(); // consume the literal
172 if (scanner->str == "I")
174 else if (scanner->str == "Pi")
176 else if (scanner->str == "Euler")
178 else if (scanner->str == "Catalan")
180 bug("unknown literal: \"" << scanner->str << "\"");
183 ex parser::operator()(std::istream& input)
185 scanner->switch_input(&input);
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");
198 ex parser::operator()(const std::string& input)
200 std::istringstream is(input);
201 ex ret = operator()(is);
205 int parser::get_next_tok()
207 token = scanner->gettok();
211 parser::parser(const symtab& syms_, const bool strict_,
212 const prototype_table& funcs_) : strict(strict_),
213 funcs(funcs_), syms(syms_)
215 scanner = new lexer();