parser 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45d8bc198add0c9c9e021a8181852b1181acb18b
4
- data.tar.gz: 8f88c887edf5beec84bffaf73eaf010067d1b311
3
+ metadata.gz: 1651bd7bc0d39e00057fd6f24dd4a6ef2239e108
4
+ data.tar.gz: 31d38b4035c2cb105075c204ff321b725653c7c9
5
5
  SHA512:
6
- metadata.gz: 4aab5ebeaf4117abc466c56bf10a69398e426a6976cb23f013a4629a40ef28692bd2a001fff899a755b932ff83e47ec220e4935d8f51fde456a2b632c171ebfc
7
- data.tar.gz: 231b1dcb3345a4b692fd2f4e9efb50f4d836a848cd0ce9002b6b99a06c6345767bb3cce4ea299ea76990a858c1e7eaebcbbd4ef9a23e57e6d11d90b6114f9c09
6
+ metadata.gz: 6452eeb521655b074987d3d17daf6b5cfbbd979769c3a892db9ba970a2f774180ad81eeeb8d9f2e412c6c14d8bed95510061266144e24a9fada054ec3071aae4
7
+ data.tar.gz: 4454c5b7db370bc275a2deb63eca54c8c8d896db0726a5a9f688576d8219670ff49527cac3a636551a920e773b7bfd2104b6570cb448e6fe7182d75b4de80b1d
@@ -49,9 +49,12 @@ module Parser
49
49
  end
50
50
 
51
51
  def initialize(name, first_line = 1)
52
- @name = name
53
- @first_line = first_line
54
- @source = nil
52
+ @name = name
53
+ @source = nil
54
+ @first_line = first_line
55
+
56
+ @lines = nil
57
+ @line_begins = nil
55
58
  end
56
59
 
57
60
  def read
@@ -71,56 +74,70 @@ module Parser
71
74
  end
72
75
 
73
76
  def source=(source)
77
+ if @source
78
+ raise ArgumentError, "Source::Buffer is immutable"
79
+ end
80
+
74
81
  if source.respond_to? :encoding
75
82
  source = source.dup if source.frozen?
76
83
  source = self.class.reencode_string(source)
77
84
  end
78
85
 
79
- @source = source.freeze
80
-
81
- freeze
86
+ @source = source.freeze
82
87
  end
83
88
 
84
89
  def decompose_position(position)
85
- line = line_for(position)
86
- line_begin = line_begin_positions[line]
90
+ line_no, line_begin = line_for(position)
87
91
 
88
- [ @first_line + line, position - line_begin ]
92
+ [ @first_line + line_no, position - line_begin ]
89
93
  end
90
94
 
91
95
  def source_line(line)
92
- mapped_line = line - @first_line
93
-
94
- # Consider improving this naïve implementation.
95
- source_line = source.lines.drop(mapped_line).first
96
-
97
- # Line endings will be commonly present for all lines
98
- # except the last one. It does not make sense to keep them.
99
- if source_line.end_with? "\n"
100
- source_line.chomp
101
- else
102
- source_line
96
+ unless @lines
97
+ @lines = @source.lines.map do |source_line|
98
+ # Line endings will be commonly present for all lines
99
+ # except the last one. It does not make sense to keep them.
100
+ if source_line.end_with? "\n"
101
+ source_line.chomp
102
+ else
103
+ source_line
104
+ end
105
+ end
103
106
  end
107
+
108
+ @lines[line - @first_line]
104
109
  end
105
110
 
106
111
  private
107
112
 
108
- def line_begin_positions
109
- # TODO: Optimize this.
110
- [0] + source.
111
- each_char.
112
- with_index.
113
- select do |char, index|
114
- char == "\n"
115
- end.map do |char, index|
116
- index + 1
113
+ def line_begins
114
+ unless @line_begins
115
+ @line_begins, index = [ [ 0, 0 ] ], 1
116
+
117
+ @source.each_char do |char|
118
+ if char == "\n"
119
+ @line_begins << [ @line_begins.length, index ]
120
+ end
121
+
122
+ index += 1
117
123
  end
124
+
125
+ @line_begins = @line_begins.reverse
126
+ end
127
+
128
+ @line_begins
118
129
  end
119
130
 
120
131
  def line_for(position)
121
- # TODO: Optimize this.
122
- line_begin_positions.rindex do |line_beg|
123
- line_beg <= position
132
+ if line_begins.respond_to? :bsearch
133
+ line_begins.bsearch do |line, line_begin|
134
+ line_begin <= position
135
+ end
136
+ else
137
+ # Slower variant for <= Ruby 2.0.
138
+ line_begins.find do |line, line_begin|
139
+ line_begin <= position
140
+ end
124
141
  end
125
142
  end
126
143
  end
@@ -1,3 +1,3 @@
1
1
  module Parser
2
- VERSION = '1.3.2'
2
+ VERSION = '1.3.3'
3
3
  end
@@ -18,10 +18,17 @@ class TestSourceBuffer < MiniTest::Unit::TestCase
18
18
  @buffer.source = 'foo'
19
19
  assert_equal 'foo', @buffer.source
20
20
 
21
- assert @buffer.frozen?
22
21
  assert @buffer.source.frozen?
23
22
  end
24
23
 
24
+ def test_source_double_setter
25
+ @buffer.source = 'foo'
26
+
27
+ assert_raises(ArgumentError) do
28
+ @buffer.source = 'bar'
29
+ end
30
+ end
31
+
25
32
  def test_read
26
33
  tempfile = Tempfile.new('parser')
27
34
  tempfile.write('foobar')
@@ -31,7 +38,6 @@ class TestSourceBuffer < MiniTest::Unit::TestCase
31
38
  buffer.read
32
39
  assert_equal 'foobar', buffer.source
33
40
 
34
- assert buffer.frozen?
35
41
  assert buffer.source.frozen?
36
42
  end
37
43
 
@@ -41,11 +47,6 @@ class TestSourceBuffer < MiniTest::Unit::TestCase
41
47
  end
42
48
  end
43
49
 
44
- def test_line_begin_positions
45
- @buffer.source = "1\nfoo\nbar"
46
- assert_equal [0, 2, 6], @buffer.send(:line_begin_positions)
47
- end
48
-
49
50
  def test_decompose_position
50
51
  @buffer.source = "1\nfoo\nbar"
51
52
 
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: 1.3.2
4
+ version: 1.3.3
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-05-13 00:00:00.000000000 Z
11
+ date: 2013-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast