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