Skip to content

Commit 9669151

Browse files
committed
add support for Cangjie, #895, #896
1 parent 1f6e855 commit 9669151

8 files changed

+435
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,7 @@ file with the `--read-lang-def` or `--force-lang-def` options.
14321432

14331433
These file extensions map to multiple languages:
14341434

1435+
* `cj` files could be Clojure or Cangjie
14351436
* `cl` files could be Lisp or OpenCL
14361437
* `cls` files could be Visual Basic, TeX or Apex Class
14371438
* `cs` files could be C# or Smalltalk

Unix/cloc

+54
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ my %Extension_Collision = (
822822
'Visual Basic/TeX/Apex Class' => [ 'cls' ] ,
823823
'Scheme/SaltStack' => [ 'sls' ] ,
824824
'SKILL/.NET IL' => [ 'il' ] ,
825+
'Clojure/Cangjie' => [ 'cj' ] ,
825826
);
826827
my @Autogen_to_ignore = no_autogen_files($list_no_autogen);
827828
if ($opt_force_lang_def) {
@@ -5466,6 +5467,11 @@ sub print_language_info { # {{{1
54665467
push @{$extensions{'SaltStack'}} , "sls";
54675468
delete $extensions{'Scheme/SaltStack'};
54685469
}
5470+
if (!$language or $language =~ /^(Clojure|Cangjie)$/i) {
5471+
push @{$extensions{'Clojure'}} , "cj";
5472+
push @{$extensions{'Cangjie'}} , "cj";
5473+
delete $extensions{'Clojure/Cangjie'};
5474+
}
54695475
if ($opt_explain) {
54705476
return unless $extensions{$language};
54715477
if ($prefix) {
@@ -6837,6 +6843,8 @@ sub classify_file { # {{{1
68376843
return Pascal_or_Pawn($full_file, $rh_Err, $raa_errors);
68386844
} elsif ($Language_by_Extension{$extension} eq 'SKILL/.NET IL') {
68396845
return SKILL_or_DotNetIL($full_file, $rh_Err, $raa_errors);
6846+
} elsif ($Language_by_Extension{$extension} eq 'Clojure/Cangjie') {
6847+
return Clojure_or_Cangjie($full_file, $rh_Err, $raa_errors);
68406848
} elsif ($Language_by_Extension{$extension} eq 'Brainfuck') {
68416849
if (really_is_bf($full_file)) {
68426850
return $Language_by_Extension{$extension};
@@ -8789,6 +8797,7 @@ sub set_constants { # {{{1
87898797
'cljs.hl' => 'Clojure' ,
87908798
'cl2' => 'Clojure' ,
87918799
'boot' => 'Clojure' ,
8800+
'cj' => 'Clojure/Cangjie' ,
87928801
'clj' => 'Clojure' ,
87938802
'cljs' => 'ClojureScript' ,
87948803
'cljc' => 'ClojureC' ,
@@ -9917,6 +9926,11 @@ sub set_constants { # {{{1
99179926
[ 'rm_comments_in_strings', '"', '//', '' ],
99189927
[ 'call_regexp_common' , 'C++' ],
99199928
],
9929+
'Cangjie' => [
9930+
[ 'rm_comments_in_strings', '"', '/*', '*/' ],
9931+
[ 'rm_comments_in_strings', '"', '//', '' ],
9932+
[ 'call_regexp_common' , 'C++' ],
9933+
],
99209934
'Carbon' => [
99219935
[ 'rm_comments_in_strings', '"', '/*', '*/' ],
99229936
[ 'rm_comments_in_strings', '"', '//', '' ],
@@ -9938,6 +9952,7 @@ sub set_constants { # {{{1
99389952
],
99399953
'ClojureScript' => [ [ 'remove_matches' , '^\s*;' ], ],
99409954
'ClojureC' => [ [ 'remove_matches' , '^\s*;' ], ],
9955+
'Clojure/Cangjie' => [ [ 'die' , ], ], # never called
99419956
'CMake' => [
99429957
[ 'remove_matches' , '^\s*#' ],
99439958
[ 'remove_inline' , '#.*$' ],
@@ -11718,6 +11733,7 @@ sub set_constants { # {{{1
1171811733
'Cake Build Script' => 1.36,
1171911734
'C++' => 1.51,
1172011735
'Cadence' => 3.00,
11736+
'Cangjie' => 3.00,
1172111737
'Carbon' => 1.51,
1172211738
'CCS' => 5.33,
1172311739
'Civet' => 3.00,
@@ -12093,6 +12109,7 @@ sub set_constants { # {{{1
1209312109
'MATLAB/Mathematica/Objective-C/MUMPS/Mercury' => 1.00,
1209412110
'IDL/Qt Project/Prolog/ProGuard' => 1.00,
1209512111
'SKILL/.NET IL' => 1.00,
12112+
'Clojure/Cangjie' => 1.00,
1209612113
);
1209712114
# 1}}}
1209812115
%{$rh_Known_Binary_Archives} = ( # {{{1
@@ -13136,6 +13153,43 @@ sub SKILL_or_DotNetIL { # {{{1
1313613153
return ".NET IL";
1313713154
}
1313813155
} # 1}}}
13156+
sub Clojure_or_Cangjie { # {{{1
13157+
my ($file , # in
13158+
$rh_Err , # in hash of error codes
13159+
$raa_errors , # out
13160+
) = @_;
13161+
13162+
print "-> Clojure_or_Cangjie($file)\n" if $opt_v > 2;
13163+
13164+
my $lang = undef;
13165+
my $IN = open_file('<', $file, 1);
13166+
if (!defined $IN) {
13167+
push @{$raa_errors}, [$rh_Err->{'Unable to read'} , $file];
13168+
return $lang;
13169+
}
13170+
my $clojure = 0;
13171+
my $cangjie = 0;
13172+
while (<$IN>) {
13173+
next if /^\s*$/;
13174+
if (/^\s*(;|\(|\[)/) {
13175+
$clojure += 50;
13176+
} elsif (/^(import|func|main)/) {
13177+
$cangjie += 50;
13178+
} elsif (/{\s*$/ or /^\s*}$/) {
13179+
$cangjie += 5;
13180+
} elsif (/^\s*(procedure|let|foreach)\b/) {
13181+
$clojure += 1;
13182+
}
13183+
}
13184+
$IN->close;
13185+
13186+
print "<- Clojure_or_Cangjie($file: clojure=$clojure, .NET IL=$cangjie\n" if $opt_v > 2;
13187+
if ($clojure > $cangjie) {
13188+
return "Clojure";
13189+
} else {
13190+
return "Cangjie";
13191+
}
13192+
} # 1}}}
1313913193
sub html_colored_text { # {{{1
1314013194
# http://www.pagetutor.com/pagetutor/makapage/pics/net216-2.gif
1314113195
my ($color, $text) = @_;

Unix/t/00_C.t

+10
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ my @Tests = (
167167
'ref' => '../tests/outputs/build.cake.yaml',
168168
'args' => '../tests/inputs/build.cake',
169169
},
170+
{
171+
'name' => 'Cangjie',
172+
'ref' => '../tests/outputs/functional.cj.yaml',
173+
'args' => '../tests/inputs/functional.cj',
174+
},
175+
{
176+
'name' => 'Clojure',
177+
'ref' => '../tests/outputs/ch10-REPL-oriented-repl-interactions.cj.yaml',
178+
'args' => '../tests/inputs/ch10-REPL-oriented-repl-interactions.cj',
179+
},
170180
{
171181
'name' => 'Circom',
172182
'ref' => '../tests/outputs/eddsa.circom.yaml',

cloc

+54
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ my %Extension_Collision = (
814814
'Visual Basic/TeX/Apex Class' => [ 'cls' ] ,
815815
'Scheme/SaltStack' => [ 'sls' ] ,
816816
'SKILL/.NET IL' => [ 'il' ] ,
817+
'Clojure/Cangjie' => [ 'cj' ] ,
817818
);
818819
my @Autogen_to_ignore = no_autogen_files($list_no_autogen);
819820
if ($opt_force_lang_def) {
@@ -5458,6 +5459,11 @@ sub print_language_info { # {{{1
54585459
push @{$extensions{'SaltStack'}} , "sls";
54595460
delete $extensions{'Scheme/SaltStack'};
54605461
}
5462+
if (!$language or $language =~ /^(Clojure|Cangjie)$/i) {
5463+
push @{$extensions{'Clojure'}} , "cj";
5464+
push @{$extensions{'Cangjie'}} , "cj";
5465+
delete $extensions{'Clojure/Cangjie'};
5466+
}
54615467
if ($opt_explain) {
54625468
return unless $extensions{$language};
54635469
if ($prefix) {
@@ -6829,6 +6835,8 @@ sub classify_file { # {{{1
68296835
return Pascal_or_Pawn($full_file, $rh_Err, $raa_errors);
68306836
} elsif ($Language_by_Extension{$extension} eq 'SKILL/.NET IL') {
68316837
return SKILL_or_DotNetIL($full_file, $rh_Err, $raa_errors);
6838+
} elsif ($Language_by_Extension{$extension} eq 'Clojure/Cangjie') {
6839+
return Clojure_or_Cangjie($full_file, $rh_Err, $raa_errors);
68326840
} elsif ($Language_by_Extension{$extension} eq 'Brainfuck') {
68336841
if (really_is_bf($full_file)) {
68346842
return $Language_by_Extension{$extension};
@@ -8781,6 +8789,7 @@ sub set_constants { # {{{1
87818789
'cljs.hl' => 'Clojure' ,
87828790
'cl2' => 'Clojure' ,
87838791
'boot' => 'Clojure' ,
8792+
'cj' => 'Clojure/Cangjie' ,
87848793
'clj' => 'Clojure' ,
87858794
'cljs' => 'ClojureScript' ,
87868795
'cljc' => 'ClojureC' ,
@@ -9909,6 +9918,11 @@ sub set_constants { # {{{1
99099918
[ 'rm_comments_in_strings', '"', '//', '' ],
99109919
[ 'call_regexp_common' , 'C++' ],
99119920
],
9921+
'Cangjie' => [
9922+
[ 'rm_comments_in_strings', '"', '/*', '*/' ],
9923+
[ 'rm_comments_in_strings', '"', '//', '' ],
9924+
[ 'call_regexp_common' , 'C++' ],
9925+
],
99129926
'Carbon' => [
99139927
[ 'rm_comments_in_strings', '"', '/*', '*/' ],
99149928
[ 'rm_comments_in_strings', '"', '//', '' ],
@@ -9930,6 +9944,7 @@ sub set_constants { # {{{1
99309944
],
99319945
'ClojureScript' => [ [ 'remove_matches' , '^\s*;' ], ],
99329946
'ClojureC' => [ [ 'remove_matches' , '^\s*;' ], ],
9947+
'Clojure/Cangjie' => [ [ 'die' , ], ], # never called
99339948
'CMake' => [
99349949
[ 'remove_matches' , '^\s*#' ],
99359950
[ 'remove_inline' , '#.*$' ],
@@ -11710,6 +11725,7 @@ sub set_constants { # {{{1
1171011725
'Cake Build Script' => 1.36,
1171111726
'C++' => 1.51,
1171211727
'Cadence' => 3.00,
11728+
'Cangjie' => 3.00,
1171311729
'Carbon' => 1.51,
1171411730
'CCS' => 5.33,
1171511731
'Civet' => 3.00,
@@ -12085,6 +12101,7 @@ sub set_constants { # {{{1
1208512101
'MATLAB/Mathematica/Objective-C/MUMPS/Mercury' => 1.00,
1208612102
'IDL/Qt Project/Prolog/ProGuard' => 1.00,
1208712103
'SKILL/.NET IL' => 1.00,
12104+
'Clojure/Cangjie' => 1.00,
1208812105
);
1208912106
# 1}}}
1209012107
%{$rh_Known_Binary_Archives} = ( # {{{1
@@ -13128,6 +13145,43 @@ sub SKILL_or_DotNetIL { # {{{1
1312813145
return ".NET IL";
1312913146
}
1313013147
} # 1}}}
13148+
sub Clojure_or_Cangjie { # {{{1
13149+
my ($file , # in
13150+
$rh_Err , # in hash of error codes
13151+
$raa_errors , # out
13152+
) = @_;
13153+
13154+
print "-> Clojure_or_Cangjie($file)\n" if $opt_v > 2;
13155+
13156+
my $lang = undef;
13157+
my $IN = open_file('<', $file, 1);
13158+
if (!defined $IN) {
13159+
push @{$raa_errors}, [$rh_Err->{'Unable to read'} , $file];
13160+
return $lang;
13161+
}
13162+
my $clojure = 0;
13163+
my $cangjie = 0;
13164+
while (<$IN>) {
13165+
next if /^\s*$/;
13166+
if (/^\s*(;|\(|\[)/) {
13167+
$clojure += 50;
13168+
} elsif (/^(import|func|main)/) {
13169+
$cangjie += 50;
13170+
} elsif (/{\s*$/ or /^\s*}$/) {
13171+
$cangjie += 5;
13172+
} elsif (/^\s*(procedure|let|foreach)\b/) {
13173+
$clojure += 1;
13174+
}
13175+
}
13176+
$IN->close;
13177+
13178+
print "<- Clojure_or_Cangjie($file: clojure=$clojure, .NET IL=$cangjie\n" if $opt_v > 2;
13179+
if ($clojure > $cangjie) {
13180+
return "Clojure";
13181+
} else {
13182+
return "Cangjie";
13183+
}
13184+
} # 1}}}
1313113185
sub html_colored_text { # {{{1
1313213186
# http://www.pagetutor.com/pagetutor/makapage/pics/net216-2.gif
1313313187
my ($color, $text) = @_;

0 commit comments

Comments
 (0)