Class: Parser::Builders::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/builders/default.rb

Overview

Default AST builder. Uses AST::Nodes.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefault

Initializes attributes:

* `emit_file_line_as_literals`: `true`


243
244
245
# File 'lib/parser/builders/default.rb', line 243

def initialize
  @emit_file_line_as_literals = true
end

Class Attribute Details

.emit_arg_inside_procarg0Boolean

AST compatibility attribute; causes a single non-mlhs block argument to be wrapped in s(:procarg0).

If set to false (the default), block arguments ‘|a|` are emitted as `s(:args, s(:procarg0, :a))`

If set to true, block arguments ‘|a|` are emitted as `s(:args, s(:procarg0, s(:arg, :a))`

Returns:

  • (Boolean)


97
98
99
# File 'lib/parser/builders/default.rb', line 97

def emit_arg_inside_procarg0
  @emit_arg_inside_procarg0
end

.emit_encodingBoolean

AST compatibility attribute; locations of ‘__ENCODING__` are not the same as locations of `Encoding::UTF_8` causing problems during rewriting, all new code should set this attribute to true.

If set to false (the default), ‘__ENCODING__` is emitted as ` s(:const, s(:const, nil, :Encoding), :UTF_8)`.

If set to true, ‘__ENCODING__` is emitted as `s(:__ENCODING__)`.

Returns:

  • (Boolean)


58
59
60
# File 'lib/parser/builders/default.rb', line 58

def emit_encoding
  @emit_encoding
end

.emit_forward_argBoolean

AST compatibility attribute; arguments forwarding initially didn’t have support for leading arguments (i.e. ‘def m(a, …); end` was a syntax error). However, Ruby 3.0 added support for any number of arguments in front of the `…`.

If set to false (the default):

1. `def m(...) end` is emitted as
   s(:def, :m, s(:forward_args), nil)
2. `def m(a, b, ...) end` is emitted as
   s(:def, :m,
     s(:args, s(:arg, :a), s(:arg, :b), s(:forward_arg)))

If set to true it uses a single format:

1. `def m(...) end` is emitted as
   s(:def, :m, s(:args, s(:forward_arg)))
2. `def m(a, b, ...) end` is emitted as
   s(:def, :m, s(:args, s(:arg, :a), s(:arg, :b), s(:forward_arg)))

It does’t matter that much on 2.7 (because there can’t be any leading arguments), but on 3.0 it should be better enabled to use a single AST format.

Returns:

  • (Boolean)


126
127
128
# File 'lib/parser/builders/default.rb', line 126

def emit_forward_arg
  @emit_forward_arg
end

.emit_indexBoolean

AST compatibility attribute; indexed assignment, ‘x[] = 1`, is not semantically equivalent to calling the method directly, `x.[]=(1)`. Specifically, in the former case, the expression’s value is always 1, and in the latter case, the expression’s value is the return value of the ‘[]=` method.

If set to false (the default), ‘self` is emitted as `s(:send, s(:self), :[], s(:int, 1))`, and `self = 2` is emitted as `s(:send, s(:self), :[]=, s(:int, 1), s(:int, 2))`.

If set to true, ‘self` is emitted as `s(:index, s(:self), s(:int, 1))`, and `self = 2` is emitted as `s(:indexasgn, s(:self), s(:int, 1), s(:int, 2))`.

Returns:

  • (Boolean)


80
81
82
# File 'lib/parser/builders/default.rb', line 80

def emit_index
  @emit_index
end

.emit_kwargsObject

AST compatibility attribute; Starting from Ruby 2.7 keyword arguments of method calls that are passed explicitly as a hash (i.e. with curly braces) are treated as positional arguments and Ruby 2.7 emits a warning on such method call. Ruby 3.0 given an ArgumentError.

If set to false (the default) the last hash argument is emitted as ‘hash`:

“‘ (send nil :foo

(hash
  (pair
    (sym :bar)
    (int 42))))

“‘

If set to true it is emitted as ‘kwargs`:

“‘ (send nil :foo

(kwargs
  (pair
    (sym :bar)
    (int 42))))

“‘

Note that ‘kwargs` node is just a replacement for `hash` argument, so if there’s are multiple arguments (or a ‘kwsplat`) all of them are wrapped into `kwargs` instead of `hash`:

“‘ (send nil :foo

(kwargs
  (pair
    (sym :a)
    (int 42))
  (kwsplat
    (send nil :b))
  (pair
    (sym :c)
    (int 10))))

“‘



174
175
176
# File 'lib/parser/builders/default.rb', line 174

def emit_kwargs
  @emit_kwargs
end

.emit_lambdaBoolean

AST compatibility attribute; since ‘-> {}` is not semantically equivalent to `lambda {}`, all new code should set this attribute to true.

If set to false (the default), ‘-> {}` is emitted as `s(:block, s(:send, nil, :lambda), s(:args), nil)`.

If set to true, ‘-> {}` is emitted as `s(:block, s(:lambda), s(:args), nil)`.

Returns:

  • (Boolean)


22
23
24
# File 'lib/parser/builders/default.rb', line 22

def emit_lambda
  @emit_lambda
end

.emit_match_patternObject

AST compatibility attribute; Starting from 3.0 Ruby returns true/false from single-line pattern matching with ‘in` keyword.

Before 3.0 there was an exception if given value doesn’t match pattern.

NOTE: This attribute affects only Ruby 2.7 grammar. 3.0 grammar always emits ‘match_pattern`/`match_pattern_p`

If compatibility attribute set to false ‘foo in bar` is emitted as `in_match`:

“‘ (in-match

(send nil :foo)
(match-var :bar))

“‘

If set to true it’s emitted as ‘match_pattern_p`: “` (match-pattern-p

(send nil :foo)
(match-var :bar))

“‘



203
204
205
# File 'lib/parser/builders/default.rb', line 203

def emit_match_pattern
  @emit_match_pattern
end

.emit_procarg0Boolean

AST compatibility attribute; block arguments of ‘m { |a| }` are not semantically equivalent to block arguments of `m { |a,| }` or `m { |a, b| }`, all new code should set this attribute to true.

If set to false (the default), arguments of ‘m { |a| }` are emitted as `s(:args, s(:arg, :a))`.

If set to true, arguments of ‘m { |a| }` are emitted as `s(:args, s(:procarg0, :a)).

Returns:

  • (Boolean)


40
41
42
# File 'lib/parser/builders/default.rb', line 40

def emit_procarg0
  @emit_procarg0
end

Instance Attribute Details

#emit_file_line_as_literalsBoolean

If set to true (the default), ‘__FILE__` and `__LINE__` are transformed to literal nodes. For example, `s(:str, “lib/foo.rb”)` and `s(:int, 10)`.

If set to false, ‘__FILE__` and `__LINE__` are emitted as-is, i.e. as `s(:__FILE__)` and `s(:__LINE__)` nodes.

Source maps are identical in both cases.

Returns:

  • (Boolean)


237
238
239
# File 'lib/parser/builders/default.rb', line 237

def emit_file_line_as_literals
  @emit_file_line_as_literals
end

#parserObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



225
226
227
# File 'lib/parser/builders/default.rb', line 225

def parser
  @parser
end

Class Method Details

.modernizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



211
212
213
214
215
216
217
218
219
220
# File 'lib/parser/builders/default.rb', line 211

def modernize
  @emit_lambda = true
  @emit_procarg0 = true
  @emit_encoding = true
  @emit_index = true
  @emit_arg_inside_procarg0 = true
  @emit_forward_arg = true
  @emit_kwargs = true
  @emit_match_pattern = true
end