Sha256: 00b1bbd1fe924a463aca2c7fdc9b84c6856858948a4bc783f82436fb79063df8

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require "parser/rubynext"

module RubyNext
  module Language
    module BuilderExt
      def match_pattern(lhs, match_t, rhs)
        n(:match_pattern, [lhs, rhs],
          binary_op_map(lhs, match_t, rhs))
      end

      def match_pattern_p(lhs, match_t, rhs)
        n(:match_pattern_p, [lhs, rhs],
          binary_op_map(lhs, match_t, rhs))
      end
    end

    class Builder < ::Parser::Builders::Default
      modernize

      # Unparser doesn't support kwargs node yet
      self.emit_kwargs = false if respond_to?(:emit_kwargs=)

      unless method_defined?(:match_pattern_p)
        include BuilderExt
      end
    end

    class << self
      def parser
        ::Parser::RubyNext.new(Builder.new).tap do |prs|
          prs.diagnostics.tap do |diagnostics|
            diagnostics.all_errors_are_fatal = true
          end
        end
      end

      def parse(source, file = "(string)")
        buffer = ::Parser::Source::Buffer.new(file).tap do |buffer|
          buffer.source = source
        end

        parser.parse(buffer)
      rescue ::Parser::SyntaxError => e
        raise ::SyntaxError, e.message
      end

      def parse_with_comments(source, file = "(string)")
        buffer = ::Parser::Source::Buffer.new(file).tap do |buffer|
          buffer.source = source
        end

        parser.parse_with_comments(buffer)
      rescue ::Parser::SyntaxError => e
        raise ::SyntaxError, e.message
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-next-core-0.11.0 lib/ruby-next/language/parser.rb