Skip to content

Commit

Permalink
Allow negative numbers in "list" command's last field to mean range f…
Browse files Browse the repository at this point in the history
…irst+last .. first.
  • Loading branch information
rocky committed Jul 23, 2011
1 parent c2b4a78 commit ed699c0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
3 changes: 2 additions & 1 deletion app/cmd_parse.kpeg
Expand Up @@ -163,6 +163,7 @@ not_space_colons = ( not_space_colon )+:ary { ary.join }
filename = dbl_string | not_space_colons
file_pos_sep = sp+ | ':'
integer = </[0-9]+/> { text.to_i }
sinteger = </[+-]?[0-9]+/> { text.to_i }
line_number = integer
vm_offset = '@' integer:int
Expand Down Expand Up @@ -229,7 +230,7 @@ breakpoint_stmt = location:loc - if_unless:iu - condition:cond {
# the command. Also, "list" with nothing else is
# handled there as well
list_special_targets = <'.' | '-'> { text }
list_stmt = (list_special_targets | location):loc - (integer:int)? {
list_stmt = (list_special_targets | location):loc - (sinteger:int)? {
List.new(loc, int)
}
| (list_special_targets | location):loc {
Expand Down
33 changes: 30 additions & 3 deletions app/cmd_parser.rb
Expand Up @@ -1379,6 +1379,32 @@ def _integer
return _tmp
end

# sinteger = < /[+-]?[0-9]+/ > { text.to_i }
def _sinteger

_save = self.pos
while true # sequence
_text_start = self.pos
_tmp = scan(/\A(?-mix:[+-]?[0-9]+)/)
if _tmp
text = get_text(_text_start)
end
unless _tmp
self.pos = _save
break
end
@result = begin; text.to_i ; end
_tmp = true
unless _tmp
self.pos = _save
end
break
end # end sequence

set_failed_rule :_sinteger unless _tmp
return _tmp
end

# line_number = integer
def _line_number
_tmp = apply(:_integer)
Expand Down Expand Up @@ -1790,7 +1816,7 @@ def _list_special_targets
return _tmp
end

# list_stmt = ((list_special_targets | location):loc - integer:int? { List.new(loc, int) } | (list_special_targets | location):loc { List.new(loc, nil) })
# list_stmt = ((list_special_targets | location):loc - sinteger:int? { List.new(loc, int) } | (list_special_targets | location):loc { List.new(loc, nil) })
def _list_stmt

_save = self.pos
Expand Down Expand Up @@ -1821,7 +1847,7 @@ def _list_stmt
break
end
_save3 = self.pos
_tmp = apply(:_integer)
_tmp = apply(:_sinteger)
int = @result
unless _tmp
_tmp = true
Expand Down Expand Up @@ -1912,6 +1938,7 @@ def _list_stmt
Rules[:_filename] = rule_info("filename", "(dbl_string | not_space_colons)")
Rules[:_file_pos_sep] = rule_info("file_pos_sep", "(sp+ | \":\")")
Rules[:_integer] = rule_info("integer", "< /[0-9]+/ > { text.to_i }")
Rules[:_sinteger] = rule_info("sinteger", "< /[+-]?[0-9]+/ > { text.to_i }")
Rules[:_line_number] = rule_info("line_number", "integer")
Rules[:_vm_offset] = rule_info("vm_offset", "\"@\" integer:int { Position.new(nil, nil, :offset, int) }")
Rules[:_position] = rule_info("position", "(vm_offset | line_number:l { Position.new(nil, nil, :line, l) })")
Expand All @@ -1921,7 +1948,7 @@ def _list_stmt
Rules[:_breakpoint_stmt_no_condition] = rule_info("breakpoint_stmt_no_condition", "location:loc { Breakpoint.new(loc, false, 'true') }")
Rules[:_breakpoint_stmt] = rule_info("breakpoint_stmt", "(location:loc - if_unless:iu - condition:cond { Breakpoint.new(loc, iu == 'unless', cond) } | breakpoint_stmt_no_condition)")
Rules[:_list_special_targets] = rule_info("list_special_targets", "< (\".\" | \"-\") > { text }")
Rules[:_list_stmt] = rule_info("list_stmt", "((list_special_targets | location):loc - integer:int? { List.new(loc, int) } | (list_special_targets | location):loc { List.new(loc, nil) })")
Rules[:_list_stmt] = rule_info("list_stmt", "((list_special_targets | location):loc - sinteger:int? { List.new(loc, int) } | (list_special_targets | location):loc { List.new(loc, nil) })")
end
if __FILE__ == $0
# require 'rubygems'; require_relative '../lib/trepanning';
Expand Down
6 changes: 4 additions & 2 deletions processor/command/list.rb
Expand Up @@ -41,8 +41,9 @@ class Trepan::Command::ListCommand < Trepan::Command
If the location form is used with a subsequent parameter, the
parameter is the starting line number. When there two numbers are
given, the last number value is treated as a stopping line unless it
is less than the start line, in which case it is taken to mean the
number of lines to list instead.
is positive and less than the start line. In this case, it is taken to
mean the number of lines to list instead. If last is negative, we start
that many lines back from first and list to first.
Wherever a number is expected, it does not need to be a constant --
just something that evaluates to a positive integer.
Expand All @@ -58,6 +59,7 @@ class Trepan::Command::ListCommand < Trepan::Command
#{NAME} foo.rb 5 6 # list lines 5 and 6 of foo.rb
#{NAME} foo.rb 5 2 # Same as above, since 2 < 5.
#{NAME} foo.rb:5 2 # Same as above
#{NAME} foo.rb 15 -5 # List lines 10..15 of foo
#{NAME} FileUtils.cp # List lines around the FileUtils.cp function.
#{NAME} . # List lines centered from where we currently are stopped
#{NAME} . 3 # List 3 lines starting from where we currently are stopped
Expand Down
3 changes: 2 additions & 1 deletion processor/list.rb
Expand Up @@ -11,7 +11,7 @@ class Trepan
class CmdProcessor < VirtualCmdProcessor

# If last is less than first, assume last is a count rather than an
# end line number.
# end line number. If last is negative, range is [first+last..first].
def adjust_last(first, last)
last < first ? first + last - 1 : last
end
Expand Down Expand Up @@ -89,6 +89,7 @@ def parse_list_cmd(position_str, listsize, center_correction=0)
end
end
if last
first, last = [first + last, first] if last < 0
last = adjust_last(first, last)
else
first = [1, first - center_correction].max
Expand Down

0 comments on commit ed699c0

Please sign in to comment.