]> www.ginac.de Git - ginac.git/blobdiff - ginac/parser/parse_context.h
[BUGFIX] Fix crash in parser.
[ginac.git] / ginac / parser / parse_context.h
index 81b2cd15a9ada239ae9c2e55efaa32fce66a455f..015ca13517dbce52410fa67a8317030e5a3ce30f 100644 (file)
@@ -3,7 +3,7 @@
  *  Interface to parser context. */
 
 /*
- *  GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2024 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -63,7 +63,21 @@ typedef std::pair<std::string, std::size_t> prototype;
  * The parser uses (an associative array of) such functions to construct
  * (GiNaC) classes and functions from a sequence of characters.
  */
-typedef ex (*reader_func)(const exvector& args);
+class reader_func {
+       enum { FUNCTION_PTR, GINAC_FUNCTION };
+public:
+       reader_func(ex (*func_)(const exvector& args))
+               : type(FUNCTION_PTR), serial(0), func(func_) {}
+       reader_func(unsigned serial_)
+               : type(GINAC_FUNCTION), serial(serial_), func(nullptr) {}
+       ex operator()(const exvector& args) const;
+private:
+       unsigned type;
+       unsigned serial;
+       ex (*func)(const exvector& args);
+};
+
+
 
 /**
  * Prototype table.
@@ -88,14 +102,33 @@ typedef ex (*reader_func)(const exvector& args);
  *       function pointer!! The unsigned has to correspond to the serial number of
  *       the defined GiNaC function.
  */
-typedef std::map<prototype, reader_func> prototype_table;
+class PrototypeLess
+{
+public:
+       bool operator()(const prototype& p1, const prototype& p2) const
+       {
+               int s = p1.first.compare(p2.first);
+               if (s == 0) {
+                       if ((p1.second == 0) || (p2.second == 0)) return false;
+                       return p1.second < p2.second;
+               }
+               return s < 0;
+       }
+};
+typedef std::map<prototype, reader_func, PrototypeLess> prototype_table;
 
 /**
  * Default prototype table.
  *
- * It supports most of builtin GiNaC functions.
+ * It supports all defined GiNaC functions and "pow", "sqrt", and "power".
  */
 extern const prototype_table& get_default_reader();
+/**
+ * Builtin prototype table.
+ *
+ * It supports only the builtin GiNaC functions and "pow", "sqrt", and "power".
+ */
+extern const prototype_table& get_builtin_reader();
 
 } // namespace GiNaC