chemp is a tool for parsing chemical formulas.
takes molecular formula of substance. for given correct formula it extracts information about compound:
- chemical composition
- molar mass
- mass percent of each element in composition
Usage
use chemp;
let compound = parse.unwrap;
// getters for each component of compound
compound.components.values.for_each;
// list of elements in order they parsed
// nested groups are flattened
compound.composition.iter.for_each;
// get molar mass of compound
compound.molar_mass;
println!;
// compound: Compound {
// composition: [
// Element {
// chemical_element: Magnesium,
// subscript: 1,
// },
// Element {
// chemical_element: Sulfur,
// subscript: 1,
// },
// Element {
// chemical_element: Oxygen,
// subscript: 4,
// },
// Element {
// chemical_element: Hydrogen,
// subscript: 14,
// },
// Element {
// chemical_element: Oxygen,
// subscript: 7,
// },
// ],
// components: {
// "O": Component {
// chemical_element: Oxygen,
// atoms_count: 11,
// mass_percent: 71.40498,
// },
// "S": Component {
// chemical_element: Sulfur,
// atoms_count: 1,
// mass_percent: 13.007879,
// },
// "Mg": Component {
// chemical_element: Magnesium,
// atoms_count: 1,
// mass_percent: 9.861401,
// },
// "H": Component {
// chemical_element: Hydrogen,
// atoms_count: 14,
// mass_percent: 5.725739,
// },
// },
// molar_mass: 246.466,
// }
The parser grammar
substance = coefficient? component+ hydrate?
component = element | group
group = '(' component+ ')' subscript?
element = symbol subscript?
hydrate = '*' coefficient? water
symbol = uppercased | uppercased lowercased
subscript = digit+
coefficient = digit+
water = 'H2O'
uppercased = {'A'..'Z'}
lowercased = {'a'..'z'}
digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'