]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/src/TDictionary.cc
Initial revision
[helm.git] / helm / DEVEL / mathml_editor / src / TDictionary.cc
1
2 #include <sstream>
3
4 #include "dom.hh"
5 #include "TDictionary.hh"
6 #include "TTokenizer.hh"
7
8 static TDictionary::Entry undefinedEntry;
9
10 void
11 TDictionary::load(const char* uri)
12 {
13   DOM::DOMImplementation di;
14
15   DOM::Document doc = di.createDocumentFromURI(uri);
16   assert(doc);
17
18   DOM::Element root = doc.get_documentElement();
19   assert(root);
20
21   TTokenizer tokenizer;
22
23   for (DOM::Node p = root.get_firstChild(); p; p = p.get_nextSibling())
24     if (p.get_nodeType() == DOM::Node::ELEMENT_NODE && p.get_nodeName() == "entry")
25       {
26         DOM::Element el = p;
27         assert(el);
28         assert(el.hasAttribute("name"));
29
30         std::string name = el.getAttribute("name");
31         if (entries.find(name) != entries.end())
32           cerr << "WARNING: entry `" << name << "' is being redefined" << endl;
33
34         Entry entry;
35
36         if (el.hasAttribute("class"))
37           {
38             std::string cls = el.getAttribute("class");
39             if (cls == "o") entry.cls = OPERATOR;
40             else if (cls == "i") entry.cls = IDENTIFIER;
41             else if (cls == "n") entry.cls == NUMBER;
42             else entry.cls = MACRO;
43           }
44         else
45           entry.cls = MACRO;
46
47         if (el.hasAttribute("val"))
48           {
49             entry.value = el.getAttribute("val");
50             if (entry.cls == MACRO)
51               cerr << "WARNING: `" << name << "' has a specified value, but is classified as macro" << endl;
52           }
53
54         if (el.hasAttribute("pattern"))
55           {
56             if (entry.cls != MACRO)
57               cerr << "WARNING: `" << name << "' has a specified pattern, but is not classified as macro" << endl;
58
59             std::string pattern = el.getAttribute("pattern");
60             if (pattern == "{}")
61               entry.leftOpen = entry.rightOpen = 1;
62             else if (pattern == "{")
63               entry.leftOpen = 1;
64             else if (pattern == "}")
65               entry.rightOpen = 1;
66             else
67               entry.pattern = tokenizer.tokenize(pattern);
68           }
69
70         if (el.hasAttribute("infix"))
71           {
72             std::istringstream is(el.getAttribute("infix"));
73             unsigned infix;
74             is >> infix;
75             entry.infix = infix;
76             if (!el.hasAttribute("prefix")) entry.prefix = infix;
77             if (!el.hasAttribute("postfix")) entry.postfix = infix;
78           }
79
80         if (el.hasAttribute("prefix"))
81           {
82             std::istringstream is(el.getAttribute("prefix"));
83             unsigned prefix;
84             is >> prefix;
85             entry.prefix = prefix;
86             if (!el.hasAttribute("infix"))
87               {
88                 entry.infix = prefix;
89                 if (!el.hasAttribute("postfix")) entry.postfix = prefix;
90               }
91           }
92
93         if (el.hasAttribute("postfix"))
94           {
95             std::istringstream is(el.getAttribute("postfix"));
96             unsigned postfix;
97             is >> postfix;
98             entry.postfix = postfix;
99             if (!el.hasAttribute("infix"))
100               {
101                 entry.infix = postfix;
102                 if (!el.hasAttribute("prefix")) entry.prefix = postfix;
103               }
104           }
105
106         if (el.hasAttribute("limits"))
107           {
108             std::istringstream is(el.getAttribute("limits"));
109             unsigned limits;
110             is >> limits;
111             entry.limits = limits;
112           }
113
114         if (el.hasAttribute("embellishment"))
115           {
116             std::istringstream is(el.getAttribute("embellishment"));
117             unsigned embellishment;
118             is >> embellishment;
119             entry.embellishment = embellishment;
120           }
121
122         if (el.hasAttribute("delimiter"))
123           {
124             if (entry.cls != OPERATOR && !entry.embellishment)
125               cerr << "WARNING: `" << name << "' delimiter ignored for non-operator" << endl;
126
127             std::istringstream is(el.getAttribute("delimiter"));
128             unsigned delimiter;
129             is >> delimiter;
130             entry.delimiter = delimiter;
131           }
132
133         if (el.hasAttribute("table"))
134           {
135             if (entry.cls != MACRO)
136               cerr << "WARNING: `" << name << "' table ignored for non-macro" << endl;
137             
138             std::istringstream is(el.getAttribute("table"));
139             unsigned table;
140             is >> table;
141             entry.table = table;
142           }
143
144         entries[name] = entry;
145       }
146 }
147
148 const TDictionary::Entry&
149 TDictionary::find(const std::string& name) const
150 {
151   Dictionary::const_iterator p = entries.find(name);
152   if (p != entries.end()) return (*p).second;
153   else
154     {
155       cerr << "ERROR: unknown entry `" << name << "'" << endl;
156       return undefinedEntry;
157     }
158 }
159
160 bool
161 TDictionary::Entry::paramDelimited(unsigned i) const
162 {
163   assert(i < pattern.size());
164   assert(pattern[i].category == TToken::PARAMETER);
165   // a parameter is delimited if it is NOT the last one
166   // AND the next argument is not a parameter
167   return i + 1 < pattern.size() && pattern[i + 1].category != TToken::PARAMETER;
168 }