Skip to content

Commit faac877

Browse files
committed
Ignore interleaved comments in the Parser
1 parent 5ed0fe0 commit faac877

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/parser.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
class Parser
2-
attr_reader :lines
3-
42
def initialize(input)
5-
input = input.gsub(%r{//.*$}, '').strip
6-
@lines = input.lines
3+
@lines = split_lines(input)
74
end
85

96
def has_more_commands?
10-
!@lines.empty?
7+
!lines.empty?
118
end
129

1310
def advance
14-
@lines.shift
11+
lines.shift
12+
end
13+
14+
private
15+
16+
attr_reader :lines
17+
18+
def split_lines(input)
19+
input
20+
.lines
21+
.map { |line| line.sub(%r{//.*$}, '').strip }
22+
.reject(&:empty?)
1523
end
1624
end

spec/unit/parser_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@
102102
expect(parser.has_more_commands?).to be_falsy
103103
end
104104
end
105+
106+
context 'with interleaved comments' do
107+
let(:input) do
108+
<<-eop
109+
@R0
110+
// An interleaved comment
111+
D=M
112+
eop
113+
end
114+
115+
it 'leaves no more commands' do
116+
expect(parser.has_more_commands?).to be_truthy
117+
parser.advance
118+
expect(parser.has_more_commands?).to be_truthy
119+
parser.advance
120+
expect(parser.has_more_commands?).to be_falsy
121+
end
122+
end
105123
end
106124

107125
describe '#command_type' do

0 commit comments

Comments
 (0)