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


#
# Copyright (c) 2013 by Lifted Studios.  All Rights Reserved.
#

require 'treetop'

module KangaRuby
  module SequenceDiagram
    include Treetop::Runtime

    def root
      @root ||= :diagram
    end

    module Diagram0
      def actions
        elements
      end
    end

    def _nt_diagram
      start_index = index
      if node_cache[:diagram].has_key?(index)
        cached = node_cache[:diagram][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
        r1 = _nt_action
        if r1
          s0 << r1
        else
          break
        end
      end
      r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
      r0.extend(Diagram0)

      node_cache[:diagram][start_index] = r0

      r0
    end

    module Action0
      def participant1
        elements[0]
      end

      def arrow
        elements[1]
      end

      def participant2
        elements[2]
      end

    end

    module Action1
      def from
        elements[0].name
      end

      def style
        case elements[1].text_value
        when '->' then :solid
        when '-->' then :dotted
        end
      end

      def to
        elements[2].name
      end
    end

    def _nt_action
      start_index = index
      if node_cache[:action].has_key?(index)
        cached = node_cache[:action][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_participant
      s0 << r1
      if r1
        r2 = _nt_arrow
        s0 << r2
        if r2
          r3 = _nt_participant
          s0 << r3
          if r3
            if has_terminal?("\n", false, index)
              r4 = instantiate_node(SyntaxNode,input, index...(index + 1))
              @index += 1
            else
              terminal_parse_failure("\n")
              r4 = nil
            end
            s0 << r4
          end
        end
      end
      if s0.last
        r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
        r0.extend(Action0)
        r0.extend(Action1)
      else
        @index = i0
        r0 = nil
      end

      node_cache[:action][start_index] = r0

      r0
    end

    module Participant0
      def name
        text_value.strip
      end
    end

    def _nt_participant
      start_index = index
      if node_cache[:participant].has_key?(index)
        cached = node_cache[:participant][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
      if s0.empty?
        @index = i0
        r0 = nil
      else
        r0 = instantiate_node(SyntaxNode,input, i0...index, s0)
        r0.extend(Participant0)
      end

      node_cache[:participant][start_index] = r0

      r0
    end

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

      i0 = index
      if has_terminal?('->', false, index)
        r1 = instantiate_node(SyntaxNode,input, index...(index + 2))
        @index += 2
      else
        terminal_parse_failure('->')
        r1 = nil
      end
      if r1
        r0 = r1
      else
        if has_terminal?('-->', false, index)
          r2 = instantiate_node(SyntaxNode,input, index...(index + 3))
          @index += 3
        else
          terminal_parse_failure('-->')
          r2 = nil
        end
        if r2
          r0 = r2
        else
          @index = i0
          r0 = nil
        end
      end

      node_cache[:arrow][start_index] = r0

      r0
    end

  end

  class SequenceDiagramParser < Treetop::Runtime::CompiledParser
    include SequenceDiagram
  end

end
