From 16a63b78df1d0a911b96e9ad33137081611c6e16 Mon Sep 17 00:00:00 2001 From: Henrik Grubbstrom Date: Wed, 12 Mar 2008 22:55:57 +0100 Subject: [PATCH] Added detector and parser for Pike (http://pike.ida.liu.se/). --- ext/ohcount_native/generator.rb | 2 ++ lib/ohcount/detector.rb | 38 +++++++++++++++++++++++++-------- lib/ohcount/sloc_info.rb | 1 + 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/ext/ohcount_native/generator.rb b/ext/ohcount_native/generator.rb index 6a79228..65ed221 100644 --- a/ext/ohcount_native/generator.rb +++ b/ext/ohcount_native/generator.rb @@ -44,6 +44,7 @@ module Ohcount pascal = CMonoglot.new("pascal", '//', ['{','}'], true, false) perl = CMonoglot.new("perl", '#', ['^=\\\\w+', '^=cut[ \t]*\\\\n'], true, true) phplanguage = CMonoglot.new("php", '//', [e('/*'), e('*/')], true, true, {:polyglot_name => 'phplanguage'}) + pike = CMonoglot.new("pike", '//', [e('/*'), e('*/')], true, false) python = PythonMonoglot.new("python") ruby = CMonoglot.new("ruby", '#', nil, true, true) rexx = CMonoglot.new("rexx", nil, [e('/*'), e('*/')], true, true) @@ -90,6 +91,7 @@ module Ohcount objective_c, pascal , perl , + pike , phplanguage , python , ruby , diff --git a/lib/ohcount/detector.rb b/lib/ohcount/detector.rb index 3051beb..383a85c 100644 --- a/lib/ohcount/detector.rb +++ b/lib/ohcount/detector.rb @@ -23,6 +23,9 @@ class Ohcount::Detector # A performance hack -- once we've checked for the presence of *.m files, the result # is stored here to avoid checking twice. attr_accessor :contains_m + # A performance hack -- once we've checked for the presence of *.pike and *.pmod files, the result + # is stored here to avoid checking twice. + attr_accessor :contains_pike_or_pmod end # The primary entry point for the detector. @@ -156,6 +159,8 @@ class Ohcount::Detector '.pm' => "perl", '.perl' => "perl", '.ph' => "perl", + '.pike' => "pike", + '.pmod' => "pike", '.py' => "python", '.rhtml'=> "rhtml", '.rb' => "ruby", @@ -231,20 +236,35 @@ class Ohcount::Detector unless defined?(file_context.filenames.contains_m) file_context.filenames.extend(ContainsM) file_context.filenames.contains_m = file_context.filenames.select { |a| a =~ /\.m$/ }.any? + file_context.filenames.contains_pike_or_pmod = file_context.filenames.select { |a| a =~ /\.p(ike|mod)$/ }.any? + end + unless file_context.filenames.contains_pike_or_pmod + return 'cncpp' unless file_context.filenames.contains_m + end + + if file_context.filenames.contains_m + # if the dir contains a matching *.m file, likely objective_c + if file_context.filename =~ /\.h$/ + m_counterpart = file_context.filename.gsub(/\.h$/, ".m") + return 'objective_c' if file_context.filenames.include?(m_counterpart) + end + + # ok - it just might be objective_c, let's check contents for objective_c signatures + objective_c_signatures = /(^\s*@interface)|(^\s*@end)/ + objective_c += lines_matching(buffer, objective_c_signatures) + + return 'objective_c' if objective_c > 1 end - return 'cncpp' unless file_context.filenames.contains_m - # if the dir contains a matching *.m file, likely objective_c - if file_context.filename =~ /\.h$/ - m_counterpart = file_context.filename.gsub(/\.h$/, ".m") - return 'objective_c' if file_context.filenames.include?(m_counterpart) + if file_context.filenames.contains_pike_or_pmod + # The string "pike" and a selection of common Pike keywords. + pike_signatures = /([Pp][Ii][Kk][Ee])|(string )|(mapping)|(multiset)|(import )|(inherit )|(predef)/ + pike = lines_matching(buffer, pike_signatures) + return 'pike' if pike > 0 end - # ok - it just might be objective_c, let's check contents for objective_c signatures - objective_c_signatures = /(^\s*@interface)|(^\s*@end)/ - objective_c += lines_matching(buffer, objective_c_signatures) - return objective_c > 1 ? 'objective_c' : 'cncpp' + return 'cncpp' end # Tests whether the provided buffer contains binary or text content. diff --git a/lib/ohcount/sloc_info.rb b/lib/ohcount/sloc_info.rb index 5a141fc..3cf2382 100644 --- a/lib/ohcount/sloc_info.rb +++ b/lib/ohcount/sloc_info.rb @@ -64,6 +64,7 @@ class Ohcount::SlocInfo 'pascal' => {:nice_name => 'Pascal' , :category => 0}, 'perl' => {:nice_name => 'Perl' , :category => 0}, 'php' => {:nice_name => 'PHP' , :category => 0}, + 'pike' => {:nice_name => 'Pike' , :category => 0}, 'python' => {:nice_name => 'Python' , :category => 0}, 'rexx' => {:nice_name => 'rexx' , :category => 0}, 'ruby' => {:nice_name => 'Ruby' , :category => 0}, -- 2.32.0.93.g670b81a890