File: ParserState.cxx

package info (click to toggle)
sgml-tools 1.0.9-15
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,464 kB
  • ctags: 11,469
  • sloc: cpp: 59,330; ansic: 24,525; perl: 3,140; sh: 971; makefile: 845; lex: 561; lisp: 309; awk: 142; sed: 5
file content (829 lines) | stat: -rwxr-xr-x 20,445 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// Copyright (c) 1994 James Clark
// See the file COPYING for copying permission.

#ifdef __GNUG__
#pragma implementation
#endif
#include "splib.h"
#include "ParserState.h"
#include "InternalInputSource.h"
#include "MessageArg.h"
#include "macros.h"
#include "SgmlParser.h"
#include "IListIter.h"
#include "ParserMessages.h"
#include "Undo.h"
#include "Trie.h"

#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif

const Location ParserState::nullLocation_;
sig_atomic_t ParserState::dummyCancel_ = 0;

static const size_t eventSizes[] = {
#define EVENT(c, f) sizeof(c),
#include "events.h"
#undef EVENT
};

static const size_t internalSizes[] = {
  sizeof(InternalInputSource),
  sizeof(OpenElement),
  sizeof(UndoStartTag),
  sizeof(UndoEndTag),
  sizeof(UndoTransition)
};

static
size_t maxSize(const size_t *v, size_t n, size_t max = 0)
{
  for (size_t i = 0; i < n; i++) {
    if (v[i] > max)
      max = v[i];
  }
  return max;
}

ParserState::ParserState(const Ptr<EntityManager> &em,
			 const ParserOptions &opt,
			 unsigned subdocLevel,
			 Phase finalPhase)
: entityManager_(em),
  options_(opt),
  validate_(opt.errorValid),
  inInstance_(0),
  keepingMessages_(0),
  eventAllocator_(maxSize(eventSizes, SIZEOF(eventSizes)), 50),
  internalAllocator_(maxSize(internalSizes, SIZEOF(internalSizes), EntityOrigin::allocSize), 50),
  handler_(&eventQueue_),
  subdocLevel_(subdocLevel),
  inputLevel_(0),
  specialParseInputLevel_(0),
  markedSectionLevel_(0),
  markedSectionSpecialLevel_(0),
  currentMode_(proMode),
  hadLpd_(0),
  resultAttributeSpecMode_(0),
  pass2_(0),
  activeLinkTypesSubsted_(0),
  allowPass2_(0),
  hadPass2Start_(0),
  pcdataRecovering_(0),
  currentMarkup_(0),
  cancelPtr_(&dummyCancel_),
  finalPhase_(finalPhase),
  hadAfdrDecl_(0),
  stupidNetTrick_(0)
{
}

void ParserState::inheritActiveLinkTypes(const ParserState &parent)
{
  activeLinkTypes_ = parent.activeLinkTypes_;
  activeLinkTypesSubsted_ = parent.activeLinkTypesSubsted_;
}

void ParserState::allDone()
{
  phase_ = noPhase;
}

void ParserState::setPass2Start()
{
  ASSERT(inputLevel_ == 1);
  if (hadPass2Start_)
    return;
  hadPass2Start_ = 1;
  if (!pass2() && sd().link() && activeLinkTypes_.size() > 0) {
    allowPass2_ = 1;
    pass1Handler_.init(handler_);
    handler_ = &pass1Handler_;
    const InputSourceOrigin *p
      = currentLocation().origin()->asInputSourceOrigin();
    pass2StartOffset_= p->startOffset(currentLocation().index());
  }
  else {
    allowPass2_ = 0;
    currentInput()->willNotRewind();
  }
}

void ParserState::allLinkTypesActivated()
{
  if (activeLinkTypes_.size() == 0 && inputLevel_ == 1)
    currentInput()->willNotRewind();
}

