]> www.ginac.de Git - ginac.git/blob - ginac/parser/parse_context.hpp
Faster, better (recursive descent) expression parser.
[ginac.git] / ginac / parser / parse_context.hpp
1 #ifndef _GINAC_PARSE_CONTEXT_HPP
2 #define _GINAC_PARSE_CONTEXT_HPP
3 #include <string>
4 #include <cstddef> // size_t
5 #include "ex.h"
6 #include "symbol.h"
7 #include <map>
8 #include <utility>
9
10 namespace GiNaC
11 {
12
13 /**
14  * Establishes correspondence between the strings and symbols.
15  * The parser will create missing symbols (if not instructed otherwise,
16  * in which case it fails if the expression contains unknown symbols).
17  * The .second element of pair helps to distinguish between the user
18  * supplied symbols and parser generated ones. The .first is the symbol
19  * itself
20  */
21 typedef std::map<std::string, std::pair<symbol, bool> > symtab;
22
23 /**
24  * Find the symbol with the @a name in the symbol table @a syms.
25  *
26  * If symbol is missing and @a strict = false, insert it, otherwise
27  * throw an exception.
28  */
29 extern const symbol&
30 find_or_insert_symbol(const std::string& name, symtab& syms,
31                       const bool strict);
32
33 /**
34  * Function (or class ctor) prototype
35  * .first is  the name of function(or ctor),
36  * .second is the number of arguments (each of type ex)
37  */
38 typedef std::pair<std::string, std::size_t> prototype;
39
40 /**
41  * A (C++) function for reading functions and classes from the stream.
42  *
43  * The parser uses (an associative array of) such functions to construct
44  * (GiNaC) classes and functions from a sequence of characters.
45  */
46 typedef ex (*reader_func)(const exvector& args);
47
48 /**
49  * Prototype table.
50  *
51  * If parser sees an expression which looks like a function call (e.g.
52  * foo(x+y, z^2, t)), it looks up such a table to find out which
53  * function (or class) corresponds to the given name and has the given
54  * number of the arguments.
55  *
56  * N.B.
57  *
58  * 1. The function don't have to return a (GiNaC) function or class, it
59  *    can return any expression.
60  * 2. Overloaded functions/ctors are paritally supported, i.e. there might
61  *    be several functions with the same name, but they should take different
62  *    number of arguments.
63  * 3. User can extend the parser via custom prototype tables. It's possible
64  *    to read user defined classes, create abbreviations, etc.
65  */
66 typedef std::map<prototype, reader_func> prototype_table;
67
68 /**
69  * Default prototype table.
70  *
71  * It supports most of builtin GiNaC functions.
72  */
73 extern const prototype_table& get_default_reader();
74
75 }
76
77 #endif // _GINAC_PARSE_CONTEXT_HPP
78