unicode_gc_cat -> unicode_General_Category
[disorder] / scripts / make-unidata
index 8f58c08..81f347d 100755 (executable)
@@ -44,6 +44,7 @@
 #  - ...
 #
 use strict;
+use File::Basename;
 
 sub out {
     print @_ or die "$!\n";
@@ -73,19 +74,19 @@ my $minld = 0;                      # max/min lower case offset
 # Make sure we have our desired input files.  We explicitly specify a
 # Unicode standard version to make sure that a given version of DisOrder
 # supports a given version of Unicode.
-sub need_input {
-    my $f = shift;
-    if(!-e $f) {
-       system("wget http://www.unicode.org/Public/5.0.0/ucd/$f");
-       chmod(0444, $f);
+sub input {
+    my $path = shift;
+    my $lpath = basename($path);
+    if(!-e $lpath) {
+       system("wget http://www.unicode.org/Public/5.0.0/ucd/$path");
+       chmod(0444, $lpath) or die "$lpath: $!\n";
     }
+    open(STDIN, "<$lpath") or die "$lpath: $!\n";
 }
 
-need_input("UnicodeData.txt");
-need_input("CaseFolding.txt");
 
 # Read the main data file
-open(STDIN, "<UnicodeData.txt") or die "UnicodeData.txt: $!\n";
+input("UnicodeData.txt");
 while(<>) {
     my @f = split(/;/, $_);
     my $c = hex($f[0]);                # codepoint
@@ -127,6 +128,66 @@ while(<>) {
     $max = $c if $c > $max;
 }
 
+sub read_prop_with_ranges {
+    my $path = shift;
+    my $propkey = shift;
+    input($path);
+    while(<>) {
+       chomp;
+       s/\s*\#.*//;
+       next if $_ eq '';
+       my ($range, $propval) = split(/\s*;\s*/, $_);
+       if($range =~ /(.*)\.\.(.*)/) {
+           for my $c (hex($1) .. hex($2)) {
+               if(exists $data{$c}) {
+                   $data{$c}->{$propkey} = $propval;
+               }
+           }
+       } else {
+           my $c = hex($range);
+           if(exists $data{$c}) {
+               $data{$c}->{$propkey} = $propval;
+           }
+       }
+    }
+}
+
+# Grapheme_Break etc
+# NB we do this BEFORE filling in blanks so that the Hangul characters
+# don't get filled in; we can compute their properties mechanically.
+read_prop_with_ranges("auxiliary/GraphemeBreakProperty.txt", "gbreak");
+read_prop_with_ranges("auxiliary/WordBreakProperty.txt", "wbreak");
+read_prop_with_ranges("auxiliary/SentenceBreakProperty.txt", "sbreak");
+
+# Compute the full list and fill in the Extend category properly
+my %gbreak = ();
+my %wbreak = ();
+my %sbreak = ();
+for my $c (keys %data) {
+    if(!exists $data{$c}->{gbreak}) {
+       $data{$c}->{gbreak} = 'Other';
+    }
+    $gbreak{$data{$c}->{gbreak}} = 1;
+
+    if(!exists $data{$c}->{wbreak}) {
+       if($data{$c}->{gbreak} eq 'Extend') {
+           $data{$c}->{wbreak} = 'Extend';
+       } else {
+           $data{$c}->{wbreak} = 'Other';
+       }
+    }
+    $wbreak{$data{$c}->{wbreak}} = 1;
+
+    if(!exists $data{$c}->{sbreak}) {
+       if($data{$c}->{gbreak} eq 'Extend') {
+           $data{$c}->{sbreak} = 'Extend';
+       } else {
+           $data{$c}->{sbreak} = 'Other';
+       }
+    }
+    $sbreak{$data{$c}->{sbreak}} = 1;
+}
+
 # Round up the maximum value to a whole number of subtables
 $max += ($modulus - 1) - ($max % $modulus);
 
@@ -137,14 +198,17 @@ for(my $c = 0; $c <= $max; ++$c) {
            "gc" => "Cn",       # not assigned
            "ccc" => 0,
            "ud" => 0,
-           "ld" => 0
+           "ld" => 0,
+           "wbreak" => 'Other',
+           "gbreak" => 'Other',
+           "sbreak" => 'Other',
            };
     }
 }
 $cats{'Cn'} = 1;
 
 # Read the casefolding data too
-open(STDIN, "<CaseFolding.txt") or die "CaseFolding.txt: $!\n";
+input("CaseFolding.txt");
 while(<>) {
     chomp;
     next if /^\#/ or $_ eq '';
@@ -188,9 +252,27 @@ out("/* Automatically generated file, see scripts/make-unidata */\n",
     "#define UNIDATA_H\n");
 
 # TODO choose stable values for General_Category
-out("enum unicode_gc_cat {\n",
+out("enum unicode_General_Category {\n",
+    join(",\n",
+        map("  unicode_General_Category_$_", sort keys %cats)), "\n};\n");
+
+out("enum unicode_Grapheme_Break {\n",
+    join(",\n",
+        map("  unicode_Grapheme_Break_$_", sort keys %gbreak)),
+    "\n};\n");
+out("extern const char *const unicode_Grapheme_Break_names[];\n");
+
+out("enum unicode_Word_Break {\n",
     join(",\n",
-        map("  unicode_gc_$_", sort keys %cats)), "\n};\n");
+        map("  unicode_Word_Break_$_", sort keys %wbreak)),
+    "\n};\n");
+out("extern const char *const unicode_Word_Break_names[];\n");
+
+out("enum unicode_Sentence_Break {\n",
+    join(",\n",
+        map("  unicode_Sentence_Break_$_", sort keys %sbreak)),
+    "\n};\n");
+out("extern const char *const unicode_Sentence_Break_names[];\n");
 
 out("enum unicode_flags {\n",
     "  unicode_normalize_before_casefold = 1\n",
@@ -220,8 +302,11 @@ out("struct unidata {\n",
     "  ".choosetype($minud, $maxud)." upper_offset;\n",
     "  ".choosetype($minld, $maxld)." lower_offset;\n",
     "  ".choosetype(0, $maxccc)." ccc;\n",
-    "  char gc;\n",
+    "  char general_category;\n",
     "  uint8_t flags;\n",
+    "  char grapheme_break;\n",
+    "  char word_break;\n",
+    "  char sentence_break;\n",
     "};\n");
 # compat, canon and casefold do have have non-BMP characters, so we
 # can't use a simple 16-bit table.  We could use UTF-8 or UTF-16
@@ -244,9 +329,30 @@ out("/* Automatically generated file, see scripts/make-unidata */\n",
     "#include \"types.h\"\n",
     "#include \"unidata.h\"\n");
 
-# Short aliases for general category codes
+# Short aliases to keep .c file small
 
-out(map(sprintf("#define %s unicode_gc_%s\n", $_, $_), sort keys %cats));
+out(map(sprintf("#define %s unicode_General_Category_%s\n", $_, $_),
+       sort keys %cats));
+out(map(sprintf("#define GB%s unicode_Grapheme_Break_%s\n", $_, $_),
+       sort keys %gbreak));
+out(map(sprintf("#define WB%s unicode_Word_Break_%s\n", $_, $_),
+       sort keys %wbreak));
+out(map(sprintf("#define SB%s unicode_Sentence_Break_%s\n", $_, $_),
+       sort keys %sbreak));
+
+# Names for *_Break properties
+out("const char *const unicode_Grapheme_Break_names[] = {\n",
+    join(",\n",
+        map("  \"$_\"", sort keys %gbreak)),
+    "\n};\n");
+out("const char *const unicode_Word_Break_names[] = {\n",
+    join(",\n",
+        map("  \"$_\"", sort keys %wbreak)),
+    "\n};\n");
+out("const char *const unicode_Sentence_Break_names[] = {\n",
+    join(",\n",
+        map("  \"$_\"", sort keys %sbreak)),
+    "\n};\n");
 
 # Generate the decomposition mapping tables.  We look out for duplicates
 # in order to save space and report this as decompsaved at the end.  In
@@ -317,19 +423,24 @@ for(my $base = 0; $base <= $max; $base += $modulus) {
        my $canonsym = ($data{$c}->{canonsym} or "0");
        my $compatsym = ($data{$c}->{compatsym} or "0");
        my $cfsym = ($data{$c}->{cfsym} or "0");
-       my $flags = ($data{$c}->{ypogegrammeni}
-                    ? "unicode_normalize_before_casefold"
-                    : 0);
+       my @flags = ();
+       if($data{$c}->{ypogegrammeni}) {
+           push(@flags, "unicode_normalize_before_casefold");
+       }
+       my $flags = @flags ? join("|", @flags) : 0;
        push(@t, "{".
             join(",",
                  $compatsym,
                  $canonsym,
                  $cfsym,
-                 "$d->{ud}",
-                 "$d->{ld}",
-                 "$d->{ccc}",
-                 "$d->{gc}",
+                 $d->{ud},
+                 $d->{ld},
+                 $d->{ccc},
+                 $d->{gc},
                  $flags,
+                 "GB$d->{gbreak}",
+                 "WB$d->{wbreak}",
+                 "SB$d->{sbreak}",
             )."}");
     }
     my $t = join(",\n", @t);