[ANN] ruby-ast-c++ 0.0.1

A

Alexander Kellett

over the last few hours i finally got around to writing
an elsa based c++ "parsetree" style library for ruby.

an example usage is parsing of the Qt header qstring.h, and
listing of all the method names defined in QString.

anyone with interest please contact me for sources,
Alex
 
P

Phil Tomson

over the last few hours i finally got around to writing
an elsa based c++ "parsetree" style library for ruby.

an example usage is parsing of the Qt header qstring.h, and
listing of all the method names defined in QString.

anyone with interest please contact me for sources,
Alex


Definately interested. What is elsa?


Phil
 
A

Alexander Kellett

--Apple-Mail-4--926366406
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed

Definately interested. What is elsa?

http://www.cs.berkeley.edu/~smcpeak/elkhound/sources/elsa/index.html
i just wrote a simple wrapper that parses the generated ast.
with this its pretty simple to get out needed information.
wrapper is attached, its really trivial :)

--Apple-Mail-4--926366406
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
x-unix-mode=0644;
name="t.rb"
Content-Disposition: attachment;
filename=t.rb

# g++ -E /Developer/qt/include/QtCore/qstring.h -I /Developer/qt/include/Qt -I /usr/include/gcc/darwin/3.3/ >! qstring.h.out
%w(pp tempfile).each{|r|require r}
ast_cache_fn = "/tmp/qtr4_ast_raw.cache"
ast_tree_cache_fn = "/tmp/qtr4_ast_tree.cache"
should_parse = false
should_dissect = false
if should_parse
blacklist_headers = %w(qatomic)
sect, src = nil, ""
IO.read("qstring.h.out").each_line {
|l|
if l =~ /^# \d+? "(.*?)"/
sect = $1
else
src << l + "\n" unless sect =~ /#{blacklist_headers.join "|"}/
end
}
tf = Tempfile.new "qtr4_postproc"
tf.write src
tf.close
system "./ccparse -tr printTypedAST #{tf.path} > #{ast_cache_fn}"
end
objs = nil
if should_dissect
old_indent = 0
ast = IO.read ast_cache_fn
obj, stack = [], []
objs = obj
ast.each_line {
|l|
l =~ /^(\s*)/
indent = $1.length
if indent > old_indent
new_obj = [l]
stack << new_obj
obj << new_obj
obj = new_obj
elsif indent == old_indent
obj << l
elsif indent < old_indent
((old_indent - indent) / 2).times {
obj = stack.pop
}
obj = stack.last || objs
obj << l
end
old_indent = indent
}
File.open(ast_tree_cache_fn, "w") { |file| file.print Marshal.dump(objs) }
else
File.open(ast_tree_cache_fn, "r") { |file| objs = Marshal.load(file) }
end
class Array
def down re
objs = self
objs.each_with_index {
|obj,idx|
next unless obj.is_a? String
return objs[idx+1] if obj =~ re
}
nil
end
def matching re
objs = self
objs.each_with_index {
|obj,idx|
next unless obj.is_a? String
yield objs[idx+1], obj if obj =~ re
}
end
end
method_list = []
objs.down(/tree = TranslationUnit/).down(/topForms:/).matching(/topForms\[.*?\] = TF_decl/) {
|top_form,|
top_form.down(/decl =/).matching(/spec = TS_classSpec/) {
|class_spec,|
class_name_string = class_spec.down(/name = /).grep(/name = /).first
class_name = class_name_string.chomp.slice(class_name_string.index("=") + 2..-1)
next unless class_name == "QString"
class_spec.down(/members = MemberList/).down(/list:/).matching(/list\[.*?\]/) {
|decl, line|
next if line =~ /(MR_access|MR_func)/
decllist = decl.down(/d = Declaration/).down(/decllist:/)
next if decllist.nil?
decllist.matching(/decllist\[.*?\]/) {
|fdecl,|
func_spec = fdecl.down(/decl = D_func/)
next if func_spec.nil?
method_spec = func_spec.down(/base =/)
next if method_spec.grep(/name = /).empty?
method_name_string = method_spec.down(/name = /).grep(/(name|fakeName) = /).first
method_name = method_name_string.chomp.slice(method_name_string.index("=") + 2..-1)
method_list << method_name
}
}
}
}
p method_list

--Apple-Mail-4--926366406
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed


Alex
--Apple-Mail-4--926366406--
 
G

George Ogata

Definately interested.

I've got a C99 (not C++) AST-constructor if you're interested too.
Pure ruby (right down to the lexer) though, so pretty slow at the
moment. The AST nodes are of different Node classes rather than just
being nested arrays. You can also use it to parse snippets (e.g.,
just a statement or a type name or something).

If you're interested.
 
S

Shashank Date

George said:
I've got a C99 (not C++) AST-constructor if you're interested too.
Pure ruby (right down to the lexer) though, so pretty slow at the
moment. The AST nodes are of different Node classes rather than just
being nested arrays. You can also use it to parse snippets (e.g.,
just a statement or a type name or something).

If you're interested.

Very, very interested ! Please let me know where I can get it :)
-- shanko
 
R

Ryan Davis

I've got a C99 (not C++) AST-constructor if you're interested too.
Pure ruby (right down to the lexer) though, so pretty slow at the
moment. The AST nodes are of different Node classes rather than just
being nested arrays. You can also use it to parse snippets (e.g.,
just a statement or a type name or something).
If you're interested.

GIMMIE!!!! WANT WANT WANT!!!
 
P

Phil Tomson

I've got a C99 (not C++) AST-constructor if you're interested too.
Pure ruby (right down to the lexer) though, so pretty slow at the
moment. The AST nodes are of different Node classes rather than just
being nested arrays. You can also use it to parse snippets (e.g.,
just a statement or a type name or something).

If you're interested.


Yes, please share. These sorts of things could really help us to
translate the Ruby C libs into pure Ruby.

Phil
 
R

Ryan Davis

Yes, please share. These sorts of things could really help us to
translate the Ruby C libs into pure Ruby.

I don't think that is the case actually. There is just far too much
going on on the C side that isn't necessary once ported to ruby. We
hand translated a smallish C file and it dropped about 40% off the
size.

What we CAN do with it is make another feeder into the ruby2c tool
chain and play with making more analysis tools. I'd LOVE to have
something that can do the C family so I can extend parse_tree_abc and
do complexity metrics on C (and with some work objective-c) code. This
can help us identify the stickier files and get cracking on those first
(or at least help people decide if they want to take a crack at it :).
 
G

George Ogata

Shashank Date said:
Very, very interested ! Please let me know where I can get it :)
-- shanko

Sorry for taking a while to follow up. Give me some time to doc' it
up properly and I'll have it on RubyForge shortly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top