parser 1.4.2 → 2.0.0.beta1

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.
@@ -17,6 +17,9 @@ module Parser
17
17
  require 'parser/source/buffer'
18
18
  require 'parser/source/range'
19
19
 
20
+ require 'parser/source/comment'
21
+ require 'parser/source/comment/associator'
22
+
20
23
  require 'parser/source/rewriter'
21
24
  require 'parser/source/rewriter/action'
22
25
 
@@ -2,13 +2,13 @@ module Parser
2
2
  module AST
3
3
 
4
4
  class Node < ::AST::Node
5
- attr_reader :source_map
5
+ attr_reader :location
6
6
 
7
- alias src source_map
7
+ alias loc location
8
8
 
9
9
  def assign_properties(properties)
10
- if (source_map = properties[:source_map])
11
- @source_map = source_map
10
+ if (location = properties[:location])
11
+ @location = location
12
12
  end
13
13
  end
14
14
  end
@@ -48,7 +48,6 @@ module Parser
48
48
  alias on_lvasgn process_var_asgn_node
49
49
  alias on_ivasgn process_var_asgn_node
50
50
  alias on_gvasgn process_var_asgn_node
51
- alias on_cvdecl process_var_asgn_node
52
51
  alias on_cvasgn process_var_asgn_node
53
52
 
54
53
  alias on_and_asgn process_regular_node
@@ -72,7 +71,7 @@ module Parser
72
71
  node.updated(nil, [ scope_node, name ])
73
72
  end
74
73
 
75
- def on_cdecl(node)
74
+ def on_casgn(node)
76
75
  scope_node, name, value_node = *node
77
76
 
78
77
  scope_node = process(scope_node) if scope_node
@@ -51,7 +51,7 @@ module Parser
51
51
 
52
52
  def reset
53
53
  @source_buffer = nil
54
- @def_level = 0 # count of nested def's.
54
+ @def_level = 0 # count of nested def's.
55
55
 
56
56
  @lexer.reset
57
57
  @static_env.reset
@@ -60,8 +60,8 @@ module Parser
60
60
  end
61
61
 
62
62
  def parse(source_buffer)
63
- @source_buffer = source_buffer
64
63
  @lexer.source_buffer = source_buffer
64
+ @source_buffer = source_buffer
65
65
 
66
66
  do_parse
67
67
  ensure
@@ -70,6 +70,34 @@ module Parser
70
70
  @lexer.source_buffer = nil
71
71
  end
72
72
 
73
+ def parse_with_comments(source_buffer)
74
+ @lexer.comments = []
75
+
76
+ [ parse(source_buffer), @lexer.comments ]
77
+ ensure
78
+ @lexer.comments = nil
79
+ end
80
+
81
+ # Currently, token stream format returned by #lex is not documented,
82
+ # but is considered part of a public API and only changed according
83
+ # to Semantic Versioning.
84
+ #
85
+ # However, note that the exact token composition of various constructs
86
+ # might vary. For example, a string `"foo"` is represented equally well
87
+ # by `:tSTRING_BEG " :tSTRING_CONTENT foo :tSTRING_END "` and
88
+ # `:tSTRING "foo"`; such details must not be relied upon.
89
+ #
90
+ def tokenize(source_buffer)
91
+ @lexer.tokens = []
92
+
93
+ ast, comments = parse(source_buffer)
94
+
95
+ [ ast, comments, @lexer.tokens ]
96
+ ensure
97
+ @lexer.tokens = nil
98
+ end
99
+
100
+ # @api internal
73
101
  def in_def?
74
102
  @def_level > 0
75
103
  end
@@ -85,7 +85,7 @@ module Parser
85
85
  str = parts.first
86
86
 
87
87
  n(:sym, [ str.children.first.to_sym ],
88
- collection_map(begin_t, str.src.expression, end_t))
88
+ collection_map(begin_t, str.loc.expression, end_t))
89
89
  elsif @parser.version == 18 && parts.empty?
90
90
  diagnostic(:error, ERRORS[:empty_symbol], loc(begin_t).join(loc(end_t)))
91
91
  else
@@ -175,7 +175,7 @@ module Parser
175
175
  def pair_list_18(list)
176
176
  if list.size % 2 != 0
177
177
  message = ERRORS[:odd_hash]
178
- diagnostic :error, message, list.last.src.expression
178
+ diagnostic :error, message, list.last.loc.expression
179
179
  else
180
180
  list.
181
181
  each_slice(2).map do |key, value|
@@ -185,9 +185,12 @@ module Parser
185
185
  end
186
186
  end
187
187
 
