Class: RuboCop::AST::NodePattern::LexerRex

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/ast/node_pattern/lexer.rex.rb

Overview

The generated lexer RuboCop::AST::NodePattern::LexerRex

Direct Known Subclasses

Lexer

Defined Under Namespace

Classes: LexerError, ScanError

Constant Summary collapse

CONST_NAME =

:stopdoc:

/[A-Z:][a-zA-Z_:]+/
SYMBOL_NAME =
/[\w+@*\/?!<>=~|%^&-]+|\[\]=?/
IDENTIFIER =
/[a-z][a-zA-Z0-9_]*/
NODE_TYPE =
/[a-z][a-zA-Z0-9_-]*/
CALL =
/(?:#{CONST_NAME}\.)?#{IDENTIFIER}[!?]?/
REGEXP_BODY =
/(?:[^\/]|\\\/)*/
REGEXP =
/\/(#{REGEXP_BODY})(?<!\\)\/([imxo]*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

The file name / path



43
44
45
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 43

def filename
  @filename
end

#ssObject Also known as: match

The StringScanner for this lexer.



48
49
50
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 48

def ss
  @ss
end

#stateObject

The current lexical state.



53
54
55
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 53

def state
  @state
end

Instance Method Details

#actionObject

Yields on the current action.



69
70
71
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 69

def action
  yield
end

#locationObject

The current location in the parse.



104
105
106
107
108
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 104

def location
  [
    (filename || "<input>"),
  ].compact.join(":")
end

#matchesObject

The match groups for the current scan.



60
61
62
63
64
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 60

def matches
  m = (1..9).map { |i| ss[i] }
  m.pop until m[-1] or m.empty?
  m
end

#next_tokenObject

Lex the next token.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 113

def next_token

  token = nil

  until ss.eos? or token do
    token =
      case state
      when nil then
        case
        when ss.skip(/\s+/) then
          # do nothing
        when ss.skip(/:(#{SYMBOL_NAME})/o) then
          action { emit :tSYMBOL, &:to_sym }
        when ss.skip(/"(.+?)"/) then
          action { emit :tSTRING }
        when ss.skip(/[-+]?\d+\.\d+/) then
          action { emit :tNUMBER, &:to_f }
        when ss.skip(/[-+]?\d+/) then
          action { emit :tNUMBER, &:to_i }
        when ss.skip(/#{Regexp.union(
                  %w"( ) { | } [ ] < > $ ! ^ ` ... + * ? ,"
                )}/o) then
          action { emit ss.matched, &:to_sym }
        when ss.skip(/#{REGEXP}/o) then
          action { emit_regexp }
        when ss.skip(/%?(#{CONST_NAME})/o) then
          action { emit :tPARAM_CONST }
        when ss.skip(/%([a-z_]+)/) then
          action { emit :tPARAM_NAMED }
        when ss.skip(/%(\d*)/) then
          action { emit(:tPARAM_NUMBER) { |s| s.empty? ? 1 : s.to_i } } # Map `%` to `%1`
        when ss.skip(/_(#{IDENTIFIER})/o) then
          action { emit :tUNIFY }
        when ss.skip(/_/o) then
          action { emit :tWILDCARD }
        when ss.skip(/\#(#{CALL})/o) then
          action { @state = :ARG; emit :tFUNCTION_CALL, &:to_sym }
        when ss.skip(/#{IDENTIFIER}\?/o) then
          action { @state = :ARG; emit :tPREDICATE, &:to_sym }
        when ss.skip(/#{NODE_TYPE}/o) then
          action { emit :tNODE_TYPE, &:to_sym }
        when ss.skip(/\#.*/) then
          action { emit_comment }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      when :ARG then
        case
        when ss.skip(/\(/) then
          action { @state = nil; emit :tARG_LIST }
        when ss.skip(//) then
          action { @state = nil }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      else
        raise ScanError, "undefined state at #{location}: '#{state}'"
      end # token = case state

    next unless token # allow functions to trigger redo w/ nil
  end # while

  raise LexerError, "bad lexical result at #{location}: #{token.inspect}" unless
    token.nil? || (Array === token && token.size >= 2)

  # auto-switch state
  self.state = token.last if token && token.first == :state

  token
end

#parse(str) ⇒ Object

Parse the given string.



84
85
86
87
88
89
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 84

def parse str
  self.ss     = scanner_class.new str
  self.state  ||= nil

  do_parse
end

#parse_file(path) ⇒ Object

Read in and parse the file at path.



94
95
96
97
98
99
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 94

def parse_file path
  self.filename = path
  open path do |f|
    parse f.read
  end
end

#scanner_classObject

The current scanner class. Must be overridden in subclasses.



77
78
79
# File 'lib/rubocop/ast/node_pattern/lexer.rex.rb', line 77

def scanner_class
  StringScanner
end