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 +4 -4
- data/lib/parser/source/buffer.rb +49 -32
- data/lib/parser/version.rb +1 -1
- data/test/test_source_buffer.rb +8 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1651bd7bc0d39e00057fd6f24dd4a6ef2239e108
|
4
|
+
data.tar.gz: 31d38b4035c2cb105075c204ff321b725653c7c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6452eeb521655b074987d3d17daf6b5cfbbd979769c3a892db9ba970a2f774180ad81eeeb8d9f2e412c6c14d8bed95510061266144e24a9fada054ec3071aae4
|
7
|
+
data.tar.gz: 4454c5b7db370bc275a2deb63eca54c8c8d896db0726a5a9f688576d8219670ff49527cac3a636551a920e773b7bfd2104b6570cb448e6fe7182d75b4de80b1d
|
data/lib/parser/source/buffer.rb
CHANGED
@@ -49,9 +49,12 @@ module Parser
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def initialize(name, first_line = 1)
|
52
|
-
@name
|
53
|
-
@
|
54
|
-
@
|
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
|
80
|
-
|
81
|
-
freeze
|
86
|
+
@source = source.freeze
|
82
87
|
end
|
83
88
|
|
84
89
|
def decompose_position(position)
|
85
|
-
|
86
|
-
line_begin = line_begin_positions[line]
|
90
|
+
line_no, line_begin = line_for(position)
|
87
91
|
|
88
|
-
[ @first_line +
|
92
|
+
[ @first_line + line_no, position - line_begin ]
|
89
93
|
end
|
90
94
|
|
91
95
|
def source_line(line)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
-
|
122
|
-
|
123
|
-
|
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
|
data/lib/parser/version.rb
CHANGED
data/test/test_source_buffer.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2013-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ast
|