typeql/
typeql.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5 */
6
7#![deny(rust_2018_idioms)]
8#![deny(rust_2021_compatibility)]
9#![deny(rust_2024_compatibility)]
10#![deny(elided_lifetimes_in_paths)]
11#![deny(unused_must_use)]
12#![allow(edition_2024_expr_fragment_specifier)]
13
14use std::{collections::HashSet, sync::OnceLock};
15
16use itertools::chain;
17
18pub use crate::{
19    annotation::Annotation,
20    common::{error::Error, identifier::Identifier, token, Result},
21    expression::Expression,
22    pattern::Pattern,
23    query::Query,
24    schema::definable::{Definable, Function},
25    statement::Statement,
26    type_::{Label, ScopedLabel, TypeRef, TypeRefAny},
27    value::Literal,
28    variable::Variable,
29};
30use crate::{
31    parser::{
32        visit_eof_definition_function, visit_eof_definition_struct, visit_eof_label, visit_eof_query,
33        visit_query_prefix,
34    },
35    schema::definable::Struct,
36};
37
38pub mod annotation;
39pub mod builder;
40pub mod common;
41pub mod expression;
42pub mod parser;
43pub mod pattern;
44mod pretty;
45pub mod query;
46pub mod schema;
47pub mod statement;
48pub mod type_;
49mod util;
50pub mod value;
51mod variable;
52
53pub fn parse_query(typeql_query: &str) -> Result<Query> {
54    visit_eof_query(typeql_query.trim_end())
55}
56
57pub fn parse_queries(mut typeql_queries: &str) -> Result<Vec<Query>> {
58    let mut queries = Vec::new();
59    while !typeql_queries.trim().is_empty() {
60        let (query, remainder_index) = parse_query_from(typeql_queries)?;
61        queries.push(query);
62        typeql_queries = &typeql_queries[remainder_index..];
63    }
64    Ok(queries)
65}
66
67pub fn parse_query_from(string: &str) -> Result<(Query, usize)> {
68    let query = visit_query_prefix(string)?;
69    let end_offset = query.span.unwrap().end_offset;
70    Ok((query, end_offset))
71}
72
73pub fn parse_label(typeql_label: &str) -> Result<Label> {
74    visit_eof_label(typeql_label)
75}
76
77pub fn parse_definition_function(typeql_function: &str) -> Result<Function> {
78    visit_eof_definition_function(typeql_function.trim_end())
79}
80
81pub fn parse_definition_struct(typeql_struct: &str) -> Result<Struct> {
82    visit_eof_definition_struct(typeql_struct.trim_end())
83}
84
85static RESERVED_KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
86pub fn is_reserved_keyword(word: &str) -> bool {
87    RESERVED_KEYWORDS
88        .get_or_init(|| {
89            chain!(token::Kind::NAMES, token::Keyword::NAMES, token::Order::NAMES, token::BooleanValue::NAMES,)
90                .map(|s| *s)
91                .collect::<HashSet<&'static str>>()
92        })
93        .contains(word)
94}