188
- def associate(begin_t, pairs, end_t)
189
- n(:hash, [ *pairs ],
190
- collection_map(begin_t, pairs, end_t))
188
+ def pair_keyword(key_t, value)
189
+ key_map, pair_map = pair_keyword_map(key_t, value)
190
+
191
+ key = n(:sym, [ value(key_t).to_sym ], key_map)
192
+
193
+ n(:pair, [ key, value ], pair_map)
191
194
  end
192
195
 
193
196
  def kwsplat(dstar_t, arg)
@@ -195,6 +198,11 @@ module Parser
195
198
  unary_op_map(dstar_t, arg))
196
199
  end
197
200
 
201
+ def associate(begin_t, pairs, end_t)
202
+ n(:hash, [ *pairs ],
203
+ collection_map(begin_t, pairs, end_t))
204
+ end
205
+
198
206
  # Ranges
199
207
 
200
208
  def range_inclusive(lhs, dot2_t, rhs)
@@ -249,16 +257,16 @@ module Parser
249
257
  def accessible(node)
250
258
  case node.type
251
259
  when :__FILE__
252
- n(:str, [ node.src.expression.source_buffer.name ],
253
- node.src)
260
+ n(:str, [ node.loc.expression.source_buffer.name ],
261
+ node.loc)
254
262
 
255
263
  when :__LINE__
256
- n(:int, [ node.src.expression.line ],
257
- node.src)
264
+ n(:int, [ node.loc.expression.line ],
265
+ node.loc)
258
266
 
259
267
  when :__ENCODING__
260
268
  n(:const, [ n(:const, [ nil, :Encoding], nil), :UTF_8 ],
261
- node.src)
269
+ node.loc)
262
270
 
263
271
  when :ident
264
272
  name, = *node
@@ -305,11 +313,7 @@ module Parser
305
313
  def assignable(node)
306
314
  case node.type
307
315
  when :cvar
308
- if @parser.in_def?
309
- node.updated(:cvasgn)
310
- else
311
- node.updated(:cvdecl)
312
- end
316
+ node.updated(:cvasgn)
313
317
 
314
318
  when :ivar
315
319
  node.updated(:ivasgn)
@@ -320,10 +324,10 @@ module Parser
320
324
  when :const
321
325
  if @parser.in_def?
322
326
  message = ERRORS[:dynamic_const]
323
- diagnostic :error, message, node.src.expression
327
+ diagnostic :error, message, node.loc.expression
324
328
  end
325
329
 
326
- node.updated(:cdecl)
330
+ node.updated(:casgn)
327
331
 
328
332
  when :ident
329
333
  name, = *node
@@ -334,29 +338,26 @@ module Parser
334
338
  when :nil, :self, :true, :false,
335
339
  :__FILE__, :__LINE__, :__ENCODING__
336
340
  message = ERRORS[:invalid_assignment]
337
- diagnostic :error, message, node.src.expression
341
+ diagnostic :error, message, node.loc.expression
338
342
 
339
343
  when :back_ref, :nth_ref
340
344
  message = ERRORS[:backref_assignment]
341
- diagnostic :error, message, node.src.expression
345
+ diagnostic :error, message, node.loc.expression
342
346
  end
343
347
  end
344
348
 
345
349
  def assign(lhs, eql_t, rhs)
346
350
  (lhs << rhs).updated(nil, nil,
347
- :source_map => lhs.src.
351
+ :location => lhs.loc.
348
352
  with_operator(loc(eql_t)).
349
353
  with_expression(join_exprs(lhs, rhs)))
350
354
  end
351
355
 
352
356
  def op_assign(lhs, op_t, rhs)
353
357
  case lhs.type
354
- when :gvasgn, :ivasgn, :lvasgn, :cvasgn, :cvdecl,
355
- :cdecl,
356
- :send
357
-
358
+ when :gvasgn, :ivasgn, :lvasgn, :cvasgn, :casgn, :send
358
359
  operator = value(op_t)[0..-1].to_sym
359
- source_map = lhs.src.
360
+ source_map = lhs.loc.
360
361
  with_operator(loc(op_t)).
361
362
  with_expression(join_exprs(lhs, rhs))
362
363
 
@@ -371,7 +372,7 @@ module Parser
371
372
 
372
373
  when :back_ref, :nth_ref
373
374
  message = ERRORS[:backref_assignment]
374
- diagnostic :error, message, lhs.src.expression
375
+ diagnostic :error, message, lhs.loc.expression
375
376
  end
376
377
  end
377
378
 
@@ -413,20 +414,20 @@ module Parser
413
414
  #
414
415
 
