summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rexml-3.4.1/lib/rexml/parsers/baseparser.rb
blob: 44aacfa2fe69a1d1780153a1336b4f8ccad21187 (plain)
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# frozen_string_literal: true
require_relative '../parseexception'
require_relative '../undefinednamespaceexception'
require_relative '../security'
require_relative '../source'
require 'set'
require "strscan"

module REXML
  module Parsers
    unless [].respond_to?(:tally)
      module EnumerableTally
        refine Enumerable do
          def tally
            counts = {}
            each do |item|
              counts[item] ||= 0
              counts[item] += 1
            end
            counts
          end
        end
      end
      using EnumerableTally
    end

    if StringScanner::Version < "3.0.8"
      module StringScannerCaptures
        refine StringScanner do
          def captures
            values_at(*(1...size))
          end
        end
      end
      using StringScannerCaptures
    end

    # = Using the Pull Parser
    # <em>This API is experimental, and subject to change.</em>
    #  parser = PullParser.new( "<a>text<b att='val'/>txet</a>" )
    #  while parser.has_next?
    #    res = parser.next
    #    puts res[1]['att'] if res.start_tag? and res[0] == 'b'
    #  end
    # See the PullEvent class for information on the content of the results.
    # The data is identical to the arguments passed for the various events to
    # the StreamListener API.
    #
    # Notice that:
    #  parser = PullParser.new( "<a>BAD DOCUMENT" )
    #  while parser.has_next?
    #    res = parser.next
    #    raise res[1] if res.error?
    #  end
    #
    # Nat Price gave me some good ideas for the API.
    class BaseParser
      LETTER = '[:alpha:]'
      DIGIT = '[:digit:]'

      COMBININGCHAR = '' # TODO
      EXTENDER = ''      # TODO

      NCNAME_STR= "[#{LETTER}_][-[:alnum:]._#{COMBININGCHAR}#{EXTENDER}]*"
      QNAME_STR= "(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})"
      QNAME = /(#{QNAME_STR})/

      # Just for backward compatibility. For example, kramdown uses this.
      # It's not used in REXML.
      UNAME_STR= "(?:#{NCNAME_STR}:)?#{NCNAME_STR}"

      NAMECHAR = '[\-\w\.:]'
      NAME = "([\\w:]#{NAMECHAR}*)"
      NMTOKEN = "(?:#{NAMECHAR})+"
      NMTOKENS = "#{NMTOKEN}(\\s+#{NMTOKEN})*"
      REFERENCE = "&(?:#{NAME};|#\\d+;|#x[0-9a-fA-F]+;)"
      REFERENCE_RE = /#{REFERENCE}/

      DOCTYPE_START = /\A\s*<!DOCTYPE\s/um
      DOCTYPE_END = /\A\s*\]\s*>/um
      ATTRIBUTE_PATTERN = /\s*(#{QNAME_STR})\s*=\s*(["'])(.*?)\4/um
      COMMENT_START = /\A<!--/u
      COMMENT_PATTERN = /<!--(.*?)-->/um
      CDATA_START = /\A<!\[CDATA\[/u
      CDATA_END = /\A\s*\]\s*>/um
      CDATA_PATTERN = /<!\[CDATA\[(.*?)\]\]>/um
      XMLDECL_START = /\A<\?xml\s/u;
      XMLDECL_PATTERN = /<\?xml\s+(.*?)\?>/um
      INSTRUCTION_START = /\A<\?/u
      INSTRUCTION_PATTERN = /<\?#{NAME}(\s+.*?)?\?>/um
      TAG_MATCH = /\A<((?>#{QNAME_STR}))/um
      CLOSE_MATCH = /\A\s*<\/(#{QNAME_STR})\s*>/um

      VERSION = /\bversion\s*=\s*["'](.*?)['"]/um
      ENCODING = /\bencoding\s*=\s*["'](.*?)['"]/um
      STANDALONE = /\bstandalone\s*=\s*["'](.*?)['"]/um

      ENTITY_START = /\A\s*<!ENTITY/
      ELEMENTDECL_START = /\A\s*<!ELEMENT/um
      ELEMENTDECL_PATTERN = /\A\s*(<!ELEMENT.*?)>/um
      SYSTEMENTITY = /\A\s*(%.*?;)\s*$/um
      ENUMERATION = "\\(\\s*#{NMTOKEN}(?:\\s*\\|\\s*#{NMTOKEN})*\\s*\\)"
      NOTATIONTYPE = "NOTATION\\s+\\(\\s*#{NAME}(?:\\s*\\|\\s*#{NAME})*\\s*\\)"
      ENUMERATEDTYPE = "(?:(?:#{NOTATIONTYPE})|(?:#{ENUMERATION}))"
      ATTTYPE = "(CDATA|ID|IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS|#{ENUMERATEDTYPE})"
      ATTVALUE = "(?:\"((?:[^<&\"]|#{REFERENCE})*)\")|(?:'((?:[^<&']|#{REFERENCE})*)')"
      DEFAULTDECL = "(#REQUIRED|#IMPLIED|(?:(#FIXED\\s+)?#{ATTVALUE}))"
      ATTDEF = "\\s+#{NAME}\\s+#{ATTTYPE}\\s+#{DEFAULTDECL}"
      ATTDEF_RE = /#{ATTDEF}/
      ATTLISTDECL_START = /\A\s*<!ATTLIST/um
      ATTLISTDECL_PATTERN = /\A\s*<!ATTLIST\s+#{NAME}(?:#{ATTDEF})*\s*>/um

      TEXT_PATTERN = /\A([^<]*)/um

      # Entity constants
      PUBIDCHAR = "\x20\x0D\x0Aa-zA-Z0-9\\-()+,./:=?;!*@$_%#"
      SYSTEMLITERAL = %Q{((?:"[^"]*")|(?:'[^']*'))}
      PUBIDLITERAL = %Q{("[#{PUBIDCHAR}']*"|'[#{PUBIDCHAR}]*')}
      EXTERNALID = "(?:(?:(SYSTEM)\\s+#{SYSTEMLITERAL})|(?:(PUBLIC)\\s+#{PUBIDLITERAL}\\s+#{SYSTEMLITERAL}))"
      NDATADECL = "\\s+NDATA\\s+#{NAME}"
      PEREFERENCE = "%#{NAME};"
      ENTITYVALUE = %Q{((?:"(?:[^%&"]|#{PEREFERENCE}|#{REFERENCE})*")|(?:'([^%&']|#{PEREFERENCE}|#{REFERENCE})*'))}
      PEDEF = "(?:#{ENTITYVALUE}|#{EXTERNALID})"
      ENTITYDEF = "(?:#{ENTITYVALUE}|(?:#{EXTERNALID}(#{NDATADECL})?))"
      PEDECL = "<!ENTITY\\s+(%)\\s+#{NAME}\\s+#{PEDEF}\\s*>"
      GEDECL = "<!ENTITY\\s+#{NAME}\\s+#{ENTITYDEF}\\s*>"
      ENTITYDECL = /\s*(?:#{GEDECL})|\s*(?:#{PEDECL})/um

      NOTATIONDECL_START = /\A\s*<!NOTATION/um
      EXTERNAL_ID_PUBLIC = /\A\s*PUBLIC\s+#{PUBIDLITERAL}\s+#{SYSTEMLITERAL}\s*/um
      EXTERNAL_ID_SYSTEM = /\A\s*SYSTEM\s+#{SYSTEMLITERAL}\s*/um
      PUBLIC_ID = /\A\s*PUBLIC\s+#{PUBIDLITERAL}\s*/um

      EREFERENCE = /&(?!#{NAME};)/

      DEFAULT_ENTITIES = {
        'gt' => [/&gt;/, '&gt;', '>', />/],
        'lt' => [/&lt;/, '&lt;', '<', /</],
        'quot' => [/&quot;/, '&quot;', '"', /"/],
        "apos" => [/&apos;/, "&apos;", "'", /'/]
      }

      module Private
        PEREFERENCE_PATTERN = /#{PEREFERENCE}/um
        TAG_PATTERN = /((?>#{QNAME_STR}))\s*/um
        CLOSE_PATTERN = /(#{QNAME_STR})\s*>/um
        ATTLISTDECL_END = /\s+#{NAME}(?:#{ATTDEF})*\s*>/um
        NAME_PATTERN = /#{NAME}/um
        GEDECL_PATTERN = "\\s+#{NAME}\\s+#{ENTITYDEF}\\s*>"
        PEDECL_PATTERN = "\\s+(%)\\s+#{NAME}\\s+#{PEDEF}\\s*>"
        ENTITYDECL_PATTERN = /(?:#{GEDECL_PATTERN})|(?:#{PEDECL_PATTERN})/um
        CARRIAGE_RETURN_NEWLINE_PATTERN = /\r\n?/
        CHARACTER_REFERENCES = /&#((?:\d+)|(?:x[a-fA-F0-9]+));/
        DEFAULT_ENTITIES_PATTERNS = {}
        default_entities = ['gt', 'lt', 'quot', 'apos', 'amp']
        default_entities.each do |term|
          DEFAULT_ENTITIES_PATTERNS[term] = /&#{term};/
        end
        XML_PREFIXED_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
      end
      private_constant :Private

      def initialize( source )
        self.stream = source
        @listeners = []
        @prefixes = Set.new
        @entity_expansion_count = 0
        @entity_expansion_limit = Security.entity_expansion_limit
        @entity_expansion_text_limit = Security.entity_expansion_text_limit
        @source.ensure_buffer
      end

      def add_listener( listener )
        @listeners << listener
      end

      attr_reader :source
      attr_reader :entity_expansion_count
      attr_writer :entity_expansion_limit
      attr_writer :entity_expansion_text_limit

      def stream=( source )
        @source = SourceFactory.create_from( source )
        reset
      end

      def reset
        @closed = nil
        @have_root = false
        @document_status = nil
        @tags = []
        @stack = []
        @entities = []
        @namespaces = {"xml" => Private::XML_PREFIXED_NAMESPACE}
        @namespaces_restore_stack = []
      end

      def position
        if @source.respond_to? :position
          @source.position
        else
          # FIXME
          0
        end
      end

      # Returns true if there are no more events
      def empty?
        return (@source.empty? and @stack.empty?)
      end

      # Returns true if there are more events.  Synonymous with !empty?
      def has_next?
        return !(@source.empty? and @stack.empty?)
      end

      # Push an event back on the head of the stream.  This method
      # has (theoretically) infinite depth.
      def unshift token
        @stack.unshift(token)
      end

      # Peek at the +depth+ event in the stack.  The first element on the stack
      # is at depth 0.  If +depth+ is -1, will parse to the end of the input
      # stream and return the last event, which is always :end_document.
      # Be aware that this causes the stream to be parsed up to the +depth+
      # event, so you can effectively pre-parse the entire document (pull the
      # entire thing into memory) using this method.
      def peek depth=0
        raise %Q[Illegal argument "#{depth}"] if depth < -1
        temp = []
        if depth == -1
          temp.push(pull()) until empty?
        else
          while @stack.size+temp.size < depth+1
            temp.push(pull())
          end
        end
        @stack += temp if temp.size > 0
        @stack[depth]
      end

      # Returns the next event.  This is a +PullEvent+ object.
      def pull
        @source.drop_parsed_content

        pull_event.tap do |event|
          @listeners.each do |listener|
            listener.receive event
          end
        end
      end

      def pull_event
        if @closed
          x, @closed = @closed, nil
          return [ :end_element, x ]
        end
        if empty?
          if @document_status == :in_doctype
            raise ParseException.new("Malformed DOCTYPE: unclosed", @source)
          end
          unless @tags.empty?
            path = "/" + @tags.join("/")
            raise ParseException.new("Missing end tag for '#{path}'", @source)
          end
          return [ :end_document ]
        end
        return @stack.shift if @stack.size > 0
        #STDERR.puts @source.encoding
        #STDERR.puts "BUFFER = #{@source.buffer.inspect}"

        @source.ensure_buffer
        if @document_status == nil
          start_position = @source.position
          if @source.match?("<?", true)
            return process_instruction
          elsif @source.match?("<!", true)
            if @source.match?("--", true)
              md = @source.match(/(.*?)-->/um, true)
              if md.nil?
                raise REXML::ParseException.new("Unclosed comment", @source)
              end
              if /--|-\z/.match?(md[1])
                raise REXML::ParseException.new("Malformed comment", @source)
              end
              return [ :comment, md[1] ]
            elsif @source.match?("DOCTYPE", true)
              base_error_message = "Malformed DOCTYPE"
              unless @source.match?(/\s+/um, true)
                if @source.match?(">")
                  message = "#{base_error_message}: name is missing"
                else
                  message = "#{base_error_message}: invalid name"
                end
                @source.position = start_position
                raise REXML::ParseException.new(message, @source)
              end
              name = parse_name(base_error_message)
              @source.match?(/\s*/um, true) # skip spaces
              if @source.match?("[", true)
                id = [nil, nil, nil]
                @document_status = :in_doctype
              elsif @source.match?(">", true)
                id = [nil, nil, nil]
                @document_status = :after_doctype
                @source.ensure_buffer
              else
                id = parse_id(base_error_message,
                              accept_external_id: true,
                              accept_public_id: false)
                if id[0] == "SYSTEM"
                  # For backward compatibility
                  id[1], id[2] = id[2], nil
                end
                @source.match?(/\s*/um, true) # skip spaces
                if @source.match?("[", true)
                  @document_status = :in_doctype
                elsif @source.match?(">", true)
                  @document_status = :after_doctype
                  @source.ensure_buffer
                else
                  message = "#{base_error_message}: garbage after external ID"
                  raise REXML::ParseException.new(message, @source)
                end
              end
              args = [:start_doctype, name, *id]
              if @document_status == :after_doctype
                @source.match?(/\s*/um, true)
                @stack << [ :end_doctype ]
              end
              return args
            else
              message = "Invalid XML"
              raise REXML::ParseException.new(message, @source)
            end
          end
        end
        if @document_status == :in_doctype
          @source.match?(/\s*/um, true) # skip spaces
          start_position = @source.position
          if @source.match?("<!", true)
            if @source.match?("ELEMENT", true)
              md = @source.match(/(.*?)>/um, true)
              raise REXML::ParseException.new( "Bad ELEMENT declaration!", @source ) if md.nil?
              return [ :elementdecl, "<!ELEMENT" + md[1] ]
            elsif @source.match?("ENTITY", true)
              match_data = @source.match(Private::ENTITYDECL_PATTERN, true)
              unless match_data
                raise REXML::ParseException.new("Malformed entity declaration", @source)
              end
              match = [:entitydecl, *match_data.captures.compact]
              ref = false
              if match[1] == '%'
                ref = true
                match.delete_at 1
              end
              # Now we have to sort out what kind of entity reference this is
              if match[2] == 'SYSTEM'
                # External reference
                match[3] = match[3][1..-2] # PUBID
                match.delete_at(4) if match.size > 4 # Chop out NDATA decl
                # match is [ :entity, name, SYSTEM, pubid(, ndata)? ]
              elsif match[2] == 'PUBLIC'
                # External reference
                match[3] = match[3][1..-2] # PUBID
                match[4] = match[4][1..-2] # HREF
                match.delete_at(5) if match.size > 5 # Chop out NDATA decl
                # match is [ :entity, name, PUBLIC, pubid, href(, ndata)? ]
              elsif Private::PEREFERENCE_PATTERN.match?(match[2])
                raise REXML::ParseException.new("Parameter entity references forbidden in internal subset: #{match[2]}", @source)
              else
                match[2] = match[2][1..-2]
                match.pop if match.size == 4
                # match is [ :entity, name, value ]
              end
              match << '%' if ref
              return match
            elsif @source.match?("ATTLIST", true)
              md = @source.match(Private::ATTLISTDECL_END, true)
              raise REXML::ParseException.new( "Bad ATTLIST declaration!", @source ) if md.nil?
              element = md[1]
              contents = "<!ATTLIST" + md[0]

              pairs = {}
              values = md[0].strip.scan( ATTDEF_RE )
              values.each do |attdef|
                unless attdef[3] == "#IMPLIED"
                  attdef.compact!
                  val = attdef[3]
                  val = attdef[4] if val == "#FIXED "
                  pairs[attdef[0]] = val
                  if attdef[0] =~ /^xmlns:(.*)/
                    @namespaces[$1] = val
                  end
                end
              end
              return [ :attlistdecl, element, pairs, contents ]
            elsif @source.match?("NOTATION", true)
              base_error_message = "Malformed notation declaration"
              unless @source.match?(/\s+/um, true)
                if @source.match?(">")
                  message = "#{base_error_message}: name is missing"
                else
                  message = "#{base_error_message}: invalid name"
                end
                @source.position = start_position
                raise REXML::ParseException.new(message, @source)
              end
              name = parse_name(base_error_message)
              id = parse_id(base_error_message,
                            accept_external_id: true,
                            accept_public_id: true)
              @source.match?(/\s*/um, true) # skip spaces
              unless @source.match?(">", true)
                message = "#{base_error_message}: garbage before end >"
                raise REXML::ParseException.new(message, @source)
              end
              return [:notationdecl, name, *id]
            elsif md = @source.match(/--(.*?)-->/um, true)
              case md[1]
              when /--/, /-\z/
                raise REXML::ParseException.new("Malformed comment", @source)
              end
              return [ :comment, md[1] ] if md
            end
          elsif match = @source.match(/(%.*?;)\s*/um, true)
            return [ :externalentity, match[1] ]
          elsif @source.match?(/\]\s*>/um, true)
            @document_status = :after_doctype
            return [ :end_doctype ]
          end
          if @document_status == :in_doctype
            raise ParseException.new("Malformed DOCTYPE: invalid declaration", @source)
          end
        end
        if @document_status == :after_doctype
          @source.match?(/\s*/um, true)
        end
        begin
          start_position = @source.position
          if @source.match?("<", true)
            # :text's read_until may remain only "<" in buffer. In the
            # case, buffer is empty here. So we need to fill buffer
            # here explicitly.
            @source.ensure_buffer
            if @source.match?("/", true)
              @namespaces_restore_stack.pop
              last_tag = @tags.pop
              md = @source.match(Private::CLOSE_PATTERN, true)
              if md and !last_tag
                message = "Unexpected top-level end tag (got '#{md[1]}')"
                raise REXML::ParseException.new(message, @source)
              end
              if md.nil? or last_tag != md[1]
                message = "Missing end tag for '#{last_tag}'"
                message += " (got '#{md[1]}')" if md
                @source.position = start_position if md.nil?
                raise REXML::ParseException.new(message, @source)
              end
              return [ :end_element, last_tag ]
            elsif @source.match?("!", true)
              md = @source.match(/([^>]*>)/um)
              #STDERR.puts "SOURCE BUFFER = #{source.buffer}, #{source.buffer.size}"
              raise REXML::ParseException.new("Malformed node", @source) unless md
              if md[0][0] == ?-
                md = @source.match(/--(.*?)-->/um, true)

                if md.nil? || /--|-\z/.match?(md[1])
                  raise REXML::ParseException.new("Malformed comment", @source)
                end

                return [ :comment, md[1] ]
              else
                md = @source.match(/\[CDATA\[(.*?)\]\]>/um, true)
                return [ :cdata, md[1] ] if md
              end
              raise REXML::ParseException.new( "Declarations can only occur "+
                "in the doctype declaration.", @source)
            elsif @source.match?("?", true)
              return process_instruction
            else
              # Get the next tag
              md = @source.match(Private::TAG_PATTERN, true)
              unless md
                @source.position = start_position
                raise REXML::ParseException.new("malformed XML: missing tag start", @source)
              end
              tag = md[1]
              @document_status = :in_element
              @prefixes.clear
              @prefixes << md[2] if md[2]
              push_namespaces_restore
              attributes, closed = parse_attributes(@prefixes)
              # Verify that all of the prefixes have been defined
              for prefix in @prefixes
                unless @namespaces.key?(prefix)
                  raise UndefinedNamespaceException.new(prefix,@source,self)
                end
              end

              if closed
                @closed = tag
                pop_namespaces_restore
              else
                if @tags.empty? and @have_root
                  raise ParseException.new("Malformed XML: Extra tag at the end of the document (got '<#{tag}')", @source)
                end
                @tags.push( tag )
              end
              @have_root = true
              return [ :start_element, tag, attributes ]
            end
          else
            text = @source.read_until("<")
            if text.chomp!("<")
              @source.position -= "<".bytesize
            end
            if @tags.empty?
              unless /\A\s*\z/.match?(text)
                if @have_root
                  raise ParseException.new("Malformed XML: Extra content at the end of the document (got '#{text}')", @source)
                else
                  raise ParseException.new("Malformed XML: Content at the start of the document (got '#{text}')", @source)
                end
              end
              return pull_event if @have_root
            end
            return [ :text, text ]
          end
        rescue REXML::UndefinedNamespaceException
          raise
        rescue REXML::ParseException
          raise
        rescue => error
          raise REXML::ParseException.new( "Exception parsing",
            @source, self, (error ? error : $!) )
        end
        return [ :dummy ]
      end
      private :pull_event

      def entity( reference, entities )
        return unless entities

        value = entities[ reference ]
        return if value.nil?

        record_entity_expansion
        unnormalize( value, entities )
      end

      # Escapes all possible entities
      def normalize( input, entities=nil, entity_filter=nil )
        copy = input.clone
        # Doing it like this rather than in a loop improves the speed
        copy.gsub!( EREFERENCE, '&amp;' )
        entities.each do |key, value|
          copy.gsub!( value, "&#{key};" ) unless entity_filter and
                                      entity_filter.include?(entity)
        end if entities
        copy.gsub!( EREFERENCE, '&amp;' )
        DEFAULT_ENTITIES.each do |key, value|
          copy.gsub!( value[3], value[1] )
        end
        copy
      end

      # Unescapes all possible entities
      def unnormalize( string, entities=nil, filter=nil )
        if string.include?("\r")
          rv = string.gsub( Private::CARRIAGE_RETURN_NEWLINE_PATTERN, "\n" )
        else
          rv = string.dup
        end
        matches = rv.scan( REFERENCE_RE )
        return rv if matches.size == 0
        rv.gsub!( Private::CHARACTER_REFERENCES ) {
          m=$1
          if m.start_with?("x")
            code_point = Integer(m[1..-1], 16)
          else
            code_point = Integer(m, 10)
          end
          [code_point].pack('U*')
        }
        matches.collect!{|x|x[0]}.compact!
        if filter
          matches.reject! do |entity_reference|
            filter.include?(entity_reference)
          end
        end
        if matches.size > 0
          matches.tally.each do |entity_reference, n|
            entity_expansion_count_before = @entity_expansion_count
            entity_value = entity( entity_reference, entities )
            if entity_value
              if n > 1
                entity_expansion_count_delta =
                  @entity_expansion_count - entity_expansion_count_before
                record_entity_expansion(entity_expansion_count_delta * (n - 1))
              end
              re = Private::DEFAULT_ENTITIES_PATTERNS[entity_reference] || /&#{entity_reference};/
              rv.gsub!( re, entity_value )
              if rv.bytesize > @entity_expansion_text_limit
                raise "entity expansion has grown too large"
              end
            else
              er = DEFAULT_ENTITIES[entity_reference]
              rv.gsub!( er[0], er[2] ) if er
            end
          end
          rv.gsub!( Private::DEFAULT_ENTITIES_PATTERNS['amp'], '&' )
        end
        rv
      end

      private
      def add_namespace(prefix, uri)
        @namespaces_restore_stack.last[prefix] = @namespaces[prefix]
        if uri.nil?
          @namespaces.delete(prefix)
        else
          @namespaces[prefix] = uri
        end
      end

      def push_namespaces_restore
        namespaces_restore = {}
        @namespaces_restore_stack.push(namespaces_restore)
        namespaces_restore
      end

      def pop_namespaces_restore
        namespaces_restore = @namespaces_restore_stack.pop
        namespaces_restore.each do |prefix, uri|
          if uri.nil?
            @namespaces.delete(prefix)
          else
            @namespaces[prefix] = uri
          end
        end
      end

      def record_entity_expansion(delta=1)
        @entity_expansion_count += delta
        if @entity_expansion_count > @entity_expansion_limit
          raise "number of entity expansions exceeded, processing aborted."
        end
      end

      def need_source_encoding_update?(xml_declaration_encoding)
        return false if xml_declaration_encoding.nil?
        return false if /\AUTF-16\z/i =~ xml_declaration_encoding
        true
      end

      def parse_name(base_error_message)
        md = @source.match(Private::NAME_PATTERN, true)
        unless md
          if @source.match?(/\S/um)
            message = "#{base_error_message}: invalid name"
          else
            message = "#{base_error_message}: name is missing"
          end
          raise REXML::ParseException.new(message, @source)
        end
        md[0]
      end

      def parse_id(base_error_message,
                   accept_external_id:,
                   accept_public_id:)
        if accept_external_id and (md = @source.match(EXTERNAL_ID_PUBLIC, true))
          pubid = system = nil
          pubid_literal = md[1]
          pubid = pubid_literal[1..-2] if pubid_literal # Remove quote
          system_literal = md[2]
          system = system_literal[1..-2] if system_literal # Remove quote
          ["PUBLIC", pubid, system]
        elsif accept_public_id and (md = @source.match(PUBLIC_ID, true))
          pubid = system = nil
          pubid_literal = md[1]
          pubid = pubid_literal[1..-2] if pubid_literal # Remove quote
          ["PUBLIC", pubid, nil]
        elsif accept_external_id and (md = @source.match(EXTERNAL_ID_SYSTEM, true))
          system = nil
          system_literal = md[1]
          system = system_literal[1..-2] if system_literal # Remove quote
          ["SYSTEM", nil, system]
        else
          details = parse_id_invalid_details(accept_external_id: accept_external_id,
                                             accept_public_id: accept_public_id)
          message = "#{base_error_message}: #{details}"
          raise REXML::ParseException.new(message, @source)
        end
      end

      def parse_id_invalid_details(accept_external_id:,
                                   accept_public_id:)
        public = /\A\s*PUBLIC/um
        system = /\A\s*SYSTEM/um
        if (accept_external_id or accept_public_id) and @source.match?(/#{public}/um)
          if @source.match?(/#{public}(?:\s+[^'"]|\s*[\[>])/um)
            return "public ID literal is missing"
          end
          unless @source.match?(/#{public}\s+#{PUBIDLITERAL}/um)
            return "invalid public ID literal"
          end
          if accept_public_id
            if @source.match?(/#{public}\s+#{PUBIDLITERAL}\s+[^'"]/um)
              return "system ID literal is missing"
            end
            unless @source.match?(/#{public}\s+#{PUBIDLITERAL}\s+#{SYSTEMLITERAL}/um)
              return "invalid system literal"
            end
            "garbage after system literal"
          else
            "garbage after public ID literal"
          end
        elsif accept_external_id and @source.match?(/#{system}/um)
          if @source.match?(/#{system}(?:\s+[^'"]|\s*[\[>])/um)
            return "system literal is missing"
          end
          unless @source.match?(/#{system}\s+#{SYSTEMLITERAL}/um)
            return "invalid system literal"
          end
          "garbage after system literal"
        else
          unless @source.match?(/\A\s*(?:PUBLIC|SYSTEM)\s/um)
            return "invalid ID type"
          end
          "ID type is missing"
        end
      end

      def process_instruction
        name = parse_name("Malformed XML: Invalid processing instruction node")
        if @source.match?(/\s+/um, true)
          match_data = @source.match(/(.*?)\?>/um, true)
          unless match_data
            raise ParseException.new("Malformed XML: Unclosed processing instruction", @source)
          end
          content = match_data[1]
        else
          content = nil
          unless @source.match?("?>", true)
            raise ParseException.new("Malformed XML: Unclosed processing instruction", @source)
          end
        end
        if name == "xml"
          if @document_status
            raise ParseException.new("Malformed XML: XML declaration is not at the start", @source)
          end
          version = VERSION.match(content)
          version = version[1] unless version.nil?
          encoding = ENCODING.match(content)
          encoding = encoding[1] unless encoding.nil?
          if need_source_encoding_update?(encoding)
            @source.encoding = encoding
          end
          if encoding.nil? and /\AUTF-16(?:BE|LE)\z/i =~ @source.encoding
            encoding = "UTF-16"
          end
          standalone = STANDALONE.match(content)
          standalone = standalone[1] unless standalone.nil?
          return [ :xmldecl, version, encoding, standalone ]
        end
        [:processing_instruction, name, content]
      end

      if StringScanner::Version < "3.1.1"
        def scan_quote
          @source.match(/(['"])/, true)&.[](1)
        end
      else
        def scan_quote
          case @source.peek_byte
          when 34 # '"'.ord
            @source.scan_byte
            '"'
          when 39 # "'".ord
            @source.scan_byte
            "'"
          else
            nil
          end
        end
      end

      def parse_attributes(prefixes)
        attributes = {}
        expanded_names = {}
        closed = false
        while true
          if @source.match?(">", true)
            return attributes, closed
          elsif @source.match?("/>", true)
            closed = true
            return attributes, closed
          elsif match = @source.match(QNAME, true)
            name = match[1]
            prefix = match[2]
            local_part = match[3]

            unless @source.match?(/\s*=\s*/um, true)
              message = "Missing attribute equal: <#{name}>"
              raise REXML::ParseException.new(message, @source)
            end
            unless quote = scan_quote
              message = "Missing attribute value start quote: <#{name}>"
              raise REXML::ParseException.new(message, @source)
            end
            start_position = @source.position
            value = @source.read_until(quote)
            unless value.chomp!(quote)
              @source.position = start_position
              message = "Missing attribute value end quote: <#{name}>: <#{quote}>"
              raise REXML::ParseException.new(message, @source)
            end
            @source.match?(/\s*/um, true)
            if prefix == "xmlns"
              if local_part == "xml"
                if value != Private::XML_PREFIXED_NAMESPACE
                  msg = "The 'xml' prefix must not be bound to any other namespace "+
                    "(http://www.w3.org/TR/REC-xml-names/#ns-decl)"
                  raise REXML::ParseException.new( msg, @source, self )
                end
              elsif local_part == "xmlns"
                msg = "The 'xmlns' prefix must not be declared "+
                  "(http://www.w3.org/TR/REC-xml-names/#ns-decl)"
                raise REXML::ParseException.new( msg, @source, self)
              end
              add_namespace(local_part, value)
            elsif prefix
              prefixes << prefix unless prefix == "xml"
            end

            if attributes[name]
              msg = "Duplicate attribute #{name.inspect}"
              raise REXML::ParseException.new(msg, @source, self)
            end

            unless prefix == "xmlns"
              uri = @namespaces[prefix]
              expanded_name = [uri, local_part]
              existing_prefix = expanded_names[expanded_name]
              if existing_prefix
                message = "Namespace conflict in adding attribute " +
                          "\"#{local_part}\": " +
                          "Prefix \"#{existing_prefix}\" = \"#{uri}\" and " +
                          "prefix \"#{prefix}\" = \"#{uri}\""
                raise REXML::ParseException.new(message, @source, self)
              end
              expanded_names[expanded_name] = prefix
            end

            attributes[name] = value
          else
            message = "Invalid attribute name: <#{@source.buffer.split(%r{[/>\s]}).first}>"
            raise REXML::ParseException.new(message, @source)
          end
        end
      end
    end
  end
end

=begin
  case event[0]
  when :start_element
  when :text
  when :end_element
  when :processing_instruction
  when :cdata
  when :comment
  when :xmldecl
  when :start_doctype
  when :end_doctype
  when :externalentity
  when :elementdecl
  when :entity
  when :attlistdecl
  when :notationdecl
  when :end_doctype
  end
=end