Skip to main content

sct_scg/
lib.rs

1// SPDX-FileCopyrightText: Cadasto B.V.
2// SPDX-License-Identifier: BUSL-1.1
3
4//! The SNOMED CT Compositional Grammar: the syntax of post-coordinated
5//! expressions.
6//!
7//! A `logos` lexer and a `winnow` parser faithful to the normative ABNF
8//! (<https://docs.snomed.org/snomed-ct-specifications/snomed-ct-compositional-grammar-specification/design/5-syntax-specification>),
9//! a syntax tree named after the grammar's rules, and a printer whose output
10//! parses back to the same tree.
11//!
12//! The crate is syntax only: it resolves no concept identifier, checks no
13//! concept model, and decides no subsumption.
14#![doc(test(attr(deny(warnings))))]
15
16pub mod ast;
17pub mod front;
18pub mod lexer;
19pub mod parser;
20mod print;
21
22use crate::ast::Expression;
23use crate::front::ParseError;
24
25/// Parses an expression in Compositional Grammar.
26///
27/// # Errors
28///
29/// Returns [`ParseError`] with the byte offset of the first character or token
30/// the grammar does not admit. An identifier that names no concept still
31/// parses; only the syntax is checked here.
32///
33/// # Examples
34///
35/// ```
36/// let tree = sct_scg::parse("73211009 |Diabetes mellitus|")?;
37/// assert_eq!(tree.to_string(), "73211009 |Diabetes mellitus|");
38/// # Ok::<(), sct_scg::front::ParseError>(())
39/// ```
40pub fn parse(input: &str) -> Result<Expression, ParseError> {
41    front::parse::<lexer::Kind, _>(input, "a valid expression", parser::whole)
42}
43
44impl std::str::FromStr for Expression {
45    type Err = ParseError;
46
47    fn from_str(s: &str) -> Result<Self, Self::Err> {
48        parse(s)
49    }
50}