1use crate::bytecode::CompiledScript;
3use crate::clientscript::{self, Source, format::Format};
4use crate::compiler::{Compiler, compile_file};
5use crate::diagnostics::{DiagnosticsCollector, Phase, Severity};
6use crate::ide::Index;
7use crate::lexer::Lexer;
8use crate::parser::{Parser, ScriptFile};
9use crate::pointer_checker::PointerChecker;
10use crate::sources::Scope;
11use crate::symbol::SymbolRegistry;
12use crate::text::LineIndex;
13use crate::token::Kind;
14use crate::typechecker::TypeChecker;
15use crate::writer::encode_script;
16use crate::{SourceProvider, lints};
17use std::collections::hash_map::DefaultHasher;
18use std::collections::{BTreeMap, HashMap, HashSet};
19use std::hash::{Hash, Hasher};
20use std::ops::Range;
21use std::path::{Path, PathBuf};
22use std::sync::{
23 Arc, OnceLock,
24 atomic::{AtomicBool, Ordering},
25};
26
27#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
28pub struct SourceId(pub u64);
29
30pub enum Target {
31 Rune,
32 Client(Format),
33}
34
35impl Target {
36 fn includes(&self, scope: Scope) -> bool {
37 scope == Scope::Global
38 || matches!(
39 (self, scope),
40 (Self::Rune, Scope::Rune) | (Self::Client(_), Scope::Client)
41 )
42 }
43}
44
45#[derive(Clone, Default)]
46pub struct CancellationToken(Arc<AtomicBool>);
47
48impl CancellationToken {
49 pub fn cancel(&self) {
50 self.0.store(true, Ordering::Relaxed);
51 }
52 pub fn is_cancelled(&self) -> bool {
53 self.0.load(Ordering::Relaxed)
54 }
55}
56
57#[derive(Clone, Default)]
58pub struct Options {
59 pub include_tests: bool,
60 pub lint: bool,
61 pub skip_shared_lints: bool,
62 pub check_pointers: bool,
63 pub cancellation: CancellationToken,
64}
65
66pub struct Document {
67 pub id: SourceId,
68 pub label: PathBuf,
69 pub scope: Scope,
70 pub version: i64,
71 pub text: Arc<str>,
72 pub lines: LineIndex,
73 pub config: Option<String>,
74 pub(crate) parsed: Option<Source>,
75 pub(crate) tokens: Vec<TokenInfo>,
76 pub(crate) spans: Vec<Range<usize>>,
77 pub(crate) diagnostics: DiagnosticsCollector,
78 test_line: Option<usize>,
79}
80
81#[derive(Clone, Copy)]
82pub(crate) struct TokenInfo {
83 pub kind: Kind,
84 pub line: usize,
85}
86
87impl Document {
88 pub fn syntax(&self) -> Option<&ScriptFile> {
89 self.parsed.as_ref().map(|source| &source.file)
90 }
91}
92
93pub struct Artifact {
94 pub source: SourceId,
95 pub id: i32,
96 pub name: String,
97 pub bytes: Vec<u8>,
98}
99
100#[derive(Clone, Debug, PartialEq, Eq)]
102pub struct IdAssignment {
103 pub trigger: String,
104 pub name: String,
105 pub id: i32,
106}
107
108pub struct Analysis {
109 pub diagnostics: DiagnosticsCollector,
110 pub assignments: Vec<IdAssignment>,
111 pub cancelled: bool,
112 pub checked_documents: usize,
113}
114
115pub struct Compilation {
116 pub analysis: Analysis,
117 pub artifacts: Vec<Artifact>,
118}
119
120pub struct Project {
123 pub(crate) documents: BTreeMap<SourceId, Document>,
124 pub(crate) registry: SymbolRegistry,
125 target: Target,
126 options: Options,
127 next_source: u64,
128 declarations: Vec<(Scope, String, String)>,
129 allocated: BTreeMap<(String, String), i32>,
130 pub(crate) generation: u64,
131 analyzed_generation: Option<u64>,
132 cached_diagnostics: DiagnosticsCollector,
133 checked_full: Option<(u64, DiagnosticsCollector)>,
134 pub(crate) index: OnceLock<Index>,
135 registry_generation: u64,
136 checked: HashMap<SourceId, (i64, u64, DiagnosticsCollector)>,
137}
138
139impl Project {
140 pub fn new(target: Target, registry: SymbolRegistry, options: Options) -> Self {
141 Self {
142 documents: BTreeMap::new(),
143 registry,
144 target,
145 options,
146 next_source: 0,
147 declarations: Vec::new(),
148 allocated: BTreeMap::new(),
149 generation: 0,
150 analyzed_generation: None,
151 cached_diagnostics: DiagnosticsCollector::new(),
152 checked_full: None,
153 index: OnceLock::new(),
154 registry_generation: 0,
155 checked: HashMap::new(),
156 }
157 }
158
159 pub fn registry(&self) -> &SymbolRegistry {
160 &self.registry
161 }
162 pub fn set_cancellation(&mut self, token: CancellationToken) {
163 self.options.cancellation = token;
164 }
165 pub fn documents(&self) -> impl Iterator<Item = &Document> {
166 self.documents.values()
167 }
168 pub fn document(&self, id: SourceId) -> Option<&Document> {
169 self.documents.get(&id)
170 }
171
172 pub fn set_registry(&mut self, registry: SymbolRegistry) {
174 self.allocated.retain(|(trigger, name), _| {
175 !registry
176 .preloaded_script_ids
177 .contains_key(&format!("{trigger}:{name}"))
178 });
179 self.registry = registry;
180 self.registry_generation += 1;
181 self.index.take();
182 self.generation += 1;
183 }
184
185 pub fn open(
186 &mut self,
187 label: impl Into<PathBuf>,
188 scope: Scope,
189 version: i64,
190 text: impl Into<Arc<str>>,
191 ) -> SourceId {
192 let id = SourceId(self.next_source);
193 self.next_source += 1;
194 self.set_document(id, label.into(), scope, version, text.into());
195 id
196 }
197
198 pub fn open_config(
199 &mut self,
200 label: impl Into<PathBuf>,
201 scope: Scope,
202 version: i64,
203 text: impl Into<Arc<str>>,
204 kind: impl Into<String>,
205 ) -> SourceId {
206 let id = SourceId(self.next_source);
207 self.next_source += 1;
208 self.set_config(id, label.into(), scope, version, text.into(), kind.into());
209 id
210 }
211
212 fn set_config(
213 &mut self,
214 id: SourceId,
215 label: PathBuf,
216 scope: Scope,
217 version: i64,
218 text: Arc<str>,
219 kind: String,
220 ) {
221 Arc::make_mut(&mut self.registry.visibility).bind(label.clone(), scope);
222 self.documents.insert(
223 id,
224 Document {
225 id,
226 label,
227 scope,
228 version,
229 lines: LineIndex::new(&text),
230 text,
231 config: Some(kind),
232 parsed: None,
233 tokens: Vec::new(),
234 spans: Vec::new(),
235 diagnostics: DiagnosticsCollector::new(),
236 test_line: None,
237 },
238 );
239 self.index.take();
240 self.generation += 1;
241 }
242
243 pub fn update(
245 &mut self,
246 id: SourceId,
247 version: i64,
248 text: impl Into<Arc<str>>,
249 ) -> Result<(), &'static str> {
250 let old = self.documents.get_mut(&id).ok_or("Unknown source ID")?;
251 if version <= old.version {
252 return Err("Document version must increase");
253 }
254 let text = text.into();
255 if old.text == text {
256 old.version = version;
257 return Ok(());
258 }
259 let (label, scope, config) = (old.label.clone(), old.scope, old.config.clone());
260 if let Some(kind) = config {
261 self.set_config(id, label, scope, version, text, kind);
262 } else {
263 self.set_document(id, label, scope, version, text);
264 }
265 Ok(())
266 }
267
268 pub fn close(&mut self, id: SourceId) -> bool {
269 let Some(document) = self.documents.remove(&id) else {
270 return false;
271 };
272 Arc::make_mut(&mut self.registry.visibility).unbind(&document.label);
273 self.checked.remove(&id);
274 self.index.take();
275 self.generation += 1;
276 true
277 }
278
279 fn set_document(
280 &mut self,
281 id: SourceId,
282 label: PathBuf,
283 scope: Scope,
284 version: i64,
285 text: Arc<str>,
286 ) {
287 Arc::make_mut(&mut self.registry.visibility).bind(label.clone(), scope);
288 let test_line = crate::find_testscript_line(&text);
289 let end = if !self.options.include_tests {
290 test_line
291 .and_then(|line| LineIndex::new(&text).line(line - 1))
292 .map_or(text.len(), |range| range.start)
293 } else {
294 text.len()
295 };
296 let (tokens, spans, lex_errors) = Lexer::new(&text[..end], &label).tokenize_spanned();
297 let mut diagnostics = DiagnosticsCollector::new();
298 for error in lex_errors {
299 diagnostics.error(
300 label.clone(),
301 error.line,
302 error.position,
303 error.message,
304 Phase::Lexing,
305 );
306 }
307 let token_info = tokens
308 .iter()
309 .map(|token| TokenInfo {
310 kind: token.kind,
311 line: token.line,
312 })
313 .collect();
314 let (file, errors) = Parser::new(tokens, &label).parse_recovering();
315 for error in errors {
316 diagnostics.error(
317 label.clone(),
318 error.line,
319 error.position,
320 error.message,
321 Phase::Parsing,
322 );
323 }
324 let parsed = Some(Source {
325 path: label.clone(),
326 file,
327 });
328 self.documents.insert(
329 id,
330 Document {
331 id,
332 label,
333 scope,
334 version,
335 lines: LineIndex::new(&text),
336 config: None,
337 text,
338 parsed,
339 tokens: token_info,
340 spans,
341 diagnostics,
342 test_line: test_line.filter(|_| self.options.include_tests),
343 },
344 );
345 self.index.take();
346 self.generation += 1;
347 }
348
349 pub fn analyze(&mut self) -> Analysis {
350 let mut diagnostics = DiagnosticsCollector::new();
351 if self.options.cancellation.is_cancelled() {
352 return self.analysis(diagnostics, true);
353 }
354 if self.analyzed_generation == Some(self.generation) {
355 return self.analysis(self.cached_diagnostics.clone(), false);
356 }
357 for (scope, trigger, name) in self.declarations.drain(..) {
358 if scope == Scope::Client && matches!(trigger.as_str(), "proc" | "clientscript") {
359 self.registry.client_script_params.remove(&name);
360 }
361 self.registry.remove_script(&trigger, &name);
362 Arc::make_mut(&mut self.registry.visibility).undeclare(&trigger, &name, scope);
363 }
364 let mut used: HashMap<i32, String> = self
365 .registry
366 .preloaded_script_ids
367 .iter()
368 .map(|(name, &id)| (id, name.clone()))
369 .collect();
370 for ((trigger, name), &id) in &self.allocated {
371 used.insert(id, format!("{trigger}:{name}"));
372 }
373 let mut next = used.keys().copied().max().unwrap_or(-1).checked_add(1);
374 let mut declared = HashSet::new();
375 for document in self.documents.values() {
376 let visibility = Arc::make_mut(&mut self.registry.visibility);
377 visibility.bind(document.label.clone(), document.scope);
378 let Some(source) = &document.parsed else {
379 continue;
380 };
381 for script in &source.file.scripts {
382 if script.trigger == "command" {
383 continue;
384 }
385 visibility.declare(&script.trigger, &script.name, document.scope);
386 self.declarations.push((
387 document.scope,
388 script.trigger.clone(),
389 script.name.clone(),
390 ));
391 }
392 }
393 clientscript::hooks::register_callbacks(
394 &mut self.registry,
395 self.documents
396 .values()
397 .filter(|document| document.scope == Scope::Client)
398 .filter_map(|document| document.parsed.as_ref())
399 .flat_map(|source| &source.file.scripts),
400 );
401 for document in self
402 .documents
403 .values()
404 .filter(|doc| self.target.includes(doc.scope))
405 {
406 if self.options.cancellation.is_cancelled() {
407 return self.analysis(diagnostics, true);
408 }
409 diagnostics.merge(document.diagnostics.clone());
410 let Some(source) = &document.parsed else {
411 continue;
412 };
413 for script in &source.file.scripts {
414 if script.trigger == "command" {
415 continue;
416 }
417 if matches!(self.target, Target::Client(_)) {
418 for statement in &script.body {
419 clientscript::register_variables(statement, &mut self.registry);
420 }
421 }
422 let key = format!("{}:{}", script.trigger, script.name);
423 if !declared.insert(key.clone()) {
424 diagnostics.error(
425 document.label.clone(),
426 script.line,
427 1,
428 format!("Duplicate script [{},{}]", script.trigger, script.name),
429 Phase::SymbolRegistration,
430 );
431 continue;
432 }
433 if matches!(self.target, Target::Client(_))
434 && !matches!(script.trigger.as_str(), "proc" | "clientscript")
435 {
436 diagnostics.error(
437 document.label.clone(),
438 script.line,
439 1,
440 format!("Invalid client trigger {}", script.trigger),
441 Phase::SymbolRegistration,
442 );
443 continue;
444 }
445 if let Some(trigger) = self.registry.trigger(&script.trigger) {
446 if !trigger.allows_returns && !script.return_types.is_empty() {
447 diagnostics.warning(
448 document.label.clone(),
449 script.line,
450 1,
451 format!("Trigger '{}' does not allow return values", script.trigger),
452 Phase::SymbolRegistration,
453 );
454 }
455 } else if self.registry.profile.is_some() {
456 diagnostics.error(
457 document.label.clone(),
458 script.line,
459 1,
460 format!(
461 "Trigger '{}' is not defined by the supplied engine profile",
462 script.trigger
463 ),
464 Phase::SymbolRegistration,
465 );
466 }
467 if let Some(message) = Compiler::validate_trigger_subject(
468 &script.trigger,
469 &script.name,
470 &self.registry,
471 ) {
472 diagnostics.warning(
473 document.label.clone(),
474 script.line,
475 1,
476 message,
477 Phase::SymbolRegistration,
478 );
479 }
480 let allocated_key = (script.trigger.clone(), script.name.clone());
481 let supplied = self.registry.preloaded_script_ids.get(&key).copied();
482 let id = supplied
483 .or_else(|| self.allocated.get(&allocated_key).copied())
484 .or(next);
485 let Some(id) = id.filter(|id| *id >= 0) else {
486 diagnostics.error(
487 document.label.clone(),
488 script.line,
489 1,
490 "Script IDs must fit a nonnegative i32".into(),
491 Phase::SymbolRegistration,
492 );
493 continue;
494 };
495 if let Some(other) = used.get(&id).filter(|other| **other != key) {
496 diagnostics.error(
497 document.label.clone(),
498 script.line,
499 1,
500 format!("Script ID {id} is also assigned to {other}"),
501 Phase::SymbolRegistration,
502 );
503 continue;
504 }
505 used.insert(id, key);
506 next = next.and_then(|next| {
507 if id >= next {
508 id.checked_add(1)
509 } else {
510 Some(next)
511 }
512 });
513 if supplied.is_none() {
514 self.allocated.insert(allocated_key, id);
515 }
516 self.registry
517 .set_script_id(&script.trigger, &script.name, id);
518 self.registry.register_script(
519 script.name.clone(),
520 script.trigger.clone(),
521 script.params.iter().map(|p| p.param_type).collect(),
522 script.return_types.clone(),
523 );
524 if matches!(self.target, Target::Client(_)) {
525 self.registry
526 .set_stack_script(&script.trigger, &script.name);
527 }
528 if document.test_line.is_some_and(|line| script.line >= line) {
529 self.registry
530 .mark_test_script(&script.trigger, &script.name);
531 }
532 }
533 }
534 let mut interface = DefaultHasher::new();
535 self.registry_generation.hash(&mut interface);
536 self.registry.game_vars.len().hash(&mut interface);
537 for document in self.documents.values() {
538 document.id.hash(&mut interface);
539 document.scope.hash(&mut interface);
540 if let Some(source) = &document.parsed {
541 for script in &source.file.scripts {
542 script.trigger.hash(&mut interface);
543 script.name.hash(&mut interface);
544 script.params.len().hash(&mut interface);
545 document
546 .test_line
547 .is_some_and(|line| script.line >= line)
548 .hash(&mut interface);
549 for parameter in &script.params {
550 parameter.param_type.hash(&mut interface);
551 }
552 script.return_types.hash(&mut interface);
553 }
554 }
555 }
556 let interface = interface.finish();
557 let mut checked_documents = 0;
558 for document in self
559 .documents
560 .values()
561 .filter(|doc| self.target.includes(doc.scope))
562 {
563 if self.options.cancellation.is_cancelled() {
564 return self.analysis(diagnostics, true);
565 }
566 if let Some((version, signature, cached)) = self.checked.get(&document.id)
567 && *version == document.version
568 && *signature == interface
569 {
570 diagnostics.merge(cached.clone());
571 continue;
572 }
573 let mut checker = TypeChecker::new(&self.registry);
574 if let Some(source) = &document.parsed {
575 checker.check_file_with_test_boundary(
576 &source.file,
577 &document.label,
578 document.test_line,
579 );
580 }
581 checked_documents += 1;
582 self.checked.insert(
583 document.id,
584 (document.version, interface, checker.diagnostics.clone()),
585 );
586 diagnostics.merge(checker.diagnostics);
587 }
588 self.cached_diagnostics = diagnostics.clone();
589 self.analyzed_generation = Some(self.generation);
590 let mut analysis = self.analysis(diagnostics, false);
591 analysis.checked_documents = checked_documents;
592 analysis
593 }
594
595 fn analysis(&self, diagnostics: DiagnosticsCollector, cancelled: bool) -> Analysis {
596 Analysis {
597 diagnostics,
598 cancelled,
599 checked_documents: 0,
600 assignments: self
601 .allocated
602 .iter()
603 .map(|((trigger, name), &id)| IdAssignment {
604 trigger: trigger.clone(),
605 name: name.clone(),
606 id,
607 })
608 .collect(),
609 }
610 }
611
612 pub fn compile(&mut self) -> Compilation {
613 let mut analysis = self.analyze();
614 if analysis.cancelled || analysis.diagnostics.has_errors() {
615 return Compilation {
616 analysis,
617 artifacts: Vec::new(),
618 };
619 }
620 let mut scripts = Vec::<(SourceId, CompiledScript)>::new();
621 for document in self
622 .documents
623 .values()
624 .filter(|doc| self.target.includes(doc.scope))
625 {
626 if self.options.cancellation.is_cancelled() {
627 analysis.cancelled = true;
628 break;
629 }
630 let Some(source) = &document.parsed else {
631 continue;
632 };
633 let compiled = compile_file(
634 &source.file,
635 &document.label,
636 &self.registry,
637 matches!(self.target, Target::Client(_)),
638 &mut analysis.diagnostics,
639 );
640 scripts.extend(compiled.into_iter().map(|script| (document.id, script)));
641 }
642
643 analysis.cancelled |= self.options.cancellation.is_cancelled();
644 if analysis.cancelled || analysis.diagnostics.has_errors() {
645 return Compilation {
646 analysis,
647 artifacts: Vec::new(),
648 };
649 }
650
651 let check_pointers =
652 self.options.check_pointers || matches!(self.target, Target::Client(_));
653 if check_pointers || self.options.lint {
654 let sources: HashMap<_, _> = self
655 .documents
656 .values()
657 .map(|doc| {
658 (
659 doc.label.to_string_lossy().into_owned(),
660 Arc::new(doc.text.to_string()),
661 )
662 })
663 .collect();
664 let source = SourceProvider::Eager(&sources);
665 let (ids, compiled): (Vec<_>, Vec<_>) = scripts.into_iter().unzip();
666 if check_pointers {
667 let mut checker = match self.target {
668 Target::Client(_) => PointerChecker::new_client(&compiled, &self.registry),
669 Target::Rune => PointerChecker::new(&compiled, &self.registry),
670 };
671 checker.set_source_cache(source);
672 let names = compiled
673 .iter()
674 .map(|script| script.name.clone())
675 .collect::<Vec<_>>();
676 analysis.diagnostics.merge(checker.validate_names(&names));
677 }
678 if self.options.lint {
679 analysis
680 .diagnostics
681 .merge(lints::run_lints(&compiled, Some(source)));
682 }
683 scripts = ids.into_iter().zip(compiled).collect();
684 }
685 let mut artifacts = Vec::with_capacity(scripts.len());
686 for (id, script) in scripts {
687 if self.options.cancellation.is_cancelled() {
688 analysis.cancelled = true;
689 break;
690 }
691 let document = &self.documents[&id];
692 let bytes = match &self.target {
693 Target::Rune => Ok(encode_script(&script)),
694 Target::Client(format) => clientscript::encode(&script, format),
695 };
696 match bytes {
697 Ok(bytes) => artifacts.push(Artifact {
698 source: id,
699 id: script.id,
700 name: script.name,
701 bytes,
702 }),
703 Err(error) => analysis.diagnostics.error(
704 document.label.clone(),
705 1,
706 1,
707 error.to_string(),
708 Phase::CodeGeneration,
709 ),
710 }
711 }
712 analysis.cancelled |= self.options.cancellation.is_cancelled();
713 if analysis.cancelled || analysis.diagnostics.has_errors() {
714 artifacts.clear();
715 }
716 artifacts.sort_by_key(|artifact| artifact.id);
717 Compilation {
718 analysis,
719 artifacts,
720 }
721 }
722
723 pub fn check(&mut self) -> Analysis {
724 let mut analysis = self.analyze();
725 if analysis.cancelled {
726 return analysis;
727 }
728 if let Some((generation, diagnostics)) = &self.checked_full
729 && *generation == self.generation
730 {
731 analysis.diagnostics = diagnostics.clone();
732 return analysis;
733 }
734 let mut compiled = Vec::new();
735 for document in self
736 .documents
737 .values()
738 .filter(|doc| self.target.includes(doc.scope))
739 {
740 if self.options.cancellation.is_cancelled() {
741 analysis.cancelled = true;
742 return analysis;
743 }
744 if analysis.diagnostics.diagnostics().iter().any(|diagnostic| {
745 diagnostic.file == document.label && diagnostic.severity == Severity::Error
746 }) {
747 continue;
748 }
749 let Some(source) = &document.parsed else {
750 continue;
751 };
752 compiled.extend(compile_file(
753 &source.file,
754 &document.label,
755 &self.registry,
756 matches!(self.target, Target::Client(_)),
757 &mut analysis.diagnostics,
758 ));
759 }
760 let sources = self
761 .documents
762 .values()
763 .filter(|doc| doc.config.is_none())
764 .map(|doc| {
765 (
766 doc.label.to_string_lossy().into_owned(),
767 Arc::new(doc.text.to_string()),
768 )
769 })
770 .collect::<HashMap<_, _>>();
771 let source = SourceProvider::Eager(&sources);
772 let lint_diagnostics = lints::run_lints(&compiled, Some(source));
773 if self.options.skip_shared_lints {
774 let shared: HashSet<_> = self
775 .documents
776 .values()
777 .filter(|document| document.scope == Scope::Global)
778 .map(|document| &document.label)
779 .collect();
780 for diagnostic in lint_diagnostics.diagnostics() {
781 if !shared.contains(&diagnostic.file) {
782 analysis.diagnostics.add(diagnostic.clone());
783 }
784 }
785 } else {
786 analysis.diagnostics.merge(lint_diagnostics);
787 }
788 {
789 let mut checker = match self.target {
790 Target::Client(_) => PointerChecker::new_client(&compiled, &self.registry),
791 Target::Rune => PointerChecker::new(&compiled, &self.registry),
792 };
793 checker.set_source_cache(source);
794 let names = compiled
795 .iter()
796 .map(|script| script.name.clone())
797 .collect::<Vec<_>>();
798 analysis.diagnostics.merge(checker.validate_names(&names));
799 }
800 analysis.cancelled = self.options.cancellation.is_cancelled();
801 if !analysis.cancelled {
802 self.checked_full = Some((self.generation, analysis.diagnostics.clone()));
803 }
804 analysis
805 }
806
807 pub fn source_text(&self, label: &Path) -> Option<&str> {
808 self.documents
809 .values()
810 .find(|doc| doc.label == label)
811 .map(|doc| doc.text.as_ref())
812 }
813}