Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Partial cutover of uing KPEG for to parse locations
  • Loading branch information
rocky committed Mar 7, 2011
1 parent 0a29adb commit 83d1f0b
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 238 deletions.
9 changes: 6 additions & 3 deletions app/cmd_parse.kpeg
Expand Up @@ -111,7 +111,7 @@ internal_class_module_chain =
local_internal_identifier:parent id_separator:sep internal_class_module_chain:child
{
let = parent.name[0..0]
type = (let.capitalize == let) ? :constant : :variable
type = (let =~ /A-Z/) ? :constant : :variable
SymbolEntry.new(type, string, [parent, child, sep])
}
| local_identifier
Expand All @@ -130,7 +130,7 @@ class_module_chain =
internal_class_module_chain:child
{
let = parent.name[0..0]
type = (let.capitalize == let) ? :constant : :variable
type = (let =~ /A-Z/) ? :constant : :variable
SymbolEntry.new(type, string, [parent, child, sep])
}
| identifier
Expand Down Expand Up @@ -185,9 +185,12 @@ file_colon_line = file_no_colon:file ':' position:pos {
location =
position
| file_colon_line
| <filename>:file &{ File.exist?(file) } {
| <filename>:file &{ File.exist?(file) } file_pos_sep position:pos {
Position.new(:file, file, pos.position_type, pos.position)
}
| <filename>:file &{ File.exist?(file) } {
Position.new(:file, file, nil, nil)
}
| class_module_chain?:fn file_pos_sep position:pos {
Position.new(:fn, fn, pos.position_type, pos.position)
}
Expand Down
69 changes: 41 additions & 28 deletions app/cmd_parse.rb
Expand Up @@ -4,6 +4,7 @@

class Trepan
module CmdParser

# Given a KPeg parse object, return the method of that parse or raise a
# Name error if we can't find a method. parent_class is the parent class of
# the object we've found so far and "binding" is used if we need
Expand Down Expand Up @@ -56,16 +57,22 @@ def resolve_method(m, bind, parent_class = nil)
else
begin
errmsg = "Can't get method for #{name.inspect}"
# parent_class = eval('self', bind) if !parent_class && bind
if m.chain && m.chain[0]
parent_obj = eval("#{m.chain[0].name}", bind) if !parent_class && bind
end
parent = parent_class || parent_obj
meth =
if parent_class
errmsg << "in #{parent_class}"
if parent_class.respond_to?('instance_methods') &&
parent_class.instance_methods.member?(name.to_sym)
parent_class.instance_method(name.to_sym)
else
parent_class.method(name)
if parent
errmsg << "in #{parent}"
lookup_name = m.chain && m.chain[1] ? m.chain[1].name : name
if parent.respond_to?('instance_methods') &&
parent.instance_methods.member?(lookup_name.to_sym)
parent.instance_method(lookup_name.to_sym)
elsif parent.respond_to?('methods')
parent.method(lookup_name.to_sym)
end
elsif m.chain && m.chain[1]
eval("#{m.chain[0].name}.method(#{lookup_name.name.inspect})", bind)
else
eval("self.method(#{name.inspect})", bind)
end
Expand All @@ -77,15 +84,21 @@ def resolve_method(m, bind, parent_class = nil)
end
end

# Return the method by evaluating parse_struct.
# nil is returned if we can't parse str
def meth_for_parse_struct(parse_struct, start_binding)
resolve_method(parse_struct, start_binding)
end

# Parse str and return the method associated with that.
# nil is returned if we can't parse str
def meth_for_string(str, start_binding)
cp = CmdParse.new(str)
@cp ? @cp.setup_parser(str) : @cp = CmdParse.new(str)
begin
if cp._class_module_chain
if @cp._class_module_chain
# Did we match all of it?
if cp.result.name == str
resolve_method(cp.result, start_binding)
if @cp.result.name == str
meth_for_parse_struct(@cp.result, start_binding)
else
nil
end
Expand All @@ -97,6 +110,16 @@ def meth_for_string(str, start_binding)
return nil
end
end

def parse_terminal(terminal_name, loc_str)
@cp ? @cp.setup_parser(loc_str) : @cp = CmdParse.new(loc_str)
@cp.send(terminal_name) ? @cp : nil
end

def parse_location(loc_str)
parse = parse_terminal(:_location, loc_str)
parse ? parse.result : nil
end
end
end

Expand Down Expand Up @@ -141,28 +164,18 @@ def testing; 5 end
p meth_for_string('Testing.testing', binding)
p meth_for_string('File.basename', binding)
x = File
# require_relative '../lib/trepanning'
# debugger
p meth_for_string('x.basename', binding)
def x.five; 5; end
p meth_for_string('x.five', binding)
p x.five

# match = MethodName.parse('5', :root => :line_number)
# p match.value
p parse_terminal(:_line_number, '5').result
p parse_terminal(:_vm_offset, '@5').result

# match = MethodName.parse('@5', :root => :vm_offset)
# p match.value

# # Location stuff
# ['fn', 'fn 5', 'fn @5', '@5', '5'].each do |location|
# begin
# match = MethodName.parse(location, :root => :location)
# p [location, 'succeeded', match.value]
# rescue Citrus::ParseError
# p [location, 'failed']
# end
# end
# Location stuff
['fn', 'fn 5', 'fn @5', '@5', '5'].each do |location|
p parse_location(location)
end

end

86 changes: 66 additions & 20 deletions app/cmd_parser.rb
Expand Up @@ -811,7 +811,7 @@ def _id_separator
return _tmp
end

# internal_class_module_chain = (local_internal_identifier:parent id_separator:sep internal_class_module_chain:child { let = parent.name[0..0] type = (let.capitalize == let) ? :constant : :variable SymbolEntry.new(type, string, [parent, child, sep]) } | local_identifier)
# internal_class_module_chain = (local_internal_identifier:parent id_separator:sep internal_class_module_chain:child { let = parent.name[0..0] type = (let =~ /A-Z/) ? :constant : :variable SymbolEntry.new(type, string, [parent, child, sep]) } | local_identifier)
def _internal_class_module_chain

_save = self.pos
Expand Down Expand Up @@ -839,7 +839,7 @@ def _internal_class_module_chain
end
@result = begin;
let = parent.name[0..0]
type = (let.capitalize == let) ? :constant : :variable
type = (let =~ /A-Z/) ? :constant : :variable
SymbolEntry.new(type, string, [parent, child, sep])
; end
_tmp = true
Expand All @@ -860,7 +860,7 @@ def _internal_class_module_chain
return _tmp
end

# class_module_chain = (leading_identifier:parent id_separator:sep internal_class_module_chain:child { let = parent.name[0..0] type = (let.capitalize == let) ? :constant : :variable SymbolEntry.new(type, string, [parent, child, sep]) } | identifier)
# class_module_chain = (leading_identifier:parent id_separator:sep internal_class_module_chain:child { let = parent.name[0..0] type = (let =~ /A-Z/) ? :constant : :variable SymbolEntry.new(type, string, [parent, child, sep]) } | identifier)
def _class_module_chain

_save = self.pos
Expand Down Expand Up @@ -888,7 +888,7 @@ def _class_module_chain
end
@result = begin;
let = parent.name[0..0]
type = (let.capitalize == let) ? :constant : :variable
type = (let =~ /A-Z/) ? :constant : :variable
SymbolEntry.new(type, string, [parent, child, sep])
; end
_tmp = true
Expand Down Expand Up @@ -1254,7 +1254,7 @@ def _file_colon_line
return _tmp
end

# location = (position | file_colon_line | < filename >:file &{ File.exist?(file) } { Position.new(:file, file, pos.position_type, pos.position) } | class_module_chain?:fn file_pos_sep position:pos { Position.new(:fn, fn, pos.position_type, pos.position) } | class_module_chain?:fn { Position.new(:fn, fn, nil, nil) })
# location = (position | file_colon_line | < filename >:file &{ File.exist?(file) } file_pos_sep position:pos { Position.new(:file, file, pos.position_type, pos.position) } | < filename >:file &{ File.exist?(file) } { Position.new(:file, file, nil, nil) } | class_module_chain?:fn file_pos_sep position:pos { Position.new(:fn, fn, pos.position_type, pos.position) } | class_module_chain?:fn { Position.new(:fn, fn, nil, nil) })
def _location

_save = self.pos
Expand Down Expand Up @@ -1285,6 +1285,17 @@ def _location
self.pos = _save1
break
end
_tmp = apply('file_pos_sep', :_file_pos_sep)
unless _tmp
self.pos = _save1
break
end
_tmp = apply('position', :_position)
pos = @result
unless _tmp
self.pos = _save1
break
end
@result = begin;
Position.new(:file, file, pos.position_type, pos.position)
; end
Expand All @@ -1300,62 +1311,94 @@ def _location

_save3 = self.pos
while true # sequence
_text_start = self.pos
_tmp = apply('filename', :_filename)
if _tmp
set_text(_text_start)
end
file = @result
unless _tmp
self.pos = _save3
break
end
_save4 = self.pos
_tmp = begin; File.exist?(file) ; end
self.pos = _save4
unless _tmp
self.pos = _save3
break
end
@result = begin;
Position.new(:file, file, nil, nil)
; end
_tmp = true
unless _tmp
self.pos = _save3
end
break
end # end sequence

break if _tmp
self.pos = _save

_save5 = self.pos
while true # sequence
_save6 = self.pos
_tmp = apply('class_module_chain', :_class_module_chain)
@result = nil unless _tmp
unless _tmp
_tmp = true
self.pos = _save4
self.pos = _save6
end
fn = @result
unless _tmp
self.pos = _save3
self.pos = _save5
break
end
_tmp = apply('file_pos_sep', :_file_pos_sep)
unless _tmp
self.pos = _save3
self.pos = _save5
break
end
_tmp = apply('position', :_position)
pos = @result
unless _tmp
self.pos = _save3
self.pos = _save5
break
end
@result = begin;
Position.new(:fn, fn, pos.position_type, pos.position)
; end
_tmp = true
unless _tmp
self.pos = _save3
self.pos = _save5
end
break
end # end sequence

break if _tmp
self.pos = _save

_save5 = self.pos
_save7 = self.pos
while true # sequence
_save6 = self.pos
_save8 = self.pos
_tmp = apply('class_module_chain', :_class_module_chain)
@result = nil unless _tmp
unless _tmp
_tmp = true
self.pos = _save6
self.pos = _save8
end
fn = @result
unless _tmp
self.pos = _save5
self.pos = _save7
break
end
@result = begin;
Position.new(:fn, fn, nil, nil)
; end
_tmp = true
unless _tmp
self.pos = _save5
self.pos = _save7
end
break
end # end sequence
Expand All @@ -1370,24 +1413,27 @@ def _location
end
if __FILE__ == $0
# require 'rubygems'; require 'trepanning';
%w(A::B @@classvar abc01! @ivar

cp = CmdParse.new('', true)
%w(A::B @@classvar abc01! @ivar @ivar.meth
Object A::B::C A::B::C::D A::B.c A.b.c.d).each do |name|
cp = CmdParse.new(name, true)
cp.setup_parser(name, true)
res = cp._class_module_chain
p res
p cp.string
p cp.result
end
%w(A::B:5 A::B:@5 @@classvar abc01!:10 @ivar).each do |name|
cp = CmdParse.new(name, true)
cp.setup_parser(name, true)
res = cp._location
p res
p cp.string
p cp.result
end
# require 'trepanning';
["#{__FILE__}:10", 'A::B 5'].each do |name|
cp = CmdParse.new(name, true)
["#{__FILE__}:10", 'A::B 5',
"#{__FILE__} 20"].each do |name|
cp.setup_parser(name, true)
res = cp._location
p res
p cp.string
Expand Down
6 changes: 5 additions & 1 deletion app/irb.rb
Expand Up @@ -44,7 +44,11 @@ def self.execute(conf, *opts)
opts.join(' ')
end
dbg_cmdproc = conf.workspace.instance_variable_get('@dbg_cmdproc')
dbg_cmdproc.run_command($trepan_command)
if dbg_cmdproc
dbg_cmdproc.run_command($trepan_command)
else
puts "Something's wrong with debugger setup of irb"
end
end
end

Expand Down
Empty file removed data/debugger-stop.right
Empty file.

0 comments on commit 83d1f0b

Please sign in to comment.