parser 2.5.0.0 → 2.6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.travis.yml +41 -17
  4. data/CHANGELOG.md +88 -2
  5. data/README.md +4 -2
  6. data/Rakefile +2 -1
  7. data/ci/run_rubocop_specs +14 -0
  8. data/doc/AST_FORMAT.md +71 -14
  9. data/lib/parser/all.rb +1 -0
  10. data/lib/parser/ast/processor.rb +5 -0
  11. data/lib/parser/base.rb +6 -1
  12. data/lib/parser/builders/default.rb +90 -15
  13. data/lib/parser/color.rb +32 -0
  14. data/lib/parser/context.rb +9 -0
  15. data/lib/parser/current.rb +16 -7
  16. data/lib/parser/lexer/dedenter.rb +31 -3
  17. data/lib/parser/lexer/explanation.rb +6 -6
  18. data/lib/parser/lexer/literal.rb +13 -1
  19. data/lib/parser/lexer/stack_state.rb +4 -0
  20. data/lib/parser/lexer.rb +10180 -9297
  21. data/lib/parser/lexer.rl +81 -25
  22. data/lib/parser/macruby.rb +2 -2
  23. data/lib/parser/macruby.y +2 -2
  24. data/lib/parser/meta.rb +1 -1
  25. data/lib/parser/ruby18.rb +2 -2
  26. data/lib/parser/ruby18.y +2 -2
  27. data/lib/parser/ruby19.rb +2 -2
  28. data/lib/parser/ruby19.y +2 -2
  29. data/lib/parser/ruby20.rb +2 -2
  30. data/lib/parser/ruby20.y +2 -2
  31. data/lib/parser/ruby21.rb +2 -2
  32. data/lib/parser/ruby21.y +2 -2
  33. data/lib/parser/ruby22.rb +2 -2
  34. data/lib/parser/ruby22.y +2 -2
  35. data/lib/parser/ruby23.rb +2 -2
  36. data/lib/parser/ruby23.y +2 -2
  37. data/lib/parser/ruby24.rb +1740 -1719
  38. data/lib/parser/ruby24.y +56 -31
  39. data/lib/parser/ruby25.rb +3226 -3243
  40. data/lib/parser/ruby25.y +87 -68
  41. data/lib/parser/ruby26.rb +6674 -0
  42. data/lib/parser/ruby26.y +2413 -0
  43. data/lib/parser/rubymotion.rb +2 -2
  44. data/lib/parser/rubymotion.y +2 -2
  45. data/lib/parser/runner/ruby_parse.rb +4 -3
  46. data/lib/parser/runner.rb +5 -0
  47. data/lib/parser/source/buffer.rb +5 -3
  48. data/lib/parser/source/comment/associator.rb +3 -3
  49. data/lib/parser/source/comment.rb +4 -4
  50. data/lib/parser/source/map/index.rb +33 -0
  51. data/lib/parser/source/map.rb +1 -1
  52. data/lib/parser/source/range.rb +1 -1
  53. data/lib/parser/source/rewriter.rb +1 -1
  54. data/lib/parser/source/tree_rewriter.rb +3 -3
  55. data/lib/parser/version.rb +1 -1
  56. data/lib/parser.rb +1 -0
  57. data/parser.gemspec +2 -2
  58. data/test/helper.rb +13 -2
  59. data/test/parse_helper.rb +5 -1
  60. data/test/test_current.rb +2 -0
  61. data/test/test_encoding.rb +9 -0
  62. data/test/test_lexer.rb +44 -2
  63. data/test/test_parser.rb +539 -38
  64. data/test/test_source_buffer.rb +6 -0
  65. data/test/test_source_rewriter.rb +1 -1
  66. data/test/test_source_tree_rewriter.rb +5 -9
  67. metadata +10 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 17012e63bed58664c7f879ca77f492e6d551962a