Boolean ParserState::maybeStartPass2()
{
  if (pass2_ || !allowPass2_)
    return 0;
  handler_ = pass1Handler_.origHandler();
  if (!nActiveLink() || pass1Handler_.hadError()) {
    while (!pass1Handler_.empty()) {
      if (cancelled())
	return 0;
      pass1Handler_.get()->handle(*handler_);
    }
    InputSource *top = 0;
    for (IListIter<InputSource> iter(inputStack_);
	 !iter.done();
	 iter.next())
      top = iter.cur();
    if (top)
      top->willNotRewind();
    return 0;
  }
  pass1Handler_.clear();
  while (inputLevel_ > 1) {
    InputSource *p = inputStack_.get();
    inputLevel_--;
    delete p;
  }
  // Caller will call allDone() if inputLevel_ is 0.
  if (inputLevel_ == 0)
    return 0;
  if (!inputStack_.head()->rewind(*this)) {
    inputLevel_ = 0;
    delete inputStack_.get();
    return 0;
  }
  inputStack_.head()->willNotRewind();
  for (; pass2StartOffset_ > 0; pass2StartOffset_--)
    if (inputStack_.head()->get(messenger()) == InputSource::eE) {
      message(ParserMessages::pass2Ee);
      inputLevel_ = 0;
      delete inputStack_.get();
      return 0;
    }
  specialParseInputLevel_ = 0;
  markedSectionLevel_ = 0;
  markedSectionSpecialLevel_ = 0;
  currentMode_ = proMode;
  hadLpd_ = 0;
  allowPass2_ = 0;
  hadPass2Start_ = 0;
  currentMarkup_ = 0;
  inputLevel_ = 1;
  inInstance_ = 0;
  defDtd_.clear();
  defLpd_.clear();
  dtd_[0].swap(pass1Dtd_);
  dtd_.clear();
  dsEntity_.clear();
  currentDtd_.clear();
  currentDtdConst_.clear();
  phase_ = noPhase;
  pass2_ = 1;
  lpd_.clear();
  allLpd_.clear();
  return 1;
}

Boolean ParserState::referenceDsEntity(const Location &loc)
{
  if (dsEntity_.isNull())
    return 0;
  Ptr<EntityOrigin> origin
    = EntityOrigin::make(internalAllocator(), dsEntity_, loc);
  dsEntity_->dsReference(*this, origin);
  dsEntity_.clear();
  return inputLevel() > 1;
}

void ParserState::startDtd(const StringC &name)
{
  defDtd_ = new Dtd(name, dtd_.size() == 0);
  defLpd_.clear();
  for (size_t i = 0; i < options().includes.size(); i++) {
    StringC name = options().includes[i];
    const SubstTable<Char> *subst = syntax().entitySubstTable();
    for (size_t j = 0; j < name.size(); j++)
      subst->subst(name[j]);
    Text text;
    text.addChars(syntax().reservedName(Syntax::rINCLUDE), Location());
    Entity *entity
      = new InternalTextEntity(name,
			       Entity::parameterEntity,
			       Location(),
			       text,
			       InternalTextEntity::none);
    entity->setUsed();
    defDtd_->insertEntity(entity);
  }
  currentDtd_ = defDtd_;
  currentDtdConst_ = defDtd_;
  currentMode_ = dsMode;
}

void ParserState::endDtd()
{
  dtd_.push_back(defDtd_);
  defDtd_.clear();
  currentDtd_.clear();
  currentDtdConst_.clear();
  currentMode_ = proMode;
}

void ParserState::startLpd(Ptr<Lpd> &lpd)
{
  defLpd_ = lpd;
  defDtd_ = defLpd_->sourceDtd();
  currentDtd_ = defLpd_->sourceDtd();
  currentDtdConst_ = defLpd_->sourceDtd();
  currentMode_ = dsMode;
}

void ParserState::endLpd()
{
  hadLpd_ = 1;
  if (defLpd_->active())
    lpd_.push_back(defLpd_);
  allLpd_.push_back(defLpd_);
  defLpd_.clear();
  currentDtd_.clear();
  currentDtdConst_.clear();
  currentMode_ = proMode;
}

void ParserState::popInputStack()
{
  ASSERT(inputLevel_ > 0);
  InputSource *p = inputStack_.get();
  inputLevel_--;
  delete p;
  if (specialParseInputLevel_ > 0 && inputLevel_ == specialParseInputLevel_)
    currentMode_ = specialParseMode_;
  if (currentMode_ == dsiMode
      && inputLevel_ == 1
      && markedSectionLevel_ == 0)
    currentMode_ = dsMode;
}

void ParserState::setSd(ConstPtr<Sd> sd)
{
  sd_ = sd;
  mayDefaultAttribute_ = (sd_->omittag() || sd_->shorttag());
}