415
416
  def def_method(def_t, name_t, args,
416
- body, end_t, comments)
417
+ body, end_t)
417
418
  n(:def, [ value(name_t).to_sym, args, body ],
418
419
  definition_map(def_t, nil, name_t, end_t))
419
420
  end
420
421
 
421
422
  def def_singleton(def_t, definee, dot_t,
422
423
  name_t, args,
423
- body, end_t, comments)
424
+ body, end_t)
424
425
  case definee.type
425
426
  when :int, :str, :dstr, :sym, :dsym,
426
427
  :regexp, :array, :hash
427
428
 
428
429
  message = ERRORS[:singleton_literal]
429
- diagnostic :error, message, definee.src.expression
430
+ diagnostic :error, message, definee.loc.expression
430
431
 
431
432
  else
432
433
  n(:defs, [ definee, value(name_t).to_sym, args, body ],
@@ -462,7 +463,7 @@ module Parser
462
463
  n(:optarg, [ value(name_t).to_sym, value ],
463
464
  variable_map(name_t).
464
465
  with_operator(loc(eql_t)).
465
- with_expression(loc(name_t).join(value.src.expression)))
466
+ with_expression(loc(name_t).join(value.loc.expression)))
466
467
  end
467
468
 
468
469
  def restarg(star_t, name_t=nil)
@@ -512,7 +513,7 @@ module Parser
512
513
  expr.updated(:arg)
513
514
  else
514
515
  n(:arg_expr, [ expr ],
515
- expr.src)
516
+ expr.loc)
516
517
  end
517
518
  end
518
519
 
@@ -523,7 +524,7 @@ module Parser
523
524
  expr.updated(:restarg)
524
525
  else
525
526
  n(:restarg_expr, [ expr ],
526
- expr.src)
527
+ expr.loc)
527
528
  end
528
529
  end
529
530
 
@@ -532,7 +533,7 @@ module Parser
532
533
  expr.updated(:blockarg)
533
534
  else
534
535
  n(:blockarg_expr, [ expr ],
535
- expr.src)
536
+ expr.loc)
536
537
  end
537
538
  end
538
539
 
@@ -562,11 +563,11 @@ module Parser
562
563
 
563
564
  if last_arg && last_arg.type == :block_pass
564
565
  diagnostic :error, ERRORS[:block_and_blockarg],
565
- last_arg.src.expression
566
+ last_arg.loc.expression
566
567
  end
567
568
 
568
569
  n(:block, [ method_call, args, body ],
569
- block_map(method_call.src.expression, begin_t, end_t))
570
+ block_map(method_call.loc.expression, begin_t, end_t))
570
571
  end
571
572
 
572
573
  def block_pass(amper_t, arg)
@@ -782,7 +783,7 @@ module Parser
782
783
  expr_map(loc(begin_t).join(loc(end_t))))
783
784
  elsif body.type == :mlhs ||
784
785
  (body.type == :begin &&
785
- body.src.begin.nil? && body.src.end.nil?)
786
+ body.loc.begin.nil? && body.loc.end.nil?)
786
787
  # Synthesized (begin) from compstmt "a; b" or (mlhs)
787
788
  # from multi_lhs "(a, b) = *foo".
788
789
  n(body.type, body.children,
@@ -802,7 +803,7 @@ module Parser
802
803
  def check_condition(cond)
803
804
  if cond.type == :masgn
804
805
  diagnostic :error, ERRORS[:masgn_as_condition],
805
- cond.src.expression
806
+ cond.loc.expression
806
807
  elsif cond.type == :begin
807
808
  check_condition(cond.children.last)
808
809
  end
@@ -826,7 +827,7 @@ module Parser
826
827
  map[this_name] = this_arg
827
828
  elsif arg_name_collides?(this_name, that_name)
828
829
  diagnostic :error, ERRORS[:duplicate_argument],
829
- this_arg.src.name, [ that_arg.src.name ]
830
+ this_arg.loc.name, [ that_arg.loc.name ]
830
831
  end
831
832
 
832
833
  when :mlhs
@@ -854,17 +855,17 @@ module Parser
854
855
  # SOURCE MAPS
855
856
  #
856
857
 
857
- def n(type, children, map)
858
- AST::Node.new(type, children, :source_map => map)
858
+ def n(type, children, source_map)
859
+ AST::Node.new(type, children, :location => source_map)
859
860
  end
860
861
 
861
- def n0(type, map)
862
- n(type, [], map)
862
+ def n0(type, source_map)
863
+ n(type, [], source_map)
863
864
  end
864
865
 
865
866
  def join_exprs(left_expr, right_expr)
866
- left_expr.src.expression.
867
- join(right_expr.src.expression)
867
+ left_expr.loc.expression.
868
+ join(right_expr.loc.expression)
868
869
  end
869
870
 
870
871
  def token_map(token)
@@ -904,6 +905,25 @@ module Parser
904
905
  loc(symbol_t))