4
- data.tar.gz: 1ba7782eabfc5d33ffbcd5a3f5c7ce1b149e0eb1
2
+ SHA256:
3
+ metadata.gz: c4603be64062f43f204650161874219f6a1250d81dd11705ca39482e7b70e968
4
+ data.tar.gz: 4ac43b4edcd13c7ac53d801533492999a5786138f485c9d06b9fa5329635790a
5
5
  SHA512:
6
- metadata.gz: 88787ec044608f586f8d1f9f2ced18005d1af68b5ff6f4cfc7633161bb8e6004aac032803da28c2b37255f98023b47c33d322806096239b1166dc117bf6a1459
7
- data.tar.gz: 564a7e7e4e75bb412677011482eef36cff953fd023f3b889f0bc922ec0f51e36d6110cd1f01cc18d0d670a286a71aa58b8ad2ef3b268c7582e3afbe4cd45e442
6
+ metadata.gz: 770c2d3e8eca4f64081b10d6053e9576ec795fdb0605b5282dfd0b312fa2de71e04478177a3458a2e0fa36d1f484654f72b642658df91fa997388bff0ef07af3
7
+ data.tar.gz: 81fecd3087121fbee6e26dfc09d9cabc5449f237379e514dc88852fd85d1dd6f6189435fca1817409341b4eb505c6016afefb85e48c4170f930223480a85e8de
data/.gitignore CHANGED
@@ -27,5 +27,6 @@ lib/parser/ruby22.rb
27
27
  lib/parser/ruby23.rb
28
28
  lib/parser/ruby24.rb
29
29
  lib/parser/ruby25.rb
30
+ lib/parser/ruby26.rb
30
31
  lib/parser/macruby.rb
31
32
  lib/parser/rubymotion.rb
data/.travis.yml CHANGED
@@ -1,21 +1,45 @@
1
1
  dist: trusty
2
2
  language: ruby
3
- rvm:
4
- - 2.0.0
5
- - 2.2.9
6
- - 2.3.6
7
- - 2.4.3
8
- - 2.5.0
9
- - ruby-head
10
- - jruby-9.1.15.0
11
- - rbx-2
12
3
  matrix:
13
- allow_failures:
14
- - rvm: ruby-head
15
- - rvm: rbx-2
4
+ include:
5
+ - name: 2.0.0 / Parser tests
6
+ rvm: 2.0.0
7
+ script: bundle exec rake test_cov
8
+ - name: 2.2.10 / Parser tests
9
+ rvm: 2.2.10
10
+ script: bundle exec rake test_cov
11
+ - name: 2.3.8 / Parser tests
12
+ rvm: 2.3.8
13
+ script: bundle exec rake test_cov
14
+ - name: 2.4.5 / Parser tests
15
+ rvm: 2.4.5
16
+ script: bundle exec rake test_cov
17
+ - name: 2.5.3 / Parser tests
18
+ rvm: 2.5.3
19
+ script: bundle exec rake test_cov
20
+ - name: 2.6.0 / Parser tests
21
+ rvm: 2.6.0
22
+ script: bundle exec rake test_cov
23
+ - name: ruby-head / Parser tests
24
+ rvm: ruby-head
25
+ script: bundle exec rake test_cov
26
+ - name: jruby-9.1.15.0 / Parser tests
27
+ rvm: jruby-9.1.15.0
28
+ script: bundle exec rake test_cov
29
+ - name: rbx-2 / Parser tests
30
+ rvm: rbx-2
31
+ script: bundle exec rake test_cov
32
+ - name: 2.5.3 / Rubocop tests
33
+ rvm: 2.5.3
34
+ script: ./ci/run_rubocop_specs
35
+ - name: 2.6.0 / Rubocop tests
36
+ rvm: 2.6.0
37
+ script: ./ci/run_rubocop_specs
38
+ allow_failures:
39
+ - rvm: ruby-head
40
+ - rvm: rbx-2
41
+ - script: ./ci/run_rubocop_specs
16
42
  before_install:
