summaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-09-22 05:22:50 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-09-22 05:22:50 +0000
commit2c179216717d75837de7a6ff092fd773670f4d11 (patch)
tree6a275ae9914fc4157c21a36402ce9d87a250fa8a /sample
parent61d1dce5a226d3bf8393fd10777db09b046f68e6 (diff)
* parse.y [ripper]: on__scan event removed.
* parse.y [ripper]: event name is changed: on__XXX -> on_XXX. * ext/ripper/eventids2.c: ditto. * ext/ripper/ripper.rb.in: ditto. * ext/ripper/lib/ripper.rb: sync with ripper.rb.in. * ext/ripper/lib/ripper/tokenizer: ditto. * ext/ripper/lib/ripper/filter: new file. * sample/ripper/colorize.rb: new file. * sample/ripper/strip-comment.rb: new file. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6952 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample')
-rw-r--r--sample/ripper/colorize.rb30
-rw-r--r--sample/ripper/strip-comment.rb19
2 files changed, 49 insertions, 0 deletions
diff --git a/sample/ripper/colorize.rb b/sample/ripper/colorize.rb
new file mode 100644
index 0000000000..505e757013
--- /dev/null
+++ b/sample/ripper/colorize.rb
@@ -0,0 +1,30 @@
+require 'ripper/filter'
+
+class ColorizeFilter < Ripper::Filter
+ def on_default(event, tok, f)
+ f << escape(tok)
+ end
+
+ def on_comment(tok, f)
+ f << %Q[<span class="comment">#{escape(tok)}</span>]
+ end
+
+ def on_tstring_content(tok, f)
+ f << %Q[<span class="string">#{escape(tok)}</span>]
+ end
+
+ ESC = {
+ '&' => '&amp;',
+ '<' => '&lt;',
+ '>' => '&gt;'
+ }
+
+ def escape(str)
+ tbl = ESC
+ str.gsub(/[&<>]/) {|ch| tbl[ch] }
+ end
+end
+
+if $0 == __FILE__
+ ColorizeFilter.new(ARGF).parse($stdout)
+end
diff --git a/sample/ripper/strip-comment.rb b/sample/ripper/strip-comment.rb
new file mode 100644
index 0000000000..bd26b6a377
--- /dev/null
+++ b/sample/ripper/strip-comment.rb
@@ -0,0 +1,19 @@
+# $Id$
+
+require 'ripper/filter'
+
+class CommentStripper < Ripper::Filter
+ def CommentStripper.strip(src)
+ new(src).parse(nil)
+ end
+
+ def on_default(event, token, data)
+ print token
+ end
+
+ def on_comment(token, data)
+ puts
+ end
+end
+
+CommentStripper.strip(ARGF)