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


module HelpGrammar
  include Treetop::Runtime

  def root
    @root ||= :line
  end

  module Line0
    def spaces
      elements[0]
    end

    def sws
      elements[1]
    end

    def text
      elements[2]
    end

  end

  module Line1
    def to_tex(opened)
      res = ""
      unless sws.empty?
        if !opened.is_open
          res << "\\begin{description}\n"
          opened.is_open = true
        end
        res << "\\item["
        res << sws.elements.map { |s| s.to_tex }.join(",")
        res << "] "
      end
      res << "\\noindent " unless text.text_value.empty?
      res << quote(text.text_value)
    end
  end

  def _nt_line
    start_index = index
    if node_cache[:line].has_key?(index)
      cached = node_cache[:line][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_spaces
    s0 << r1
    if r1
      s2, i2 = [], index
      loop do
        r3 = _nt_switches
        if r3
          s2 << r3
        else
          break
        end
      end
      r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
      s0 << r2
      if r2
        r4 = _nt_text
        s0 << r4
        if r4
          s5, i5 = [], index
          loop do
            if has_terminal?('\G[\\n]', true, index)
              r6 = true
              @index += 1
            else
              r6 = nil
            end
            if r6
              s5 << r6
            else
              break
            end
          end
          r5 = instantiate_node(SyntaxNode,input, i5...index, s5)
          s0 << r5
        end
      end
    end
    if s0.last
      r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
      r0.extend(Line0)
      r0.extend(Line1)
    else
      @index = i0
      r0 = nil
    end

    node_cache[:line][start_index] = r0

    r0
  end

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

    s0, i0 = [], index
    loop do
      if has_terminal?('\G[\\s]', true, index)
        r1 = true
        @index += 1
      else
        r1 = nil
      end
      if r1
        s0 << r1
      else
        break
      end
    end
    r0 = instantiate_node(SyntaxNode,input, i0...index, s0)

    node_cache[:spaces][start_index] = r0

    r0
  end

  module Switches0
    def sw
      elements[1]
    end
  end

  module Switches1
    def to_tex
      sw.to_tex
    end
  end

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

    i0, s0 = index, []
    if has_terminal?("-", false, index)
      r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
      @index += 1
    else
      terminal_parse_failure("-")
      r1 = nil
    end
    s0 << r1
    if r1
      i2 = index
      r3 = _nt_short_switch
      if r3
        r2 = r3
      else
        r4 = _nt_long_switch
        if r4
          r2 = r4
        else
          @index = i2
          r2 = nil
        end
      end
      s0 << r2
    end
    if s0.last
      r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
      r0.extend(Switches0)
      r0.extend(Switches1)
    else
      @index = i0
      r0 = nil
    end

    node_cache[:switches][start_index] = r0

    r0
  end

  module ShortSwitch0
    def name
      elements[0]
    end

    def spaces
      elements[2]
    end
  end

  module ShortSwitch1
    def to_tex
      "\\Opt{-#{name.text_value}}"
    end
  end

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

    i0, s0 = index, []
    if has_terminal?('\G[a-zA-Z]', true, index)
      r1 = true
      @index += 1
    else
      r1 = nil
    end
    s0 << r1
    if r1
      if has_terminal?(",", false, index)
        r3 = instantiate_node(SyntaxNode,input, index...(index + 1))
        @index += 1
      else
        terminal_parse_failure(",")
        r3 = nil
      end
      if r3
        r2 = r3
      else
        r2 = instantiate_node(SyntaxNode,input, index...index)
      end
      s0 << r2
      if r2
        r4 = _nt_spaces
        s0 << r4
      end
    end
    if s0.last
      r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
      r0.extend(ShortSwitch0)
      r0.extend(ShortSwitch1)
    else
      @index = i0
      r0 = nil
    end

    node_cache[:short_switch][start_index] = r0

    r0
  end

  module LongSwitch0
  end

  module LongSwitch1
    def name
      elements[1]
    end

    def val
      elements[2]
    end

    def spaces
      elements[4]
    end
  end

  module LongSwitch2
    def to_tex
      val.empty? ? "\\LOpt{#{quote(name.text_value)}}" : "\\LOptArg{#{quote(name.text_value)}}{#{quote(val.text_value)}}"
    end
  end

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

    i0, s0 = index, []
    if has_terminal?("-", false, index)
      r1 = instantiate_node(SyntaxNode,input, index...(index + 1))
      @index += 1
    else
      terminal_parse_failure("-")
      r1 = nil
    end
    s0 << r1
    if r1
      s2, i2 = [], index
      loop do
        if has_terminal?('\G[^,\\s=]', true, index)
          r3 = true
          @index += 1
        else
          r3 = nil
        end
        if r3
          s2 << r3
        else
          break
        end
      end
      if s2.empty?
        @index = i2
        r2 = nil
      else
        r2 = instantiate_node(SyntaxNode,input, i2...index, s2)
      end
      s0 << r2
      if r2
        i5, s5 = index, []
        if has_terminal?("=", false, index)
          r6 = instantiate_node(SyntaxNode,input, index...(index + 1))
          @index += 1
        else
          terminal_parse_failure("=")
          r6 = nil
        end
        s5 << r6
        if r6
          s7, i7 = [], index
          loop do
            if has_terminal?('\G[^,\\s]', true, index)
              r8 = true
              @index += 1
            else
              r8 = nil
            end
            if r8
              s7 << r8
            else
              break
            end
          end
          if s7.empty?
            @index = i7
            r7 = nil
          else
            r7 = instantiate_node(SyntaxNode,input, i7...index, s7)
          end
          s5 << r7
        end
        if s5.last
          r5 = instantiate_node(SyntaxNode,input, i5...index, s5)
          r5.extend(LongSwitch0)
        else
          @index = i5
          r5 = nil
        end
        if r5
          r4 = r5
        else
          r4 = instantiate_node(SyntaxNode,input, index...index)
        end
        s0 << r4
        if r4
          if has_terminal?(",", false, index)
            r10 = instantiate_node(SyntaxNode,input, index...(index + 1))
            @index += 1
          else
            terminal_parse_failure(",")
            r10 = nil
          end
          if r10
            r9 = r10
          else
            r9 = instantiate_node(SyntaxNode,input, index...index)
          end
          s0 << r9
          if r9
            r11 = _nt_spaces
            s0 << r11
          end
        end
      end
    end
    if s0.last
      r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
      r0.extend(LongSwitch1)
      r0.extend(LongSwitch2)
    else
      @index = i0
      r0 = nil
    end

    node_cache[:long_switch][start_index] = r0

    r0
  end

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

    s0, i0 = [], index
    loop do
      if has_terminal?('\G[^\\n]', true, index)
        r1 = true
        @index += 1
      else
        r1 = nil
      end
      if r1
        s0 << r1
      else
        break
      end
    end
    r0 = instantiate_node(SyntaxNode,input, i0...index, s0)

    node_cache[:text][start_index] = r0

    r0
  end

end

class HelpGrammarParser < Treetop::Runtime::CompiledParser
  include HelpGrammar
end

