Skip to content
Browse files

Backport new-style parser to 1.8 compatible syntax (and semantics) fo…

…r ripper1.8
  • Loading branch information...
1 parent 96f50c2 commit 1ee355ecc61ace52d4993e7f5358ebf9c5f7fe83 @lsegal committed Jan 11, 2011
View
7 Rakefile
@@ -21,8 +21,11 @@ end
begin
hide = '_spec\.rb$,spec_helper\.rb$,ruby_lex\.rb$,autoload\.rb$'
- hide += ',legacy\/.+_handler,html_syntax_highlight_helper18\.rb$' if RUBY19
- hide += ',ruby_parser\.rb$,ast_node\.rb$,handlers\/ruby\/[^\/]+\.rb$,html_syntax_highlight_helper\.rb$' if RUBY18
+ if HAVE_RIPPER
+ hide += ',legacy\/.+_handler,html_syntax_highlight_helper18\.rb$'
+ else
+ hide += ',ruby_parser\.rb$,ast_node\.rb$,handlers\/ruby\/[^\/]+\.rb$,html_syntax_highlight_helper\.rb$'
+ end
require 'rspec'
require 'rspec/core/rake_task'
View
8 lib/yard.rb
@@ -32,6 +32,14 @@ def self.load_plugins; YARD::Config.load_plugins end
# Keep track of Ruby version for compatibility code
RUBY19, RUBY18 = *(RUBY_VERSION >= "1.9.1" ? [true, false] : [false, true])
+begin
+ require 'ripper'
+rescue LoadError
+ensure
+ HAVE_RIPPER = defined?(::Ripper) ? true : false
+ LEGACY_PARSER = !HAVE_RIPPER
+end
+
# Whether or not continuations are (properly) supported
begin
begin; require 'continuation'; rescue LoadError; end
View
2 lib/yard/autoload.rb
@@ -198,7 +198,7 @@ module Helpers # Namespace for template helpers
autoload :BaseHelper, __p('templates/helpers/base_helper')
autoload :FilterHelper, __p('templates/helpers/filter_helper')
autoload :HtmlHelper, __p('templates/helpers/html_helper')
- autoload :HtmlSyntaxHighlightHelper, __p('templates/helpers/html_syntax_highlight_helper' + (RUBY18 ? '18' : ''))
+ autoload :HtmlSyntaxHighlightHelper, __p('templates/helpers/html_syntax_highlight_helper' + (LEGACY_PARSER ? '18' : ''))
autoload :MarkupHelper, __p('templates/helpers/markup_helper')
autoload :MethodHelper, __p('templates/helpers/method_helper')
autoload :ModuleHelper, __p('templates/helpers/module_helper')
View
6 lib/yard/handlers/ruby/class_handler.rb
@@ -23,14 +23,14 @@ class YARD::Handlers::Ruby::ClassHandler < YARD::Handlers::Ruby::Base
elsif klass
create_attributes(klass, members_from_tags(klass))
end
- parse_block(statement[2], namespace: klass)
+ parse_block(statement[2], :namespace => klass)
if undocsuper
raise YARD::Parser::UndocumentableError, 'superclass (class was added without superclass)'
end
elsif statement.type == :sclass
if statement[0] == s(:var_ref, s(:kw, "self"))
- parse_block(statement[1], namespace: namespace, scope: :class)
+ parse_block(statement[1], :namespace => namespace, :scope => :class)
else
classname = statement[0].source
proxy = Proxy.new(namespace, classname)
@@ -46,7 +46,7 @@ class YARD::Handlers::Ruby::ClassHandler < YARD::Handlers::Ruby::Base
if classname[0,1] =~ /[A-Z]/
register ClassObject.new(namespace, classname) if Proxy === proxy
- parse_block(statement[1], namespace: proxy, scope: :class)
+ parse_block(statement[1], :namespace => proxy, :scope => :class)
else
raise YARD::Parser::UndocumentableError, "class '#{classname}'"
end
View
2 lib/yard/handlers/ruby/method_condition_handler.rb
@@ -3,6 +3,6 @@ class YARD::Handlers::Ruby::MethodConditionHandler < YARD::Handlers::Ruby::Base
handles :if_mod, :unless_mod
process do
- parse_block(statement.then_block, owner: owner)
+ parse_block(statement.then_block, :owner => owner)
end
end
View
2 lib/yard/handlers/ruby/private_constant_handler.rb
@@ -6,7 +6,7 @@ class YARD::Handlers::Ruby::PrivateConstantHandler < YARD::Handlers::Ruby::Base
process do
errors = []
statement.parameters.each do |param|
- next unless param.respond_to?(:type)
+ next unless AstNode === param
begin
privatize_constant(param)
rescue UndocumentableError => err
View
37 lib/yard/parser/ruby/ast_node.rb
@@ -87,13 +87,13 @@ def source
# List of all known keywords
# @return [Hash]
- KEYWORDS = { class: true, alias: true, lambda: true, do_block: true,
- def: true, defs: true, begin: true, rescue: true, rescue_mod: true,
- if: true, if_mod: true, else: true, elsif: true, case: true,
- when: true, next: true, break: true, retry: true, redo: true,
- return: true, throw: true, catch: true, until: true, until_mod: true,
- while: true, while_mod: true, yield: true, yield0: true, zsuper: true,
- unless: true, unless_mod: true, for: true, super: true, return0: true }
+ KEYWORDS = { :class => true, :alias => true, :lambda => true, :do_block => true,
+ :def => true, :defs => true, :begin => true, :rescue => true, :rescue_mod => true,
+ :if => true, :if_mod => true, :else => true, :elsif => true, :case => true,
+ :when => true, :next => true, :break => true, :retry => true, :redo => true,
+ :return => true, :throw => true, :catch => true, :until => true, :until_mod => true,
+ :while => true, :while_mod => true, :yield => true, :yield0 => true, :zsuper => true,
+ :unless => true, :unless_mod => true, :for => true, :super => true, :return0 => true }
# @group Creating an AstNode
@@ -110,10 +110,12 @@ def self.node_class_for(type)
MethodCallNode
when :if, :elsif, :if_mod, :unless, :unless_mod
ConditionalNode
- when /_ref\Z/
- ReferenceNode
else
- AstNode
+ if type.to_s =~ /_ref\Z/
+ ReferenceNode
+ else
+ AstNode
+ end
end
end
@@ -251,26 +253,27 @@ def show
# @return [nil] pretty prints the node
def pretty_print(q)
- objs = [*self.dup, :__last__]
+ objs = self.dup + [:__last__]
objs.unshift(type) if type && type != :list
- options = {}
+ options = []
if @docstring
- options[:docstring] = docstring
+ options << ['docstring', docstring]
end
if @source_range || @line_range
- options[:line] = line_range
- options[:source] = source_range
+ options << ['line', line_range]
+ options << ['source', source_range]
end
objs.pop if options.size == 0
q.group(3, 's(', ')') do
q.seplist(objs, nil, :each) do |v|
if v == :__last__
- q.seplist(options, nil, :each) do |k, v2|
+ q.seplist(options, nil, :each) do |arr|
+ k, v2 = *arr
q.group(3) do
q.text k
- q.group(3) do
+ q.group(3) do
q.text ': '
q.pp v2
end
View
56 lib/yard/parser/ruby/ruby_parser.rb
@@ -1,4 +1,8 @@
-require 'ripper'
+begin
+ require 'ripper'
+rescue LoadError => e
+ begin; require 'ripper18'; rescue LoadError; raise(e) end
+end
module YARD
module Parser
@@ -117,32 +121,32 @@ def enumerator
PARSER_EVENT_TABLE.each do |event, arity|
node_class = AstNode.node_class_for(event)
- if /_new\z/ =~ event and arity == 0
+ if /_new\z/ =~ event.to_s and arity == 0
module_eval(<<-eof, __FILE__, __LINE__ + 1)
def on_#{event}(*args)
- #{node_class}.new(:list, args, listchar: charno...charno, listline: lineno..lineno)
+ #{node_class}.new(:list, args, :listchar => charno...charno, :listline => lineno..lineno)
end
eof
- elsif /_add(_.+)?\z/ =~ event
+ elsif /_add(_.+)?\z/ =~ event.to_s
module_eval(<<-eof, __FILE__, __LINE__ + 1)
- undef on_#{event} if instance_method(:on_#{event})
+ begin; undef on_#{event}; rescue NameError; end
def on_#{event}(list, item)
list.push(item)
list
end
eof
elsif MAPPINGS.has_key?(event)
module_eval(<<-eof, __FILE__, __LINE__ + 1)
- undef on_#{event} if instance_method(:on_#{event})
+ begin; undef on_#{event}; rescue NameError; end
def on_#{event}(*args)
visit_event #{node_class}.new(:#{event}, args)
end
eof
else
module_eval(<<-eof, __FILE__, __LINE__ + 1)
- undef on_#{event} if instance_method(:on_#{event})
+ begin; undef on_#{event}; rescue NameError; end
def on_#{event}(*args)
- #{node_class}.new(:#{event}, args, listline: lineno..lineno, listchar: charno...charno)
+ #{node_class}.new(:#{event}, args, :listline => lineno..lineno, :listchar => charno...charno)
end
eof
end
@@ -151,17 +155,18 @@ def on_#{event}(*args)
SCANNER_EVENTS.each do |event|
ast_token = AST_TOKENS.include?(event)
module_eval(<<-eof, __FILE__, __LINE__ + 1)
- undef on_#{event} if instance_method(:on_#{event})
+ begin; undef on_#{event}; rescue NameError; end
def on_#{event}(tok)
visit_ns_token(:#{event}, tok, #{ast_token.inspect})
end
eof
end
- REV_MAPPINGS.select {|k| k.is_a?(Symbol) }.each do |event, value|
+ REV_MAPPINGS.select {|k,v| k.is_a?(Symbol) }.each do |pair|
+ event, value = *pair
ast_token = AST_TOKENS.include?(event)
module_eval(<<-eof, __FILE__, __LINE__ + 1)
- undef on_#{event} if instance_method(:on_#{event})
+ begin; undef on_#{event}; rescue NameError; end
def on_#{event}(tok)
(@map[:#{event}] ||= []) << [lineno, charno]
visit_ns_token(:#{event}, tok, #{ast_token.inspect})
@@ -171,7 +176,7 @@ def on_#{event}(tok)
[:kw, :op].each do |event|
module_eval(<<-eof, __FILE__, __LINE__ + 1)
- undef on_#{event} if instance_method(:on_#{event})
+ begin; undef on_#{event}; rescue NameError; end
def on_#{event}(tok)
unless @last_ns_token == [:kw, "def"] ||
(@tokens.last && @tokens.last[0] == :symbeg)
@@ -184,7 +189,7 @@ def on_#{event}(tok)
[:sp, :nl, :ignored_nl].each do |event|
module_eval(<<-eof, __FILE__, __LINE__ + 1)
- undef on_#{event} if instance_method(:on_#{event})
+ begin; undef on_#{event}; rescue NameError; end
def on_#{event}(tok)
add_token(:#{event}, tok)
@charno += tok.length
@@ -193,7 +198,8 @@ def on_#{event}(tok)
end
def visit_event(node)
- lstart, sstart = *@map[MAPPINGS[node.type]].pop
+ map = @map[MAPPINGS[node.type]]
+ lstart, sstart = *(map ? map.pop : [lineno, lineno])
node.source_range = Range.new(sstart, @ns_charno - 1)
node.line_range = Range.new(lstart, lineno)
node
@@ -214,7 +220,7 @@ def visit_ns_token(token, data, ast_token = false)
@charno += data.length
@ns_charno = charno
if ast_token
- AstNode.new(token, [data], line: lineno..lineno, char: ch..charno-1, token: true)
+ AstNode.new(token, [data], :line => lineno..lineno, :char => ch..charno-1, :token => true)
end
end
@@ -281,7 +287,7 @@ def on_aref(*args)
ll, lc = *@map[:aref].pop
sr = args.first.source_range.first..lc
lr = args.first.line_range.first..ll
- AstNode.new(:aref, args, char: sr, line: lr)
+ AstNode.new(:aref, args, :char => sr, :line => lr)
end
def on_rbracket(tok)
@@ -303,17 +309,17 @@ def on_top_const_ref(*args)
def on_const_path_ref(*args)
klass = AstNode.node_class_for(:const_path_ref)
- klass.new(:const_path_ref, args, listline: lineno..lineno, listchar: charno..charno)
+ klass.new(:const_path_ref, args, :listline => lineno..lineno, :listchar => charno..charno)
end
[:if_mod, :unless_mod, :while_mod].each do |kw|
node_class = AstNode.node_class_for(kw)
module_eval(<<-eof, __FILE__, __LINE__ + 1)
- undef on_#{kw} if instance_method(:on_#{kw})
+ begin; undef on_#{kw}; rescue NameError; end
def on_#{kw}(*args)
sr = args.last.source_range.first..args.first.source_range.last
lr = args.last.line_range.first..args.first.line_range.last
- #{node_class}.new(:#{kw}, args, line: lr, char: sr)
+ #{node_class}.new(:#{kw}, args, :line => lr, :char => sr)
end
eof
end
@@ -336,7 +342,7 @@ def on_lambda(*args)
end
def on_string_content(*args)
- AstNode.new(:string_content, args, listline: lineno..lineno, listchar: charno..charno)
+ AstNode.new(:string_content, args, :listline => lineno..lineno, :listchar => charno..charno)
end
def on_rescue(exc, *args)
@@ -345,7 +351,7 @@ def on_rescue(exc, *args)
end
def on_void_stmt
- AstNode.new(:void_stmt, [], line: lineno..lineno, char: charno...charno)
+ AstNode.new(:void_stmt, [], :line => lineno..lineno, :char => charno...charno)
end
def on_params(*args)
@@ -354,26 +360,26 @@ def on_params(*args)
if arg.first.class == Array
arg.map! do |sub_arg|
if sub_arg.class == Array
- AstNode.new(:default_arg, sub_arg, listline: lineno..lineno, listchar: charno..charno)
+ AstNode.new(:default_arg, sub_arg, :listline => lineno..lineno, :listchar => charno..charno)
else
sub_arg
end
end
end
- AstNode.new(:list, arg, listline: lineno..lineno, listchar: charno..charno)
+ AstNode.new(:list, arg, :listline => lineno..lineno, :listchar => charno..charno)
else
arg
end
end
- ParameterNode.new(:params, args, listline: lineno..lineno, listchar: charno..charno)
+ ParameterNode.new(:params, args, :listline => lineno..lineno, :listchar => charno..charno)
end
def on_label(data)
add_token(:label, data)
ch = charno
@charno += data.length
@ns_charno = charno
- AstNode.new(:label, [data[0...-1]], line: lineno..lineno, char: ch..charno-1, token: true)
+ AstNode.new(:label, [data[0...-1]], :line => lineno..lineno, :char => ch..charno-1, :token => true)
end
def on_comment(comment)
View
4 lib/yard/parser/source_parser.rb
@@ -139,7 +139,7 @@ def parser_type_for_extension(extension)
# @return [Symbol] the validated parser type
# @private
def validated_parser_type(type)
- RUBY18 && type == :ruby ? :ruby18 : type
+ !HAVE_RIPPER && type == :ruby ? :ruby18 : type
end
private
@@ -171,7 +171,7 @@ def parse_in_order(*files)
self.parser_type = :ruby
- register_parser_type :ruby, Ruby::RubyParser if RUBY19
+ register_parser_type :ruby, Ruby::RubyParser if HAVE_RIPPER
register_parser_type :ruby18, Ruby::Legacy::RubyParser
register_parser_type :c, CParser, ['c', 'cc', 'cxx', 'cpp']
View
2 spec/handlers/alias_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}AliasHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}AliasHandler" do
before(:all) { parse_file :alias_handler_001, __FILE__ }
it "should throw alias into namespace object list" do
View
2 spec/handlers/attribute_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}AttributeHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}AttributeHandler" do
before(:all) { parse_file :attribute_handler_001, __FILE__ }
def read_write(namespace, name, read, write, scope = :instance)
View
2 spec/handlers/base_spec.rb
@@ -138,7 +138,7 @@ def test_handler(file, stmts, creates = true)
end
[:ruby, :ruby18].each do |parser_type|
- next if parser_type == :ruby && !RUBY19
+ next if parser_type == :ruby && LEGACY_PARSER
describe "Parser type = #{parser_type.inspect}" do
Parser::SourceParser.parser_type = parser_type
it "should allow handler to be specific to a file" do
View
2 spec/handlers/class_condition_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}ClassConditionHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ClassConditionHandler" do
before(:all) { parse_file :class_condition_handler_001, __FILE__ }
def verify_method(*names)
View
2 spec/handlers/class_handler_spec.rb
@@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}ClassHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ClassHandler" do
before(:all) { parse_file :class_handler_001, __FILE__ }
it "should parse a class block with docstring" do
View
2 spec/handlers/class_variable_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}ClassVariableHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ClassVariableHandler" do
before(:all) { parse_file :class_variable_handler_001, __FILE__ }
it "should not parse class variables inside methods" do
View
4 spec/handlers/constant_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}ConstantHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ConstantHandler" do
before(:all) { parse_file :constant_handler_001, __FILE__ }
it "should not parse constants inside methods" do
@@ -60,5 +60,5 @@
it "should raise undocumentable error in 1.9 parser for Struct.new assignment to non-const" do
undoc_error "nonconst = Struct.new"
- end if RUBY19
+ end unless LEGACY_PARSER
end
View
2 spec/handlers/exception_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}ExceptionHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ExceptionHandler" do
before(:all) { parse_file :exception_handler_001, __FILE__ }
it "should not document an exception outside of a method" do
View
2 spec/handlers/extend_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}ExtendHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ExtendHandler" do
before(:all) { parse_file :extend_handler_001, __FILE__ }
it "should include modules at class scope" do
View
2 spec/handlers/method_condition_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}MethodConditionHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}MethodConditionHandler" do
before(:all) { parse_file :method_condition_handler_001, __FILE__ }
it "should not parse regular if blocks in methods" do
View
2 spec/handlers/method_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}MethodHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}MethodHandler" do
before(:all) do
log.enter_level(Logger::ERROR) do
parse_file :method_handler_001, __FILE__
View
2 spec/handlers/mixin_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}MixinHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}MixinHandler" do
before(:all) { parse_file :mixin_handler_001, __FILE__ }
it "should handle includes from classes or modules" do
View
2 spec/handlers/module_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}ModuleHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ModuleHandler" do
before(:all) { parse_file :module_handler_001, __FILE__ }
it "should parse a module block" do
View
4 spec/handlers/private_constant_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}PrivateConstantHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}PrivateConstantHandler" do
before(:all) { parse_file :private_constant_handler_001, __FILE__ }
it "should handle private_constant statement" do
@@ -16,7 +16,7 @@
it "should fail if parameter is not String, Symbol or Constant" do
undoc_error 'class Foo; private_constant x; end'
undoc_error 'class Foo; X = 1; private_constant X.new("hi"); end'
- end if RUBY19
+ end unless LEGACY_PARSER
it "should fail if constant can't be recognized" do
undoc_error 'class Foo2; private_constant :X end'
View
2 spec/handlers/process_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}ProcessHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ProcessHandler" do
before(:all) { parse_file :process_handler_001, __FILE__ }
it "should only work for classes that extend YARD::Handlers::*" do
View
2 spec/handlers/ruby/base_spec.rb
@@ -87,4 +87,4 @@ class MethCallHandler < Handlers::Ruby::Base
end
invalid MethCallHandler, ast[9]
end
-end if RUBY19
+end if HAVE_RIPPER
View
2 spec/handlers/visibility_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}VisibilityHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}VisibilityHandler" do
before(:all) { parse_file :visibility_handler_001, __FILE__ }
it "should be able to set visibility to public" do
View
2 spec/handlers/yield_handler_spec.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
-describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}YieldHandler" do
+describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}YieldHandler" do
before(:all) { parse_file :yield_handler_001, __FILE__ }
it "should only parse yield blocks in methods" do
View
49 spec/parser/ruby/ast_node_spec.rb
@@ -4,32 +4,29 @@
include YARD::Parser::Ruby
-if RUBY19
- describe YARD::Parser::Ruby::AstNode do
- describe "#jump" do
- it "should jump to the first specific inner node if found" do
- ast = s(:paren, s(:paren, s(:params, s(s(:ident, "hi"), s(:ident, "bye")))))
- ast.jump(:params)[0][0].type.should equal(:ident)
- end
-
- it "should return the original ast if no inner node is found" do
- ast = s(:paren, s(:list, s(:list, s(s(:ident, "hi"), s(:ident, "bye")))))
- ast.jump(:params).object_id.should == ast.object_id
- end
+describe YARD::Parser::Ruby::AstNode do
+ describe "#jump" do
+ it "should jump to the first specific inner node if found" do
+ ast = s(:paren, s(:paren, s(:params, s(s(:ident, "hi"), s(:ident, "bye")))))
+ ast.jump(:params)[0][0].type.should equal(:ident)
+ end
+
+ it "should return the original ast if no inner node is found" do
+ ast = s(:paren, s(:list, s(:list, s(s(:ident, "hi"), s(:ident, "bye")))))
+ ast.jump(:params).object_id.should == ast.object_id
end
-
- describe '#pretty_print' do
- it "should show a list of nodes" do
- obj = YARD::Parser::Ruby::RubyParser.parse("# x\nbye", "x").ast
- out = StringIO.new
- PP.pp(obj, out)
- out.rewind
- out.read.should == "s(s(:var_ref,\n" +
- " s(:ident, \"bye\", line: 2..2, source: 4..6),\n" +
- " docstring: \"x\",\n" +
- " line: 2..2,\n" +
- " source: 4..6))\n"
- end
+ end
+
+ describe '#pretty_print' do
+ it "should show a list of nodes" do
+ obj = YARD::Parser::Ruby::RubyParser.parse("# x\nbye", "x").ast
+ out = StringIO.new
+ PP.pp(obj, out)
+ out.string.should == "s(s(:var_ref,\n" +
+ " s(:ident, \"bye\", line: 2..2, source: 4..6),\n" +
+ " docstring: \"x\",\n" +
+ " line: 2..2,\n" +
+ " source: 4..6))\n"
end
end
-end
+end if HAVE_RIPPER
View
354 spec/parser/ruby/ruby_parser_spec.rb
@@ -1,200 +1,198 @@
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
-if RUBY19
- describe YARD::Parser::Ruby::RubyParser do
- def stmt(stmt)
- YARD::Parser::Ruby::RubyParser.new(stmt, nil).parse.root.first
- end
-
- def stmts(stmts)
- YARD::Parser::Ruby::RubyParser.new(stmts, nil).parse.root
- end
-
- def tokenize(stmt)
- YARD::Parser::Ruby::RubyParser.new(stmt, nil).parse.tokens
- end
-
- describe '#parse' do
- it "should get comment line numbers" do
- s = stmt <<-eof
- # comment
- # comment
- # comment
- def method; end
- eof
- s.comments.should == "comment\ncomment\ncomment"
- s.comments_range.should == (1..3)
-
- s = stmt <<-eof
-
- # comment
- # comment
- def method; end
- eof
- s.comments.should == "comment\ncomment"
- s.comments_range.should == (2..3)
-
- s = stmt <<-eof
- # comment
- # comment
-
- def method; end
- eof
- s.comments.should == "comment\ncomment"
- s.comments_range.should == (1..2)
-
- s = stmt <<-eof
- # comment
- def method; end
- eof
- s.comments.should == "comment"
- s.comments_range.should == (1..1)
-
- s = stmt <<-eof
- def method; end # comment
- eof
- s.comments.should == "comment"
- s.comments_range.should == (1..1)
- end
-
- it "should only look up to two lines back for comments" do
- s = stmt <<-eof
- # comments
+describe YARD::Parser::Ruby::RubyParser do
+ def stmt(stmt)
+ YARD::Parser::Ruby::RubyParser.new(stmt, nil).parse.root.first
+ end
+
+ def stmts(stmts)
+ YARD::Parser::Ruby::RubyParser.new(stmts, nil).parse.root
+ end
+
+ def tokenize(stmt)
+ YARD::Parser::Ruby::RubyParser.new(stmt, nil).parse.tokens
+ end
+
+ describe '#parse' do
+ it "should get comment line numbers" do
+ s = stmt <<-eof
+ # comment
+ # comment
+ # comment
+ def method; end
+ eof
+ s.comments.should == "comment\ncomment\ncomment"
+ s.comments_range.should == (1..3)
+
+ s = stmt <<-eof
+
+ # comment
+ # comment
+ def method; end
+ eof
+ s.comments.should == "comment\ncomment"
+ s.comments_range.should == (2..3)
+
+ s = stmt <<-eof
+ # comment
+ # comment
+
+ def method; end
+ eof
+ s.comments.should == "comment\ncomment"
+ s.comments_range.should == (1..2)
+
+ s = stmt <<-eof
+ # comment
+ def method; end
+ eof
+ s.comments.should == "comment"
+ s.comments_range.should == (1..1)
+
+ s = stmt <<-eof
+ def method; end # comment
+ eof
+ s.comments.should == "comment"
+ s.comments_range.should == (1..1)
+ end
+
+ it "should only look up to two lines back for comments" do
+ s = stmt <<-eof
+ # comments
- # comments
+ # comments
- def method; end
- eof
- s.comments.should == "comments"
+ def method; end
+ eof
+ s.comments.should == "comments"
- s = stmt <<-eof
- # comments
+ s = stmt <<-eof
+ # comments
- def method; end
- eof
- s.comments.should == nil
+ def method; end
+ eof
+ s.comments.should == nil
- ss = stmts <<-eof
- # comments
+ ss = stmts <<-eof
+ # comments
- def method; end
+ def method; end
- # hello
- def method2; end
- eof
- ss[0].comments.should == nil
- ss[1].comments.should == 'hello'
- end
-
- it "should handle 1.9 lambda syntax with args" do
- src = "->(a,b,c=1,*args,&block) { hello_world }"
- stmt(src).source.should == src
- end
-
- it "should handle 1.9 lambda syntax" do
- src = "-> { hello_world }"
- stmt(src).source.should == src
- end
-
- it "should handle standard lambda syntax" do
- src = "lambda { hello_world }"
- stmt(src).source.should == src
- end
-
- it "should throw a ParserSyntaxError on invalid code" do
- lambda { stmt("Foo, bar.") }.should raise_error(YARD::Parser::ParserSyntaxError)
- end
-
- it "should handle bare hashes as method parameters" do
- src = "command :a => 1, :b => 2, :c => 3"
- stmt(src).jump(:command)[1].source.should == ":a => 1, :b => 2, :c => 3"
+ # hello
+ def method2; end
+ eof
+ ss[0].comments.should == nil
+ ss[1].comments.should == 'hello'
+ end
+
+ it "should handle 1.9 lambda syntax with args" do
+ src = "->(a,b,c=1,*args,&block) { hello_world }"
+ stmt(src).source.should == src
+ end
+
+ it "should handle 1.9 lambda syntax" do
+ src = "-> { hello_world }"
+ stmt(src).source.should == src
+ end
- src = "command a: 1, b: 2, c: 3"
- stmt(src).jump(:command)[1].source.should == "a: 1, b: 2, c: 3"
- end
-
- it "should handle source for hash syntax" do
- src = "{ :a => 1, :b => 2, :c => 3 }"
- stmt(src).jump(:hash).source.should == "{ :a => 1, :b => 2, :c => 3 }"
- end
-
- it "should handle an empty hash" do
- stmt("{}").jump(:hash).source.should == "{}"
- end
-
- it "new hash label syntax should show label without colon" do
- ast = stmt("{ a: 1 }").jump(:label)
- ast[0].should == "a"
- ast.source.should == "a:"
- end
+ it "should handle standard lambda syntax" do
+ src = "lambda { hello_world }"
+ stmt(src).source.should == src
+ end
+
+ it "should throw a ParserSyntaxError on invalid code" do
+ lambda { stmt("Foo, bar.") }.should raise_error(YARD::Parser::ParserSyntaxError)
+ end
+
+ it "should handle bare hashes as method parameters" do
+ src = "command :a => 1, :b => 2, :c => 3"
+ stmt(src).jump(:command)[1].source.should == ":a => 1, :b => 2, :c => 3"
- it "should handle begin/rescue blocks" do
- ast = stmt("begin; X; rescue => e; Y end").jump(:rescue)
- ast.source.should == "rescue => e; Y end"
+ src = "command a: 1, b: 2, c: 3"
+ stmt(src).jump(:command)[1].source.should == "a: 1, b: 2, c: 3"
+ end
+
+ it "should handle source for hash syntax" do
+ src = "{ :a => 1, :b => 2, :c => 3 }"
+ stmt(src).jump(:hash).source.should == "{ :a => 1, :b => 2, :c => 3 }"
+ end
+
+ it "should handle an empty hash" do
+ stmt("{}").jump(:hash).source.should == "{}"
+ end
+
+ it "new hash label syntax should show label without colon" do
+ ast = stmt("{ a: 1 }").jump(:label)
+ ast[0].should == "a"
+ ast.source.should == "a:"
+ end
+
+ it "should handle begin/rescue blocks" do
+ ast = stmt("begin; X; rescue => e; Y end").jump(:rescue)
+ ast.source.should == "rescue => e; Y end"
- ast = stmt("begin; X; rescue A => e; Y end").jump(:rescue)
- ast.source.should == "rescue A => e; Y end"
+ ast = stmt("begin; X; rescue A => e; Y end").jump(:rescue)
+ ast.source.should == "rescue A => e; Y end"
- ast = stmt("begin; X; rescue A, B => e; Y end").jump(:rescue)
- ast.source.should == "rescue A, B => e; Y end"
- end
-
- it "should handle method rescue blocks" do
- ast = stmt("def x; A; rescue Y; B end")
- ast.source.should == "def x; A; rescue Y; B end"
- ast.jump(:rescue).source.should == "rescue Y; B end"
- end
-
- it "should handle defs with keywords as method name" do
- ast = stmt("# docstring\nclass A;\ndef class; end\nend")
- ast.jump(:class).docstring.should == "docstring"
- ast.jump(:class).line_range.should == (2..4)
- end
-
- it "should end source properly on array reference" do
- ast = stmt("AS[0, 1 ] ")
- ast.source.should == 'AS[0, 1 ]'
+ ast = stmt("begin; X; rescue A, B => e; Y end").jump(:rescue)
+ ast.source.should == "rescue A, B => e; Y end"
+ end
+
+ it "should handle method rescue blocks" do
+ ast = stmt("def x; A; rescue Y; B end")
+ ast.source.should == "def x; A; rescue Y; B end"
+ ast.jump(:rescue).source.should == "rescue Y; B end"
+ end
+
+ it "should handle defs with keywords as method name" do
+ ast = stmt("# docstring\nclass A;\ndef class; end\nend")
+ ast.jump(:class).docstring.should == "docstring"
+ ast.jump(:class).line_range.should == (2..4)
+ end
+
+ it "should end source properly on array reference" do
+ ast = stmt("AS[0, 1 ] ")
+ ast.source.should == 'AS[0, 1 ]'
- ast = stmt("def x(a = S[1]) end").jump(:default_arg)
- ast.source.should == 'a = S[1]'
- end
-
- it "should end source properly on if/unless mod" do
- %w(if unless while).each do |mod|
- stmt("A=1 #{mod} true").source.should == "A=1 #{mod} true"
- end
- end
-
- it "should show proper source for assignment" do
- stmt("A=1").jump(:assign).source.should == "A=1"
- end
-
- it "should show proper source for a top_const_ref" do
- s = stmt("::\nFoo::Bar")
- s.jump(:top_const_ref).source.should == "::\nFoo"
- s.should be_ref
- s.jump(:top_const_ref).should be_ref
- s.source.should == "::\nFoo::Bar"
- s.line_range.to_a.should == [1, 2]
+ ast = stmt("def x(a = S[1]) end").jump(:default_arg)
+ ast.source.should == 'a = S[1]'
+ end
+
+ it "should end source properly on if/unless mod" do
+ %w(if unless while).each do |mod|
+ stmt("A=1 #{mod} true").source.should == "A=1 #{mod} true"
end
-
- it "should show proper source for heredoc" do
- src = "def foo\n foo(<<-XML, 1, 2)\n bar\n\n XML\nend"
+ end
+
+ it "should show proper source for assignment" do
+ stmt("A=1").jump(:assign).source.should == "A=1"
+ end
+
+ it "should show proper source for a top_const_ref" do
+ s = stmt("::\nFoo::Bar")
+ s.jump(:top_const_ref).source.should == "::\nFoo"
+ s.should be_ref
+ s.jump(:top_const_ref).should be_ref
+ s.source.should == "::\nFoo::Bar"
+ s.line_range.to_a.should == [1, 2]
+ end
+
+ it "should show proper source for heredoc" do
+ src = "def foo\n foo(<<-XML, 1, 2)\n bar\n\n XML\nend"
+ s = stmt(src)
+ t = tokenize(src)
+ s.source.should == src
+ t.map {|x| x[1] }.join.should == src
+ end
+
+ it "should show proper source for string" do
+ ["'", '"'].each do |q|
+ src = "#{q}hello\n\nworld#{q}"
s = stmt(src)
- t = tokenize(src)
s.source.should == src
- t.map {|x| x[1] }.join.should == src
- end
-
- it "should show proper source for string" do
- ["'", '"'].each do |q|
- src = "#{q}hello\n\nworld#{q}"
- s = stmt(src)
- s.source.should == src
- end
end
end
end
-end
+end if HAVE_RIPPER
View
4 spec/parser/source_parser_spec.rb
@@ -256,7 +256,7 @@ module Hello # FAIL
result.enumerator[0].source.encoding.to_s.send(msg) == 'Shift_JIS'
end
end
- end if RUBY19
+ end if RUBY19 && HAVE_RIPPER
end
describe '#parse_in_order' do
@@ -290,7 +290,7 @@ def in_order_parse(*files)
YARD::Parser::SourceParser.parse_string("int main() { }", :d)
end
- if RUBY19
+ if HAVE_RIPPER
it "should display a warning for a syntax error (with new parser)" do
err_msg = "Syntax error in `(stdin)`:(1,3): syntax error, unexpected $undefined, expecting $end"
log.should_receive(:warn).with(err_msg)
View
12 spec/templates/helpers/html_syntax_highlight_helper_spec.rb
@@ -15,16 +15,16 @@
html_syntax_highlight("def x\nend").should == "def x\nend"
end
- it "should highlight source (ruby18)" do
+ it "should highlight source (legacy)" do
should_receive(:options).and_return(:no_highlight => false)
expect = "<span class='def def kw'>def</span><span class='x identifier id'>x</span>
<span class='string val'>'x'</span><span class='plus op'>+</span>
<span class='regexp val'>/x/i</span><span class='end end kw'>end</span>"
result = html_syntax_highlight("def x\n 'x' + /x/i\nend")
html_equals_string(result, expect)
- end if RUBY18
+ end if LEGACY_PARSER
- it "should highlight source (ruby19)" do
+ it "should highlight source (ripper)" do
should_receive(:options).and_return(:no_highlight => false)
expect = "<span class='kw'>def</span> <span class='id x'>x</span>
<span class='tstring'><span class='tstring_beg'>'</span>
@@ -34,11 +34,11 @@
<span class='regexp_end'>/i</span></span>\n<span class='kw'>end</span>"
result = html_syntax_highlight("def x\n 'x' + /x/i\nend")
html_equals_string(result, expect)
- end if RUBY19
+ end if HAVE_RIPPER
- it "should return escaped unhighlighted source if a syntax error is found (ruby19)" do
+ it "should return escaped unhighlighted source if a syntax error is found (ripper)" do
should_receive(:options).and_return(:no_highlight => false)
html_syntax_highlight("def &x; ... end").should == "def &amp;x; ... end"
- end if RUBY19
+ end if HAVE_RIPPER
end
end

0 comments on commit 1ee355e

Please sign in to comment.
Something went wrong with that request. Please try again.