1 /* This file is part of EdiTeX, an editor of mathematical
2 * expressions based on TeX syntax.
4 * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
5 * 2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * For more information, please visit the project's home page
22 * http://helm.cs.unibo.it/editex/
23 * or send an email to <lpadovan@cs.unibo.it>
29 #include "TDictionary.hh"
30 #include "TTokenizer.hh"
31 #include "CLoggerConsole.hh"
33 static TDictionary::Entry undefinedEntry;
36 getURIBase(const std::string& uri)
38 std::string::size_type slash = uri.rfind('/');
39 if (slash != std::string::npos) return uri.substr(0, slash + 1);
44 getURIName(const std::string& uri)
46 std::string::size_type slash = uri.rfind('/');
47 if (slash != std::string::npos) return uri.substr(slash + 1, uri.size());
52 TDictionary::load(const std::string& uri)
54 load(getURIName(uri), getURIBase(uri));
58 TDictionary::load(const std::string& name, const std::string& base)
60 logger.debug("Dictionary: loading `" + base + name + "'");
62 DOM::DOMImplementation di;
63 DOM::Document doc = di.createDocumentFromURI((base + name).c_str());
69 TDictionary::load(const DOM::Document& doc, const std::string& base)
73 DOM::Element root = doc.get_documentElement();
76 CLoggerConsole logger;
77 TTokenizer tokenizer(logger);
79 for (DOM::Node p = root.get_firstChild(); p; p = p.get_nextSibling())
80 if (p.get_nodeType() == DOM::Node::ELEMENT_NODE && p.get_nodeName() == "include")
84 if (el.hasAttribute("href"))
86 // WARNING: this may result into an infinite loop!
87 std::string href = el.getAttribute("href");
88 std::string newBase = getURIBase(href);
89 std::string newName = getURIName(href);
90 if (newBase != "") load(newName, newBase);
91 else load(newName, base);
94 logger.warning("Dictionary: include statement with no href attribute (ignored)");
96 else if (p.get_nodeType() == DOM::Node::ELEMENT_NODE && p.get_nodeName() == "entry")
100 assert(el.hasAttribute("name"));
102 std::string name = el.getAttribute("name");
103 if (entries.find(name) != entries.end())
104 logger.info("Dictionary: `" + name + "' is being redefined");
108 if (el.hasAttribute("class"))
110 std::string cls = el.getAttribute("class");
111 if (cls == "o") entry.cls = OPERATOR;
112 else if (cls == "i") entry.cls = IDENTIFIER;
113 else if (cls == "n") entry.cls == NUMBER;
114 else entry.cls = MACRO;
119 if (el.hasAttribute("val"))
121 entry.value = el.getAttribute("val");
122 if (entry.cls == MACRO)
123 logger.warning("Dictionary: `" + name + "' has a specified value, but is classified as macro");
126 if (el.hasAttribute("pattern"))
128 if (entry.cls != MACRO)
129 logger.warning("Dictionary: `" + name + "' has a specified pattern, but is not classified as macro");
131 std::string pattern = el.getAttribute("pattern");
133 entry.leftOpen = entry.rightOpen = 1;
134 else if (pattern == "{")
136 else if (pattern == "}")
139 entry.pattern = tokenizer.tokenize(pattern);
143 if (el.hasAttribute("infix"))
145 std::istringstream is(el.getAttribute("infix"));
149 if (!el.hasAttribute("prefix")) entry.prefix = infix;
150 if (!el.hasAttribute("postfix")) entry.postfix = infix;
153 if (el.hasAttribute("prefix"))
155 std::istringstream is(el.getAttribute("prefix"));
158 entry.prefix = prefix;
159 if (!el.hasAttribute("infix"))
161 entry.infix = prefix;
162 if (!el.hasAttribute("postfix")) entry.postfix = prefix;
166 if (el.hasAttribute("postfix"))
168 std::istringstream is(el.getAttribute("postfix"));
171 entry.postfix = postfix;
172 if (!el.hasAttribute("infix"))
174 entry.infix = postfix;
175 if (!el.hasAttribute("prefix")) entry.prefix = postfix;
180 if (el.hasAttribute("limits"))
182 std::istringstream is(el.getAttribute("limits"));
185 entry.limits = limits;
188 if (el.hasAttribute("embellishment"))
190 std::istringstream is(el.getAttribute("embellishment"));
191 unsigned embellishment;
193 entry.embellishment = embellishment;
196 if (el.hasAttribute("delimiter"))
198 if (entry.cls != OPERATOR && !entry.embellishment)
199 logger.warning("Dictionary: `" + name + "' delimiter ignored for non-operator");
201 std::istringstream is(el.getAttribute("delimiter"));
204 entry.delimiter = delimiter;
207 if (el.hasAttribute("table"))
209 if (entry.cls != MACRO)
210 logger.warning("Dictionary: `" + name + "' table ignored for non-macro");
212 std::istringstream is(el.getAttribute("table"));
218 entries[name] = entry;
222 const TDictionary::Entry&
223 TDictionary::find(const std::string& name) const
225 Dictionary::const_iterator p = entries.find(name);
226 if (p != entries.end()) return (*p).second;
229 cerr << "ERROR: unknown entry `" << name << "'" << endl;
230 return undefinedEntry;
235 TDictionary::Entry::paramDelimited(unsigned i) const
237 assert(i < pattern.size());
238 assert(pattern[i].category == TToken::PARAMETER);
239 // a parameter is delimited if it is NOT the last one
240 // AND the next argument is not a parameter
241 return i + 1 < pattern.size() && pattern[i + 1].category != TToken::PARAMETER;
245 TDictionary::Entry::lastDelimiter(unsigned i) const
247 assert(i < pattern.size());
248 assert(pattern[i].category != TToken::PARAMETER);
249 // a token is the last delimiter if it is the last token
250 // of the pattern or if the next token is a parameter)
251 return i + 1 == pattern.size() || pattern[i + 1].category == TToken::PARAMETER;
255 TDictionary::Entry::previousParam(unsigned i) const
257 // this method return the position in the pattern of the
258 // parameter placed in a position preceding i.
259 // If no preceding i parameter present, the method return
261 // To know the position of the last parameter, call this
262 // method with i == pattern.size()
265 while (pattern[j].category != TToken::PARAMETER)
268 else return pattern.size();