]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/mathml_editor/src/TDictionary.cc
debian release 0.4.1-1
[helm.git] / helm / DEVEL / mathml_editor / src / TDictionary.cc
index 303d743d441aa3399dc8639eb17354e9284bff14..3cf2334876ded1f66367a302c558675c6c017f51 100644 (file)
@@ -4,24 +4,44 @@
 #include "dom.hh"
 #include "TDictionary.hh"
 #include "TTokenizer.hh"
+#include "CLoggerConsole.hh"
 
 static TDictionary::Entry undefinedEntry;
 
 void
-TDictionary::load(const char* uri)
+TDictionary::load(const std::string& uri)
 {
+  logger.debug("Dictionary: loading `" + uri + "'");
+
   DOM::DOMImplementation di;
+  DOM::Document doc = di.createDocumentFromURI(uri.c_str());
+  assert(doc);
+  load(doc);
+}
 
-  DOM::Document doc = di.createDocumentFromURI(uri);
+void
+TDictionary::load(const DOM::Document& doc)
+{
   assert(doc);
 
   DOM::Element root = doc.get_documentElement();
   assert(root);
 
-  TTokenizer tokenizer;
+  CLoggerConsole logger;
+  TTokenizer tokenizer(logger);
 
   for (DOM::Node p = root.get_firstChild(); p; p = p.get_nextSibling())
-    if (p.get_nodeType() == DOM::Node::ELEMENT_NODE && p.get_nodeName() == "entry")
+    if (p.get_nodeType() == DOM::Node::ELEMENT_NODE && p.get_nodeName() == "include")
+      {
+       DOM::Element el = p;
+       assert(el);
+       if (el.hasAttribute("href"))
+         // WARNING: this may result into an infinite loop!
+         load(std::string(el.getAttribute("href")));
+       else
+         logger.warning("Dictionary: include statement with no href attribute (ignored)");
+      }
+    else if (p.get_nodeType() == DOM::Node::ELEMENT_NODE && p.get_nodeName() == "entry")
       {
        DOM::Element el = p;
        assert(el);
@@ -29,7 +49,7 @@ TDictionary::load(const char* uri)
 
        std::string name = el.getAttribute("name");
        if (entries.find(name) != entries.end())
-         cerr << "WARNING: entry `" << name << "' is being redefined" << endl;
+         logger.info("Dictionary: `" + name + "' is being redefined");
 
        Entry entry;
 
@@ -48,13 +68,13 @@ TDictionary::load(const char* uri)
          {
            entry.value = el.getAttribute("val");
            if (entry.cls == MACRO)
-             cerr << "WARNING: `" << name << "' has a specified value, but is classified as macro" << endl;
+             logger.warning("Dictionary: `" + name + "' has a specified value, but is classified as macro");
          }
 
        if (el.hasAttribute("pattern"))
          {
            if (entry.cls != MACRO)
-             cerr << "WARNING: `" << name << "' has a specified pattern, but is not classified as macro" << endl;
+             logger.warning("Dictionary: `" + name + "' has a specified pattern, but is not classified as macro");
 
            std::string pattern = el.getAttribute("pattern");
            if (pattern == "{}")
@@ -67,6 +87,7 @@ TDictionary::load(const char* uri)
              entry.pattern = tokenizer.tokenize(pattern);
          }
 
+#if 0
        if (el.hasAttribute("infix"))
          {
            std::istringstream is(el.getAttribute("infix"));
@@ -102,6 +123,7 @@ TDictionary::load(const char* uri)
                if (!el.hasAttribute("prefix")) entry.prefix = postfix;
              }
          }
+#endif
 
        if (el.hasAttribute("limits"))
          {
@@ -122,7 +144,7 @@ TDictionary::load(const char* uri)
        if (el.hasAttribute("delimiter"))
          {
            if (entry.cls != OPERATOR && !entry.embellishment)
-             cerr << "WARNING: `" << name << "' delimiter ignored for non-operator" << endl;
+             logger.warning("Dictionary: `" + name + "' delimiter ignored for non-operator");
 
            std::istringstream is(el.getAttribute("delimiter"));
            unsigned delimiter;
@@ -133,8 +155,8 @@ TDictionary::load(const char* uri)
        if (el.hasAttribute("table"))
          {
            if (entry.cls != MACRO)
-             cerr << "WARNING: `" << name << "' table ignored for non-macro" << endl;
-           
+             logger.warning("Dictionary: `" + name + "' table ignored for non-macro");
+
            std::istringstream is(el.getAttribute("table"));
            unsigned table;
            is >> table;
@@ -166,3 +188,32 @@ TDictionary::Entry::paramDelimited(unsigned i) const
   // AND the next argument is not a parameter
   return i + 1 < pattern.size() && pattern[i + 1].category != TToken::PARAMETER;
 }
+
+bool
+TDictionary::Entry::lastDelimiter(unsigned i) const
+{
+  assert(i < pattern.size());
+  assert(pattern[i].category != TToken::PARAMETER);
+  // a token is the last delimiter if it is the last token 
+  // of the pattern or if the next token is a parameter)
+  return i + 1 == pattern.size() || pattern[i + 1].category == TToken::PARAMETER;
+}
+
+unsigned
+TDictionary::Entry::previousParam(unsigned i) const
+{
+  // this method return the position in the pattern of the 
+  // parameter placed in a position preceding i.
+  // If no preceding i parameter present, the method return
+  // pattern.size().
+  // To know the position of the last parameter, call this 
+  // method with i == pattern.size()
+  unsigned j = i - 1;
+
+  while (pattern[j].category != TToken::PARAMETER)
+    {
+      if (j) j--;
+      else return pattern.size();
+    }
+  return j;
+}