Permalink
Browse files

new parser plugin design to work around obscure autoload failure

  • Loading branch information...
1 parent 79923c4 commit f69f1858a788622f54c8a15c3f003ccfc7a9c69b @quix committed Feb 24, 2011
Showing with 93 additions and 86 deletions.
  1. +4 −0 CHANGES.rdoc
  2. +1 −1 Rakefile
  3. +83 −81 lib/live_ast_ripper.rb
  4. +5 −4 lib/live_ast_ripper/test_forms.rb
View
@@ -1,6 +1,10 @@
= live_ast_ripper ChangeLog
+== Version 0.4.0
+
+* new parser plugin design to work around obscure autoload failure
+
== Version 0.3.0
* initial release
View
@@ -3,7 +3,7 @@ require_relative 'devel/jumpstart'
Jumpstart.new "live_ast_ripper" do |s|
s.developers << ["James M. Lawrence", "quixoticsycophant@gmail.com"]
s.github_user = "quix"
- s.version = "0.3.0"
+ s.version = "0.4.0"
end
# testing done inside live_ast
View
@@ -1,98 +1,100 @@
require 'ripper'
-module LiveAST
- class Parser
- class << self
- #
- # Whether to strip line/column and other personality traits.
- #
- # This is Ripper-specific -- not part of the live_ast API.
- #
- attr_accessor :steamroll
- end
-
+class LiveASTRipper
+ class << self
#
- # Returns a line --> sexp hash where sexp corresponds to the
- # method or block defined at the given line.
+ # Whether to strip line/column and other personality traits.
#
- def parse(code)
- @defs = {}
- process(Ripper.sexp(code))
- @defs
- end
+ # This is Ripper-specific -- not part of the live_ast API.
+ #
+ attr_accessor :steamroll
+ end
+
+ #
+ # Returns a line --> sexp hash where sexp corresponds to the
+ # method or block defined at the given line.
+ #
+ def parse(code)
+ @defs = {}
+ process(Ripper.sexp(code))
+ @defs
+ end
- def process(sexp)
- case sexp[0]
- when :def
- process_def(sexp)
- when :method_add_block
- process_method_add_block(sexp)
- else
- sexp.map { |elem|
- elem.is_a?(Array) ? process(elem) : elem
- }
- end
+ def process(sexp)
+ case sexp[0]
+ when :def
+ process_def(sexp)
+ when :method_add_block
+ process_method_add_block(sexp)
+ else
+ sexp.map { |elem|
+ elem.is_a?(Array) ? process(elem) : elem
+ }
end
-
- def process_def(sexp)
- line = sexp[1][2][0]
+ end
+
+ def process_def(sexp)
+ line = sexp[1][2][0]
- result = []
- result << sexp.shift
- result << sexp.shift
- result << process(sexp.shift)
- result << process(sexp.shift)
+ result = []
+ result << sexp.shift
+ result << sexp.shift
+ result << process(sexp.shift)
+ result << process(sexp.shift)
- result = steamroll(result) if Parser.steamroll
- store_sexp(result, line)
+ result = steamroll(result) if LiveASTRipper.steamroll
+ store_sexp(result, line)
- []
- end
+ []
+ end
- def process_method_add_block(sexp)
- line =
- case sexp[1][0]
- when :method_add_arg
- sexp[1][1][1].last[0]
- when :call, :command_call
- sexp[1][3][2][0]
- when :command
- sexp[1][1][2][0]
- end
+ def process_method_add_block(sexp)
+ line =
+ case sexp[1][0]
+ when :method_add_arg
+ sexp[1][1][1].last[0]
+ when :call, :command_call
+ sexp[1][3][2][0]
+ when :command
+ sexp[1][1][2][0]
+ end
- result = []
- result << sexp.shift
- result << process(sexp.shift)
- result << process(sexp.shift)
-
- result = steamroll(result) if Parser.steamroll
- store_sexp(result, line)
+ result = []
+ result << sexp.shift
+ result << process(sexp.shift)
+ result << process(sexp.shift)
+
+ result = steamroll(result) if LiveASTRipper.steamroll
+ store_sexp(result, line)
- []
- end
+ []
+ end
- def store_sexp(sexp, line)
- @defs[line] = @defs.has_key?(line) ? :multiple : sexp
- end
+ def store_sexp(sexp, line)
+ @defs[line] = @defs.has_key?(line) ? :multiple : sexp
+ end
- def steamroll(sexp)
- sexp.map { |elem|
- if elem.is_a? Array
- sub_elem =
- if elem[0].is_a?(Symbol) and elem[0][0] == "@"
- elem[0..-2]
- else
- elem
- end
- steamroll(sub_elem)
- elsif elem == :brace_block or elem == :do_block
- :block
- else
- elem
- end
- }
- end
+ def steamroll(sexp)
+ sexp.map { |elem|
+ if elem.is_a? Array
+ sub_elem =
+ if elem[0].is_a?(Symbol) and elem[0][0] == "@"
+ elem[0..-2]
+ else
+ elem
+ end
+ steamroll(sub_elem)
+ elsif elem == :brace_block or elem == :do_block
+ :block
+ else
+ elem
+ end
+ }
end
end
-LiveAST::Parser.autoload :TestForms, "live_ast_ripper/test_forms"
+LiveASTRipper.autoload :TestForms, "live_ast_ripper/test_forms"
+
+module LiveAST
+ @parser = LiveASTRipper
+end
@@ -1,8 +1,6 @@
-# testing assumes sexps are steamrolled
-LiveAST::Parser.steamroll = true
-
-module LiveAST::Parser::TestForms
+# For testing with LiveAST.
+module LiveASTRipper::TestForms
#
# no_arg_def(:f, "A#f") returns the ast of
#
@@ -214,3 +212,6 @@ def binop_proc_new(op)
[:var_ref, [:@ident, "y"]]]]]]
end
end
+
+# testing assumes sexps are steamrolled
+LiveASTRipper.steamroll = true

0 comments on commit f69f185

Please sign in to comment.