Skip to content

Commit 8544814

Browse files
committed
Add basic specs for column numbers from lexer
1 parent ad317e2 commit 8544814

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/opal/parser/lexer.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ def next_token
123123
value = self.yylval
124124
location = [@line, @tok_column]
125125

126+
# once location is stored, ensure next token starts in correct place
127+
@tok_column = @column
128+
126129
[token, [value, location]]
127130
end
128131

spec/cli/lexer_spec.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,30 @@
1818
expect_lines("=begin\nfoo\nbar\n=end\n42\n43").to eq([5, 6])
1919
end
2020

21+
it "sets correct column for each token" do
22+
expect_columns("1").to eq([0])
23+
expect_columns("1;2; 3").to eq([0, 2, 5])
24+
expect_columns(" \t3").to eq([5])
25+
end
26+
27+
it "sets the column to 0 on each new line" do
28+
expect_columns("1\n2\n\n\n3\n 4").to eq([0, 0, 0, 1])
29+
end
30+
31+
it "sets column with regard to whitespace and other tokens before it" do
32+
expect_columns("true; false; self\n ni;").to eq([0, 7, 15, 2])
33+
end
34+
2135
def expect_lines(source)
22-
parsed = Opal::Parser.new.parse(source)
23-
nodes = parsed.type == :block ? parsed.children : [parsed]
36+
expect(parsed_nodes(source).map { |sexp| sexp.line })
37+
end
2438

25-
expect(nodes.map { |sexp| sexp.line })
39+
def expect_columns(source)
40+
expect(parsed_nodes(source).map { |sexp| sexp.column })
41+
end
42+
43+
def parsed_nodes(source)
44+
parsed = Opal::Parser.new.parse(source)
45+
parsed.type == :block ? parsed.children : [parsed]
2646
end
2747
end

0 commit comments

Comments
 (0)