Skip to content

Commit

Permalink
Implementing better forward-compatible support.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvande committed Aug 25, 2010
1 parent c9fa93c commit 07c2214
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 82 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -18,6 +18,8 @@ into fine, high-quality documentation.

At present, this module will:

* Work with YARD 0.6 -- and likely beyond!
* Provided http://github.com/pvande/yard.git gets merged in
* Parse a package declaration
* Provided it's the first thing on a line
* Parse a named sub declaration
Expand Down
79 changes: 10 additions & 69 deletions lib/yard-perl-plugin.rb
Expand Up @@ -6,86 +6,27 @@
require File.dirname(__FILE__) + '/yard/formatters/pod_formatter'
require File.dirname(__FILE__) + '/yard/templates/helpers/pod_helper'



# Monkeypatches for YARD 0.5.4
module YARD
module Handlers
class Processor
alias :handler_base_namespace_without_perl :handler_base_namespace
def handler_base_namespace
case parser_type
when :perl; Perl
else handler_base_namespace_without_perl
end
end
end
Processor.register_handler_namespace :perl, Perl
end

module Parser
class SourceParser
alias :parser_type_for_filename_without_perl :parser_type_for_filename
def parser_type_for_filename(filename)
case (File.extname(filename)[1..-1] || "").downcase
when "pl", "pm"
:perl
else # when "rb", "rbx", "erb"
parser_type_for_filename_without_perl(filename)
end
end

alias :parse_statements_without_perl :parse_statements
def parse_statements(content)
return Perl.parse(content, file) if parser_type == :perl
parse_statements_without_perl(content)
end
end

class Ruby::Legacy::TokenList
def parse_content(content)
lex = Ruby::Legacy::RubyLex.new(content)

# if Perl...

op = lex.instance_variable_get(:@OP)
head = op.instance_variable_get(:@head)
tree = head.instance_variable_get(:@Tree)

op.def_rule('\%') do |op, io|
t = lex.identify_identifier
t.set_text("\\%#{t.text}")
end

tree['%'].postproc = proc do |op, io|
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
lex.identify_quotation('%')
elsif lex.peek(0) == '='
lex.getc
Token(TkOPASGN, "%").set_text("%=")
elsif @lex_state == EXPR_ARG and @space_seen and lex.peek(0) !~ /\s/
lex.identify_quotation('%')
else
@lex_state = EXPR_BEG
Token("%").set_text("%")
end
end

# end

while tk = lex.token do
self << convert_token(lex, tk)
end
end
end
SourceParser.register_parser_type :perl, Perl::PerlParser, %w[ pm pl ]
end

module Templates
module Engine
register_template_path File.join(File.dirname(__FILE__), '..', 'templates')
Engine.register_template_path File.join(File.dirname(__FILE__), '..', 'templates')

module Helpers::HtmlHelper
def html_syntax_highlight_perl(source)
# Until such time as we can leverage another library to do our work...
html_syntax_highlight_plain(source)
end
end
end

module Tags
Library.define_tag "Method Scope", :scope
end
end
end
1 change: 1 addition & 0 deletions lib/yard/handlers/perl/sub_handler.rb
Expand Up @@ -4,6 +4,7 @@ class YARD::Handlers::Perl::SubHandler < YARD::Handlers::Perl::Base
process do
method = register MethodObject.new($__PACKAGE__ || :root, statement.name) do |m|
m.source = statement.text
m.source_type = :perl
m.visibility = statement.visibility
end

Expand Down
37 changes: 24 additions & 13 deletions lib/yard/parser/perl.rb
Expand Up @@ -3,12 +3,13 @@ module Parser
module Perl

class Line
attr_reader :file, :text, :line
attr_reader :file, :text, :line, :group

def initialize(file, text, line)
@file = file
@text = text
@line = line
def initialize(file, text, line, group)
@file = file
@text = text
@line = line
@group = group
end

def ==(other)
Expand Down Expand Up @@ -72,21 +73,31 @@ def name
end
end

class << self
def parse(content, file='(string)')
line = 0
@stack = content.lines.collect do |src|
class PerlParser < YARD::Parser::Base
def initialize(source, filename)
@source = source
@filename = filename
end

def parse
group = nil
line = 0
@stack = @source.lines.collect do |src|
case src
when /^\s*use namespace::clean;/
:private
when /^# @group\s+(.+)\s*$/
group = $1
when /^# @endgroup\s*$/
group = nil
when /^\s*#/
Comment.new(file, src, line += 1)
Comment.new(@filename, src, line += 1, group)
when /^\s*package/
Package.new(file, src, line += 1)
Package.new(@filename, src, line += 1, group)
when /^\s*sub/
Sub.new(file, src, line += 1)
Sub.new(@filename, src, line += 1, group)
else
Line.new(file, src, line += 1)
Line.new(@filename, src, line += 1, group)
end
end

Expand Down

0 comments on commit 07c2214

Please sign in to comment.