17
- - gem install bundler
18
- - bundle --version
19
- - gem --version
20
- script:
21
- - bundle exec rake test_cov
43
+ - gem install bundler
44
+ - bundle --version
45
+ - gem --version
data/CHANGELOG.md CHANGED
@@ -1,8 +1,94 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
- Not released (2018-02-16)
5
- -------------------------
4
+ v2.6.0.0 (2019-01-16)
5
+ ---------------------
6
+
7
+ API modifications:
8
+ * 2.6.0 was released, unmark is as -dev. (#538) (Ilya Bylich)
9
+
10
+ Bugs fixed:
11
+ * Fix parsing of "\\\n" escaped sequences in various literals. (#539) (Ilya Bylich)
12
+
13
+ v2.5.3.0 (2018-10-29)
14
+ ---------------------
15
+
16
+ Bugs fixed:
17
+ * lexer.rl: Fix parsing of 'm :key => m do; m() do end; end'. (#526) (Ilya Bylich)
18
+ * lexer.rl: Fix parsing of ambiguous 1re. (#523) (Ilya Bylich)
19
+
20
+ v2.5.1.2 (2018-07-10)
21
+ ---------------------
22
+
23
+ Bugs fixed:
24
+ * lexer.rl: Partially revert 5ba072d and properly handle 'm = -> *args do end'. (Ilya Bylich)
25
+
26
+ v2.5.1.1 (2018-07-10)
27
+ ---------------------
28
+
29
+ Features implemented:
30
+ * ruby26.y: Endless ranges support. (Ilya Bylich)
31
+
32
+ Bugs fixed:
33
+ * lexer.rl: Fix parsing of 'm = -> *args do end'. (Ilya Bylich)
34
+ * AST::Processor: Properly recurse into "kwsplat" nodes (Nelson Elhage)
35
+ * ruby24, ruby25, ruby26: Fix cmdargs after command_args followed by tLBRACE_ARG. This commit tracks upstream commit ruby/ruby@f168dbd. (Ilya Bylich)
36
+ * lexer.rl: Fix parsing of `let (:a) { m do; end }`. (Ilya Bylich)
37
+
38
+ v2.5.1.0 (2018-04-12)
39
+ ---------------------
40
+
41
+ API modifications:
42
+ * Parser::Current: bump latest 2.2 branch to 2.2.10. (Ilya Bylich)
43
+
44
+ Features implemented:
45
+ * ruby26.y: Raise a syntax error on 'else' without 'rescue'. This commit tracks upstream commit ruby/ruby@140512d. (Ilya Bylich)
46
+
47
+ Bugs fixed:
48
+ * lexer.rl, Dedenter: Treat slash in heredocs as a line continuation. (Ilya Bylich)
49
+ * lexer.rl: Fix parsing of `a ? b + '': nil`. (Ilya Bylich)
50
+ * lexer.rl: Reject `m a: {} {}` and `m [] {}` since 25. (Ilya Bylich)
51
+ * builders/default: allow class/module/dynamic constant definition in the method body if there's a sclass between them. (bug #490) (Ilya Bylich)
52
+ * lexer.rl: Emit :!@ as :!, :~@ as :~. (Ilya Bylich)
53
+ * parse{23,24,25}.y: use only CMDARG/COND _PUSH/POP for cmdarg/cond management. (bug #481) (Ilya Bylich)
54
+ * lexer.rl: emit tSTRING_BEG and tSTRING_DBEG one by one to allow parser to properly manipulate cmdarg stack. (bug #480) (Ilya Bylich)
55
+
56
+ v2.5.0.4 (2018-03-13)
57
+ ---------------------
58
+
59
+ Bugs fixed:
60
+ * AST::Processor: handle on_index, on_indexasgn, on_lambda. (Ilya Bylich)
61
+
62
+ v2.5.0.3 (2018-03-06)
63
+ ---------------------
64
+
65
+ Bugs fixed:
66
+ * Accept `BEGIN` and `END` as correct method name (#463) (Masataka Pocke Kuwabara)
67
+ * Parser::Source::Buffer: Fixed parsing of encoding comment when the file contains only shebang. (Ilya Bylich)
68
+
69
+ v2.5.0.2 (2018-02-22)
70
+ ---------------------
71
+
72
+ Bugs fixed:
73
+ * ruby24.y, ruby25.y: Replicate cmdargs handling from MRI. (#453) (Ilya Bylich)
74
+
75
+ v2.5.0.1 (2018-02-21)
76
+ ---------------------
77
+
78
+ Features implemented:
79
+ * builders/default: __ENCODING__: emit as s(:__ENCODING__) via AST opt-in. (whitequark)
80
+ * ruby25.y: Extract expr_value_do rule. This commit tracks upstream commit ruby/ruby@508533f. (Ilya Bylich)
81
+ * ruby25.y: Extract begin_block rule. This commit tracks upstream commit ruby/ruby@762d23c. (Ilya Bylich)
82
+ * ruby25.y: Allow class and method definition in the while condition. (#432) (Ilya Bylich)
83
+ * ruby25: Allow `-> do rescue; end`. (#431) (Ilya Bylich)
84
+
85
+ Bugs fixed:
86
+ * parser/current: latest released Ruby series is 2.5.x. (whitequark)
87
+ * builders/default: x[], x[]=1: emit as s(:index), s(:indexasgn) via AST opt-in. (whitequark)
88
+ * lexer.rl: "#{-> foo {}}": fix parsing of interpolated lambda with open args. (Ilya Bylich)
89
+
90
+ v2.5.0.0 (2018-02-16)
91
+ ---------------------
6
92
 
7
93
  API modifications:
8
94
  * Parser::Current: bump to 2.2.9 and 2.3.6. (Stan Hu)
data/README.md CHANGED
@@ -24,8 +24,10 @@ below for explanation of `emit_*` calls):
24
24
 
25
25
  require 'parser/current'
26
26
  # opt-in to most recent AST format:
27
- Parser::Builders::Default.emit_lambda = true
27
+ Parser::Builders::Default.emit_lambda = true
28
28
  Parser::Builders::Default.emit_procarg0 = true
29
+ Parser::Builders::Default.emit_encoding = true
30
+ Parser::Builders::Default.emit_index = true
29
31
 
30
32
  Parse a chunk of code:
31
33
 
@@ -215,7 +217,7 @@ at some point.
215
217
  Sometimes it is necessary to modify the format of AST nodes that are already being emitted
216
218
  in a way that would break existing applications. To avoid such breakage, applications
217
219
  must opt-in to these modifications; without explicit opt-in, Parser will continue to emit
218
- the old AST node format. The most recent set of opt-ins is speified in
220
+ the old AST node format. The most recent set of opt-ins is specified in
219
221
  the [usage section](#usage) of this README.
220
222
 
221
223
  ## Compatibility with Ruby MRI
data/Rakefile CHANGED
@@ -30,6 +30,7 @@ GENERATED_FILES = %w(lib/parser/lexer.rb
30
30
  lib/parser/ruby23.rb
31
31
  lib/parser/ruby24.rb
32
32
  lib/parser/ruby25.rb
33
+ lib/parser/ruby26.rb
33
34
  lib/parser/macruby.rb
34
35
  lib/parser/rubymotion.rb)
35
36
 
@@ -104,7 +105,7 @@ task :changelog do
104
105
 
105
106
  current_version = "#{$1} (#{date})" if version =~ /(v[\d\w.]+)/
106
107
  current_version = "Not released (#{date})" \
107
- if version =~ /(^| |\/)#{Regexp.escape branch}$/
108
+ if version =~ /(^| |\/)#{Regexp.escape branch}$/ && !branch.start_with?('v')
108
109
 
109
110
  next if current_version.nil?
110
111
  changelog[current_version] # add a hash
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -eux
4
+
5
+ bundle install
6
+ rake generate
7
+
8
+ git clone https://github.com/rubocop-hq/rubocop.git --depth=1
9
+ cd rubocop
10
+ export BUNDLE_GEMFILE=Gemfile
11
+ echo "gem 'parser', path: '../'" > Gemfile.local
12
+
13
+ bundle install
14
+ bundle exec rake spec
data/doc/AST_FORMAT.md CHANGED
@@ -322,6 +322,23 @@ Format:
322
322
  ~~~~~ expression
323
323
  ~~~
324
324
 
325
+
326
+ ### Endless (2.6)
327
+
328
+ Format:
329
+
330
+ ~~~
331
+ (irange (int 1) nil)
332
+ "1.."
333
+ ~~ operator
334
+ ~~~ expression
335
+
336
+ (erange (int 1) nil)
337
+ "1..."
338
+ ~~~ operator
339
+ ~~~~ expression
340
+ ~~~
341
+
325
342
  ## Access
326
343
 
327
344
  ### Self
@@ -1081,18 +1098,6 @@ Format:
1081
1098
  ~ selector
1082
1099
  ^ operator
1083
1100
  ~~~~~~~~~ expression
1084
-
1085
- (send (lvar :foo) :[] (int 1))
1086
- "foo[i]"
1087
- ~~~ selector
1088
- ~~~~~~ expression
1089
-
1090
- (send (lvar :bar) :[]= (int 1) (int 2) (lvar :baz))
1091
- "bar[1, 2] = baz"
1092
- ~~~~~~ selector
1093
- ^ operator
1094
- ~~~~~~~~~~~~~~~ expression
1095
-
1096
1101
  ~~~
1097
1102
 
1098
1103
  ### To superclass
@@ -1135,6 +1140,26 @@ Format:
1135
1140
  ~~~~~~~~~~ expression
1136
1141
  ~~~
1137
1142
 
1143
+ ### Indexing
1144
+
1145
+ Format:
1146
+
1147
+ ~~~
1148
+ (index (lvar :foo) (int 1))
1149
+ "foo[1]"
1150
+ ^ begin
1151
+ ^ end
1152
+ ~~~~~~ expression
1153
+
1154
+ (indexasgn (lvar :bar) (int 1) (int 2) (lvar :baz))
1155
+ "bar[1, 2] = baz"
1156
+ ^ begin
1157
+ ^ end
1158
+ ^ operator
1159
+ ~~~~~~~~~~~~~~~ expression
1160
+
1161
+ ~~~
1162
+
1138
1163
  ### Passing a literal block
1139
1164
 
1140
1165
  ~~~
@@ -1285,8 +1310,8 @@ Format:
1285
1310
  ~~~~~~ keyword
1286
1311
  ~~~~ begin
1287
1312
  ~~~~ else
1288
- ~~~ end
1289
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1313
+ ~~~ end
1314
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
1290
1315
 
1291
1316
  "unless cond; iftrue; else; iffalse; end"
1292
1317
  ~~~~~~ keyword
@@ -1676,3 +1701,35 @@ Format:
1676
1701
  ~~ selector
1677
1702
  ~~~~~~~~~~~~~~~~~~~~~~ expression
1678
1703
  ~~~
1704
+
1705
+ ## Special constants
1706
+
1707
+ ### File
1708
+
1709
+ Format:
1710
+
1711
+ ~~~
1712
+ (__FILE__)
1713
+ "__FILE__"
1714
+ ~~~~~~~~ expression
1715
+ ~~~
1716
+
1717
+ ### Line
1718
+
1719
+ Format:
1720
+
1721
+ ~~~
1722
+ (__LINE__)
1723
+ "__LINE__"
1724
+ ~~~~~~~~ expression
1725
+ ~~~
1726
+
1727
+ ### Encoding
1728
+
1729
+ Format:
1730
+
1731
+ ~~~
1732
+ (__ENCODING__)
1733
+ "__ENCODING__"
1734
+ ~~~~~~~~~~~~ expression
1735
+ ~~~
data/lib/parser/all.rb CHANGED
@@ -8,3 +8,4 @@ require 'parser/ruby22'
8
8
  require 'parser/ruby23'
9
9
  require 'parser/ruby24'
10
10
  require 'parser/ruby25'
11
+ require 'parser/ruby26'
@@ -16,6 +16,7 @@ module Parser
16
16
  alias on_regexp process_regular_node
17
17
  alias on_xstr process_regular_node
18
18
  alias on_splat process_regular_node
19
+ alias on_kwsplat process_regular_node
19
20
  alias on_array process_regular_node
20
21
  alias on_pair process_regular_node
21
22
  alias on_hash process_regular_node
@@ -166,7 +167,11 @@ module Parser
166
167
 
167
168
  alias on_csend on_send
168
169
 
170
+ alias on_index process_regular_node
171
+ alias on_indexasgn process_regular_node
172
+
169
173
  alias on_block process_regular_node
174
+ alias on_lambda process_regular_node
170
175
 
171
176
  alias on_while process_regular_node
172
177
  alias on_while_post process_regular_node
data/lib/parser/base.rb CHANGED
@@ -131,6 +131,9 @@ module Parser
131
131
  @builder.parser = self
132
132
  @context = Context.new
133
133
 
134
+ # Last emitted token
135
+ @last_token = nil
136
+
134
137
  if self.class::Racc_debug_parser && ENV['RACC_DEBUG']
135
138
  @yydebug = true
136
139
  end
@@ -222,7 +225,9 @@ module Parser
222
225
  private
223
226
 
224
227
  def next_token
225
- @lexer.advance
228
+ token = @lexer.advance
229
+ @last_token = token
230
+ token
226
231
  end
227
232
 
228
233
  def check_kwarg_name(name_t)
@@ -31,10 +31,10 @@ module Parser
31
31
  # all new code should set this attribute to true.
32
32
  #
33
33
  # If set to false (the default), arguments of `m { |a| }` are emitted as
34
- # `s(:args, s(:arg, :a))`
34
+ # `s(:args, s(:arg, :a))`.
35
35
  #
36
36
  # If set to true, arguments of `m { |a| }` are emitted as
37
- # `s(:args, s(:procarg0, :a))
37
+ # `s(:args, s(:procarg0, :a)).
38
38
  #
39
39
  # @return [Boolean]
40
40
  attr_accessor :emit_procarg0
@@ -42,12 +42,54 @@ module Parser
42
42
 
43
43
  @emit_procarg0 = false
44
44
 
45
+ class << self
46
+ ##
47
+ # AST compatibility attribute; locations of `__ENCODING__` are not the same
48
+ # as locations of `Encoding::UTF_8` causing problems during rewriting,
49
+ # all new code should set this attribute to true.
50
+ #
51
+ # If set to false (the default), `__ENCODING__` is emitted as
52
+ # ` s(:const, s(:const, nil, :Encoding), :UTF_8)`.
53
+ #
54
+ # If set to true, `__ENCODING__` is emitted as
55
+ # `s(:__ENCODING__)`.
56
+ #
57
+ # @return [Boolean]
58
+ attr_accessor :emit_encoding
59
+ end
60
+
61
+ @emit_encoding = false
62
+
63
+ class << self
64
+ ##
65
+ # AST compatibility attribute; indexed assignment, `x[] = 1`, is not
66
+ # semantically equivalent to calling the method directly, `x.[]=(1)`.
67
+ # Specifically, in the former case, the expression's value is always 1,
68
+ # and in the latter case, the expression's value is the return value
69
+ # of the `[]=` method.
70
+ #
71
+ # If set to false (the default), `self[1]` is emitted as
72
+ # `s(:send, s(:self), :[], s(:int, 1))`, and `self[1] = 2` is
73
+ # emitted as `s(:send, s(:self), :[]=, s(:int, 1), s(:int, 2))`.
74
+ #
75
+ # If set to true, `self[1]` is emitted as
76
+ # `s(:index, s(:self), s(:int, 1))`, and `self[1] = 2` is
77
+ # emitted as `s(:indexasgn, s(:self), s(:int, 1), s(:int, 2))`.
78
+ #
79
+ # @return [Boolean]
80
+ attr_accessor :emit_index
81
+ end
82
+
83
+ @emit_index = false
84
+
45
85
  class << self
46
86
  ##
47
87
  # @api private
48
88
  def modernize
49
89
  @emit_lambda = true
50
90
  @emit_procarg0 = true
91
+ @emit_encoding = true
92
+ @emit_index = true
51
93
  end
52
94
  end
53
95
 
@@ -56,7 +98,7 @@ module Parser
56
98
  attr_accessor :parser
57
99
 
58
100
  ##
59
- # If set to true, `__FILE__` and `__LINE__` are transformed to
101
+ # If set to true (the default), `__FILE__` and `__LINE__` are transformed to
60
102
  # literal nodes. For example, `s(:str, "lib/foo.rb")` and `s(:int, 10)`.
61
103
  #
62
104
  # If set to false, `__FILE__` and `__LINE__` are emitted as-is,
@@ -358,12 +400,12 @@ module Parser
358
400
 
359
401
  def range_inclusive(lhs, dot2_t, rhs)
360
402
  n(:irange, [ lhs, rhs ],
361
- binary_op_map(lhs, dot2_t, rhs))
403
+ range_map(lhs, dot2_t, rhs))
362
404
  end
363
405
 
364
406
  def range_exclusive(lhs, dot3_t, rhs)
365
407
  n(:erange, [ lhs, rhs ],
366
- binary_op_map(lhs, dot3_t, rhs))
408
+ range_map(lhs, dot3_t, rhs))
367
409
  end
368
410
 
369
411
  #
@@ -424,8 +466,12 @@ module Parser
424
466
  end
425
467
 
426
468
  when :__ENCODING__
427
- n(:const, [ n(:const, [ nil, :Encoding], nil), :UTF_8 ],
428
- node.loc.dup)
469
+ if !self.class.emit_encoding
470
+ n(:const, [ n(:const, [ nil, :Encoding], nil), :UTF_8 ],
471
+ node.loc.dup)
472
+ else
473
+ node
474
+ end
429
475
 
430
476
  when :ident
431
477
  name, = *node
@@ -481,7 +527,7 @@ module Parser
481
527
  node.updated(:gvasgn)
482
528
 
483
529
  when :const
484
- if @parser.context.indirectly_in_def?
530
+ unless @parser.context.dynamic_const_definition_allowed?
485
531
  diagnostic :error, :dynamic_const, nil, node.loc.expression
486
532
  end
487
533
 
@@ -515,12 +561,16 @@ module Parser
515
561
 
516
562
  def op_assign(lhs, op_t, rhs)
517
563
  case lhs.type
518
- when :gvasgn, :ivasgn, :lvasgn, :cvasgn, :casgn, :send, :csend
564
+ when :gvasgn, :ivasgn, :lvasgn, :cvasgn, :casgn, :send, :csend, :index
519
565
  operator = value(op_t)[0..-1].to_sym
520
566
  source_map = lhs.loc.
521
567
  with_operator(loc(op_t)).
522
568
  with_expression(join_exprs(lhs, rhs))
523
569
 
570
+ if lhs.type == :index
571
+ lhs = lhs.updated(:indexasgn)
572
+ end
573
+
524
574
  case operator
525
575
  when :'&&'
526
576
  n(:and_asgn, [ lhs, rhs ], source_map)
@@ -785,7 +835,7 @@ module Parser
785
835
  diagnostic :error, :block_and_blockarg, nil, last_arg.loc.expression, [loc(begin_t)]
786
836
  end
787
837
 
788
- if [:send, :csend, :super, :zsuper, :lambda].include?(method_call.type)
838
+ if [:send, :csend, :index, :super, :zsuper, :lambda].include?(method_call.type)
789
839
  n(:block, [ method_call, args, body ],
790
840
  block_map(method_call.loc.expression, begin_t, end_t))
791
841
  else
@@ -825,14 +875,24 @@ module Parser
825
875
  end
826
876
 
827
877
  def index(receiver, lbrack_t, indexes, rbrack_t)
828
- n(:send, [ receiver, :[], *indexes ],
829
- send_index_map(receiver, lbrack_t, rbrack_t))
878
+ if self.class.emit_index
879
+ n(:index, [ receiver, *indexes ],
880
+ index_map(receiver, lbrack_t, rbrack_t))
881
+ else
882
+ n(:send, [ receiver, :[], *indexes ],
883
+ send_index_map(receiver, lbrack_t, rbrack_t))
884
+ end
830
885
  end
831
886
 
832
887
  def index_asgn(receiver, lbrack_t, indexes, rbrack_t)
833
- # Incomplete method call.
834
- n(:send, [ receiver, :[]=, *indexes ],
835
- send_index_map(receiver, lbrack_t, rbrack_t))
888
+ if self.class.emit_index
889
+ n(:indexasgn, [ receiver, *indexes ],
890
+ index_map(receiver, lbrack_t, rbrack_t))
891
+ else
892
+ # Incomplete method call.
893
+ n(:send, [ receiver, :[]=, *indexes ],
894
+ send_index_map(receiver, lbrack_t, rbrack_t))
895
+ end
836
896
  end
837
897
 
838
898
  def binary_op(receiver, operator_t, arg)
@@ -1328,6 +1388,16 @@ module Parser
1328
1388
  Source::Map::Operator.new(loc(op_t), expr_l)
1329
1389
  end
1330
1390
 
1391
+ def range_map(start_e, op_t, end_e)
1392
+ if end_e
1393
+ expr_l = join_exprs(start_e, end_e)
1394
+ else
1395
+ expr_l = start_e.loc.expression.join(loc(op_t))
1396
+ end
1397
+
1398
+ Source::Map::Operator.new(loc(op_t), expr_l)
1399
+ end
1400
+
1331
1401
  def arg_prefix_map(op_t, name_t=nil)
1332
1402
  if name_t.nil?
1333
1403
  expr_l = loc(op_t)
@@ -1411,6 +1481,11 @@ module Parser
1411
1481
  expr_l)
1412
1482
  end
1413
1483
 
1484
+ def index_map(receiver_e, lbrack_t, rbrack_t)
1485
+ Source::Map::Index.new(loc(lbrack_t), loc(rbrack_t),
1486
+ receiver_e.loc.expression.join(loc(rbrack_t)))
1487
+ end
1488
+
1414
1489
  def send_index_map(receiver_e, lbrack_t, rbrack_t)
1415
1490
  Source::Map::Send.new(nil, loc(lbrack_t).join(loc(rbrack_t)),
1416
1491
  nil, nil,
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Parser
4
+ module Color
5
+ def self.color(str, code, bold: false)
6
+ return str unless STDOUT.tty?
7
+ code = Array(code)
8
+ code.unshift(1) if bold
9
+ "\e[#{code.join(';')}m#{str}\e[0m"
10
+ end
11
+
12
+ def self.red(str, bold: false)
13
+ color(str, 31, bold: bold)
14
+ end
15
+
16
+ def self.green(str, bold: false)
17
+ color(str, 32, bold: bold)
18
+ end
19
+
20
+ def self.yellow(str, bold: false)
21
+ color(str, 33, bold: bold)
22
+ end
23
+
24
+ def self.magenta(str, bold: false)
25
+ color(str, 35, bold: bold)
26
+ end
27
+
28
+ def self.underline(str)
29
+ color(str, 4)
30
+ end
31
+ end
32
+ end
@@ -38,5 +38,14 @@ module Parser
38
38
  def indirectly_in_def?
39
39
  @stack.include?(:def) || @stack.include?(:defs)
40
40
  end
41
+
42
+ def class_definition_allowed?
43
+ def_index = stack.rindex { |item| [:def, :defs].include?(item) }
44
+ sclass_index = stack.rindex(:sclass)
45
+
46
+ def_index.nil? || (!sclass_index.nil? && sclass_index > def_index)
47
+ end
48
+ alias module_definition_allowed? class_definition_allowed?
49
+ alias dynamic_const_definition_allowed? class_definition_allowed?
41
50
  end
42
51
  end