Skip to main content

meta_language/
concept_records.rs

1//! The concept records shared with the JavaScript runtime.
2//!
3//! `src/data/concept-records.lino` is generated from
4//! `parity/naming/canonical-concepts.json` by
5//! `js/scripts/build-concept-records.mjs`; the npm package ships the same
6//! file, so both runtimes agree on every canonical identity, its readable
7//! English phrase, definition, constraints, source aliases and former names.
8
9use std::collections::{BTreeMap, BTreeSet};
10use std::sync::OnceLock;
11
12use serde::Deserialize;
13
14const CONCEPT_RECORDS_LINKS: &str = include_str!("data/concept-records.lino");
15
16/// Whether a canonical name is a concept (a noun phrase) or an operation (a verb phrase).
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
18#[serde(rename_all = "lowercase")]
19pub enum ConceptRole {
20    /// A concept, named by a noun phrase.
21    Concept,
22    /// An operation or relation, named by a verb phrase.
23    Operation,
24}
25
26/// The name a source (a grammar format, a programming language, a surface) gives a concept.
27#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
28pub struct SourceAlias {
29    /// The source, such as `ebnf`, `Rust` or `Russian grammar surface`.
30    pub source: String,
31    /// The name or notation the source uses for the concept.
32    pub name: String,
33}
34
35/// A concept whose phrase is a synonym of another's, with what distinguishes them.
36#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
37pub struct ConceptDistinction {
38    /// The identity of the synonymous concept.
39    pub id: String,
40    /// What distinguishes the two concepts.
41    pub reason: String,
42}
43
44/// The record of one canonical concept or operation.
45#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
46#[serde(rename_all = "camelCase", deny_unknown_fields)]
47pub struct ConceptRecord {
48    /// Stable identity, such as `grammar.character-class`.
49    pub id: String,
50    /// The readable English phrase the identity spells, such as `character class`.
51    pub phrase: String,
52    /// Whether the phrase is a noun phrase or a verb phrase.
53    pub role: ConceptRole,
54    /// What the concept means.
55    pub definition: String,
56    /// The constraints the concept satisfies.
57    pub constraints: Vec<String>,
58    /// The names other sources give the concept.
59    pub source_aliases: Vec<SourceAlias>,
60    /// Former names that still decode to the concept.
61    pub former_names: Vec<String>,
62    /// The concept this one is a use of, such as the operation a stage performs.
63    #[serde(default)]
64    pub represents: Option<String>,
65    /// The concepts with a synonymous phrase this one is distinct from.
66    #[serde(default)]
67    pub distinct_from: Vec<ConceptDistinction>,
68}
69
70#[derive(Deserialize)]
71struct ConceptRecordFile {
72    concepts: Vec<ConceptRecord>,
73}
74
75/// Returns every concept record in register order.
76#[must_use]
77pub fn concept_records() -> &'static [ConceptRecord] {
78    static RECORDS: OnceLock<Vec<ConceptRecord>> = OnceLock::new();
79    RECORDS.get_or_init(|| {
80        serde_json::from_value::<ConceptRecordFile>(
81            crate::structured_data_links::read_structured_data_lino(CONCEPT_RECORDS_LINKS)
82                .expect("the embedded concept links are valid"),
83        )
84        .expect("the embedded concept records are valid")
85        .concepts
86    })
87}
88
89/// Returns the record whose identity or former name is `name`.
90#[must_use]
91pub fn concept_record(name: &str) -> Option<&'static ConceptRecord> {
92    static NAMES: OnceLock<BTreeMap<&'static str, &'static ConceptRecord>> = OnceLock::new();
93    NAMES
94        .get_or_init(|| {
95            let mut names = BTreeMap::new();
96            for record in concept_records() {
97                names.entry(record.id.as_str()).or_insert(record);
98                for former in &record.former_names {
99                    names.entry(former.as_str()).or_insert(record);
100                }
101            }
102            names
103        })
104        .get(name)
105        .copied()
106}
107
108/// Returns the records a source (such as `ebnf` or `Rust`) names `name`, in register order.
109#[must_use]
110pub fn concept_records_for_source_name(source: &str, name: &str) -> Vec<&'static ConceptRecord> {
111    type SourceNameIndex =
112        BTreeMap<&'static str, BTreeMap<&'static str, Vec<&'static ConceptRecord>>>;
113    static SOURCES: OnceLock<SourceNameIndex> = OnceLock::new();
114    SOURCES
115        .get_or_init(|| {
116            let mut sources: SourceNameIndex = BTreeMap::new();
117            for record in concept_records() {
118                let mut seen = BTreeSet::new();
119                for alias in &record.source_aliases {
120                    let key = (alias.source.as_str(), alias.name.as_str());
121                    if seen.insert(key) {
122                        sources
123                            .entry(key.0)
124                            .or_default()
125                            .entry(key.1)
126                            .or_default()
127                            .push(record);
128                    }
129                }
130            }
131            sources
132        })
133        .get(source)
134        .and_then(|names| names.get(name))
135        .cloned()
136        .unwrap_or_default()
137}