tools/ucd/enumerated_property.mbt1///|2/// Write the trie of an enumerated property whose ordinals are3/// `values`, looked up by `lookup_<property in lowercase>`. The ordinals4/// number the constructors of the enum `enum_name`, which has `value_count`5/// of them.6fn write_enumerated_trie(7 out : @emitter.Emitter,8 property~ : String,9 prefix~ : String,10 enum_name~ : String,11 value_count~ : Int32,12 value_bits~ : Int32,13 values : FixedArray[Int32],14) -> Unit raise {15 // The enum converter casts ordinals with `%identity`.16 guard values.all(value => value >= 0 && value < value_count) else {17 fail("\{property} has an ordinal outside \{enum_name}")18 }19 out.write_value_trie(20 property~,21 prefix~,22 lookup="pub fn lookup_\{property.to_lower()}",23 lookup_doc=[24 "Look up \{property} ordinal for a character",25 "Returns ordinal value (0-\{value_count - 1}) corresponding to \{enum_name} enum",26 ],27 value_bits~,28 values,29 )30}3132///|33/// Emit the trie of a small enumerated property from its UCD file `content`,34/// whose name is `source`. `aliases` lists the short and long name of every35/// value in the order of the enum `enum_name`; the file writes short names,36/// and its `@missing` lines may spell out long ones.37fn emit_small_enumerated_property(38 content : String,39 property~ : String,40 source~ : String,41 prefix~ : String,42 enum_name~ : String,43 default~ : String,44 aliases : Array[(String, String)],45) -> String raise {46 let values = @parsing.parse_enumerated_ordinals(47 content,48 property~,49 default~,50 name => aliases.search_by(entry => name == entry.0 || name == entry.1),51 )52 let out = @emitter.Emitter::new()53 out.write_file_header("\{property} lookup data", source)54 write_enumerated_trie(55 out,56 property~,57 prefix~,58 enum_name~,59 value_count=aliases.length(),60 value_bits=3,61 values,62 )63 out.finish()64}6566///|67pub fn emit_east_asian_width(content : String) -> String raise {68 emit_small_enumerated_property(69 content,70 property="East_Asian_Width",71 source="EastAsianWidth.txt",72 prefix="eaw",73 enum_name="EastAsianWidth",74 default="N",75 [76 ("A", "Ambiguous"),77 ("F", "Fullwidth"),78 ("H", "Halfwidth"),79 ("N", "Neutral"),80 ("Na", "Narrow"),81 ("W", "Wide"),82 ],83 )84}8586///|87pub fn emit_joining_type(content : String) -> String raise {88 emit_small_enumerated_property(89 content,90 property="Joining_Type",91 source="DerivedJoiningType.txt",92 prefix="joining_type",93 enum_name="JoiningType",94 default="U",95 [96 ("U", "Non_Joining"),97 ("C", "Join_Causing"),98 ("D", "Dual_Joining"),99 ("L", "Left_Joining"),100 ("R", "Right_Joining"),101 ("T", "Transparent"),102 ],103 )104}105106///|107pub fn emit_hangul_syllable_type(content : String) -> String raise {108 emit_small_enumerated_property(109 content,110 property="Hangul_Syllable_Type",111 source="HangulSyllableType.txt",112 prefix="hangul_syllable_type",113 enum_name="HangulSyllableType",114 default="NA",115 [116 ("NA", "Not_Applicable"),117 ("L", "Leading_Jamo"),118 ("V", "Vowel_Jamo"),119 ("T", "Trailing_Jamo"),120 ("LV", "LV_Syllable"),121 ("LVT", "LVT_Syllable"),122 ],123 )124}125
///|
/// Write the trie of an enumerated property whose ordinals are
/// `values`, looked up by `lookup_<property in lowercase>`. The ordinals
/// number the constructors of the enum `enum_name`, which has `value_count`
/// of them.
fn write_enumerated_trie(
out : @emitter.Emitter,
property~ : String,
prefix~ : String,
enum_name~ : String,
value_count~ : Int32,
value_bits~ : Int32,
values : FixedArray[Int32],
) -> Unit raise {
// The enum converter casts ordinals with `%identity`.
guard values.all(value => value >= 0 && value < value_count) else {
fail("\{property} has an ordinal outside \{enum_name}")
}
out.write_value_trie(
property~,
prefix~,
lookup="pub fn lookup_\{property.to_lower()}",
lookup_doc=[
"Look up \{property} ordinal for a character",
"Returns ordinal value (0-\{value_count - 1}) corresponding to \{enum_name} enum",
],
value_bits~,
values,
)
}
///|
/// Emit the trie of a small enumerated property from its UCD file `content`,
/// whose name is `source`. `aliases` lists the short and long name of every
/// value in the order of the enum `enum_name`; the file writes short names,
/// and its `@missing` lines may spell out long ones.
fn emit_small_enumerated_property(
content : String,
property~ : String,
source~ : String,
prefix~ : String,
enum_name~ : String,
default~ : String,
aliases : Array[(String, String)],
) -> String raise {
let values = @parsing.parse_enumerated_ordinals(
content,
property~,
default~,
name => aliases.search_by(entry => name == entry.0 || name == entry.1),
)
let out = @emitter.Emitter::new()
out.write_file_header("\{property} lookup data", source)
write_enumerated_trie(
out,
property~,
prefix~,
enum_name~,
value_count=aliases.length(),
value_bits=3,
values,
)
out.finish()
}
///|
pub fn emit_east_asian_width(content : String) -> String raise {
emit_small_enumerated_property(
content,
property="East_Asian_Width",
source="EastAsianWidth.txt",
prefix="eaw",
enum_name="EastAsianWidth",
default="N",
[
("A", "Ambiguous"),
("F", "Fullwidth"),
("H", "Halfwidth"),
("N", "Neutral"),
("Na", "Narrow"),
("W", "Wide"),
],
)
}
///|
pub fn emit_joining_type(content : String) -> String raise {
emit_small_enumerated_property(
content,
property="Joining_Type",
source="DerivedJoiningType.txt",
prefix="joining_type",
enum_name="JoiningType",
default="U",
[
("U", "Non_Joining"),
("C", "Join_Causing"),
("D", "Dual_Joining"),
("L", "Left_Joining"),
("R", "Right_Joining"),
("T", "Transparent"),
],
)
}
///|
pub fn emit_hangul_syllable_type(content : String) -> String raise {
emit_small_enumerated_property(
content,
property="Hangul_Syllable_Type",
source="HangulSyllableType.txt",
prefix="hangul_syllable_type",
enum_name="HangulSyllableType",
default="NA",
[
("NA", "Not_Applicable"),
("L", "Leading_Jamo"),
("V", "Vowel_Jamo"),
("T", "Trailing_Jamo"),
("LV", "LV_Syllable"),
("LVT", "LVT_Syllable"),
],
)
}