unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

tools/emitter/emitter.mbt
1///|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