Faster, better (recursive descent) expression parser.
[ginac.git] / ginac / parser / parser.cpp
1 #include <stdexcept>
2 #include <sstream>
3 #include "parser.hpp"
4 #include "lexer.hpp"
5 #include "debug.hpp"
6 #include "mul.h"
7 #include "constant.h"
8
9 namespace GiNaC 
10 {
11
12 /// identifier_expr:  identifier |  identifier '(' expression* ')'
13 ex parser::parse_identifier_expr()
14 {
15         std::string name = scanner->str;
16         get_next_tok();  // eat identifier.
17
18         if (token != '(') // symbol
19                 return find_or_insert_symbol(name, syms, strict);
20
21         // function/ctor call.
22         get_next_tok();  // eat (
23         exvector args;
24         if (token != ')') {
25                 while (true) {
26                         ex e = parse_expression();
27                         args.push_back(e);
28
29                         if (token == ')')
30                                 break;
31
32                         if (token != ',')
33                                 throw std::invalid_argument("Expected ')' or ',' in argument list");
34
35                         get_next_tok();
36                 }
37         }
38         // Eat the ')'.
39         get_next_tok();
40         prototype the_prototype = make_pair(name, args.size());
41         prototype_table::const_iterator reader = funcs.find(the_prototype);
42         if (reader == funcs.end()) {
43                 bail_out(std::invalid_argument,
44                         "no function \"" << name << "\" with " << args.size()
45                         << " arguments");
46         }
47         ex ret = reader->second(args);
48         return ret;
49 }
50
51 /// paren_expr:  '(' expression ')'
52 ex parser::parse_paren_expr()
53 {
54         get_next_tok();  // eat (.
55         ex e = parse_expression();
56
57         if (token != ')')
58                 throw std::invalid_argument("parser::parse_paren_expr: expected ')'");
59         get_next_tok();  // eat ).
60         return e;
61 }
62
63 extern numeric* _num_1_p;
64
65 /// unary_expr: [+-] expression
66 ex parser::parse_unary_expr(const int s)
67 {
68         // consume '-' (or '+')
69         get_next_tok();
70         ex v = parse_expression();
71         switch (s) {
72                 case '-':
73                         return (new mul(v, *_num_1_p))->setflag(status_flags::dynallocated);
74                 case '+':
75                         return v;
76                 default:
77                         throw std::invalid_argument(
78                                         std::string(__func__)
79                                         + ": invalid unary operator \""
80                                         + char(s) + "\"");
81         }
82 }
83
84 /// primary: identifier_expr | number_expr | paren_expr | unary_expr 
85 ex parser::parse_primary() 
86 {
87         switch (token) {
88                 case lexer::token_type::identifier:
89                          return parse_identifier_expr();
90                 case lexer::token_type::number:
91                          return parse_number_expr();
92                 case '(': 
93                          return parse_paren_expr();
94                 case '-':
95                          return parse_unary_expr('-');
96                 case '+':
97                          return parse_unary_expr('+');
98                 case lexer::token_type::literal:
99                          return parse_literal_expr();
100                 case lexer::token_type::eof:
101                          bail_out(std::invalid_argument, "got EOF while parsing the expression");
102                 default:
103                          bail_out(std::invalid_argument, "unknown token " <<
104                                 token << " (\"" << 
105                                 (token ? std::string("") + char(token) : "NULL")
106                                 << "\")");
107         }
108 }
109
110 /// expression ::= primary binoprhs
111 ex parser::parse_expression() 
112 {
113         ex lhs = parse_primary();
114         ex res = parse_binop_rhs(0, lhs);
115         return res;
116 }
117
118 /// number_expr: number
119 ex parser::parse_number_expr()
120 {
121         ex n = numeric(scanner->str.c_str());
122         get_next_tok(); // consume the number
123         return n;
124 }
125
126 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
127 ex parser::parse_literal_expr()
128 {
129         if (scanner->str == "I")
130                 return I;
131         else if (scanner->str == "Pi")
132                 return Pi;
133         else if (scanner->str == "Euler")
134                 return Euler;
135         else if (scanner->str == "Catalan")
136                 return Catalan;
137         bug("unknown literal: \"" << scanner->str << "\"");
138 }
139
140 ex parser::operator()(std::istream& input)
141 {
142         scanner->switch_input(&input);
143         get_next_tok();
144         ex ret = parse_expression();
145         return ret;
146 }
147
148 ex parser::operator()(const std::string& input)
149 {
150         std::istringstream is(input);
151         ex ret = operator()(is);
152         return ret;
153 }
154
155 int parser::get_next_tok()
156 {
157         token = scanner->gettok();
158         return token;
159 }
160
161 parser::parser(const symtab& syms_, const prototype_table& funcs_,
162                const bool strict_) : strict(strict_), funcs(funcs_),
163         syms(syms_)
164 {
165         scanner = new lexer();
166 }
167
168 parser::~parser()
169 {
170         delete scanner;
171 }
172
173 } // namespace GiNaC