]> www.ginac.de Git - ginac.git/blob - ginac/parser/parser.h
Prettified source code.
[ginac.git] / ginac / parser / parser.h
1 /** @file parser.h
2  *
3  *  Interface to the 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 #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         /// number_expr: number
71         ex parse_number_expr();
72
73         /// unary_expr: [+-] expression
74         ex parse_unary_expr();
75
76         /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
77         ex parse_literal_expr();
78
79 public:
80         /**
81          * @param syms_ symbol table.
82          * @param funcs_ function/ctors table.
83          * @param strict_ if true, throw an exception if unknown
84          *        symbol is encountered.
85          */
86         parser(const symtab& syms_ = symtab(),
87                 const bool strict_ = false,
88                 const prototype_table& funcs_ = get_default_reader());
89         ~parser();
90
91         /// parse the stream @a input
92         ex operator()(std::istream& input);
93         /// parse the string @a input
94         ex operator()(const std::string& input);
95
96         /// report the symbol table used by parser
97         symtab get_syms() const 
98         { 
99                 return syms; 
100         }
101         /// read/write access to the symbol table
102         symtab& get_syms()
103         {
104                 return syms;
105         }
106
107         /// If true, throw an exception if an unknown symbol is encountered.
108         bool strict;
109 private:
110         /**
111          * Function/ctor table, maps a prototype (which is a name and number
112          * arguments) to a C++ function. Used for parsing identifier_expr's
113          * (see parse_identifier_expr). If expression contains unknown
114          * prototype, an exception will be thrown.
115          */
116         const prototype_table funcs;
117         /**
118          * Symbol (variable) table. Used for parsing identifier_expr's
119          * (see parse_identifier_expr). If parser is strict, exception is
120          * thrown if an unknown symbol is encountered. Non-strict parser
121          * appends unknown symbols to the symbol table.
122          */
123         symtab syms;
124         /// token scanner
125         lexer* scanner;
126         /// current token the parser is looking at
127         int token;
128         /// read the next token from the scanner
129         int get_next_tok();
130 };
131
132 } // namespace GiNaC
133
134 #endif // ndef GINAC_PARSER_H