void ParserState::setSyntax(ConstPtr<Syntax> syntax)
{
  syntax_ = syntax;
  prologSyntax_ = syntax;
  instanceSyntax_ = syntax;
}

void ParserState::setSyntaxes(ConstPtr<Syntax> prologSyntax,
			      ConstPtr<Syntax> instanceSyntax)
{
  syntax_ = prologSyntax;
  prologSyntax_ = prologSyntax;
  instanceSyntax_ = instanceSyntax;
}

void ParserState::pushInput(InputSource *in)
{
  if (!in)
    return;
  if (!syntax_.isNull() && syntax_->multicode())
    in->setMarkupScanTable(syntax_->markupScanTable());
  inputStack_.insert(in);
  inputLevel_++;
  if (specialParseInputLevel_ > 0 && inputLevel_ > specialParseInputLevel_)
    currentMode_ = rcconeMode;	// mode for rcdata in an entity
  else if (currentMode_ == dsMode)
    currentMode_ = dsiMode;
}

void ParserState::startMarkedSection(const Location &loc)
{
  markedSectionLevel_++;
  markedSectionStartLocation_.push_back(loc);
  if (currentMode_ == dsMode)
    currentMode_ = dsiMode;
  if (markedSectionSpecialLevel_)
    markedSectionSpecialLevel_++;
}

void ParserState::startSpecialMarkedSection(Mode mode, const Location &loc)
{
  markedSectionLevel_++;
  markedSectionStartLocation_.push_back(loc);
  specialParseInputLevel_ = inputLevel_;
  markedSectionSpecialLevel_ = 1;
  specialParseMode_ = currentMode_ = mode;
}

void ParserState::endMarkedSection()
{
  ASSERT(markedSectionLevel_ > 0);
  markedSectionLevel_--;
  markedSectionStartLocation_.resize(markedSectionStartLocation_.size()
					- 1);
  if (markedSectionSpecialLevel_ > 0) {
    markedSectionSpecialLevel_--;
    if (markedSectionSpecialLevel_ > 0)
      return;			// remain in imsMode
    specialParseInputLevel_ = 0;
    if (inInstance_)
      currentMode_ = contentMode();
    else
      currentMode_ = dsiMode;
  }
  if (currentMode_ == dsiMode
      && inputLevel_ == 1
      && markedSectionLevel_ == 0)
    currentMode_ = dsMode;
}

void ParserState::pushElement(OpenElement *e)
{
  ContentState::pushElement(e);
  pcdataRecovering_ = 0;
  // the start tag of this element may have been implied by data
  // inside a cdata or rcdata marked section
  if (markedSectionSpecialLevel_ == 0) {
    currentMode_ = contentMode();
    if (e->requiresSpecialParse()) {
      specialParseMode_ = currentMode_;
      specialParseInputLevel_ = inputLevel_;
    }
  }
}

// PCDATA was encountered somewhere where it was not allowed.
// Change the current mode to improve recovery.

void ParserState::pcdataRecover()
{
  switch (currentMode_) {
  case econMode:
    currentMode_ = mconMode;
    break;
  case econnetMode:
    currentMode_ = mconnetMode;
    break;
  default:
    break;
  }
  pcdataRecovering_ = 1;
}

OpenElement *ParserState::popSaveElement()
{
  OpenElement *e = ContentState::popSaveElement();
  // the end tag of this element may have been implied by data
  // inside a cdata or rcdata marked section
  if (markedSectionSpecialLevel_ == 0) {
    currentMode_ = contentMode();
    specialParseInputLevel_ = 0;
  }
  pcdataRecovering_ = 0;
  return e;
}

void ParserState::popElement()
{
  delete popSaveElement();
}
			      
Boolean ParserState::entityIsOpen(const Entity *entity) const
{
  for (IListIter<InputSource> iter(inputStack_); !iter.done(); iter.next()) {
    const EntityOrigin *eo
      = iter.cur()->currentLocation().origin()->asEntityOrigin();
    if (eo && eo->entity() == entity)
      return 1;
  }
  return 0;
}

