Class: Ripper::Lexer
Overview
:nodoc: internal use only
Defined Under Namespace
Constant Summary
Constants inherited from Ripper
EVENTS, PARSER_EVENTS, SCANNER_EVENTS
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #lex(**kw) ⇒ Object
- #parse(raise_errors: false) ⇒ Object
-
#scan(**kw) ⇒ Object
parse the code and returns elements including errors.
- #tokenize(**kw) ⇒ Object
Methods inherited from Ripper
lex, parse, sexp, sexp_raw, slice, token_match, tokenize
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
147 148 149 |
# File 'ext/ripper/lib/ripper/lexer.rb', line 147 def errors @errors end |
Instance Method Details
#lex(**kw) ⇒ Object
153 154 155 |
# File 'ext/ripper/lib/ripper/lexer.rb', line 153 def lex(**kw) parse(**kw).sort_by(&:pos).map(&:to_a) end |
#parse(raise_errors: false) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'ext/ripper/lib/ripper/lexer.rb', line 172 def parse(raise_errors: false) @errors = [] @buf = [] @stack = [] super() @buf = @stack.pop unless @stack.empty? if raise_errors and !@errors.empty? raise SyntaxError, @errors.map(&:message).join(' ;') end @buf.flatten! unless (result = @buf).empty? result.concat(@buf) until (@buf = []; super(); @buf.flatten!; @buf.empty?) end result end |
#scan(**kw) ⇒ Object
parse the code and returns elements including errors.
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'ext/ripper/lib/ripper/lexer.rb', line 158 def scan(**kw) result = (parse(**kw) + errors + @stack.flatten).uniq.sort_by {|e| [*e.pos, (e. ? -1 : 0)]} result.each_with_index do |e, i| if e.event == :on_parse_error and e.tok.empty? and (pre = result[i-1]) and pre.pos[0] == e.pos[0] and (pre.pos[1] + pre.tok.size) == e.pos[1] e.tok = pre.tok e.pos[1] = pre.pos[1] result[i-1] = e result[i] = pre end end result end |
#tokenize(**kw) ⇒ Object
149 150 151 |
# File 'ext/ripper/lib/ripper/lexer.rb', line 149 def tokenize(**kw) parse(**kw).sort_by(&:pos).map(&:tok) end |