Skip to content

Commit

Permalink
Add basic specs for column numbers from lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Nov 23, 2013
1 parent ad317e2 commit 8544814
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/opal/parser/lexer.rb
Expand Up @@ -123,6 +123,9 @@ def next_token
value = self.yylval
location = [@line, @tok_column]

# once location is stored, ensure next token starts in correct place
@tok_column = @column

[token, [value, location]]
end

Expand Down
26 changes: 23 additions & 3 deletions spec/cli/lexer_spec.rb
Expand Up @@ -18,10 +18,30 @@
expect_lines("=begin\nfoo\nbar\n=end\n42\n43").to eq([5, 6])
end

it "sets correct column for each token" do
expect_columns("1").to eq([0])
expect_columns("1;2; 3").to eq([0, 2, 5])
expect_columns(" \t3").to eq([5])
end

it "sets the column to 0 on each new line" do
expect_columns("1\n2\n\n\n3\n 4").to eq([0, 0, 0, 1])
end

it "sets column with regard to whitespace and other tokens before it" do
expect_columns("true; false; self\n ni;").to eq([0, 7, 15, 2])
end

def expect_lines(source)
parsed = Opal::Parser.new.parse(source)
nodes = parsed.type == :block ? parsed.children : [parsed]
expect(parsed_nodes(source).map { |sexp| sexp.line })
end

expect(nodes.map { |sexp| sexp.line })
def expect_columns(source)
expect(parsed_nodes(source).map { |sexp| sexp.column })
end

def parsed_nodes(source)
parsed = Opal::Parser.new.parse(source)
parsed.type == :block ? parsed.children : [parsed]
end
end

0 comments on commit 8544814

Please sign in to comment.