]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.cpp
78a03f81df0401f9f220f4955c276f0c036e5240
[ginac.git] / ginac / parser / parser.cpp
1 /** @file parser.cpp
2  *
3  *  Implementation of GiNaC's parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2010 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 "lexer.h"
25 #include "debug.h"
26 #include "mul.h"
27 #include "constant.h"
28 #include "function.h"
29
30 #include <sstream>
31 #include <stdexcept>
32
33 namespace GiNaC {
34
35 /// identifier_expr:  identifier |  identifier '(' expression* ')'
36 ex parser::parse_identifier_expr()
37 {
38         std::string name = scanner->str;
39         get_next_tok();  // eat identifier.
40
41         if (token != '(') // symbol
42                 return find_or_insert_symbol(name, syms, strict);
43
44         // function/ctor call.
45         get_next_tok();  // eat (
46         exvector args;
47         if (token != ')') {
48                 while (true) {
49                         ex e = parse_expression();
50                         args.push_back(e);
51
52                         if (token == ')')
53                                 break;
54
55                         if (token != ',')
56                                 Parse_error("expected ')' or ',' in argument list");
57
58                         get_next_tok();
59                 }
60         }
61         // Eat the ')'.
62         get_next_tok();
63         prototype the_prototype = make_pair(name, args.size());
64         prototype_table::const_iterator reader = funcs.find(the_prototype);
65         if (reader == funcs.end()) {
66                 Parse_error_("no function \"" << name << "\" with " <<
67                              args.size() << " arguments");
68         }
69         // dirty hack to distinguish between serial numbers of functions and real
70         // pointers.
71         GiNaC::function* f = NULL;
72         try {
73                 unsigned serial = (unsigned)(unsigned long)(void *)(reader->second);
74                 f = new GiNaC::function(serial, args);
75         }
76         catch ( std::runtime_error ) {
77                 if ( f ) delete f;
78                 ex ret = reader->second(args);
79                 return ret;
80         }
81         return f->setflag(status_flags::dynallocated);
82 }
83
84 /// paren_expr:  '(' expression ')'
85 ex parser::parse_paren_expr()
86 {
87         get_next_tok();  // eat (.
88         ex e = parse_expression();
89
90         if (token != ')')
91                 Parse_error("expected ')'");
92         get_next_tok();  // eat ).
93         return e;
94 }
95
96 extern numeric* _num_1_p;
97 extern ex _ex0;
98
99 /// unary_expr: [+-] expression
100 ex parser::parse_unary_expr()
101 {
102         // Unlike most other parse_* method this one does NOT consume
103         // current token so parse_binop_rhs() knows what kind of operator
104         // is being parsed.
105         
106         // There are different kinds of expressions which need to be handled:
107         // -a+b 
108         // -(a) 
109         // +a
110         // +(a)
111         // Delegete the work to parse_binop_rhs(), otherwise we end up
112         // duplicating it here. 
113         ex lhs = _ex0; // silly trick
114         ex e = parse_binop_rhs(0, lhs);
115         return e;
116 }
117
118 /// primary: identifier_expr | number_expr | paren_expr | unary_expr 
119 ex parser::parse_primary() 
120 {
121         switch (token) {
122                 case lexer::token_type::identifier:
123                          return parse_identifier_expr();
124                 case lexer::token_type::number:
125                          return parse_number_expr();
126                 case '(': 
127                          return parse_paren_expr();
128                 case '-':
129                 case '+':
130                          return parse_unary_expr();
131                 case lexer::token_type::literal:
132                          return parse_literal_expr();
133                 case lexer::token_type::eof:
134                 default:
135                          Parse_error("unexpected token");
136         }
137 }
138
139 /// expression ::= primary binoprhs
140 ex parser::parse_expression() 
141 {
142         ex lhs = parse_primary();
143         ex res = parse_binop_rhs(0, lhs);
144         return res;
145 }
146
147 /// number_expr: number
148 ex parser::parse_number_expr()
149 {
150         ex n = numeric(scanner->str.c_str());
151         get_next_tok(); // consume the number
152         return n;
153 }
154
155 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
156 ex parser::parse_literal_expr()
157 {
158         get_next_tok(); // consume the literal
159         if (scanner->str == "I")
160                 return I;
161         else if (scanner->str == "Pi")
162                 return Pi;
163         else if (scanner->str == "Euler")
164                 return Euler;
165         else if (scanner->str == "Catalan")
166                 return Catalan;
167         bug("unknown literal: \"" << scanner->str << "\"");
168 }
169
170 ex parser::operator()(std::istream& input)
171 {
172         scanner->switch_input(&input);
173         get_next_tok();
174         ex ret = parse_expression();
175         // parse_expression() stops if it encounters an unknown token.
176         // This is not a bug: since the parser is recursive checking
177         // whether the next token is valid is responsibility of the caller.
178         // Hence make sure nothing is left in the stream:
179         if (token != lexer::token_type::eof)
180                 Parse_error("expected EOF");
181
182         return ret;
183 }
184
185 ex parser::operator()(const std::string& input)
186 {
187         std::istringstream is(input);
188         ex ret = operator()(is);
189         return ret;
190 }
191
192 int parser::get_next_tok()
193 {
194         token = scanner->gettok();
195         return token;
196 }
197
198 parser::parser(const symtab& syms_, const bool strict_,
199                const prototype_table& funcs_) : strict(strict_),
200         funcs(funcs_), syms(syms_)
201 {
202         scanner = new lexer();
203 }
204
205 parser::~parser()
206 {
207         delete scanner;
208 }
209
210 } // namespace GiNaC