void ParserState::startInstance()
{
  if (!instanceSyntax_.isNull())
    syntax_ = instanceSyntax_;
  currentMode_ = econMode;
  currentDtd_ = dtd_[0];
  currentDtdConst_ = dtd_[0];
  startContent(currentDtd());
  inInstance_ = 1;
  if (sd().rank())
    currentRank_.assign(currentDtd().nRankStem(), StringC());
  currentAttributes_.clear();
  currentAttributes_.resize(currentDtd().nCurrentAttribute());
  idTable_.clear();
  const StringC &net = syntax().delimGeneral(Syntax::dNET);
  const StringC &tagc = syntax().delimGeneral(Syntax::dTAGC);
  if (net.size() > tagc.size()
      && memcmp(net.data() + net.size() - tagc.size(),
		tagc.data(),
		tagc.size() * sizeof(Char)) == 0)
    stupidNetTrick_ = 1;
}

Id *ParserState::lookupCreateId(const StringC &name)
{
  Id *id = idTable_.lookup(name);
  if (!id) {
    id = new Id(name);
    idTable_.insert(id);
  }
  return id;
}

ConstPtr<Entity>
ParserState::lookupEntity(Boolean isParameter,
			  const StringC &name,
			  const Location &useLocation,
			  Boolean referenced)
{
  Dtd *dtd;
  if (resultAttributeSpecMode_)
    dtd = defComplexLpd().resultDtd().pointer();
  else
    dtd = currentDtd_.pointer();
  if (dtd) {
    Ptr<Entity> entity(dtd->lookupEntity(isParameter, name));
    // Did we find it in pass1Dtd?
    // Did we look at the defaultEntity?
    if (!inInstance_ && pass2() && dtd->isBase()
	&& !resultAttributeSpecMode_
	&& (entity.isNull() || !entity->declInActiveLpd())) {
      ConstPtr<Entity> entity1
	= pass1Dtd_->lookupEntity(isParameter, name);
      if (!entity1.isNull() && entity1->declInActiveLpd()
	  && !entity1->defaulted()) {
	if (referenced)
	  noteReferencedEntity(entity1, 1, 0);
	return entity1;
      }
      else if (!entity.isNull()) {
	if (referenced)
	  noteReferencedEntity(entity, 0, 0);
	entity->setUsed();
	return entity;
      }
    }
    else if (!entity.isNull()) {
      entity->setUsed();
      return entity;
    }
    if (!isParameter) {
      ConstPtr<Entity> entity(dtd->defaultEntity());
      Boolean note = 0;
      Boolean usedPass1 = 0;
      if (!inInstance_ && pass2() && dtd->isBase()
	  && !resultAttributeSpecMode_
	  && (entity.isNull() || !entity->declInActiveLpd())) {
	if (referenced)
	  note = 1;
	ConstPtr<Entity> entity1 = pass1Dtd_->defaultEntity();
	if (!entity1.isNull() && entity1->declInActiveLpd()) {
	  usedPass1 = 1;
	  entity = entity1;
	}
      }
      if (!entity.isNull()) {
	Boolean mustCopy = 1;
	if (inInstance_) {
	  ConstPtr<Entity> tem 
	    = instanceDefaultedEntityTable_.lookupConst(name);
	  if (!tem.isNull()) {
	    entity = tem;
	    mustCopy = 0;
	  }
	}
	if (mustCopy) {
	  Ptr<Entity> p(entity->copy());
	  p->setName(name);
	  p->generateSystemId(*this);
	  p->setDefaulted();
	  entity = p;
	  if (inInstance_) {
	    instanceDefaultedEntityTable_.insert(p);
	    eventHandler().entityDefaulted(new (eventAllocator())
					   EntityDefaultedEvent(entity,
								useLocation));
	  }
	  else
	    dtd->insertEntity(p);
	}
	if (note)
	  noteReferencedEntity(entity, usedPass1, 1);
      }
      else
	entity = undefinedEntityTable_.lookupConst(name);
      return entity;
    }
  }
  return (Entity *)0;
}

ConstPtr<Entity> ParserState::createUndefinedEntity(const StringC &name, const Location &loc,
						    Boolean &known)
{
  known = 0;
  Text text;
  const Vector<StringC> &v = options().recoveryEntities;
  for (size_t i = 0; i < v.size(); i += 2) {
    if (v[i] == name) {
      known = 1;
      text.addChars(v[i + 1], loc);
      break;
    }
  }
  Ptr<Entity> entity(new InternalCdataEntity(name, loc, text));
  undefinedEntityTable_.insert(entity);
  return entity;
}

