Sha256: 55ca413adfc673632ac2698896a2c8ad88653c497062066c2f4042f7ec1ea2bc

Contents?: true

Size: 1.93 KB

Versions: 18

Compression:

Stored size: 1.93 KB

Contents

class Code
  class Parser
    class If < ::Code::Parser
      def parse
        if first_operator = match([IF_KEYWORD, UNLESS_KEYWORD])
          previous_cursor = cursor
          first_comments = parse_comments
          first_condition = parse_subclass(::Code::Parser::If)

          if first_condition
            first_body = parse_code

            others = []

            while (other = parse_else) || (other = parse_elsif)
              others << other
            end

            others = nil if others.empty?

            match(END_KEYWORD)

            {
              if: {
                first_operator: first_operator,
                first_comments: first_comments,
                first_condition: first_condition,
                first_body: first_body,
                others: others
              }.compact
            }
          else
            @cursor = previous_cursor
            buffer!
            parse_subclass(::Code::Parser::IfModifier)
          end
        else
          parse_subclass(::Code::Parser::IfModifier)
        end
      end

      private

      def parse_else
        return unless match(ELSE_KEYWORD)
        comments_before = parse_comments

        operator = match([IF_KEYWORD, UNLESS_KEYWORD]) || ELSE_KEYWORD

        comments_after = parse_comments

        condition = parse_subclass(::Code::Parser::If)

        body = parse_code

        {
          operator: operator,
          comments_before: comments_before,
          comments_after: comments_after,
          condition: condition,
          body: body
        }.compact
      end

      def parse_elsif
        return unless operator = match([ELSIF_KEYWORD, ELSUNLESS_KEYWORD])

        comments = parse_comments
        condition = parse_subclass(::Code::Parser::If)
        body = parse_code

        {
          operator: operator,
          comments: comments,
          condition: condition,
          body: body
        }.compact
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
template-ruby-parser-0.1.8 lib/code/parser/if.rb
code-ruby-parser-0.1.8 lib/code/parser/if.rb
template-ruby-parser-0.1.7 lib/code/parser/if.rb
code-ruby-parser-0.1.7 lib/code/parser/if.rb
template-ruby-parser-0.1.6 lib/code/parser/if.rb
code-ruby-parser-0.1.6 lib/code/parser/if.rb
template-ruby-parser-0.1.5 lib/code/parser/if.rb
code-ruby-parser-0.1.5 lib/code/parser/if.rb
template-ruby-parser-0.1.4 lib/code/parser/if.rb
code-ruby-parser-0.1.4 lib/code/parser/if.rb
template-ruby-parser-0.1.3 lib/code/parser/if.rb
code-ruby-parser-0.1.3 lib/code/parser/if.rb
template-ruby-parser-0.1.2 lib/code/parser/if.rb
code-ruby-parser-0.1.2 lib/code/parser/if.rb
template-ruby-parser-0.1.1 lib/code/parser/if.rb
code-ruby-parser-0.1.1 lib/code/parser/if.rb
template-ruby-parser-0.1.0 lib/code/parser/if.rb
code-ruby-parser-0.1.0 lib/code/parser/if.rb