hermes-parser 0.1.3

A Rust port of the Hermes JavaScript/Flow/TypeScript parser (front-end) by Tzvetan Mikov, the architect of Hermes. Not an official Meta project.
Documentation
// Copyright (c) Meta Platforms, Inc. and affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//
// GENERATED by gen_html_entities.py from include/hermes/Parser/HTMLEntities.def.
// DO NOT EDIT BY HAND.
//
//! XHTML named-entity table for the JSX lexer. Port of the
//! `initializeHTMLEntities` map in `lib/Parser/JSLexer.cpp`, but
//! emitted sorted by name so `&name;` lookup is a binary search.

/// The XHTML named entities, sorted by name. Each entry maps an entity
/// name (the bytes between `&` and `;`) to its Unicode code point.
pub static HTML_ENTITIES: [(&[u8], u32); 253] = [
    (b"AElig", 0x00c6),
    (b"Aacute", 0x00c1),
    (b"Acirc", 0x00c2),
    (b"Agrave", 0x00c0),
    (b"Alpha", 0x0391),
    (b"Aring", 0x00c5),
    (b"Atilde", 0x00c3),
    (b"Auml", 0x00c4),
    (b"Beta", 0x0392),
    (b"Ccedil", 0x00c7),
    (b"Chi", 0x03a7),
    (b"Dagger", 0x2021),
    (b"Delta", 0x0394),
    (b"ETH", 0x00d0),
    (b"Eacute", 0x00c9),
    (b"Ecirc", 0x00ca),
    (b"Egrave", 0x00c8),
    (b"Epsilon", 0x0395),
    (b"Eta", 0x0397),
    (b"Euml", 0x00cb),
    (b"Gamma", 0x0393),
    (b"Iacute", 0x00cd),
    (b"Icirc", 0x00ce),
    (b"Igrave", 0x00cc),
    (b"Iota", 0x0399),
    (b"Iuml", 0x00cf),
    (b"Kappa", 0x039a),
    (b"Lambda", 0x039b),
    (b"Mu", 0x039c),
    (b"Ntilde", 0x00d1),
    (b"Nu", 0x039d),
    (b"OElig", 0x0152),
    (b"Oacute", 0x00d3),
    (b"Ocirc", 0x00d4),
    (b"Ograve", 0x00d2),
    (b"Omega", 0x03a9),
    (b"Omicron", 0x039f),
    (b"Oslash", 0x00d8),
    (b"Otilde", 0x00d5),
    (b"Ouml", 0x00d6),
    (b"Phi", 0x03a6),
    (b"Pi", 0x03a0),
    (b"Prime", 0x2033),
    (b"Psi", 0x03a8),
    (b"Rho", 0x03a1),
    (b"Scaron", 0x0160),
    (b"Sigma", 0x03a3),
    (b"THORN", 0x00de),
    (b"Tau", 0x03a4),
    (b"Theta", 0x0398),
    (b"Uacute", 0x00da),
    (b"Ucirc", 0x00db),
    (b"Ugrave", 0x00d9),
    (b"Upsilon", 0x03a5),
    (b"Uuml", 0x00dc),
    (b"Xi", 0x039e),
    (b"Yacute", 0x00dd),
    (b"Yuml", 0x0178),
    (b"Zeta", 0x0396),
    (b"aacute", 0x00e1),
    (b"acirc", 0x00e2),
    (b"acute", 0x00b4),
    (b"aelig", 0x00e6),
    (b"agrave", 0x00e0),
    (b"alefsym", 0x2135),
    (b"alpha", 0x03b1),
    (b"amp", 0x0026),
    (b"and", 0x2227),
    (b"ang", 0x2220),
    (b"apos", 0x0027),
    (b"aring", 0x00e5),
    (b"asymp", 0x2248),
    (b"atilde", 0x00e3),
    (b"auml", 0x00e4),
    (b"bdquo", 0x201e),
    (b"beta", 0x03b2),
    (b"brvbar", 0x00a6),
    (b"bull", 0x2022),
    (b"cap", 0x2229),
    (b"ccedil", 0x00e7),
    (b"cedil", 0x00b8),
    (b"cent", 0x00a2),
    (b"chi", 0x03c7),
    (b"circ", 0x02c6),
    (b"clubs", 0x2663),
    (b"cong", 0x2245),
    (b"copy", 0x00a9),
    (b"crarr", 0x21b5),
    (b"cup", 0x222a),
    (b"curren", 0x00a4),
    (b"dArr", 0x21d3),
    (b"dagger", 0x2020),
    (b"darr", 0x2193),
    (b"deg", 0x00b0),
    (b"delta", 0x03b4),
    (b"diams", 0x2666),
    (b"divide", 0x00f7),
    (b"eacute", 0x00e9),
    (b"ecirc", 0x00ea),
    (b"egrave", 0x00e8),
    (b"empty", 0x2205),
    (b"emsp", 0x2003),
    (b"ensp", 0x2002),
    (b"epsilon", 0x03b5),
    (b"equiv", 0x2261),
    (b"eta", 0x03b7),
    (b"eth", 0x00f0),
    (b"euml", 0x00eb),
    (b"euro", 0x20ac),
    (b"exist", 0x2203),
    (b"fnof", 0x0192),
    (b"forall", 0x2200),
    (b"frac12", 0x00bd),
    (b"frac14", 0x00bc),
    (b"frac34", 0x00be),
    (b"frasl", 0x2044),
    (b"gamma", 0x03b3),
    (b"ge", 0x2265),
    (b"gt", 0x003e),
    (b"hArr", 0x21d4),
    (b"harr", 0x2194),
    (b"hearts", 0x2665),
    (b"hellip", 0x2026),
    (b"iacute", 0x00ed),
    (b"icirc", 0x00ee),
    (b"iexcl", 0x00a1),
    (b"igrave", 0x00ec),
    (b"image", 0x2111),
    (b"infin", 0x221e),
    (b"int", 0x222b),
    (b"iota", 0x03b9),
    (b"iquest", 0x00bf),
    (b"isin", 0x2208),
    (b"iuml", 0x00ef),
    (b"kappa", 0x03ba),
    (b"lArr", 0x21d0),
    (b"lambda", 0x03bb),
    (b"lang", 0x2329),
    (b"laquo", 0x00ab),
    (b"larr", 0x2190),
    (b"lceil", 0x2308),
    (b"ldquo", 0x201c),
    (b"le", 0x2264),
    (b"lfloor", 0x230a),
    (b"lowast", 0x2217),
    (b"loz", 0x25ca),
    (b"lrm", 0x200e),
    (b"lsaquo", 0x2039),
    (b"lsquo", 0x2018),
    (b"lt", 0x003c),
    (b"macr", 0x00af),
    (b"mdash", 0x2014),
    (b"micro", 0x00b5),
    (b"middot", 0x00b7),
    (b"minus", 0x2212),
    (b"mu", 0x03bc),
    (b"nabla", 0x2207),
    (b"nbsp", 0x00a0),
    (b"ndash", 0x2013),
    (b"ne", 0x2260),
    (b"ni", 0x220b),
    (b"not", 0x00ac),
    (b"notin", 0x2209),
    (b"nsub", 0x2284),
    (b"ntilde", 0x00f1),
    (b"nu", 0x03bd),
    (b"oacute", 0x00f3),
    (b"ocirc", 0x00f4),
    (b"oelig", 0x0153),
    (b"ograve", 0x00f2),
    (b"oline", 0x203e),
    (b"omega", 0x03c9),
    (b"omicron", 0x03bf),
    (b"oplus", 0x2295),
    (b"or", 0x2228),
    (b"ordf", 0x00aa),
    (b"ordm", 0x00ba),
    (b"oslash", 0x00f8),
    (b"otilde", 0x00f5),
    (b"otimes", 0x2297),
    (b"ouml", 0x00f6),
    (b"para", 0x00b6),
    (b"part", 0x2202),
    (b"permil", 0x2030),
    (b"perp", 0x22a5),
    (b"phi", 0x03c6),
    (b"pi", 0x03c0),
    (b"piv", 0x03d6),
    (b"plusmn", 0x00b1),
    (b"pound", 0x00a3),
    (b"prime", 0x2032),
    (b"prod", 0x220f),
    (b"prop", 0x221d),
    (b"psi", 0x03c8),
    (b"quot", 0x0022),
    (b"rArr", 0x21d2),
    (b"radic", 0x221a),
    (b"rang", 0x232a),
    (b"raquo", 0x00bb),
    (b"rarr", 0x2192),
    (b"rceil", 0x2309),
    (b"rdquo", 0x201d),
    (b"real", 0x211c),
    (b"reg", 0x00ae),
    (b"rfloor", 0x230b),
    (b"rho", 0x03c1),
    (b"rlm", 0x200f),
    (b"rsaquo", 0x203a),
    (b"rsquo", 0x2019),
    (b"sbquo", 0x201a),
    (b"scaron", 0x0161),
    (b"sdot", 0x22c5),
    (b"sect", 0x00a7),
    (b"shy", 0x00ad),
    (b"sigma", 0x03c3),
    (b"sigmaf", 0x03c2),
    (b"sim", 0x223c),
    (b"spades", 0x2660),
    (b"sub", 0x2282),
    (b"sube", 0x2286),
    (b"sum", 0x2211),
    (b"sup", 0x2283),
    (b"sup1", 0x00b9),
    (b"sup2", 0x00b2),
    (b"sup3", 0x00b3),
    (b"supe", 0x2287),
    (b"szlig", 0x00df),
    (b"tau", 0x03c4),
    (b"there4", 0x2234),
    (b"theta", 0x03b8),
    (b"thetasym", 0x03d1),
    (b"thinsp", 0x2009),
    (b"thorn", 0x00fe),
    (b"tilde", 0x02dc),
    (b"times", 0x00d7),
    (b"trade", 0x2122),
    (b"uArr", 0x21d1),
    (b"uacute", 0x00fa),
    (b"uarr", 0x2191),
    (b"ucirc", 0x00fb),
    (b"ugrave", 0x00f9),
    (b"uml", 0x00a8),
    (b"upsih", 0x03d2),
    (b"upsilon", 0x03c5),
    (b"uuml", 0x00fc),
    (b"weierp", 0x2118),
    (b"xi", 0x03be),
    (b"yacute", 0x00fd),
    (b"yen", 0x00a5),
    (b"yuml", 0x00ff),
    (b"zeta", 0x03b6),
    (b"zwj", 0x200d),
    (b"zwnj", 0x200c),
];

/// Look up an XHTML named entity by name (the bytes between `&`
/// and `;`). Returns its code point, or `None` if unknown.
pub fn lookup(name: &[u8]) -> Option<u32> {
    HTML_ENTITIES
        .binary_search_by(|(n, _)| (*n).cmp(name))
        .ok()
        .map(|i| HTML_ENTITIES[i].1)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn known_entities() {
        assert_eq!(lookup(b"amp"), Some(0x26));
        assert_eq!(lookup(b"lt"), Some(0x3c));
        assert_eq!(lookup(b"nope"), None);
    }

    #[test]
    fn table_is_sorted_and_full() {
        assert_eq!(HTML_ENTITIES.len(), 253);
        for w in HTML_ENTITIES.windows(2) {
            assert!(w[0].0 < w[1].0, "table not sorted by name");
        }
    }
}