tools/emitter/emitter.mbt1///|2/// Largest Unicode scalar value.3pub const MaxCodePoint : Int32 = 0x10FFFF45///|6/// Builds the text of one generated source file.7struct Emitter {8 out : StringBuilder9}1011///|12pub fn Emitter::new() -> Emitter {13 { out: StringBuilder(), }14}1516///|17pub fn Emitter::write(self : Emitter, source : String) -> Unit {18 self.out.write_string(source)19}2021///|22pub fn Emitter::write_line(self : Emitter, source : String) -> Unit {23 self.out.write_string(source)24 self.out.write_char('\n')25}2627///|28pub fn Emitter::finish(self : Emitter) -> String {29 self.out.to_string()30}3132///|33/// Uppercase hexadecimal representation of `value`, zero-padded to at least34/// `min_width` digits.35pub fn hex_upper(value : Int32, min_width~ : Int32 = 0) -> String {36 let digits = value.to_string(radix=16).to_upper()37 if digits.length() >= min_width {38 digits39 } else {40 "0".repeat(min_width - digits.length()) + digits41 }42}4344///|45/// `0x`-prefixed uppercase hexadecimal, as a MoonBit literal.46pub fn hex_literal(value : Int32, min_width~ : Int32 = 0) -> String {47 "0x" + hex_upper(value, min_width~)48}4950///|51/// A MoonBit string literal holding the given code points as `\u{...}`52/// escapes.53pub fn string_literal(code_points : ArrayView[Int32]) -> String {54 let literal = StringBuilder()55 literal.write_char('"')56 for code_point in code_points {57 literal.write_string("\\u{\{hex_upper(code_point, min_width=4)}}")58 }59 literal.write_char('"')60 literal.to_string()61}6263///|64pub fn Emitter::write_file_header(65 self : Emitter,66 title : String,67 generated_from : String,68) -> Unit {69 self.write("///|\n")70 self.write("/// \{title}\n")71 self.write("/// Generated from \{generated_from}\n\n")72}7374///|75/// Write `let <name> : <array_type> = [...]`, one element per line.76fn Emitter::write_array(77 self : Emitter,78 doc : String,79 name : String,80 array_type : String,81 elements : Array[String],82) -> Unit {83 self.write("///|\n")84 self.write("/// \{doc}\n")85 self.write("let \{name} : \{array_type} = [\n")86 for element in elements {87 self.write(" \{element},\n")88 }89 self.write("]\n\n")90}9192///|93pub fn Emitter::write_int_array(94 self : Emitter,95 doc : String,96 name : String,97 values : Array[Int32],98 hex~ : Bool,99) -> Unit {100 self.write_array(101 doc,102 name,103 "ReadOnlyArray[Int]",104 values.map(value => {105 if hex {106 hex_literal(value, min_width=4)107 } else {108 value.to_string()109 }110 }),111 )112}113114///|115/// Emit a `ReadOnlyArray[UInt16]`, half the size of `write_int_array` for116/// values that fit in 16 bits.117pub fn Emitter::write_uint16_array(118 self : Emitter,119 doc : String,120 name : String,121 values : Array[Int32],122) -> Unit raise {123 guard values.all(value => value >= 0 && value <= 0xFFFF) else {124 fail("\{name} has a value outside UInt16")125 }126 self.write_array(127 doc,128 name,129 "ReadOnlyArray[UInt16]",130 values.map(value => value.to_string()),131 )132}133134///|135pub fn Emitter::write_byte_array(136 self : Emitter,137 doc : String,138 name : String,139 values : Array[Int32],140) -> Unit {141 self.write_array(doc, name, "Bytes", values.map(value => value.to_string()))142}143144///|145/// Emit a `ReadOnlyArray[String]` holding one string per code point146/// sequence.147pub fn Emitter::write_string_array(148 self : Emitter,149 doc : String,150 name : String,151 values : Array[Array[Int32]],152) -> Unit {153 self.write_array(154 doc,155 name,156 "ReadOnlyArray[String]",157 values.map(code_points => string_literal(code_points)),158 )159}160
///|
/// Largest Unicode scalar value.
pub const MaxCodePoint : Int32 = 0x10FFFF
///|
/// Builds the text of one generated source file.
struct Emitter {
out : StringBuilder
}
///|
pub fn Emitter::new() -> Emitter {
{ out: StringBuilder(), }
}
///|
pub fn Emitter::write(self : Emitter, source : String) -> Unit {
self.out.write_string(source)
}
///|
pub fn Emitter::write_line(self : Emitter, source : String) -> Unit {
self.out.write_string(source)
self.out.write_char('\n')
}
///|
pub fn Emitter::finish(self : Emitter) -> String {
self.out.to_string()
}
///|
/// Uppercase hexadecimal representation of `value`, zero-padded to at least
/// `min_width` digits.
pub fn hex_upper(value : Int32, min_width~ : Int32 = 0) -> String {
let digits = value.to_string(radix=16).to_upper()
if digits.length() >= min_width {
digits
} else {
"0".repeat(min_width - digits.length()) + digits
}
}
///|
/// `0x`-prefixed uppercase hexadecimal, as a MoonBit literal.
pub fn hex_literal(value : Int32, min_width~ : Int32 = 0) -> String {
"0x" + hex_upper(value, min_width~)
}
///|
/// A MoonBit string literal holding the given code points as `\u{...}`
/// escapes.
pub fn string_literal(code_points : ArrayView[Int32]) -> String {
let literal = StringBuilder()
literal.write_char('"')
for code_point in code_points {
literal.write_string("\\u{\{hex_upper(code_point, min_width=4)}}")
}
literal.write_char('"')
literal.to_string()
}
///|
pub fn Emitter::write_file_header(
self : Emitter,
title : String,
generated_from : String,
) -> Unit {
self.write("///|\n")
self.write("/// \{title}\n")
self.write("/// Generated from \{generated_from}\n\n")
}
///|
/// Write `let <name> : <array_type> = [...]`, one element per line.
fn Emitter::write_array(
self : Emitter,
doc : String,
name : String,
array_type : String,
elements : Array[String],
) -> Unit {
self.write("///|\n")
self.write("/// \{doc}\n")
self.write("let \{name} : \{array_type} = [\n")
for element in elements {
self.write(" \{element},\n")
}
self.write("]\n\n")
}
///|
pub fn Emitter::write_int_array(
self : Emitter,
doc : String,
name : String,
values : Array[Int32],
hex~ : Bool,
) -> Unit {
self.write_array(
doc,
name,
"ReadOnlyArray[Int]",
values.map(value => {
if hex {
hex_literal(value, min_width=4)
} else {
value.to_string()
}
}),
)
}
///|
/// Emit a `ReadOnlyArray[UInt16]`, half the size of `write_int_array` for
/// values that fit in 16 bits.
pub fn Emitter::write_uint16_array(
self : Emitter,
doc : String,
name : String,
values : Array[Int32],
) -> Unit raise {
guard values.all(value => value >= 0 && value <= 0xFFFF) else {
fail("\{name} has a value outside UInt16")
}
self.write_array(
doc,
name,
"ReadOnlyArray[UInt16]",
values.map(value => value.to_string()),
)
}
///|
pub fn Emitter::write_byte_array(
self : Emitter,
doc : String,
name : String,
values : Array[Int32],
) -> Unit {
self.write_array(doc, name, "Bytes", values.map(value => value.to_string()))
}
///|
/// Emit a `ReadOnlyArray[String]` holding one string per code point
/// sequence.
pub fn Emitter::write_string_array(
self : Emitter,
doc : String,
name : String,
values : Array[Array[Int32]],
) -> Unit {
self.write_array(
doc,
name,
"ReadOnlyArray[String]",
values.map(code_points => string_literal(code_points)),
)
}