Skip to main content

sct_ecl/
lib.rs

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