]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.cpp
Allow underscores in identifiers.
[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 #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         ex ret = GiNaC::function((unsigned)reader->second, args);
70         return ret;
71 }
72
73 /// paren_expr:  '(' expression ')'
74 ex parser::parse_paren_expr()
75 {
76         get_next_tok();  // eat (.
77         ex e = parse_expression();
78
79         if (token != ')')
80                 Parse_error("expected ')'");
81         get_next_tok();  // eat ).
82         return e;
83 }
84
85 extern numeric* _num_1_p;
86 extern ex _ex0;
87
88 /// unary_expr: [+-] expression
89 ex parser::parse_unary_expr()
90 {
91         // Unlike most other parse_* method this one does NOT consume
92         // current token so parse_binop_rhs() knows what kind of operator
93         // is being parsed.
94         
95         // There are different kinds of expressions which need to be handled:
96         // -a+b 
97         // -(a) 
98         // +a
99         // +(a)
100         // Delegete the work to parse_binop_rhs(), otherwise we end up
101         // duplicating it here. 
102         ex lhs = _ex0; // silly trick
103         ex e = parse_binop_rhs(0, lhs);
104         return e;
105 }
106
107 /// primary: identifier_expr | number_expr | paren_expr | unary_expr 
108 ex parser::parse_primary() 
109 {
110         switch (token) {
111                 case lexer::token_type::identifier:
112                          return parse_identifier_expr();
113                 case lexer::token_type::number:
114                          return parse_number_expr();
115                 case '(': 
116                          return parse_paren_expr();
117                 case '-':
118                 case '+':
119                          return parse_unary_expr();
120                 case lexer::token_type::literal:
121                          return parse_literal_expr();
122                 case lexer::token_type::eof:
123                 default:
124                          Parse_error("unexpected token");
125         }
126 }
127
128 /// expression ::= primary binoprhs
129 ex parser::parse_expression() 
130 {
131         ex lhs = parse_primary();
132         ex res = parse_binop_rhs(0, lhs);
133         return res;
134 }
135
136 /// number_expr: number
137 ex parser::parse_number_expr()
138 {
139         ex n = numeric(scanner->str.c_str());
140         get_next_tok(); // consume the number
141         return n;
142 }
143
144 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
145 ex parser::parse_literal_expr()
146 {
147         get_next_tok(); // consume the literal
148         if (scanner->str == "I")
149                 return I;
150         else if (scanner->str == "Pi")
151                 return Pi;
152         else if (scanner->str == "Euler")
153                 return Euler;
154         else if (scanner->str == "Catalan")
155                 return Catalan;
156         bug("unknown literal: \"" << scanner->str << "\"");
157 }
158
159 ex parser::operator()(std::istream& input)
160 {
161         scanner->switch_input(&input);
162         get_next_tok();
163         ex ret = parse_expression();
164         // parse_expression() stops if it encounters an unknown token.
165         // This is not a bug: since the parser is recursive checking
166         // whether the next token is valid is responsibility of the caller.
167         // Hence make sure nothing is left in the stream:
168         if (token != lexer::token_type::eof)
169                 Parse_error("expected EOF");
170
171         return ret;
172 }
173
174 ex parser::operator()(const std::string& input)
175 {
176         std::istringstream is(input);
177         ex ret = operator()(is);
178         return ret;
179 }
180
181 int parser::get_next_tok()
182 {
183         token = scanner->gettok();
184         return token;
185 }
186
187 parser::parser(const symtab& syms_, const bool strict_,
188                const prototype_table& funcs_) : strict(strict_),
189         funcs(funcs_), syms(syms_)
190 {
191         scanner = new lexer();
192 }
193
194 parser::~parser()
195 {
196         delete scanner;
197 }
198
199 } // namespace GiNaC