forked from NomicFoundation/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefinitions.rs
171 lines (145 loc) · 5.5 KB
/
definitions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::collections::HashSet;
use crate::compiler::analysis::{Analysis, ItemMetadata};
use crate::compiler::version_set::VersionSet;
use crate::internals::Spanned;
use crate::model::{Identifier, SpannedItem, SpannedVersionSpecifier};
pub(crate) fn analyze_definitions(analysis: &mut Analysis) {
collect_top_level_items(analysis);
check_enum_items(analysis);
check_precedence_items(analysis);
}
fn collect_top_level_items(analysis: &mut Analysis) {
let language = analysis.language.clone();
for item in language.items() {
let name = get_item_name(item);
let defined_in = calculate_defined_in(analysis, item);
if analysis.metadata.contains_key(&**name) {
analysis.errors.add(name, &Errors::ExistingItem(name));
}
analysis.metadata.insert(
(**name).clone(),
ItemMetadata {
name: name.clone(),
item: item.clone(),
defined_in,
used_in: VersionSet::new(),
referenced_from: Vec::new(),
referenced_items: Vec::new(),
},
);
}
}
fn check_enum_items(analysis: &mut Analysis) {
for item in analysis.language.clone().items() {
let SpannedItem::Enum { item } = item else {
continue;
};
let mut variants = HashSet::new();
for variant in &item.variants {
let reference = &variant.reference;
if !variants.insert(&**reference) {
analysis
.errors
.add(reference, &Errors::ExistingVariant(reference));
}
}
}
}
fn check_precedence_items(analysis: &mut Analysis) {
// Make sure that all expressions have unique names across the entire language, since they produce their own kinds.
// However, they cannot be referenced from anywhere else, so no need to add them to top-level definitions.
let mut all_expressions = HashSet::new();
for item in analysis.language.clone().items() {
let SpannedItem::Precedence { item } = item else {
continue;
};
// Additionally, make sure that both precedence and primary expressions under
// the same precedence item are unique, as they will produce enum variants.
let mut current_expressions = HashSet::new();
for precedence_expression in &item.precedence_expressions {
let name = &precedence_expression.name;
if analysis.metadata.contains_key(&**name) {
analysis.errors.add(name, &Errors::ExistingItem(name));
}
if !all_expressions.insert(name) {
analysis.errors.add(name, &Errors::ExistingExpression(name));
}
current_expressions.insert(name);
}
for primary_expression in &item.primary_expressions {
let reference = &primary_expression.reference;
if !current_expressions.insert(reference) {
analysis
.errors
.add(reference, &Errors::ExistingExpression(reference));
}
}
}
}
fn get_item_name(item: &SpannedItem) -> &Spanned<Identifier> {
match item {
SpannedItem::Struct { item } => &item.name,
SpannedItem::Enum { item } => &item.name,
SpannedItem::Repeated { item } => &item.name,
SpannedItem::Separated { item } => &item.name,
SpannedItem::Precedence { item } => &item.name,
SpannedItem::Trivia { item } => &item.name,
SpannedItem::Keyword { item } => &item.name,
SpannedItem::Token { item } => &item.name,
SpannedItem::Fragment { item } => &item.name,
}
}
fn calculate_defined_in(analysis: &mut Analysis, item: &SpannedItem) -> VersionSet {
let mut defined_in = VersionSet::new();
let mut try_add_specifier = |specifier: &Option<Spanned<SpannedVersionSpecifier>>| {
if let Some(specifier) = specifier {
analysis.add_specifier(&mut defined_in, specifier);
} else {
analysis.add_all_versions(&mut defined_in);
}
};
match item {
SpannedItem::Struct { item } => {
try_add_specifier(&item.enabled);
}
SpannedItem::Enum { item } => {
try_add_specifier(&item.enabled);
}
SpannedItem::Repeated { item } => {
try_add_specifier(&item.enabled);
}
SpannedItem::Separated { item } => {
try_add_specifier(&item.enabled);
}
SpannedItem::Precedence { item } => {
try_add_specifier(&item.enabled);
}
SpannedItem::Trivia { item: _ } => {
try_add_specifier(&None);
}
SpannedItem::Keyword { item } => {
for definition in &item.definitions {
try_add_specifier(&definition.enabled);
}
}
SpannedItem::Token { item } => {
for definition in &item.definitions {
try_add_specifier(&definition.enabled);
}
}
SpannedItem::Fragment { item } => {
try_add_specifier(&item.enabled);
}
};
defined_in
}
#[allow(clippy::enum_variant_names)]
#[derive(thiserror::Error, Debug)]
enum Errors<'err> {
#[error("An item with the name '{0}' already exists.")]
ExistingItem(&'err Identifier),
#[error("A variant referencing '{0}' already exists.")]
ExistingVariant(&'err Identifier),
#[error("An expression with the name '{0}' already exists.")]
ExistingExpression(&'err Identifier),
}