# Autogenerated from a Treetop grammar. Edits may be lost.


module Mail
  module ContentTransferEncoding
    include Treetop::Runtime

    def root
      @root ||= :primary
    end

    include RFC2822

    include RFC2045

    module Primary0
      def CFWS1
        elements[0]
      end

      def encoding
        elements[1]
      end

      def CFWS2
        elements[2]
      end

      def CFWS3
        elements[4]
      end
    end

    def _nt_primary
      start_index = index
      if node_cache[:primary].has_key?(index)
        cached = node_cache[:primary][index]
        if cached
          cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
          @index = cached.interval.end
        end
        return cached
      end

      i0, s0 = index, []
      r1 = _nt_CFWS
      s0 << r1
      if r1
        r2 = _nt_encoding
        s0 << r2
        if r2
          r3 = _nt_CFWS
          s0 << r3
          if r3
            if has_terminal?(";", false, index)
              r5 = instantiate_node(SyntaxNode,input, index...(index + 1))
              @index += 1
            else
              terminal_parse_failure(";")
              r5 = nil
            end
            if r5
              r4 = r5
            else
              r4 = instantiate_node(SyntaxNode,input, index...index)
            end
            s0 << r4
            if r4
              r6 = _nt_CFWS
              s0 << r6
            end
          end
        end
      end
      if s0.last
        r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
        r0.extend(Primary0)
      else
        @index = i0
        r0 = nil
      end

      node_cache[:primary][start_index] = r0

      r0
    end

    module Encoding0
      def ietf_token
        elements[0]
      end

    end

    module Encoding1
      def text_value
        ietf_token.text_value
      end
    end

    def _nt_encoding
      start_index = index
      if node_cache[:encoding].has_key?(index)
        cached = node_cache[:encoding][index]
        if cached
          cached = SyntaxNode.new(input, index...(index + 1)) if cached == true
          @index = cached.interval.end
        end
        return cached
      end

      i0 = index
      i1, s1 = index, []
      r2 = _nt_ietf_token
      s1 << r2
      if r2
        if has_terminal?("s", false, index)
          r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
          @index += 1
        else
          terminal_parse_failure("s")
          r4 = nil
        end
        if r4
          r3 = r4
        else
          r3 = instantiate_node(SyntaxNode,input, index...index)
        end
        s1 << r3
      end
      if s1.last
        r1 = instantiate_node(SyntaxNode,input, i1...index, s1)
        r1.extend(Encoding0)
        r1.extend(Encoding1)
      else
        @index = i1
        r1 = nil
      end
      if r1
        r0 = r1
      else
        r5 = _nt_custom_x_token
        if r5
          r0 = r5
        else
          @index = i0
          r0 = nil
        end
      end

      node_cache[:encoding][start_index] = r0

      r0
    end

  end

  class ContentTransferEncodingParser < Treetop::Runtime::CompiledParser
    include ContentTransferEncoding
  end

end