1 /* $XConsortium: Syntax.C /main/2 1996/08/12 13:22:19 mgreess $ */
2 // Copyright (c) 1994 James Clark
3 // See the file COPYING for copying permission.
11 #include "CharsetInfo.h"
14 #include "MarkupScan.h"
18 namespace SP_NAMESPACE {
21 const int Syntax::referenceQuantity_[] = {
39 Syntax::Syntax(const Sd &sd)
42 categoryTable_(otherCategory),
45 markupScanTable_(MarkupScan::normal)
47 static const char lcletter[] = "abcdefghijklmnopqrstuvwxyz";
48 static const char ucletter[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
50 for (i = 0; i < 26; i++) {
51 Char lc = sd.execToDoc(lcletter[i]);
52 Char uc = sd.execToDoc(ucletter[i]);
53 set_[nameStart] += lc;
54 set_[nameStart] += uc;
55 set_[minimumData] += lc;
56 set_[minimumData] += uc;
57 set_[significant] += lc;
58 set_[significant] += uc;
59 categoryTable_.setChar(lc, nameStartCategory);
60 categoryTable_.setChar(uc, nameStartCategory);
63 static const char digits[] = "0123456789";
64 for (i = 0; i < 10; i++) {
65 Char c = sd.execToDoc(digits[i]);
67 set_[minimumData] += c;
68 set_[significant] += c;
69 categoryTable_.setChar(c, digitCategory);
71 static const char special[] = "'()+,-./:=?";
72 for (i = 0; special[i] != '\0'; i++) {
73 Char c = sd.execToDoc(special[i]);
74 set_[minimumData] += c;
75 set_[significant] += c;
77 for (i = 0; i < nQuantity; i++)
78 quantity_[i] = referenceQuantity_[i];
79 for (i = 0; i < 3; i++)
80 standardFunctionValid_[i] = 0;
84 Syntax::Syntax(const Syntax & syn)
85 : generalSubst_(syn.generalSubst_),
86 entitySubst_(syn.entitySubst_),
87 shunchar_(syn.shunchar_),
88 shuncharControls_(syn.shuncharControls_),
89 namecaseGeneral_(syn.namecaseGeneral_),
90 namecaseEntity_(syn.namecaseEntity_),
91 delimShortrefComplex_(syn.delimShortrefComplex_),
92 delimShortrefSimple_(syn.delimShortrefSimple_),
93 nameTable_(syn.nameTable_),
94 functionTable_(syn.functionTable_),
95 upperSubst_(syn.upperSubst_),
96 identitySubst_(syn.identitySubst_),
97 categoryTable_(syn.categoryTable_),
98 multicode_(syn.multicode_),
99 markupScanTable_(syn.markupScanTable_)
103 for (i = 0; i < nSet; i++)
104 set_[i] = syn.set_[i];
106 for (i = 0; i < 3; i++) {
107 standardFunction_[i] = syn.standardFunction_[i];
108 standardFunctionValid_[i] = syn.standardFunctionValid_[i];
111 for (i = 0; i < nDelimGeneral; i++)
112 delimGeneral_[i] = syn.delimGeneral_[i];
114 for (i = 0; i < nNames; i++)
115 names_[i] = syn.names_[i];
117 for (i = 0; i < nQuantity; i++)
118 quantity_[i] = syn.quantity_[i];
121 void Syntax::addNameCharacters(const ISet<Char> &set)
123 ISetIter<Char> iter(set);
125 while (iter.next(min, max)) {
126 set_[nmchar].addRange(min, max);
127 set_[significant].addRange(min, max);
128 categoryTable_.setRange(min, max, otherNameCategory);
132 void Syntax::addNameStartCharacters(const ISet<Char> &set)
134 ISetIter<Char> iter(set);
136 while (iter.next(min, max)) {
137 set_[nameStart].addRange(min, max);
138 set_[significant].addRange(min, max);
139 categoryTable_.setRange(min, max, nameStartCategory);
143 void Syntax::addSubst(Char lc, Char uc)
148 void Syntax::setStandardFunction(StandardFunction f, Char c)
150 standardFunction_[f] = c;
151 standardFunctionValid_[f] = 1;
152 set_[minimumData] += c;
154 categoryTable_.setChar(c, sCategory);
155 set_[functionChar] += c;
156 set_[significant] += c;
167 void Syntax::enterStandardFunctionNames()
169 static ReservedName name[3] = {
172 for (int i = 0; i < 3; i++)
173 if (standardFunctionValid_[i])
174 functionTable_.insert(reservedName(name[i]), standardFunction_[i]);
177 void Syntax::setDelimGeneral(int i, const StringC &str)
179 delimGeneral_[i] = str;
180 for (size_t j = 0; j < str.size(); j++)
181 set_[significant] += str[j];
184 void Syntax::addDelimShortref(const StringC &str, const CharsetInfo &charset)
186 if (str.size() == 1 && str[0] != charset.execToDesc('B') && !isB(str[0]))
187 delimShortrefSimple_.add(str[0]);
189 delimShortrefComplex_.push_back(str);
190 for (size_t i = 0; i < str.size(); i++)
191 set_[significant] += str[i];
194 void Syntax::addDelimShortrefs(const ISet<Char> &shortrefChars,
195 const CharsetInfo &charset)
197 ISetIter<Char> blankIter(set_[blank]);
199 StringC specialChars;
200 while (blankIter.next(min, max)) {
203 } while (min++ != max);
205 specialChars += charset.execToDesc('B');
206 const ISet<Char> *simpleCharsPtr = &shortrefChars;
207 ISet<Char> simpleChars;
208 for (size_t i = 0; i < specialChars.size(); i++)
209 if (shortrefChars.contains(specialChars[i])) {
210 if (simpleCharsPtr != &simpleChars) {
211 simpleChars = shortrefChars;
212 simpleCharsPtr = &simpleChars;
214 simpleChars.remove(specialChars[i]);
216 ISetIter<Char> iter(*simpleCharsPtr);
217 while (iter.next(min, max)) {
218 delimShortrefSimple_.addRange(min, max);
219 set_[significant].addRange(min, max);
223 void Syntax::addFunctionChar(const StringC &str, FunctionClass fun, Char c)
230 categoryTable_.setChar(c, sCategory);
236 markupScanTable_.setChar(c, MarkupScan::out);
239 // don't need to do anything special if we just have MSICHARs
240 markupScanTable_.setChar(c, MarkupScan::in);
244 markupScanTable_.setChar(c, MarkupScan::suppress);
247 set_[functionChar] += c;
248 set_[significant] += c;
249 functionTable_.insert(str, c);
252 void Syntax::setName(int i, const StringC &str)
255 nameTable_.insert(str, i);
258 void Syntax::setNamecaseGeneral(Boolean b)
260 namecaseGeneral_ = b;
261 generalSubst_ = b ? &upperSubst_ : &identitySubst_;
264 void Syntax::setNamecaseEntity(Boolean b)
267 entitySubst_ = b ? &upperSubst_ : &identitySubst_;
270 void Syntax::subst(Char from, Char to)
272 upperSubst_.addSubst(from, to);
275 void Syntax::addShunchar(Char c)
280 Boolean Syntax::lookupReservedName(const StringC &str,
281 ReservedName *result) const
283 const int *tem = nameTable_.lookup(str);
285 *result = ReservedName(*tem);
292 Boolean Syntax::lookupFunctionChar(const StringC &name, Char *result) const
294 const Char *p = functionTable_.lookup(name);
304 typedef HashTableIter<StringC,Char> Dummy_HashTableIter_StringC_Char;
307 Boolean Syntax::charFunctionName(Char c, const StringC *&name) const
309 HashTableIter<StringC,Char> iter(functionTable_);
311 while (iter.next(name, cp))
317 Boolean Syntax::isValidShortref(const StringC &str) const
319 if (str.size() == 1 && delimShortrefSimple_.contains(str[0]))
321 for (size_t i = 0; i < delimShortrefComplex_.size(); i++)
322 if (str == delimShortrefComplex_[i])
327 void Syntax::implySgmlChar(const CharsetInfo &docCharset)
329 docCharset.getDescSet(set_[sgmlChar]);
330 ISet<WideChar> invalid;
331 checkSgmlChar(docCharset, 0, invalid);
332 ISetIter<WideChar> iter(invalid);
334 while (iter.next(min, max)) {
337 set_[sgmlChar].remove(Char(min));
338 } while (min++ != max);
342 void Syntax::checkSgmlChar(const CharsetInfo &docCharset,
343 const ::SP_NAMESPACE_SCOPE Syntax *otherSyntax,
344 ISet<WideChar> &invalid) const
346 ISetIter<Char> iter(shunchar_);
348 while (iter.next(min, max)) {
351 if (!set_[significant].contains(min)
352 && (!otherSyntax || !otherSyntax->set_[significant].contains(min))
353 && set_[sgmlChar].contains(min))
355 } while (min++ != max);
358 if (shuncharControls_) {
360 for (i = 0; i < 32; i++)
361 checkUnivControlChar(i, docCharset, otherSyntax, invalid);
362 for (i = 127; i < 160; i++)
363 checkUnivControlChar(i, docCharset, otherSyntax, invalid);
367 void Syntax::checkUnivControlChar(UnivChar univChar,
368 const CharsetInfo &docCharset,
369 const ::SP_NAMESPACE_SCOPE Syntax *otherSyntax,
370 ISet<WideChar> &invalid) const
374 switch (docCharset.univToDesc(univChar, c, set)) {
382 ISetIter<WideChar> iter(set);
384 while (iter.next(min, max)) {
389 if (!set_[significant].contains(ch)
391 || !otherSyntax->set_[significant].contains(ch))
392 && set_[sgmlChar].contains(ch))
394 } while (min++ != max);
400 StringC Syntax::rniReservedName(ReservedName i) const
402 StringC result = delimGeneral(dRNI);
403 result += reservedName(i);
407 const SubstTable<Char> &Syntax::upperSubstTable() const
412 const StringC &Syntax::peroDelim() const
414 return delimGeneral(dPERO);