]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.h
cab02c39b679a0457e29fd49db9f551d5d1f6078
[ginac.git] / ginac / parser / parser.h
1 /** @file parser.h
2  *
3  *  Interface to the parser. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2011 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 #ifndef GINAC_PARSER_H
24 #define GINAC_PARSER_H
25
26 #include "parse_context.h"
27 #include "ex.h"
28
29 #include <stdexcept>
30
31 namespace GiNaC {
32
33 class lexer;
34
35 class parse_error : public std::invalid_argument
36 {
37 public:
38         const std::size_t line;
39         const std::size_t column;
40         parse_error(const std::string& what_,
41                     const std::size_t line_ = 0,
42                     const std::size_t column_ = 0) throw () :
43                 std::invalid_argument(what_), line(line_), column(column_)
44         { }
45 };
46
47 /**
48  * Recursive descent parser for GiNaC expressions.
49  */
50 class parser
51 {
52         // The actual parser rules (in EBNF-alike notation):
53
54         /// expression: primary binoprhs
55         ex parse_expression();
56
57         /// primary: indentifier_expr | number_expr | paren_expr | unary_expr
58         ex parse_primary();
59
60         /// binoprhs: ([+*/^-] primary)*
61         ex parse_binop_rhs(int, ex&);
62
63         /// identifier_expr: identifier |
64         ///                  identifier '(' expression (',' expression)* ')'
65         ex parse_identifier_expr();
66
67         /// paren_expr: '(' expression ')'
68         ex parse_paren_expr();
69
70         /// lst_expr: '{' expression { ',' expression } '}'
71         ex parse_lst_expr();
72
73         /// number_expr: number
74         ex parse_number_expr();
75
76         /// unary_expr: [+-] expression
77         ex parse_unary_expr();
78
79         /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
80         ex parse_literal_expr();
81
82 public:
83         /**
84          * @param syms_ symbol table.
85          * @param funcs_ function/ctors table.
86          * @param strict_ if true, throw an exception if unknown
87          *        symbol is encountered.
88          */
89         parser(const symtab& syms_ = symtab(),
90                 const bool strict_ = false,
91                 const prototype_table& funcs_ = get_default_reader());
92         ~parser();
93
94         /// parse the stream @a input
95         ex operator()(std::istream& input);
96         /// parse the string @a input
97         ex operator()(const std::string& input);
98
99         /// report the symbol table used by parser
100         symtab get_syms() const 
101         { 
102                 return syms; 
103         }
104         /// read/write access to the symbol table
105         symtab& get_syms()
106         {
107                 return syms;
108         }
109
110         /// If true, throw an exception if an unknown symbol is encountered.
111         bool strict;
112 private:
113         /**
114          * Function/ctor table, maps a prototype (which is a name and number
115          * arguments) to a C++ function. Used for parsing identifier_expr's
116          * (see parse_identifier_expr). If expression contains unknown
117          * prototype, an exception will be thrown.
118          */
119         const prototype_table funcs;
120         /**
121          * Symbol (variable) table. Used for parsing identifier_expr's
122          * (see parse_identifier_expr). If parser is strict, exception is
123          * thrown if an unknown symbol is encountered. Non-strict parser
124          * appends unknown symbols to the symbol table.
125          */
126         symtab syms;
127         /// token scanner
128         lexer* scanner;
129         /// current token the parser is looking at
130         int token;
131         /// read the next token from the scanner
132         int get_next_tok();
133 };
134
135 } // namespace GiNaC
136
137 #endif // ndef GINAC_PARSER_H