905
906
  end
906
907
 
908
+ def pair_keyword_map(key_t, value_e)
909
+ key_range = loc(key_t)
910
+
911
+ key_l = Source::Range.new(key_range.source_buffer,
912
+ key_range.begin_pos,
913
+ key_range.end_pos - 1)
914
+
915
+ colon_l = Source::Range.new(key_range.source_buffer,
916
+ key_range.end_pos - 1,
917
+ key_range.end_pos)
918
+
919
+ # key map
920
+ [ Source::Map::Collection.new(nil, nil,
921
+ key_l),
922
+ # pair map
923
+ Source::Map::Operator.new(colon_l,
924
+ key_range.join(value_e.loc.expression)) ]
925
+ end
926
+
907
927
  def expr_map(loc)
908
928
  Source::Map.new(loc)
909
929
  end
@@ -922,14 +942,14 @@ module Parser
922
942
 
923
943
  def regexp_map(begin_t, end_t, options_e)
924
944
  Source::Map::Collection.new(loc(begin_t), loc(end_t),
925
- loc(begin_t).join(options_e.src.expression))
945
+ loc(begin_t).join(options_e.loc.expression))
926
946
  end
927
947
 
928
948
  def constant_map(scope, colon2_t, name_t)
929
949
  if scope.nil?
930
950
  expr_l = loc(name_t)
931
951
  else
932
- expr_l = scope.src.expression.join(loc(name_t))
952
+ expr_l = scope.loc.expression.join(loc(name_t))
933
953
  end
934
954
 
935
955
  Source::Map::Constant.new(loc(colon2_t), loc(name_t), expr_l)
@@ -947,7 +967,7 @@ module Parser
947
967
  if arg_e.nil?
948
968
  expr_l = loc(op_t)
949
969
  else
950
- expr_l = loc(op_t).join(arg_e.src.expression)
970
+ expr_l = loc(op_t).join(arg_e.loc.expression)
951
971
  end
952
972
 
953
973
  Source::Map::Operator.new(loc(op_t), expr_l)
@@ -970,7 +990,7 @@ module Parser
970
990
  label_range.end_pos - 1)
971
991
 
972
992
  if value_e
973
- expr_l = loc(name_t).join(value_e.src.expression)
993
+ expr_l = loc(name_t).join(value_e.loc.expression)
974
994
  else
975
995
  expr_l = loc(name_t)
976
996
  end
@@ -980,7 +1000,7 @@ module Parser
980
1000
 
981
1001
  def module_definition_map(keyword_t, name_e, operator_t, end_t)
982
1002
  if name_e
983
- name_l = name_e.src.expression
1003
+ name_l = name_e.loc.expression
984
1004
  end
985
1005
 