void ParserState::noteReferencedEntity(const ConstPtr<Entity> &entity,
				       Boolean foundInPass1Dtd,
				       Boolean lookedAtDefault)
{
  LpdEntityRef ref;
  ref.entity = entity;
  ref.lookedAtDefault = lookedAtDefault;
  ref.foundInPass1Dtd = foundInPass1Dtd;
  LpdEntityRef *old = lpdEntityRefs_.lookup(ref);
  if (!old)
    lpdEntityRefs_.insert(new LpdEntityRef(ref));
}

// Compare entity definitions.
// e1 is the original (will not be an external non-text entity).
// FIXME should look at generated sysids as well.
static
Boolean sameEntityDef(const Entity *e1, const Entity *e2)
{
  if (e1->dataType() != e2->dataType())
    return 0;
  const InternalEntity *i1 = e1->asInternalEntity();
  const InternalEntity *i2 = e2->asInternalEntity();
  if (i1) {
    if (!i2)
      return 0;
    if (i1->string() != i2->string())
      return 0;
    return 1;
  }
  else if (i2)
    return 0;
  const ExternalEntity *x1 = e1->asExternalEntity();
  const ExternalEntity *x2 = e2->asExternalEntity();
  const StringC *s1 = x1->externalId().systemIdString();
  const StringC *s2 = x2->externalId().systemIdString();
  if (s1) {
    if (!s2)
      return 0;
    if (*s1 != *s2)
      return 0;
  }
  else if (s2)
    return 0;
  s1 = x1->externalId().publicIdString();
  s2 = x2->externalId().publicIdString();
  if (s1) {
    if (!s2)
      return 0;
    if (*s1 != *s2)
      return 0;
  }
  else if (s2)
    return 0;
  return 1;
}

void ParserState::checkEntityStability()
{
  LpdEntityRefSetIter iter(lpdEntityRefs_);
  LpdEntityRef *ref;
  while ((ref = iter.next()) != 0) {
    ConstPtr<Entity> entity
      = dtd_[0]->lookupEntity(ref->entity->declType()
			      == Entity::parameterEntity,
			      ref->entity->name());
    if (entity.isNull() && ref->lookedAtDefault)
      entity = dtd_[0]->defaultEntity();
    if (entity.isNull()
	? ref->foundInPass1Dtd
	: !sameEntityDef(ref->entity.pointer(), entity.pointer()))
      message(((ref->entity->declType()
		== Entity::parameterEntity)
	       ? ParserMessages::unstableLpdParameterEntity
	       : ParserMessages::unstableLpdGeneralEntity),
	      StringMessageArg(ref->entity->name()));
  }
  {
    // Ensure that the memory is released.
    LpdEntityRefSet tem;
    lpdEntityRefs_.swap(tem);
  }
}    

Boolean ParserState::appendCurrentRank(StringC &str, const RankStem *stem)
     const
{
  const StringC &suffix = currentRank_[stem->index()];
  if (suffix.size() > 0) {
    str += suffix;
    return 1;
  }
  return 0;
}

void ParserState::setCurrentRank(const RankStem *stem, const StringC &suffix)
{
  currentRank_[stem->index()] = suffix;
}

void ParserState::getCurrentToken(const SubstTable<Char> *subst,
				  StringC &str) const
{
  InputSource *in = currentInput();
  const Char *p = in->currentTokenStart();
  size_t count = in->currentTokenLength();
  str.resize(count);
  StringC::iterator s = str.begin();
  for (; count > 0; --count)
    *s++ = (*subst)[*p++];
}

void ParserState::queueMessage(MessageEvent *event)
{
  if (cancelled()) {
    delete event;
    return;
  }
  if (keepingMessages_)
    keptMessages_.append(event);
  else
    handler_->message(event);
}

void ParserState::releaseKeptMessages()
{
  keepingMessages_ = 0;
  while (!keptMessages_.empty()) {
    if (cancelled()) {
      allDone();
      return;
    }
    handler_->message(keptMessages_.get());
  }
}

void ParserState::discardKeptMessages()
{
  keepingMessages_ = 0;
  keptMessages_.clear();
}

