Last active
October 10, 2015 07:27
-
-
Save redraiment/3655563 to your computer and use it in GitHub Desktop.
Removes comments with C style (`//' and `/* */') in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'strscan' | |
lex = [/"[^\\"]*(?:\\.[^\\"]*)*"/, # string | |
/'[^\\']*(?:\\.[^\\']*)*'/, # char | |
/\/\*.*?\*\//m, # multi-line | |
/\/\/(?:.*?\\(?:\r?\n|\r))*.*/, # single-line | |
/.|\s+/] # rest | |
ARGV.each do |source| | |
stream = StringScanner.new File.read source | |
until stream.eos? do | |
code = stream.scan lex.find {|regex| stream.match? regex} | |
print code unless code.start_with?('//') || code.start_with?('/*') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment