2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: Syntax.C /main/2 1996/08/12 13:22:19 mgreess $ */
24 // Copyright (c) 1994 James Clark
25 // See the file COPYING for copying permission.
28 #pragma implementation
33 #include "CharsetInfo.h"
36 #include "MarkupScan.h"
40 namespace SP_NAMESPACE {
43 const int Syntax::referenceQuantity_[] = {
61 Syntax::Syntax(const Sd &sd)
64 categoryTable_(otherCategory),
67 markupScanTable_(MarkupScan::normal)
69 static const char lcletter[] = "abcdefghijklmnopqrstuvwxyz";
70 static const char ucletter[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
72 for (i = 0; i < 26; i++) {
73 Char lc = sd.execToDoc(lcletter[i]);
74 Char uc = sd.execToDoc(ucletter[i]);
75 set_[nameStart] += lc;
76 set_[nameStart] += uc;
77 set_[minimumData] += lc;
78 set_[minimumData] += uc;
79 set_[significant] += lc;
80 set_[significant] += uc;
81 categoryTable_.setChar(lc, nameStartCategory);
82 categoryTable_.setChar(uc, nameStartCategory);
85 static const char digits[] = "0123456789";
86 for (i = 0; i < 10; i++) {
87 Char c = sd.execToDoc(digits[i]);
89 set_[minimumData] += c;
90 set_[significant] += c;
91 categoryTable_.setChar(c, digitCategory);
93 static const char special[] = "'()+,-./:=?";
94 for (i = 0; special[i] != '\0'; i++) {
95 Char c = sd.execToDoc(special[i]);
96 set_[minimumData] += c;
97 set_[significant] += c;
99 for (i = 0; i < nQuantity; i++)
100 quantity_[i] = referenceQuantity_[i];
101 for (i = 0; i < 3; i++)
102 standardFunctionValid_[i] = 0;
106 Syntax::Syntax(const Syntax & syn)
107 : generalSubst_(syn.generalSubst_),
108 entitySubst_(syn.entitySubst_),
109 shunchar_(syn.shunchar_),
110 shuncharControls_(syn.shuncharControls_),
111 namecaseGeneral_(syn.namecaseGeneral_),
112 namecaseEntity_(syn.namecaseEntity_),
113 delimShortrefComplex_(syn.delimShortrefComplex_),
114 delimShortrefSimple_(syn.delimShortrefSimple_),
115 nameTable_(syn.nameTable_),
116 functionTable_(syn.functionTable_),
117 upperSubst_(syn.upperSubst_),
118 identitySubst_(syn.identitySubst_),
119 categoryTable_(syn.categoryTable_),
120 multicode_(syn.multicode_),
121 markupScanTable_(syn.markupScanTable_)
125 for (i = 0; i < nSet; i++)
126 set_[i] = syn.set_[i];
128 for (i = 0; i < 3; i++) {
129 standardFunction_[i] = syn.standardFunction_[i];
130 standardFunctionValid_[i] = syn.standardFunctionValid_[i];
133 for (i = 0; i < nDelimGeneral; i++)
134 delimGeneral_[i] = syn.delimGeneral_[i];
136 for (i = 0; i < nNames; i++)
137 names_[i] = syn.names_[i];
139 for (i = 0; i < nQuantity; i++)
140 quantity_[i] = syn.quantity_[i];
143 void Syntax::addNameCharacters(const ISet<Char> &set)
145 ISetIter<Char> iter(set);
147 while (iter.next(min, max)) {
148 set_[nmchar].addRange(min, max);
149 set_[significant].addRange(min, max);
150 categoryTable_.setRange(min, max, otherNameCategory);
154 void Syntax::addNameStartCharacters(const ISet<Char> &set)
156 ISetIter<Char> iter(set);
158 while (iter.next(min, max)) {
159 set_[nameStart].addRange(min, max);
160 set_[significant].addRange(min, max);
161 categoryTable_.setRange(min, max, nameStartCategory);
165 void Syntax::addSubst(Char lc, Char uc)
170 void Syntax::setStandardFunction(StandardFunction f, Char c)
172 standardFunction_[f] = c;
173 standardFunctionValid_[f] = 1;
174 set_[minimumData] += c;
176 categoryTable_.setChar(c, sCategory);
177 set_[functionChar] += c;
178 set_[significant] += c;
189 void Syntax::enterStandardFunctionNames()
191 static ReservedName name[3] = {
194 for (int i = 0; i < 3; i++)
195 if (standardFunctionValid_[i])
196 functionTable_.insert(reservedName(name[i]), standardFunction_[i]);
199 void Syntax::setDelimGeneral(int i, const StringC &str)
201 delimGeneral_[i] = str;
202 for (size_t j = 0; j < str.size(); j++)
203 set_[significant] += str[j];
206 void Syntax::addDelimShortref(const StringC &str, const CharsetInfo &charset)
208 if (str.size() == 1 && str[0] != charset.execToDesc('B') && !isB(str[0]))
209 delimShortrefSimple_.add(str[0]);
211 delimShortrefComplex_.push_back(str);
212 for (size_t i = 0; i < str.size(); i++)
213 set_[significant] += str[i];
216 void Syntax::addDelimShortrefs(const ISet<Char> &shortrefChars,
217 const CharsetInfo &charset)
219 ISetIter<Char> blankIter(set_[blank]);
221 StringC specialChars;
222 while (blankIter.next(min, max)) {
225 } while (min++ != max);
227 specialChars += charset.execToDesc('B');
228 const ISet<Char> *simpleCharsPtr = &shortrefChars;
229 ISet<Char> simpleChars;
230 for (size_t i = 0; i < specialChars.size(); i++)
231 if (shortrefChars.contains(specialChars[i])) {
232 if (simpleCharsPtr != &simpleChars) {
233 simpleChars = shortrefChars;
234 simpleCharsPtr = &simpleChars;
236 simpleChars.remove(specialChars[i]);
238 ISetIter<Char> iter(*simpleCharsPtr);
239 while (iter.next(min, max)) {
240 delimShortrefSimple_.addRange(min, max);
241 set_[significant].addRange(min, max);
245 void Syntax::addFunctionChar(const StringC &str, FunctionClass fun, Char c)
252 categoryTable_.setChar(c, sCategory);
258 markupScanTable_.setChar(c, MarkupScan::out);
261 // don't need to do anything special if we just have MSICHARs
262 markupScanTable_.setChar(c, MarkupScan::in);
266 markupScanTable_.setChar(c, MarkupScan::suppress);
269 set_[functionChar] += c;
270 set_[significant] += c;
271 functionTable_.insert(str, c);
274 void Syntax::setName(int i, const StringC &str)
277 nameTable_.insert(str, i);
280 void Syntax::setNamecaseGeneral(Boolean b)
282 namecaseGeneral_ = b;
283 generalSubst_ = b ? &upperSubst_ : &identitySubst_;
286 void Syntax::setNamecaseEntity(Boolean b)
289 entitySubst_ = b ? &upperSubst_ : &identitySubst_;
292 void Syntax::subst(Char from, Char to)
294 upperSubst_.addSubst(from, to);
297 void Syntax::addShunchar(Char c)
302 Boolean Syntax::lookupReservedName(const StringC &str,
303 ReservedName *result) const
305 const int *tem = nameTable_.lookup(str);
307 *result = ReservedName(*tem);
314 Boolean Syntax::lookupFunctionChar(const StringC &name, Char *result) const
316 const Char *p = functionTable_.lookup(name);
326 typedef HashTableIter<StringC,Char> Dummy_HashTableIter_StringC_Char;
329 Boolean Syntax::charFunctionName(Char c, const StringC *&name) const
331 HashTableIter<StringC,Char> iter(functionTable_);
333 while (iter.next(name, cp))
339 Boolean Syntax::isValidShortref(const StringC &str) const
341 if (str.size() == 1 && delimShortrefSimple_.contains(str[0]))
343 for (size_t i = 0; i < delimShortrefComplex_.size(); i++)
344 if (str == delimShortrefComplex_[i])
349 void Syntax::implySgmlChar(const CharsetInfo &docCharset)
351 docCharset.getDescSet(set_[sgmlChar]);
352 ISet<WideChar> invalid;
353 checkSgmlChar(docCharset, 0, invalid);
354 ISetIter<WideChar> iter(invalid);
356 while (iter.next(min, max)) {
359 set_[sgmlChar].remove(Char(min));
360 } while (min++ != max);
364 void Syntax::checkSgmlChar(const CharsetInfo &docCharset,
365 const ::SP_NAMESPACE_SCOPE Syntax *otherSyntax,
366 ISet<WideChar> &invalid) const
368 ISetIter<Char> iter(shunchar_);
370 while (iter.next(min, max)) {
373 if (!set_[significant].contains(min)
374 && (!otherSyntax || !otherSyntax->set_[significant].contains(min))
375 && set_[sgmlChar].contains(min))
377 } while (min++ != max);
380 if (shuncharControls_) {
382 for (i = 0; i < 32; i++)
383 checkUnivControlChar(i, docCharset, otherSyntax, invalid);
384 for (i = 127; i < 160; i++)
385 checkUnivControlChar(i, docCharset, otherSyntax, invalid);
389 void Syntax::checkUnivControlChar(UnivChar univChar,
390 const CharsetInfo &docCharset,
391 const ::SP_NAMESPACE_SCOPE Syntax *otherSyntax,
392 ISet<WideChar> &invalid) const
396 switch (docCharset.univToDesc(univChar, c, set)) {
404 ISetIter<WideChar> iter(set);
406 while (iter.next(min, max)) {
411 if (!set_[significant].contains(ch)
413 || !otherSyntax->set_[significant].contains(ch))
414 && set_[sgmlChar].contains(ch))
416 } while (min++ != max);
422 StringC Syntax::rniReservedName(ReservedName i) const
424 StringC result = delimGeneral(dRNI);
425 result += reservedName(i);
429 const SubstTable<Char> &Syntax::upperSubstTable() const
434 const StringC &Syntax::peroDelim() const
436 return delimGeneral(dPERO);