Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   How to strip ruby comments in a ruby line of code? (http://www.velocityreviews.com/forums/t860516-how-to-strip-ruby-comments-in-a-ruby-line-of-code.html)

Alexandre Mutel 11-19-2009 02:15 PM

How to strip ruby comments in a ruby line of code?
 
Short description : My question is : do you know any available method,
giving the string of a Ruby line of code, to remove comments from this
line of code?

________________

Long description :

For my dsl project, i'm loading my dsl files and applying a small
preprocess on each line before performing a global instance_eval on the
preprocessed file.

Basically, in my dsl language, it is possible to put a label followed by
a ":" starting at the beginning of a line like this:
my_label: here_is_a_dsl(arg1, arg2)

This label may be followed by a dsl instruction.

The preprocessor is transforming the previous line to this line:

newLabel(:my_label) { here_is_a_dsl(arg1, arg2) }

using the following code:
append = ""
File.open(file).each do |line|
match = line.match(/^([a-zA-Z_]\w+):[\s\r\n]+(.*)/)
if ( match.nil?)
append += line
else
append += "newLabel(:#{match[1]}) { #{match[2]} }\n"
end
end

The problem arise when there is a comment at the end of the input line :
my_label: here_is_a_dsl(arg1, arg2) # my comments

It's then generating the following line:
newLabel(:my_label) { here_is_a_dsl(arg1, arg2) # my comments }

Meanning that the "}" end block is commented and having a parse error on
the whole file.

I could put a newline after the match like this :
newLabel(:my_label) { here_is_a_dsl(arg1, arg2) # my comments
}

Unfornutately, i'm no longer able to debug my dsl language, because the
lines are not matching the preprocessed line.

-----

I would like to have something really simple and not being forced to use
a full ruby language parser to parse those lines and remove the
comments.

Any idea?

Thanks!
--
Posted via http://www.ruby-forum.com/.


Aldric Giacomoni 11-19-2009 02:31 PM

Re: How to strip ruby comments in a ruby line of code?
 
Alexandre Mutel wrote:
> Short description : My question is : do you know any available method,
> giving the string of a Ruby line of code, to remove comments from this
> line of code?
>
> I would like to have something really simple and not being forced to use
> a full ruby language parser to parse those lines and remove the
> comments.
>
> Any idea?
>
> Thanks!


I'm still only learning regular expressions (I'll do another shameless
plug for rubular.com here), but you could do this:
string = string.match(/^.*#).to_s[0...-1]

Yes, it's a poor solution, but should you have nothing else, it'll do.
--
Posted via http://www.ruby-forum.com/.


Brian Candler 11-19-2009 02:32 PM

Re: How to strip ruby comments in a ruby line of code?
 
Alexandre Mutel wrote:
> I could put a newline after the match like this :
> newLabel(:my_label) { here_is_a_dsl(arg1, arg2) # my comments
> }
>
> Unfornutately, i'm no longer able to debug my dsl language, because the
> lines are not matching the preprocessed line.


However if you eval each line individually, then you can pass in the
source line number.

def foo; end
src = "foo\nfoo\nbar"
src.each_with_index do |line,i|
eval "#{line} {\n}", binding, "DSL", i+1
end

# Result:
DSL:3: undefined method `bar' for main:Object (NoMethodError)

Otherwise, if every input line maps to exactly two output lines, you can
just patch up the line number in the exception by dividing by two.

src = "foo\nfoo\nbar\n"
begin
eval src.gsub(/\n/, "{\n}\n"), binding, "DSL", 1
rescue => e
if e.backtrace.first =~ /\A(.*):(\d+)\z/
e.backtrace.first.replace "#{$1}:#{($2.to_i+1) / 2}"
end
raise e
end
--
Posted via http://www.ruby-forum.com/.


Alexandre Mutel 11-19-2009 02:33 PM

Re: How to strip ruby comments in a ruby line of code?
 
Aldric Giacomoni wrote:
> I'm still only learning regular expressions (I'll do another shameless
> plug for rubular.com here), but you could do this:
> string = string.match(/^.*#).to_s[0...-1]
>
> Yes, it's a poor solution, but should you have nothing else, it'll do.


the problem with your solution is that this line of code will remove
valid code :
myvar_s = "#{myvar}"

The problem is to handle correctly string escape sequence... it's
possible, but it requires much more work... I just want to know if
someone else did this?!
--
Posted via http://www.ruby-forum.com/.


Alexandre Mutel 11-19-2009 02:34 PM

Re: How to strip ruby comments in a ruby line of code?
 
Brian Candler wrote:
> def foo; end
> src = "foo\nfoo\nbar"
> src.each_with_index do |line,i|
> eval "#{line} {\n}", binding, "DSL", i+1
> end
>
> # Result:
> DSL:3: undefined method `bar' for main:Object (NoMethodError)


Wooo, thanks Brian!
--
Posted via http://www.ruby-forum.com/.


Aldric Giacomoni 11-19-2009 02:44 PM

Re: How to strip ruby comments in a ruby line of code?
 
Alexandre Mutel wrote:
> Aldric Giacomoni wrote:
>> I'm still only learning regular expressions (I'll do another shameless
>> plug for rubular.com here), but you could do this:
>> string = string.match(/^.*#).to_s[0...-1]
>>
>> Yes, it's a poor solution, but should you have nothing else, it'll do.

>
> the problem with your solution is that this line of code will remove
> valid code :
> myvar_s = "#{myvar}"
>
> The problem is to handle correctly string escape sequence... it's
> possible, but it requires much more work... I just want to know if
> someone else did this?!


Actually, no, because regexps are greedy by default, so it'll go to the
very last '#' it finds.
The other solution you got is more elegant, though.. :)

--
Posted via http://www.ruby-forum.com/.


Alexandre Mutel 11-19-2009 02:44 PM

Re: How to strip ruby comments in a ruby line of code?
 
Alexandre Mutel wrote:
> Brian Candler wrote:
>> def foo; end
>> src = "foo\nfoo\nbar"
>> src.each_with_index do |line,i|
>> eval "#{line} {\n}", binding, "DSL", i+1
>> end
>>
>> # Result:
>> DSL:3: undefined method `bar' for main:Object (NoMethodError)

>
> Wooo, thanks Brian!


Woop, i was to fast. In fact, i need an eval on the whole file, because
my dsl language allow ruby code to be used (and so definition of
methods... etc.)
--
Posted via http://www.ruby-forum.com/.


Alexandre Mutel 11-19-2009 02:53 PM

Re: How to strip ruby comments in a ruby line of code?
 
Aldric Giacomoni wrote:

> Actually, no, because regexps are greedy by default, so it'll go to the
> very last '#' it finds.
> The other solution you got is more elegant, though.. :)

hum, not sure the greedy is helping there:

line = "line = \"\#{args}\""
=> "line = \"\#{args}\""
string = line.match(/^.*#/).to_s[0...-1]
=> "line = "

Expecting is : line = "#{args}"

In order to strip comments using regexp, you need to handle string
escape.


--
Posted via http://www.ruby-forum.com/.


Aldric Giacomoni 11-19-2009 02:54 PM

Re: How to strip ruby comments in a ruby line of code?
 
Aldric Giacomoni wrote:
> Alexandre Mutel wrote:
>> Aldric Giacomoni wrote:
>>> I'm still only learning regular expressions (I'll do another shameless
>>> plug for rubular.com here), but you could do this:
>>> string = string.match(/^.*#).to_s[0...-1]
>>>
>>> Yes, it's a poor solution, but should you have nothing else, it'll do.

>>
>> the problem with your solution is that this line of code will remove
>> valid code :
>> myvar_s = "#{myvar}"
>>
>> The problem is to handle correctly string escape sequence... it's
>> possible, but it requires much more work... I just want to know if
>> someone else did this?!

>
> Actually, no, because regexps are greedy by default, so it'll go to the
> very last '#' it finds.


file # => array containing each line of the file you want to clean up
file.map! do |line|
line =~ /(^.*)#/
$1
end
--
Posted via http://www.ruby-forum.com/.


Aldric Giacomoni 11-19-2009 02:54 PM

Re: How to strip ruby comments in a ruby line of code?
 
Alexandre Mutel wrote:

> Expecting is : line = "#{args}"
>
> In order to strip comments using regexp, you need to handle string
> escape.


Ah.. What if the only '#' isn't a comment. Good point.
--
Posted via http://www.ruby-forum.com/.



All times are GMT. The time now is 12:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.