]> www.ginac.de Git - ginac.git/blob - ginac/parser/parse_context.h
Added get_builtin_reader() that parses only the builtin GiNaC functions
[ginac.git] / ginac / parser / parse_context.h
1 /** @file parse_context.h
2  *
3  *  Interface to parser context. */
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_PARSE_CONTEXT_H
24 #define GINAC_PARSE_CONTEXT_H
25
26 #include "ex.h"
27 #include "symbol.h"
28
29 #include <cstddef> // for size_t
30 #include <map>
31 #include <string>
32 #include <utility>
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #ifdef HAVE_STDINT_H
37 #include <stdint.h> // for uintptr_t
38 #endif
39
40 namespace GiNaC {
41
42 /**
43  * Establishes correspondence between the strings and expressions.
44  * The parser will create missing symbols (if not instructed otherwise,
45  * in which case it fails if the expression contains unknown symbols).
46  */
47 typedef std::map<std::string, ex> symtab;
48
49 /**
50  * Find the symbol with the @a name in the symbol table @a syms.
51  *
52  * If symbol is missing and @a strict = false, insert it, otherwise
53  * throw an exception.
54  */
55 extern symbol
56 find_or_insert_symbol(const std::string& name, symtab& syms,
57                       const bool strict);
58
59 /**
60  * Function (or class ctor) prototype
61  * .first is  the name of function(or ctor),
62  * .second is the number of arguments (each of type ex)
63  */
64 typedef std::pair<std::string, std::size_t> prototype;
65
66 /**
67  * A (C++) function for reading functions and classes from the stream.
68  *
69  * The parser uses (an associative array of) such functions to construct
70  * (GiNaC) classes and functions from a sequence of characters.
71  */
72 typedef ex (*reader_func)(const exvector& args);
73
74 /**
75  * Prototype table.
76  *
77  * If parser sees an expression which looks like a function call (e.g.
78  * foo(x+y, z^2, t)), it looks up such a table to find out which
79  * function (or class) corresponds to the given name and has the given
80  * number of the arguments.
81  *
82  * N.B.
83  *
84  * 1. The function don't have to return a (GiNaC) function or class, it
85  *    can return any expression.
86  * 2. Overloaded functions/ctors are paritally supported, i.e. there might
87  *    be several functions with the same name, but they should take different
88  *    number of arguments.
89  * 3. User can extend the parser via custom prototype tables. It's possible
90  *    to read user defined classes, create abbreviations, etc.
91  *
92  * NOTE: due to a hack that allows user defined functions to be parsed, the map
93  *       value of type reader_func is internally treated as an unsigned and not as a
94  *       function pointer!! The unsigned has to correspond to the serial number of
95  *       the defined GiNaC function.
96  */
97 typedef std::map<prototype, reader_func> prototype_table;
98
99 /**
100  * Default prototype table.
101  *
102  * It supports all defined GiNaC functions and "pow", "sqrt", and "power".
103  */
104 extern const prototype_table& get_default_reader();
105 /**
106  * Builtin prototype table.
107  *
108  * It supports only the builtin GiNaC functions and "pow", "sqrt", and "power".
109  */
110 extern const prototype_table& get_builtin_reader();
111
112 } // namespace GiNaC
113
114 #endif // GINAC_PARSE_CONTEXT_H