meta_language/
self_description.rs1use std::collections::BTreeSet;
2use std::sync::OnceLock;
3
4use links_notation::{LiNo, ParserConfig, parse_lino_to_links_with_config};
5
6use crate::link_network::{LinkMetadata, LinkNetwork, LinkType};
7use crate::lino_serialization::parse_link_type;
8
9impl LinkNetwork {
10 pub(super) fn seed_self_description(structural: bool) -> Self {
11 let mut network = Self::new();
12 let roots = self_description_roots();
13 for root in roots {
14 let definition =
15 (!structural).then(|| definition_expression(&root.term, &root.references));
16 network.insert_typed_point(&root.term, root.link_type, definition.as_deref());
17 }
18
19 for root in roots {
20 let mut references = Vec::with_capacity(root.references.len() + 1);
21 let id = network
22 .find_term(&root.term)
23 .expect("seeded self-description root exists");
24 if !structural {
25 references.push(id);
26 }
27 for reference in &root.references {
28 references.push(
29 network
30 .find_term(reference)
31 .expect("seeded self-description reference exists"),
32 );
33 }
34
35 if structural {
36 network.set_references(id, &references);
37 } else {
38 network.insert_dynamic_link(
39 &references,
40 LinkMetadata::new().with_link_type(LinkType::Relation),
41 );
42 }
43 }
44
45 network
46 }
47
48 #[must_use]
50 pub fn self_description_text(&self) -> String {
51 let mut output = String::new();
52 for root in self_description_roots() {
53 let Some(id) = self.find_term(&root.term) else {
54 continue;
55 };
56 if let Some(definition) = self.definition_for(id) {
57 output.push_str(definition);
58 } else if let Some(link) = self.link(id) {
59 let references = link
60 .references()
61 .iter()
62 .map(|reference| self.link(*reference)?.metadata().term().map(str::to_owned))
63 .collect::<Option<Vec<_>>>();
64 let Some(references) = references.filter(|references| !references.is_empty())
65 else {
66 continue;
67 };
68 output.push_str(&definition_expression(&root.term, &references));
69 } else {
70 continue;
71 }
72 output.push('\n');
73 }
74 output
75 }
76}
77
78struct SelfDescriptionRoot {
79 term: String,
80 link_type: LinkType,
81 references: Vec<String>,
82}
83
84fn self_description_roots() -> &'static [SelfDescriptionRoot] {
85 static ROOTS: OnceLock<Vec<SelfDescriptionRoot>> = OnceLock::new();
86 ROOTS.get_or_init(|| {
87 let source = include_str!("data/self-description.lino");
88 let declarations =
89 parse_lino_to_links_with_config(source, &ParserConfig::without_comments())
90 .expect("packaged self-description Links notation is valid");
91 let roots: Vec<_> = declarations.into_iter().map(read_root).collect();
92 let names: BTreeSet<_> = roots.iter().map(|root| root.term.as_str()).collect();
93 assert_eq!(names.len(), roots.len(), "duplicate self-description root");
94 assert!(
95 roots
96 .iter()
97 .flat_map(|root| &root.references)
98 .all(|term| names.contains(term.as_str())),
99 "self-description reference must name a root"
100 );
101 roots
102 })
103}
104
105fn read_root(declaration: LiNo<String>) -> SelfDescriptionRoot {
106 let LiNo::Link {
107 id: Some(term),
108 values,
109 } = declaration
110 else {
111 panic!("self-description root must be a named link");
112 };
113 let mut values = values.into_iter();
114 let Some(LiNo::Link {
115 id: Some(role),
116 values: role_values,
117 }) = values.next()
118 else {
119 panic!("self-description root must have a role");
120 };
121 let [LiNo::Ref(token)] = role_values.as_slice() else {
122 panic!("self-description root role must be a single reference");
123 };
124 assert_eq!(role, "role", "unexpected self-description field");
125 let link_type = parse_link_type(token).expect("self-description role must be a link type");
126 let references: Vec<_> = values
127 .map(|reference| match reference {
128 LiNo::Ref(reference) => reference,
129 LiNo::Link { .. } => panic!("self-description definition must contain root references"),
130 })
131 .collect();
132 assert!(
133 !term.is_empty() && !references.is_empty(),
134 "invalid self-description root"
135 );
136 SelfDescriptionRoot {
137 term,
138 link_type,
139 references,
140 }
141}
142
143fn definition_expression(term: &str, references: &[String]) -> String {
144 format!("({term}: {})", references.join(" "))
145}
146
147pub fn link_network_vocabulary() -> Vec<LiNo<String>> {
148 parse_lino_to_links_with_config(
149 include_str!("data/link-network-vocabulary.lino"),
150 &ParserConfig::without_comments(),
151 )
152 .expect("packaged link network vocabulary is valid")
153}