|  | 
|  | 1 | +# frozen_string_literal: true | 
|  | 2 | + | 
|  | 3 | +module RuboCop | 
|  | 4 | +  module Ext | 
|  | 5 | +    # Extensions for `regexp_parser` gem | 
|  | 6 | +    module RegexpParser | 
|  | 7 | +      # Source map for RegexpParser nodes | 
|  | 8 | +      class Map < ::Parser::Source::Map | 
|  | 9 | +        attr_reader :body, :quantifier, :begin, :end | 
|  | 10 | + | 
|  | 11 | +        def initialize(expression, body:, quantifier: nil, begin_l: nil, end_l: nil) | 
|  | 12 | +          @begin = begin_l | 
|  | 13 | +          @end = end_l | 
|  | 14 | +          @body = body | 
|  | 15 | +          @quantifier = quantifier | 
|  | 16 | +          super(expression) | 
|  | 17 | +        end | 
|  | 18 | +      end | 
|  | 19 | + | 
|  | 20 | +      module Expression | 
|  | 21 | +        # Add `expression` and `loc` to all `regexp_parser` nodes | 
|  | 22 | +        module Base | 
|  | 23 | +          attr_accessor :origin | 
|  | 24 | + | 
|  | 25 | +          # Shortcut to `loc.expression` | 
|  | 26 | +          def expression | 
|  | 27 | +            @expression ||= origin.adjust(begin_pos: ts, end_pos: ts + full_length) | 
|  | 28 | +          end | 
|  | 29 | + | 
|  | 30 | +          # @returns a location map like `parser` does, with: | 
|  | 31 | +          #   - expression: complete expression | 
|  | 32 | +          #   - quantifier: for `+`, `{1,2}`, etc. | 
|  | 33 | +          #   - begin/end: for `[` and `]` (only CharacterSet for now) | 
|  | 34 | +          # | 
|  | 35 | +          # E.g. | 
|  | 36 | +          #     [a-z]{2,} | 
|  | 37 | +          #     ^^^^^^^^^ expression | 
|  | 38 | +          #          ^^^^ quantifier | 
|  | 39 | +          #     ^^^^^     body | 
|  | 40 | +          #     ^         begin | 
|  | 41 | +          #         ^     end | 
|  | 42 | +          # | 
|  | 43 | +          # Please open issue if you need other locations | 
|  | 44 | +          def loc | 
|  | 45 | +            @loc ||= begin | 
|  | 46 | +              Map.new(expression, **build_location) | 
|  | 47 | +            end | 
|  | 48 | +          end | 
|  | 49 | + | 
|  | 50 | +          private | 
|  | 51 | + | 
|  | 52 | +          def build_location | 
|  | 53 | +            return { body: expression } unless (q = quantifier) | 
|  | 54 | + | 
|  | 55 | +            body = expression.adjust(end_pos: -q.text.length) | 
|  | 56 | +            q_loc = expression.with(begin_pos: body.end_pos) | 
|  | 57 | +            { body: body, quantifier: q_loc } | 
|  | 58 | +          end | 
|  | 59 | +        end | 
|  | 60 | + | 
|  | 61 | +        # Provide `CharacterSet` with `begin` and `end` locations. | 
|  | 62 | +        module CharacterSet | 
|  | 63 | +          def build_location | 
|  | 64 | +            h = super | 
|  | 65 | +            body = h[:body] | 
|  | 66 | +            h.merge!( | 
|  | 67 | +              begin_l: body.with(end_pos: body.begin_pos + 1), | 
|  | 68 | +              end_l: body.with(begin_pos: body.end_pos - 1) | 
|  | 69 | +            ) | 
|  | 70 | +          end | 
|  | 71 | +        end | 
|  | 72 | +      end | 
|  | 73 | +      ::Regexp::Expression::Base.include Expression::Base | 
|  | 74 | +      ::Regexp::Expression::CharacterSet.include Expression::CharacterSet | 
|  | 75 | +    end | 
|  | 76 | +  end | 
|  | 77 | +end | 
0 commit comments