Skip to content

Commit

Permalink
adding line numbers to scope nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiokung committed Feb 6, 2009
1 parent cec5ec8 commit cd89040
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
17 changes: 13 additions & 4 deletions lib/ruby_parser_extras.rb
Expand Up @@ -491,6 +491,8 @@ def new_case expr, body
def new_class val
line, path, superclass, body = val[1], val[2], val[3], val[5]
scope = s(:scope, body).compact
scope.line = line
scope.endline = lexer.lineno
result = s(:class, path, superclass, scope)
result.line = line
result.comments = self.comments.pop
Expand All @@ -505,14 +507,15 @@ def new_compstmt val

def new_defn val
(line, bol), name, args, body = val[2], val[1], val[3], val[4]
line -= 1 if bol
body ||= s(:nil)

body ||= s(:block)
body = s(:block, body) unless body.first == :block

result = s(:defn, name.to_sym, args, s(:scope, body))
scope = s(:scope, body).line(line).endline(lexer.lineno)
result = s(:defn, name.to_sym, args, scope)
result.line = line
result.line -= 1 if bol
result.comments = self.comments.pop
result
end
Expand All @@ -523,7 +526,8 @@ def new_defs val
body ||= s(:block)
body = s(:block, body) unless body.first == :block

result = s(:defs, recv, name.to_sym, args, s(:scope, body))
scope = s(:scope, body).line(recv.line).endline(lexer.lineno)
result = s(:defs, recv, name.to_sym, args, scope)
result.line = recv.line
result.comments = self.comments.pop
result
Expand Down Expand Up @@ -563,6 +567,8 @@ def new_masgn lhs, rhs, wrap = false
def new_module val
line, path, body = val[1], val[2], val[4]
body = s(:scope, body).compact
body.line = line
body.endline = lexer.lineno
result = s(:module, path, body)
result.line = line
result.comments = self.comments.pop
Expand Down Expand Up @@ -637,9 +643,12 @@ def new_regexp val

def new_sclass val
recv, in_def, in_single, body = val[3], val[4], val[6], val[7]
line = val[2]
scope = s(:scope, body).compact
scope.line = line
scope.endline = lexer.lineno
result = s(:sclass, recv, scope)
result.line = val[2]
result.line = line
self.in_def = in_def
self.in_single = in_single
result
Expand Down
42 changes: 28 additions & 14 deletions test/test_ruby_parser.rb
Expand Up @@ -453,11 +453,13 @@ def test_position_info2

body = result.scope.block

assert_equal 1, result.line, "defn should have line number"
assert_equal 5, result.endline, "defn should have end line number"
assert_equal 2, body.call.line, "call should have line number"
assert_equal 3, body.lasgn.line, "lasgn should have line number"
assert_equal 4, body.return.line, "return should have line number"
assert_equal 1, result.line, "defn should have line number"
assert_equal 5, result.endline, "defn should have end line number"
assert_equal 1, result.scope.line, "scope should have line number"
assert_equal 5, result.scope.endline, "scope should have end line number"
assert_equal 2, body.call.line, "call should have line number"
assert_equal 3, body.lasgn.line, "lasgn should have line number"
assert_equal 4, body.return.line, "return should have line number"
end

def test_end_line_number_for_classes
Expand All @@ -479,15 +481,21 @@ def test_end_line_number_for_classes
body = result.scope.block

assert_equal pt, result
assert_equal(1, result.line, "class should have line number")
assert_equal(9, result.endline, "class should have end line number")
assert_equal(2, body.defs.line, "defs should have line number")
assert_equal(5, body.defs.endline, "defs should have end line number")
assert_equal(6, body.defn.line, "defn should have line number")
assert_equal(8, body.defn.endline, "defn should have end line number")
end

def test_end_line_number_for_modules_and_aliases
assert_equal(1, result.line, "class should have line number")
assert_equal(9, result.endline, "class should have end line number")
assert_equal(1, result.scope.line, "scope should have line number")
assert_equal(9, result.scope.endline, "scope should have end line number")
assert_equal(2, body.defs.line, "defs should have line number")
assert_equal(5, body.defs.endline, "defs should have end line number")
assert_equal(2, body.defs.scope.line, "defs scope should have line number")
assert_equal(5, body.defs.scope.endline, "defs scope should have end line number")
assert_equal(6, body.defn.line, "defn should have line number")
assert_equal(8, body.defn.endline, "defn should have end line number")
assert_equal(6, body.defn.scope.line, "defn scope should have line number")
assert_equal(8, body.defn.scope.endline, "defn scope should have end line number")
end

def test_end_line_number_for_modules_scopes_and_aliases
rb = "module A\n module B\n alias m1 m2\n end\nend\n"
pt = s(:module, :A,
s(:scope,
Expand All @@ -501,8 +509,12 @@ def test_end_line_number_for_modules_and_aliases
assert_equal pt, result
assert_equal(1, result.line, "module should have line number")
assert_equal(5, result.endline, "module should have end line number")
assert_equal(1, result.scope.line, "scope should have line number")
assert_equal(5, result.scope.endline, "scope should have end line number")
assert_equal(2, submodule.line, "submodule should have line number")
assert_equal(4, submodule.endline, "submodule should have end line number")
assert_equal(2, submodule.scope.line, "submodule scope should have line number")
assert_equal(4, submodule.scope.endline, "submodule scope should have end line number")
assert_equal(3, submodule.scope.alias.line, "alias should have line number")
assert_equal(3, submodule.scope.alias.endline, "alias should have end line number")
end
Expand Down Expand Up @@ -544,6 +556,8 @@ def m
assert_equal pt, result
assert_equal(1, result.line, "defn should have line number")
assert_equal(13, result.endline, "defn should have end line number")
assert_equal(1, result.scope.line, "scope should have line number")
assert_equal(13, result.scope.endline, "scope should have end line number")
assert_equal(2, body.find_nodes(:iter)[0].line, "proc should have line number")
assert_equal(4, body.find_nodes(:iter)[0].endline, "proc should have end line number")
assert_equal(5, body.find_nodes(:iter)[1].line, "cbrace proc should have line number")
Expand Down

0 comments on commit cd89040

Please sign in to comment.