Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion periodictable/formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,11 @@ def convert_explicit(string, location, tokens):
implicit_separator = separator | space
composite << group + ZeroOrMore(implicit_separator + group)

density = Literal('@').suppress() + number + Optional(Regex("[ni]"), default='i')
# A density annotation requires an explicit numeric value. Reusing
# ``number`` here would make a bare trailing '@' parse as density 1
# because ``number`` is optional and defaults to one for element counts.
density_number = ~White() + (fract | whole)
density = Literal('@').suppress() + density_number + Optional(Regex("[ni]"), default='i')
compound = (composite|fasta) + Optional(density, default=None)
def convert_compound(string, location, tokens):
"""convert material @ density or fasta @ density"""
Expand Down
1 change: 1 addition & 0 deletions test/test_formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def test():
# Only allow subscripts in the post position
assert check_parse_fails("₃H₂O")
assert check_parse_fails("H₂O@₁")
assert check_parse_fails("H2O@")
assert check_parse_fails("₁wt% NaCl@2.3 // H₂O@1n")

# Test composition
Expand Down