From 5ed5d8f483f28ec9ac5a1974e97fba04d231ce8f Mon Sep 17 00:00:00 2001 From: Michal Muskala Date: Thu, 1 Jun 2017 22:20:08 +0200 Subject: [PATCH] Consider elixir docs as comments Also add a test for Elixir --- Unix/cloc | 22 ++++++++++++++++++++++ Unix/t/00_C.t | 5 +++++ cloc | 22 ++++++++++++++++++++++ tests/inputs/elixir.ex | 20 ++++++++++++++++++++ tests/outputs/elixir.ex.yaml | 21 +++++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 tests/inputs/elixir.ex create mode 100644 tests/outputs/elixir.ex.yaml diff --git a/Unix/cloc b/Unix/cloc index 3ea1317..dac1cef 100755 --- a/Unix/cloc +++ b/Unix/cloc @@ -5163,6 +5163,26 @@ sub docstring_to_C { # {{{1 print "<- docstring_to_C\n" if $opt_v > 2; return @{$ra_lines}; } # 1}}} +sub elixir_doc_to_C { # {{{1 + my ($ra_lines, ) = @_; + # Converts Elixir docs to C comments. + + print "-> elixir_doc_to_C()\n" if $opt_v > 2; + + my $in_docstring = 0; + foreach (@{$ra_lines}) { + if (!$in_docstring && /(\@(module)?doc\s+(~[sScC])?['"]{3})/) { + s{$1}{/*}; + $in_docstring = 1; + } elsif ($in_docstring && /(['"]{3})/) { + s{$1}{*/}; + $in_docstring = 0; + } + } + + print "<- elixir_doc_to_C\n" if $opt_v > 2; + return @{$ra_lines}; +} # 1}}} sub Forth_paren_to_C { # {{{1 my ($ra_lines, ) = @_; # Converts Forth comment parentheses to C comments. @@ -5965,6 +5985,8 @@ sub set_constants { # {{{1 [ 'call_regexp_common' , 'HTML' ], ], 'Elixir' => [ [ 'remove_matches' , '^\s*#' ], + [ 'elixir_doc_to_C' ], + [ 'call_regexp_common' , 'C' ], [ 'remove_inline' , '#.*$' ], ], 'Erlang' => [ diff --git a/Unix/t/00_C.t b/Unix/t/00_C.t index c946ffc..7db180e 100755 --- a/Unix/t/00_C.t +++ b/Unix/t/00_C.t @@ -139,6 +139,11 @@ my @Tests = ( { 'args' => '../tests/inputs/comp.ecpp', }, { + 'name' => 'Elixir', + 'ref' => '../tests/outputs/elixir.ex.yaml', + 'args' => '../tests/inputs/elixir.ex', + }, + { 'name' => 'Focus', 'ref' => '../tests/outputs/FOCUS.focexec.yaml', 'args' => '../tests/inputs/FOCUS.focexec', diff --git a/cloc b/cloc index 713f025..b1939bd 100755 --- a/cloc +++ b/cloc @@ -5200,6 +5200,26 @@ sub docstring_to_C { # {{{1 print "<- docstring_to_C\n" if $opt_v > 2; return @{$ra_lines}; } # 1}}} +sub elixir_doc_to_C { # {{{1 + my ($ra_lines, ) = @_; + # Converts Elixir docs to C comments. + + print "-> elixir_doc_to_C()\n" if $opt_v > 2; + + my $in_docstring = 0; + foreach (@{$ra_lines}) { + if (!$in_docstring && /(\@(module)?doc\s+(~[sScC])?['"]{3})/) { + s{$1}{/*}; + $in_docstring = 1; + } elsif ($in_docstring && /(['"]{3})/) { + s{$1}{*/}; + $in_docstring = 0; + } + } + + print "<- elixir_doc_to_C\n" if $opt_v > 2; + return @{$ra_lines}; +} # 1}}} sub Forth_paren_to_C { # {{{1 my ($ra_lines, ) = @_; # Converts Forth comment parentheses to C comments. @@ -6036,6 +6056,8 @@ sub set_constants { # {{{1 [ 'call_regexp_common' , 'HTML' ], ], 'Elixir' => [ [ 'remove_matches' , '^\s*#' ], + [ 'elixir_doc_to_C' ], + [ 'call_regexp_common' , 'C' ], [ 'remove_inline' , '#.*$' ], ], 'Erlang' => [ diff --git a/tests/inputs/elixir.ex b/tests/inputs/elixir.ex new file mode 100644 index 0000000..91daec5 --- /dev/null +++ b/tests/inputs/elixir.ex @@ -0,0 +1,20 @@ +# This is a test for the Elixir SLOC counter. + +defmodule Test do + @moduledoc """ + Test module + """ + @notdoc :foo + + @doc ~S""" + Foo + """ + def foo do + @notdoc + end + + @doc ~c''' + Bar + ''' + def bar, do: :bar +end diff --git a/tests/outputs/elixir.ex.yaml b/tests/outputs/elixir.ex.yaml new file mode 100644 index 0000000..27fc9ec --- /dev/null +++ b/tests/outputs/elixir.ex.yaml @@ -0,0 +1,21 @@ +--- +# github.com/AlDanial/cloc +header : + cloc_url : github.com/AlDanial/cloc + cloc_version : 1.73 + elapsed_seconds : 0.00999093055725098 + n_files : 1 + n_lines : 20 + files_per_second : 100.09077675695 + lines_per_second : 2001.815535139 + report_file : elixir.ex.yaml +Elixir : + nFiles: 1 + blank: 3 + comment: 10 + code: 7 +SUM: + blank: 3 + comment: 10 + code: 7 + nFiles: 1