Class: Prism::LexCompat::Heredoc::DashHeredoc
- Inherits:
-
Object
- Object
- Prism::LexCompat::Heredoc::DashHeredoc
- Defined in:
- lib/prism/lex_compat.rb
Overview
Dash heredocs are a little more complicated. They are a list of tokens that need to be split on "\\n" to mimic Ripper's behavior. We also need to keep track of the state that the heredoc was opened in.
Instance Attribute Summary collapse
-
#split ⇒ Object
readonly
:nodoc:.
-
#tokens ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #<<(token) ⇒ Object
-
#initialize(split) ⇒ DashHeredoc
constructor
A new instance of DashHeredoc.
- #to_a ⇒ Object
Constructor Details
#initialize(split) ⇒ DashHeredoc
Returns a new instance of DashHeredoc.
280 281 282 283 |
# File 'lib/prism/lex_compat.rb', line 280 def initialize(split) @split = split @tokens = [] end |
Instance Attribute Details
#split ⇒ Object (readonly)
:nodoc:
278 279 280 |
# File 'lib/prism/lex_compat.rb', line 278 def split @split end |
#tokens ⇒ Object (readonly)
:nodoc:
278 279 280 |
# File 'lib/prism/lex_compat.rb', line 278 def tokens @tokens end |
Instance Method Details
#<<(token) ⇒ Object
285 286 287 |
# File 'lib/prism/lex_compat.rb', line 285 def <<(token) tokens << token end |
#to_a ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/prism/lex_compat.rb', line 289 def to_a embexpr_balance = 0 tokens.each_with_object([]) do |token, results| #$ Array[Token] case token.event when :on_embexpr_beg embexpr_balance += 1 results << token when :on_embexpr_end embexpr_balance -= 1 results << token when :on_tstring_content if embexpr_balance == 0 lineno = token[0][0] column = token[0][1] if split # Split on "\\\n" to mimic Ripper's behavior. Use a lookbehind # to keep the delimiter in the result. token.value.split(/(?<=[^\\]\\\n)|(?<=[^\\]\\\r\n)/).each_with_index do |value, index| column = 0 if index > 0 results << Token.new([[lineno, column], :on_tstring_content, value, token.state]) lineno += value.count("\n") end else results << token end else results << token end else results << token end end end |