| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2019-2021 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | FunctionalPerl::Htmlgen::Sourcelang -- detect programming language | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use FunctionalPerl::Htmlgen::Sourcelang; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | is sourcelang("use Foo;"), "Perl"; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Detect if a piece of code is Perl, or more likely some other language. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 NOTE | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||
| 26 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | package FunctionalPerl::Htmlgen::Sourcelang; | ||||||||||||||||||||||||||||
| 31 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 3 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 3 | ▶ | use experimental "signatures"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | use Exporter "import"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | our @EXPORT = qw(sourcelang); | |||||||||||||||||||||||||||
| 38 | 3 | our @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 39 | 3 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | use FP::Docstring; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 16 | ▶ | sub sourcelang { | ||||||||||||||||||||||||||
|
Subroutine
sourcelang: called CC=29 SCAR=34.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 16 | __ '($codestr) -> $langname ' | |||||||||||||||||||||||||||
| 45 | . '-- langname is either "perl", or some other string'; | ||||||||||||||||||||||||||||
| 46 | 16 | my ($str) = @_; | |||||||||||||||||||||||||||
| 47 | 16 | my $perl = 0; | |||||||||||||||||||||||||||
| 48 | 16 | my $sh = 0; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 16 | ▶ | $perl++ if $str =~ /(?:^|\n)\s*use\s+\w+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 16 | ▶ | $perl++ if $str =~ /\w+::\w+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 16 | ▶ | $perl++ if $str =~ /\$\w+\s*->\s*\w+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 16 | ▶ | $perl++ if $str =~ /\bmy\s+\$\w+\s*(?: = \s*[^;]*)?;/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 16 | ▶ | $perl++ if $str =~ /\bmy\s+\(\$\w+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 16 | ▶ | $perl++ if $str =~ /\bsub\s*\{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 16 | ▶ | $perl += 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | if $str | ||||||||||||||||||||||||||||
| 58 | =~ /\b(?:func?|sub)\b\s*(?:\w+\s*)?\((?:(?:\s*\$\w+\s*,)*\s*\$\w+\s*)?\)\s*\{/s; | ||||||||||||||||||||||||||||
| 59 | 16 | ▶ | $perl += 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | if $str | ||||||||||||||||||||||||||||
| 61 | =~ /sub maybe_/; # hack, should properly fix the regex above for HEAD^ | ||||||||||||||||||||||||||||
| 62 | 16 | ▶ | $perl += 0.5 if $str =~ /\@\{\s*/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 16 | ▶ | $perl += 0.5 if $str =~ /\bcompose\s*\(/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 16 | ▶ | $perl += 0.5 if $str =~ /\\\&\w+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 16 | ▶ | $perl += 0.5 if $str =~ /->/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 16 | ▶ | $perl += 0.5 if $str =~ /\(\s*\*\w+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 16 | ▶ | $perl += 0.5 if $str =~ /\(.*?,.*?\)/; # (1,3,4) or ([1,3,4]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 16 | ▶ | $perl += 0.5 if $str =~ /\(\s*\[.*?\].*?\)/; # ([1,3,4]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 16 | ▶ | do { $perl += 0.5; $sh += 0.5 } if $str =~ /\$\w+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 16 | ▶ | $perl += 1 if $str =~ /(?:^|\n|;)\s*push\s+\@\w+\s*,\s*/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 16 | ▶ | $perl += 1 if $str =~ /\$VAR\d+\b/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 16 | ▶ | $perl += 1 if $str =~ /(?:perlrepl|fperl)(?: *\d+)?>.*\bF\b/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 16 | ▶ | $perl += 1 if $str =~ /\blazy\s*\{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 16 | ▶ | $perl += 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | if $str =~ /\bnot \$/; # shell doesn't have "not"; except if custom | ||||||||||||||||||||||||||||
| 76 | 16 | ▶ | $perl += 1 if $str =~ /\}\s*elsif\s*\{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 16 | ▶ | $perl += 1 if $str =~ /\bexists\s*\$\w+\s*\{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 16 | ▶ | $perl += 1 if $str =~ /\bcons\s*\$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 16 | ▶ | $sh += 2 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | if $str =~ m{(?:^|\n)\s*(?:[#\$]\s*)?(?:git |gpg |ls |chmod |cd |\./)}; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Want repl sessions to be non highlighted? Do I ? | ||||||||||||||||||||||||||||
| 84 | 16 | ▶ | $sh += 10 if $str =~ m{(?:^|\n) *main> }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 16 | ▶ | ($perl >= 1 and $perl > $sh) ? "Perl" : "shell" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$perl >= 1 and $perl > $sh
MC/DC100%2/2
$perl >= 1 and $perl > $sh
$perl >= 1
$perl > $sh
Truth table100%3/3
A and B
A= $perl >= 1B= $perl > $sh
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | ▶ | use Chj::TEST; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 3 | ▶ | use FP::List; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 3 | ▶ | use FP::Either ":all"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | ▶ | sub test ($lang, $l) { | ||||||||||||||||||||||||||
|
Subroutine
test: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | lefts $l->map( | ||||||||||||||||||||||||||||
| 95 | 15 | ▶ | sub ($c) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 96 | 15 | my $l = sourcelang $c; | |||||||||||||||||||||||||||
| 97 | 15 | ▶ | $l eq $lang ? Right undef : Left [$c, $l] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | ) | ||||||||||||||||||||||||||||
| 100 | 2 | } | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | ▶ | TEST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 1 | test "Perl", list( | |||||||||||||||||||||||||||
| 104 | 'Foo::bar;', 'use Foo;', 'my $a;', 'my $abc = 2+ 2;', | ||||||||||||||||||||||||||||
| 105 | 'fun inverse ($x) { 1 / $x }', 'sub inverse ($x) { 1 / $x }', | ||||||||||||||||||||||||||||
| 106 | 'PFLANZE::Node::constructors->import;', q{ | ||||||||||||||||||||||||||||
| 107 | sub maybe_representable ($N, $D, $prefer_large = 1, | ||||||||||||||||||||||||||||
| 108 | $maybe_choose = $MAYBE_CHOOSE) | ||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | __ 'Returns the numbers containing $D that sum up to $N, or undef. | ||||||||||||||||||||||||||||
| 111 | If $prefer_large is true, tries to use large numbers, | ||||||||||||||||||||||||||||
| 112 | otherwise small (which is (much) less efficient).'; | ||||||||||||||||||||||||||||
| 113 | ... | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | }, ' | ||||||||||||||||||||||||||||
| 116 | if (not $missing) { | ||||||||||||||||||||||||||||
| 117 | $chosen | ||||||||||||||||||||||||||||
| 118 | } elsif ($missing < 0) { | ||||||||||||||||||||||||||||
| 119 | undef | ||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||
| 121 | if (exists $ns{$missing}) { | ||||||||||||||||||||||||||||
| 122 | ', ' | ||||||||||||||||||||||||||||
| 123 | cons $missing, $chosen | ||||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | ', | ||||||||||||||||||||||||||||
| 126 | ) | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 3 | null; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | TEST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 1 | test "shell", list( | |||||||||||||||||||||||||||
| 132 | 'Foo;', 'my $a', 'tar -xzf foo.tgz', q{ | ||||||||||||||||||||||||||||
| 133 | $ ./113-1-represent_integer --repl | ||||||||||||||||||||||||||||
| 134 | main> docstring \&maybe_representable | ||||||||||||||||||||||||||||
| 135 | $VAR1 = 'Returns the numbers containing $D that sum up to $N, or undef. | ||||||||||||||||||||||||||||
| 136 | If $prefer_large is true, tries to use large numbers, | ||||||||||||||||||||||||||||
| 137 | otherwise small (which is (much) less efficient).'; | ||||||||||||||||||||||||||||
| 138 | main> | ||||||||||||||||||||||||||||
| 139 | }, q{ | ||||||||||||||||||||||||||||
| 140 | main> \&maybe_representable | ||||||||||||||||||||||||||||
| 141 | $VAR1 = sub { 'DUMMY: main::maybe_representable at "./113-1-represent_integer" line 221'; __ 'Returns the numbers containing $D that sum up to $N, or undef. | ||||||||||||||||||||||||||||
| 142 | If $prefer_large is true, tries to use large numbers, | ||||||||||||||||||||||||||||
| 143 | otherwise small (which is (much) less efficient).' }; | ||||||||||||||||||||||||||||
| 144 | main> | ||||||||||||||||||||||||||||
| 145 | }, | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | ) | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 3 | null; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 3 | 1 | |||||||||||||||||||||||||||