986
1006
  Source::Map::Definition.new(loc(keyword_t),
@@ -996,7 +1016,7 @@ module Parser
996
1016
 
997
1017
  def send_map(receiver_e, dot_t, selector_t, begin_t=nil, args=[], end_t=nil)
998
1018
  if receiver_e
999
- begin_l = receiver_e.src.expression
1019
+ begin_l = receiver_e.loc.expression
1000
1020
  elsif selector_t
1001
1021
  begin_l = loc(selector_t)
1002
1022
  end
@@ -1004,7 +1024,7 @@ module Parser
1004
1024
  if end_t
1005
1025
  end_l = loc(end_t)
1006
1026
  elsif args.any?
1007
- end_l = args.last.src.expression
1027
+ end_l = args.last.loc.expression
1008
1028
  elsif selector_t
1009
1029
  end_l = loc(selector_t)
1010
1030
  end
@@ -1015,9 +1035,9 @@ module Parser
1015
1035
  end
1016
1036
 
1017
1037
  def var_send_map(variable_e)
1018
- Source::Map::Send.new(nil, variable_e.src.expression,
1038
+ Source::Map::Send.new(nil, variable_e.loc.expression,
1019
1039
  nil, nil,
1020
- variable_e.src.expression)
1040
+ variable_e.loc.expression)
1021
1041
  end
1022
1042
 
1023
1043
  def send_binary_op_map(lhs_e, selector_t, rhs_e)
@@ -1030,7 +1050,7 @@ module Parser
1030
1050
  if arg_e.nil?
1031
1051
  expr_l = loc(selector_t)
1032
1052
  else
1033
- expr_l = loc(selector_t).join(arg_e.src.expression)
1053
+ expr_l = loc(selector_t).join(arg_e.loc.expression)
1034
1054
  end
1035
1055
 
1036
1056
  Source::Map::Send.new(nil, loc(selector_t),
@@ -1041,7 +1061,7 @@ module Parser
1041
1061
  def send_index_map(receiver_e, lbrack_t, rbrack_t)
1042
1062
  Source::Map::Send.new(nil, loc(lbrack_t).join(loc(rbrack_t)),
1043
1063
  nil, nil,
1044
- receiver_e.src.expression.join(loc(rbrack_t)))
1064
+ receiver_e.loc.expression.join(loc(rbrack_t)))
1045
1065
  end
1046
1066
 
1047
1067
  def block_map(receiver_l, begin_t, end_t)
@@ -1055,9 +1075,9 @@ module Parser
1055
1075
  if end_t
1056
1076
  end_l = loc(end_t)
1057
1077
  elsif args.any? && !synthesized_nil?(args.last)
1058
- end_l = args.last.src.expression
1078
+ end_l = args.last.loc.expression
1059
1079
  elsif args.any? && args.count > 1
1060
- end_l = args[-2].src.expression
1080
+ end_l = args[-2].loc.expression
1061
1081
  else
1062
1082
  end_l = loc(keyword_t)
1063
1083
  end
@@ -1074,16 +1094,16 @@ module Parser
1074
1094
  def condition_map(keyword_t, cond_e, begin_t, body_e, else_t, else_e, end_t)
1075
1095
  if end_t
1076
1096
  end_l = loc(end_t)
1077
- elsif else_e && else_e.src.expression
1078
- end_l = else_e.src.expression
1097
+ elsif else_e && else_e.loc.expression
1098
+ end_l = else_e.loc.expression
1079
1099
  elsif loc(else_t)
1080
1100
  end_l = loc(else_t)
1081
- elsif body_e.src.expression
1082
- end_l = body_e.src.expression
1101
+ elsif body_e.loc.expression
1102
+ end_l = body_e.loc.expression
1083
1103
  elsif loc(begin_t)
1084
1104
  end_l = loc(begin_t)
1085
1105
  else
1086
- end_l = cond_e.src.expression
1106
+ end_l = cond_e.loc.expression
1087
1107
  end
1088
1108
 
1089
1109
  Source::Map::Condition.new(loc(keyword_t),
@@ -1105,9 +1125,9 @@ module Parser
1105
1125
  def rescue_body_map(keyword_t, exc_list_e, assoc_t,
1106
1126
  exc_var_e, then_t,
1107
1127
  compstmt_e)
1108
- end_l = compstmt_e.src.expression || loc(then_t)
1109
- end_l = exc_var_e.src.expression if end_l.nil? && exc_var_e
1110
- end_l = exc_list_e.src.expression if end_l.nil? && exc_list_e
1128
+ end_l = compstmt_e.loc.expression || loc(then_t)
1129
+ end_l = exc_var_e.loc.expression if end_l.nil? && exc_var_e
1130
+ end_l = exc_list_e.loc.expression if end_l.nil? && exc_list_e
1111
1131
  end_l = loc(keyword_t) if end_l.nil?
1112
1132
 
1113
1133
  Source::Map::RescueBody.new(loc(keyword_t), loc(assoc_t), loc(then_t),
@@ -1118,18 +1138,18 @@ module Parser
1118
1138
  else_t, else_e)
1119
1139
  if synthesized_nil?(compstmt_e)
1120
1140
  if keyword_t.nil?
1121
- begin_l = body_es.first.src.expression
1141
+ begin_l = body_es.first.loc.expression
1122
1142
  else
1123
1143
  begin_l = loc(keyword_t)
1124
1144
  end
1125
1145
  else
1126
- begin_l = compstmt_e.src.expression
1146
+ begin_l = compstmt_e.loc.expression
1127
1147
  end
1128
1148
 
1129
1149
  if else_t
1130
- end_l = else_e.src.expression
1150
+ end_l = else_e.loc.expression
1131
1151
  elsif !synthesized_nil?(body_es.last)
1132
- end_l = body_es.last.src.expression
1152
+ end_l = body_es.last.loc.expression
1133
1153
  else
1134
1154
  end_l = loc(keyword_t)
1135
1155
  end
@@ -1148,7 +1168,7 @@ module Parser
1148
1168
  end
1149
1169
 
1150
1170
  def synthesized_nil?(node)
1151
- node.type == :nil && node.src.expression.nil?
1171
+ node.type == :nil && node.loc.expression.nil?
1152
1172
  end
1153
1173
 
1154
1174
  def value(token)