3 * Implementation of GiNaC's parser. */
6 * GiNaC Copyright (C) 1999-2011 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
34 #include <stdint.h> // for uintptr_t
42 // Find out if ptr is a pointer to a function or a specially crafted integer.
43 // It's possible to distinguish between these because functions are aligned.
44 // Returns true if ptr is a pointer and false otherwise.
45 static bool decode_serial(unsigned& serial, const reader_func ptr)
47 uintptr_t u = (uintptr_t)(void *)ptr;
56 // Figures out if ptr is a pointer to function or a serial of GiNaC function.
57 // In the former case calls that function, in the latter case constructs
58 // GiNaC function with corresponding serial and arguments.
59 static ex dispatch_reader_fcn(const reader_func ptr, const exvector& args)
61 unsigned serial = 0; // dear gcc, could you please shut up?
62 bool is_ptr = decode_serial(serial, ptr);
66 return function(serial, args);
71 /// identifier_expr: identifier | identifier '(' expression* ')'
72 ex parser::parse_identifier_expr()
74 std::string name = scanner->str;
75 get_next_tok(); // eat identifier.
77 if (token != '(') // symbol
78 return find_or_insert_symbol(name, syms, strict);
80 // function/ctor call.
81 get_next_tok(); // eat (
85 ex e = parse_expression();
92 Parse_error("expected ')' or ',' in argument list");
99 prototype the_prototype = make_pair(name, args.size());
100 prototype_table::const_iterator reader = funcs.find(the_prototype);
101 if (reader == funcs.end()) {
102 Parse_error_("no function \"" << name << "\" with " <<
103 args.size() << " arguments");
105 // reader->second might be a pointer to a C++ function or a specially
106 // crafted serial of a GiNaC::function.
107 ex ret = dispatch_reader_fcn(reader->second, args);
111 /// paren_expr: '(' expression ')'
112 ex parser::parse_paren_expr()
114 get_next_tok(); // eat (.
115 ex e = parse_expression();
118 Parse_error("expected ')'");
119 get_next_tok(); // eat ).
123 /// lst_expr: '{' expression { ',' expression } '}'
124 ex parser::parse_lst_expr()
126 get_next_tok(); // eat {.
131 ex e = parse_expression(); // expression();
139 Parse_error("expected '}'");
142 get_next_tok(); // eat ','.
151 extern const ex _ex0;
153 /// unary_expr: [+-] expression
154 ex parser::parse_unary_expr()
156 // Unlike most other parse_* method this one does NOT consume
157 // current token so parse_binop_rhs() knows what kind of operator
160 // There are different kinds of expressions which need to be handled:
165 // Delegete the work to parse_binop_rhs(), otherwise we end up
166 // duplicating it here.
167 ex lhs = _ex0; // silly trick
168 ex e = parse_binop_rhs(0, lhs);
172 /// primary: identifier_expr | number_expr | paren_expr | unary_expr
173 ex parser::parse_primary()
176 case lexer::token_type::identifier:
177 return parse_identifier_expr();
178 case lexer::token_type::number:
179 return parse_number_expr();
181 return parse_paren_expr();
183 return parse_lst_expr();
186 return parse_unary_expr();
187 case lexer::token_type::literal:
188 return parse_literal_expr();
189 case lexer::token_type::eof:
191 Parse_error("unexpected token");
195 /// expression ::= primary binoprhs
196 ex parser::parse_expression()
198 ex lhs = parse_primary();
199 ex res = parse_binop_rhs(0, lhs);
203 /// number_expr: number
204 ex parser::parse_number_expr()
206 ex n = numeric(scanner->str.c_str());
207 get_next_tok(); // consume the number
211 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
212 ex parser::parse_literal_expr()
214 get_next_tok(); // consume the literal
215 if (scanner->str == "I")
217 else if (scanner->str == "Pi")
219 else if (scanner->str == "Euler")
221 else if (scanner->str == "Catalan")
223 bug("unknown literal: \"" << scanner->str << "\"");
226 ex parser::operator()(std::istream& input)
228 scanner->switch_input(&input);
230 ex ret = parse_expression();
231 // parse_expression() stops if it encounters an unknown token.
232 // This is not a bug: since the parser is recursive checking
233 // whether the next token is valid is responsibility of the caller.
234 // Hence make sure nothing is left in the stream:
235 if (token != lexer::token_type::eof)
236 Parse_error("expected EOF");
241 ex parser::operator()(const std::string& input)
243 std::istringstream is(input);
244 ex ret = operator()(is);
248 int parser::get_next_tok()
250 token = scanner->gettok();
254 parser::parser(const symtab& syms_, const bool strict_,
255 const prototype_table& funcs_) : strict(strict_),
256 funcs(funcs_), syms(syms_)
258 scanner = new lexer();