parser 2.0.0.pre6 → 2.0.0.pre7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57e819d2cd65b374e49ccdc64313da51dab99dec
4
- data.tar.gz: 33288353f3f0585227149c307f5ca3d8a7247012
3
+ metadata.gz: 776614b2c048d80ca7eadd5fd3c3f914c51d88ec
4
+ data.tar.gz: 01ddc7d4877aa4198de7685707d410febc010c29
5
5
  SHA512:
6
- metadata.gz: df8f0b48532727b9d74a81f91d0293fc9412942280ab02065d36f8578c4086e496ab7aa05de95b9e894be640135502af0c72e1a9aa85cb782153956e531dfafb
7
- data.tar.gz: 16b3d4f6db7babe591faa0ae73bfd78602f681a67f1f19bfe654ab7e711905c41c764431498909035f231f3ad24c70cae121787aead0e318d5fea80592e1f7c4
6
+ metadata.gz: bab2b3791e25cfe5a13fc6cd844375377c3d678f302b79f3580d10fad2cb6dda60d5d371c8388142899eb803a238c84c9ccc2d2d46d3d2d00f80b53c08df7afc
7
+ data.tar.gz: be57c964d5423e2b7f53a39db34a7e668f31fe5f9fe032678e1f4d83030f304ba3150f345c6c4b4ccfa64ffde2c652df9adc179febc7d2bfa0a8a2cb3e464eaa
@@ -1,6 +1,16 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ v2.0.0.pre7 (2013-09-10)
5
+ ------------------------
6
+
7
+ Features implemented:
8
+ * Parser::Base: add #parse_with_comments, #parse_file_with_comments. (Trent Ogren)
9
+
10
+ Bugs fixed:
11
+ * lexer.rl: "->*{}": tLAMBEG at expr_beg (fixes #103). (Peter Zotov)
12
+ * Source::Rewriter: apply actions in the insertion order. (Josh Cheek)
13
+
4
14
  v2.0.0.pre5 (2013-07-31)
5
15
  ------------------------
6
16
 
@@ -26,6 +26,60 @@ module Parser
26
26
  # @return [Parser::AST::Node]
27
27
  #
28
28
  def self.parse(string, file='(string)', line=1)
29
+ parser = default_parser
30
+ source_buffer = setup_source_buffer(file, line, string, parser.default_encoding)
31
+ parser.parse(source_buffer)
32
+ end
33
+
34
+ ##
35
+ # Parses a string of Ruby code and returns the AST and comments. If the
36
+ # source cannot be parsed, {SyntaxError} is raised and a diagnostic is
37
+ # printed to `stderr`.
38
+ #
39
+ # @example
40
+ # Parser::Base.parse_with_comments('puts "hello"')
41
+ #
42
+ # @param [String] string The block of code to parse.
43
+ # @param [String] file The name of the file the code originated from.
44
+ # @param [Numeric] line The initial line number.
45
+ # @return [Array]
46
+ #
47
+ def self.parse_with_comments(string, file='(string)', line=1)
48
+ parser = default_parser
49
+ source_buffer = setup_source_buffer(file, line, string, parser.default_encoding)
50
+ parser.parse_with_comments(source_buffer)
51
+ end
52
+
53
+ ##
54
+ # Parses Ruby source code by reading it from a file. If the source
55
+ # cannot be parsed, {SyntaxError} is raised and a diagnostic is
56
+ # printed to `stderr`.
57
+ #
58
+ # @param [String] filename Path to the file to parse.
59
+ # @return [Parser::AST::Node]
60
+ # @see #parse
61
+ #
62
+ def self.parse_file(filename)
63
+ parse(File.read(filename), filename)
64
+ end
65
+
66
+ ##
67
+ # Parses Ruby source code by reading it from a file and returns the AST and
68
+ # comments. If the source cannot be parsed, {SyntaxError} is raised and a
69
+ # diagnostic is printed to `stderr`.
70
+ #
71
+ # @param [String] filename Path to the file to parse.
72
+ # @return [Array]
73
+ # @see #parse
74
+ #
75
+ def self.parse_file_with_comments(filename)
76
+ parse_with_comments(File.read(filename), filename)
77
+ end
78
+
79
+ ##
80
+ # @return [Parser::Base] parser with the default options set.
81
+ #
82
+ def self.default_parser
29
83
  parser = new
30
84
 
31
85
  parser.diagnostics.all_errors_are_fatal = true
@@ -35,7 +89,11 @@ module Parser
35
89
  $stderr.puts(diagnostic.render)
36
90
  end
37
91
 
38
- string = string.dup.force_encoding(parser.default_encoding)
92
+ parser
93
+ end
94
+
95
+ def self.setup_source_buffer(file, line, string, encoding)
96
+ string = string.dup.force_encoding(encoding)
39
97
 
40
98
  source_buffer = Source::Buffer.new(file, line)
41
99
 
@@ -45,20 +103,9 @@ module Parser
45
103
  source_buffer.source = string
46
104
  end
47
105
 
48
- parser.parse(source_buffer)
49
- end
50
-
51
- ##
52
- # Parses Ruby source code by reading it from a file. If the source
53
- # cannot be parsed, {SyntaxError} is raised and a diagnostic is
54
- # printed to `stderr`.
55
- #
56
- # @param [String] filename Path to the file to parse.
57
- # @see #parse
58
- #
59
- def self.parse_file(filename)
60
- parse(File.read(filename), filename)
106
+ source_buffer
61
107
  end
108
+ private_class_method :setup_source_buffer
62
109
 
63
110
  attr_reader :diagnostics
64
111
  attr_reader :builder
@@ -48,11 +48,16 @@ module Parser
48
48
  end
49
49
 
50
50
  ##
51
- # Processes a diagnostic and optionally raises {Parser::SyntaxError} when
52
- # `all_errors_are_fatal` is set to true.
51
+ # Processes a `diagnostic`:
52
+ # * Passes the diagnostic to the consumer, if it's not a warning when
53
+ # `ignore_warnings` is set.
54
+ # * After that, raises {Parser::SyntaxError} when `all_errors_are_fatal`
55
+ # is set to true.
53
56
  #
54
57
  # @param [Parser::Diagnostic] diagnostic
55
58
  # @return [Parser::Diagnostic::Engine]
59
+ # @see ignore?
60
+ # @see raise?
56
61
  #
57
62
  def process(diagnostic)
58
63
  if ignore?(diagnostic)
@@ -71,6 +76,8 @@ module Parser
71
76
  protected
72
77
 
73
78
  ##
79
+ # Checks whether `diagnostic` should be ignored.
80
+ #
74
81
  # @param [Parser::Diagnostic] diagnostic
75
82
  # @return [TrueClass|FalseClass]
76
83
  #
@@ -80,6 +87,8 @@ module Parser
80
87
  end
81
88
 
82
89
  ##
90
+ # Checks whether `diagnostic` should be raised as an exception.
91
+ #
83
92
  # @param [Parser::Diagnostic] diagnostic
84
93
  # @return [TrueClass|FalseClass]
85
94
  #
@@ -1648,10 +1648,20 @@ class Parser::Lexer
1648
1648
  # KEYWORDS AND PUNCTUATION
1649
1649
  #
1650
1650
 
1651
+ # a({b=>c})
1652
+ e_lbrace
1653
+ => {
1654
+ if @lambda_stack.last == @paren_nest
1655
+ @lambda_stack.pop
1656
+ emit(:tLAMBEG)
1657
+ else
1658
+ emit_table(PUNCTUATION_BEGIN)
1659
+ end
1660
+ fbreak;
1661
+ };
1662
+
1651
1663
  # a([1, 2])
1652
1664
  e_lbrack |
1653
- # a({b=>c})
1654
- e_lbrace |
1655
1665
  # a()
1656
1666
  e_lparen
1657
1667
  => { emit_table(PUNCTUATION_BEGIN)
@@ -22,11 +22,11 @@ module Parser
22
22
  # end
23
23
  # EOF
24
24
  #
25
- # buffer = Parser::Source::Buffer.new('(example)')
26
- # buffer.code = code
27
- # parser = Parser::CurrentRuby.new
28
- # ast = parser.parse(buffer)
29
- # rewriter = RemoveDo.new
25
+ # buffer = Parser::Source::Buffer.new('(example)')
26
+ # buffer.source = code
27
+ # parser = Parser::CurrentRuby.new
28
+ # ast = parser.parse(buffer)
29
+ # rewriter = RemoveDo.new
30
30
  #
31
31
  # # Rewrite the AST, returns a String with the new form.
32
32
  # puts rewriter.rewrite(buffer, ast)
@@ -35,11 +35,12 @@ module Parser
35
35
  end
36
36
 
37
37
  def process
38
- adjustment = 0
39
- source = @source_buffer.source.dup
40
-
41
- @queue.sort_by { |action| action.range.begin_pos }.
42
- each do |action|
38
+ adjustment = 0
39
+ source = @source_buffer.source.dup
40
+ sorted_queue = @queue.sort_by.with_index do |action, index|
41
+ [action.range.begin_pos, index]
42
+ end
43
+ sorted_queue.each do |action|
43
44
  begin_pos = action.range.begin_pos + adjustment
44
45
  end_pos = begin_pos + action.range.length
45
46
 
@@ -1,3 +1,3 @@
1
1
  module Parser
2
- VERSION = '2.0.0.pre6'
2
+ VERSION = '2.0.0.pre7'
3
3
  end
@@ -2917,6 +2917,17 @@ class TestParser < Minitest::Test
2917
2917
  |~~~~~ expression},
2918
2918
  ALL_VERSIONS - %w(1.8))
