File: lexer.rb

package info (click to toggle)
ruby-rubocop-ast 1.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,256 kB
  • sloc: ruby: 15,071; yacc: 90; makefile: 9
file content (70 lines) | stat: -rw-r--r-- 1,570 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

begin
  require_relative 'lexer.rex'
rescue LoadError
  msg = '*** You must run `rake generate` to generate the lexer and the parser ***'
  puts '*' * msg.length, msg, '*' * msg.length
  raise
end

module RuboCop
  module AST
    class NodePattern
      # Lexer class for `NodePattern`
      #
      # Doc on how this fits in the compiling process:
      #   /docs/modules/ROOT/pages/node_pattern.adoc
      class Lexer < LexerRex
        Error = ScanError

        REGEXP_OPTIONS = {
          'i' => ::Regexp::IGNORECASE,
          'm' => ::Regexp::MULTILINE,
          'x' => ::Regexp::EXTENDED,
          'o' => 0
        }.freeze
        private_constant :REGEXP_OPTIONS

        attr_reader :source_buffer, :comments, :tokens

        def initialize(source)
          @tokens = []
          super()
          parse(source)
        end

        private

        # @return [token]
        def emit(type)
          value = ss[1] || ss.matched
          value = yield value if block_given?
          token = token(type, value)
          @tokens << token
          token
        end

        def emit_comment
          nil
        end

        def emit_regexp
          body = ss[1]
          options = ss[2]
          flag = options.each_char.sum { |c| REGEXP_OPTIONS[c] }

          emit(:tREGEXP) { Regexp.new(body, flag) }
        end

        def do_parse
          # Called by the generated `parse` method, do nothing here.
        end

        def token(type, value)
          [type, value]
        end
      end
    end
  end
end