void ParserState::initMessage(Message &msg)
{
  if (inInstance()) {
    StringC rniPcdata = syntax().delimGeneral(Syntax::dRNI);
    rniPcdata += syntax().reservedName(Syntax::rPCDATA);
    getOpenElementInfo(msg.openElementInfo, rniPcdata);
  }
  msg.loc = currentLocation();
}

void ParserState::dispatchMessage(Message &msg)
{
  queueMessage(new MessageEvent(msg));
}

void ParserState::dispatchMessage(const Message &msg)
{
  queueMessage(new MessageEvent(msg));
}

AttributeList *
ParserState::allocAttributeList(const ConstPtr<AttributeDefinitionList> &def,
				unsigned i)
{
  if (i < attributeLists_.size())
    attributeLists_[i]->init(def);
  else {
    attributeLists_.resize(i + 1);
    attributeLists_[i] = new AttributeList(def);
  }
  return attributeLists_[i].pointer();
}

void ParserState::activateLinkType(const StringC &name)
{
  if (!hadPass2Start_ && !pass2_)
    activeLinkTypes_.push_back(name);
  else
    message(ParserMessages::linkActivateTooLate);
}

Boolean ParserState::shouldActivateLink(const StringC &name) const
{
  if (!activeLinkTypesSubsted_) {
    // FIXME use mutable
    ParserState *state = (ParserState *)this;
    for (size_t i = 0; i < activeLinkTypes_.size(); i++)
      for (size_t j = 0; j < activeLinkTypes_[i].size(); j++)
	syntax().generalSubstTable()->subst(state->activeLinkTypes_[i][j]);
    state->activeLinkTypesSubsted_ = 1;
  }
  for (size_t i = 0; i < activeLinkTypes_.size(); i++)
    if (name == activeLinkTypes_[i])
      return 1;
  return 0;
}

Ptr<Dtd> ParserState::lookupDtd(const StringC &name)
{
  for (size_t i = 0; i < dtd_.size(); i++)
    if (dtd_[i]->name() == name)
      return dtd_[i];
  return Ptr<Dtd>();
}

ConstPtr<Lpd> ParserState::lookupLpd(const StringC &name) const
{
  for (size_t i = 0; i < allLpd_.size(); i++)
    if (allLpd_[i]->name() == name)
      return allLpd_[i];
  return ConstPtr<Lpd>();
}

ConstPtr<Notation> ParserState::getAttributeNotation(const StringC &name,
						     const Location &)
{
  ConstPtr<Notation> notation;
  if (haveCurrentDtd())
    notation = currentDtd().lookupNotation(name);
  else if (resultAttributeSpecMode_) {
    const Dtd *resultDtd = defComplexLpd().resultDtd().pointer();
    if (!resultDtd)
      return 0;
    notation = resultDtd->lookupNotation(name);
  }
  return notation; 
}

ConstPtr<Entity> ParserState::getAttributeEntity(const StringC &str,
						 const Location &loc)
{
  ConstPtr<Entity> entity = lookupEntity(0, str, loc, 0);
  if (!entity.isNull()
      && entity->defaulted()
      && options().warnDefaultEntityReference) {
    setNextLocation(loc);
    message(ParserMessages::defaultEntityInAttribute,
	    StringMessageArg(str));
  }
  return entity;
}

Boolean ParserState::defineId(const StringC &str, const Location &loc,
			      Location &prevLoc)
{
  if (!inInstance())
    return 1;
  Id *id = lookupCreateId(str);
  if (id->defined()) {
    prevLoc = id->defLocation();
    return 0;
  }
  id->define(loc);
  return 1;
}

void ParserState::noteIdref(const StringC &str, const Location &loc)
{
  if (!inInstance() || !options().errorIdref)
    return;
  Id *id = lookupCreateId(str);
  if (!id->defined())
    id->addPendingRef(loc);
}

void ParserState::noteCurrentAttribute(size_t i, AttributeValue *value)
{
  if (inInstance())
    currentAttributes_[i] = value;
}

ConstPtr<AttributeValue> ParserState::getCurrentAttribute(size_t i) const
{
  if (!inInstance())
    return ConstPtr<AttributeValue>();
  return currentAttributes_[i];
}

const Syntax &ParserState::attributeSyntax() const
{
  return syntax();
}

#ifdef SP_NAMESPACE
}
#endif