2919
2919
 
2920
+ assert_parses(
2921
+ s(:block, s(:send, nil, :lambda),
2922
+ s(:args, s(:restarg)), nil),
2923
+ %q{-> * { }},
2924
+ %q{~~ selector (send)
2925
+ | ^ begin
2926
+ | ^ end
2927
+ | ^ expression (args.restarg)
2928
+ |~~~~~~~~ expression},
2929
+ ALL_VERSIONS - %w(1.8))
2930
+
2920
2931
  assert_parses(
2921
2932
  s(:block, s(:send, nil, :lambda),
2922
2933
  s(:args), nil),
@@ -56,6 +56,18 @@ class TestSourceRewriter < Minitest::Test
56
56
  process
57
57
  end
58
58
 
59
+ def test_multiple_insertions_at_same_location
60
+ assert_equal '<([foo] bar) baz>',
61
+ @rewriter.
62
+ insert_before(range(0, 11), '<').
63
+ insert_after( range(0, 11), '>').
64
+ insert_before(range(0, 7), '(').
65
+ insert_after( range(0, 7), ')').
66
+ insert_before(range(0, 3), '[').
67
+ insert_after( range(0, 3), ']').
68
+ process
69
+ end
70
+
59
71
  def test_clobber
60
72
  diagnostics = []
61
73
  @rewriter.diagnostics.consumer = lambda do |diag|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre6
4
+ version: 2.0.0.pre7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Zotov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-02 00:00:00.000000000 Z
11
+ date: 2013-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast