Skip to main content

scon/
analysis.rs

1use std::collections::{HashMap, HashSet};
2use std::fs;
3use std::path::{Path, PathBuf};
4
5use crate::ast::{
6    ArrayItem, AstValue, Document, LocalMember, ObjectBody, SconPath, Span, StringPart,
7};
8use crate::error::{Error, ErrorCode, Result};
9use crate::limits::LoadOptions;
10use crate::loader::IncludeLoader;
11use crate::source::{self, LineIndex, Token};
12pub use crate::source::{Comment, CommentKind, SourcePosition, SourceRange, Utf16Position};
13use crate::value::Value;
14
15#[derive(Clone, Debug, Default)]
16pub struct ParseOptions {
17    pub file: Option<PathBuf>,
18}
19
20#[derive(Clone, Debug)]
21pub struct FormatOptions {
22    pub indent: usize,
23}
24
25impl Default for FormatOptions {
26    fn default() -> Self {
27        Self { indent: 2 }
28    }
29}
30
31#[derive(Clone, Debug)]
32pub struct Symbol {
33    pub path: Vec<String>,
34    pub file: Option<PathBuf>,
35    pub range: SourceRange,
36}
37
38#[derive(Clone, Copy, Debug, Eq, PartialEq)]
39pub enum DiagnosticSeverity {
40    Error,
41    Warning,
42    Information,
43    Hint,
44}
45
46#[derive(Clone, Debug)]
47pub struct DiagnosticRelatedInformation {
48    pub file: Option<PathBuf>,
49    pub range: SourceRange,
50    pub message: String,
51}
52
53#[derive(Clone, Debug)]
54pub struct Diagnostic {
55    pub code: ErrorCode,
56    pub message: String,
57    pub severity: DiagnosticSeverity,
58    pub file: Option<PathBuf>,
59    pub range: Option<SourceRange>,
60    pub related_information: Vec<DiagnosticRelatedInformation>,
61}
62
63#[derive(Clone, Copy, Debug, Eq, PartialEq)]
64pub enum DefinitionKind {
65    Field,
66}
67
68#[derive(Clone, Debug)]
69pub struct Definition {
70    pub path: Vec<String>,
71    pub kind: DefinitionKind,
72    pub file: Option<PathBuf>,
73    pub range: SourceRange,
74}
75
76#[derive(Clone, Copy, Debug, Eq, PartialEq)]
77pub enum ReferenceKind {
78    Substitution,
79    Interpolation,
80    ObjectSpread,
81    ArraySpread,
82}
83
84#[derive(Clone, Debug)]
85pub struct Reference {
86    pub path: Vec<String>,
87    pub kind: ReferenceKind,
88    pub file: Option<PathBuf>,
89    pub range: SourceRange,
90    pub target: Option<Definition>,
91}
92
93#[derive(Clone, Debug)]
94pub struct IncludeReference {
95    pub path: String,
96    pub file: Option<PathBuf>,
97    pub range: SourceRange,
98    pub resolved_path: Option<PathBuf>,
99}
100
101#[derive(Clone, Debug)]
102pub struct ParsedDocument {
103    pub file: Option<PathBuf>,
104    pub line_index: LineIndex,
105    pub tokens: Vec<Token>,
106    pub comments: Vec<Comment>,
107    pub symbols: Vec<Symbol>,
108}
109
110#[derive(Clone, Debug)]
111pub struct AnalyzedDocument {
112    pub file: Option<PathBuf>,
113    pub parsed: Option<ParsedDocument>,
114    pub diagnostics: Vec<Diagnostic>,
115    pub comments: Vec<Comment>,
116    pub symbols: Vec<Symbol>,
117    pub definitions: Vec<Definition>,
118    pub references: Vec<Reference>,
119    pub includes: Vec<IncludeReference>,
120    pub value: Option<Value>,
121}
122
123pub trait SourceStore {
124    fn read_source(&self, path: &Path) -> std::io::Result<Option<String>>;
125}
126
127#[derive(Clone, Copy, Debug, Default)]
128pub struct FileSourceStore;
129
130impl SourceStore for FileSourceStore {
131    fn read_source(&self, path: &Path) -> std::io::Result<Option<String>> {
132        match fs::read_to_string(path) {
133            Ok(source) => Ok(Some(source)),
134            Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
135            Err(err) => Err(err),
136        }
137    }
138}
139
140pub fn parse_source(source: &str, options: ParseOptions) -> Result<ParsedDocument> {
141    let document = crate::parser::parse_str(source, options.file.clone())?;
142    let line_index = LineIndex::new(source);
143    let tokens = source::collect_tokens(source);
144    let comments = source::comments_from_tokens(source, &line_index, &tokens);
145    let symbols = collect_symbols(
146        &document.body,
147        source,
148        &line_index,
149        options.file.as_ref(),
150        &[],
151    );
152    Ok(ParsedDocument {
153        file: options.file,
154        line_index,
155        tokens,
156        comments,
157        symbols,
158    })
159}
160
161pub fn analyze_source(source: &str, options: ParseOptions) -> AnalyzedDocument {
162    let file = options.file.clone();
163    let document = match crate::parser::parse_str(source, file.clone()) {
164        Ok(document) => document,
165        Err(err) => {
166            let line_index = LineIndex::new(source);
167            let tokens = source::collect_tokens(source);
168            let comments = source::comments_from_tokens(source, &line_index, &tokens);
169            return AnalyzedDocument {
170                file,
171                parsed: None,
172                diagnostics: vec![diagnostic_from_error(&err, source)],
173                comments,
174                symbols: Vec::new(),
175                definitions: Vec::new(),
176                references: Vec::new(),
177                includes: Vec::new(),
178                value: None,
179            };
180        }
181    };
182    analyze_parsed_document(
183        source,
184        document,
185        file,
186        &mut crate::loader::NoopLoader,
187        |_| None,
188    )
189}
190
191pub fn analyze_file(path: impl AsRef<Path>, options: LoadOptions) -> AnalyzedDocument {
192    analyze_file_with_store(path, options, &FileSourceStore)
193}
194
195pub fn analyze_file_with_store(
196    path: impl AsRef<Path>,
197    options: LoadOptions,
198    store: &dyn SourceStore,
199) -> AnalyzedDocument {
200    let path = path.as_ref();
201    let source = store.read_source(path).ok().flatten().unwrap_or_default();
202    let file = Some(path.to_path_buf());
203    let document = match crate::parser::parse_str(&source, file.clone()) {
204        Ok(document) => document,
205        Err(err) => {
206            let line_index = LineIndex::new(&source);
207            let tokens = source::collect_tokens(&source);
208            let comments = source::comments_from_tokens(&source, &line_index, &tokens);
209            return AnalyzedDocument {
210                file,
211                parsed: None,
212                diagnostics: vec![diagnostic_from_error(&err, &source)],
213                comments,
214                symbols: Vec::new(),
215                definitions: Vec::new(),
216                references: Vec::new(),
217                includes: Vec::new(),
218                value: None,
219            };
220        }
221    };
222    let mut loader = AnalysisLoader::new(path, options, store);
223    analyze_parsed_document(&source, document, file, &mut loader, |error| {
224        error
225            .file
226            .as_deref()
227            .and_then(|path| store.read_source(path).ok().flatten())
228    })
229}
230
231pub fn format_source(source: &str, options: FormatOptions) -> Result<String> {
232    let document = crate::parser::parse_str(source, None)?;
233    Ok(SourceFormatter::new(source, options).format_document(&document))
234}
235
236pub fn resolve_source(source: &str, options: ParseOptions) -> Result<Value> {
237    let doc = crate::parser::parse_str(source, options.file)?;
238    crate::eval::eval_document(doc, &mut crate::loader::NoopLoader)
239}
240
241pub fn resolve_file(path: impl AsRef<Path>, options: LoadOptions) -> Result<Value> {
242    crate::parse_file_with_options(path, options)
243}
244
245pub fn get_path<'a>(value: &'a Value, path: &str) -> Result<&'a Value> {
246    let path = parse_path_query(path)?;
247    let mut current = value;
248    for segment in &path {
249        let Value::Object(object) = current else {
250            return Err(Error::new(
251                ErrorCode::TypeMismatch,
252                "path segment requires object",
253            ));
254        };
255        current = object.get(segment).ok_or_else(|| {
256            Error::new(
257                ErrorCode::MissingReference,
258                format!("path {:?} is not defined", path),
259            )
260            .with_path(&path)
261        })?;
262    }
263    Ok(current)
264}
265
266pub fn diagnostic_from_error(error: &Error, source: &str) -> Diagnostic {
267    let line_index = LineIndex::new(source);
268    let range = if let Some(span) = error.span {
269        line_index.source_range(source, span)
270    } else {
271        let byte = line_index
272            .byte_for_line_character(
273                source,
274                error.line.saturating_sub(1),
275                error.column.saturating_sub(1),
276            )
277            .unwrap_or(0);
278        let start = SourcePosition {
279            line: error.line.saturating_sub(1),
280            character: error.column.saturating_sub(1),
281            byte,
282        };
283        let end = SourcePosition {
284            line: start.line,
285            character: start.character + 1,
286            byte: start.byte.saturating_add(1),
287        };
288        SourceRange { start, end }
289    };
290    Diagnostic {
291        code: error.code,
292        message: error.message.clone(),
293        severity: DiagnosticSeverity::Error,
294        file: error.file.clone(),
295        range: Some(range),
296        related_information: related_information_from_error(error, source),
297    }
298}
299
300fn analyze_parsed_document(
301    source: &str,
302    document: Document,
303    file: Option<PathBuf>,
304    loader: &mut dyn IncludeLoader,
305    diagnostic_source: impl Fn(&Error) -> Option<String>,
306) -> AnalyzedDocument {
307    let line_index = LineIndex::new(source);
308    let tokens = source::collect_tokens(source);
309    let comments = source::comments_from_tokens(source, &line_index, &tokens);
310    let mut semantic = SemanticCollector::new(source, &line_index, file.as_ref());
311    semantic.collect_body(&document.body, &[]);
312    let symbols = collect_symbols(&document.body, source, &line_index, file.as_ref(), &[]);
313    let definitions = semantic.definitions;
314    let references = semantic.references;
315    let includes = semantic.includes;
316    let parsed = ParsedDocument {
317        file: file.clone(),
318        line_index,
319        tokens,
320        comments: comments.clone(),
321        symbols: symbols.clone(),
322    };
323    let (diagnostics, value) = match crate::eval::eval_document(document, loader) {
324        Ok(value) => (Vec::new(), Some(value)),
325        Err(err) => {
326            let source = diagnostic_source(&err).unwrap_or_else(|| source.to_string());
327            (vec![diagnostic_from_error(&err, &source)], None)
328        }
329    };
330    AnalyzedDocument {
331        file,
332        parsed: Some(parsed),
333        diagnostics,
334        comments,
335        symbols,
336        definitions,
337        references,
338        includes,
339        value,
340    }
341}
342
343fn related_information_from_error(
344    error: &Error,
345    source: &str,
346) -> Vec<DiagnosticRelatedInformation> {
347    error
348        .include_stack
349        .iter()
350        .map(|path| DiagnosticRelatedInformation {
351            file: Some(path.clone()),
352            range: LineIndex::new(source).source_range(source, Span::default()),
353            message: "included from here".to_string(),
354        })
355        .collect()
356}
357
358#[derive(Clone, Debug)]
359struct FormatComment {
360    line: usize,
361    text: String,
362    span: Span,
363    emitted: bool,
364}
365
366struct SourceFormatter<'a> {
367    source: &'a str,
368    options: FormatOptions,
369    line_index: LineIndex,
370    comments: Vec<FormatComment>,
371}
372
373impl<'a> SourceFormatter<'a> {
374    fn new(source: &'a str, options: FormatOptions) -> Self {
375        let line_index = LineIndex::new(source);
376        let tokens = source::collect_tokens(source);
377        let comments = tokens
378            .into_iter()
379            .filter_map(|token| {
380                if !matches!(token.kind, source::TokenKind::Comment(_)) {
381                    return None;
382                }
383                Some(FormatComment {
384                    line: line_index
385                        .source_position(source, token.span.start_byte)
386                        .line,
387                    text: source[token.span.start_byte..token.span.end_byte].to_string(),
388                    span: token.span,
389                    emitted: false,
390                })
391            })
392            .collect();
393        Self {
394            source,
395            options,
396            line_index,
397            comments,
398        }
399    }
400
401    fn format_document(mut self, document: &Document) -> String {
402        let mut out = String::new();
403        self.emit_body(&document.body, 0, &mut out);
404        self.emit_comments_before(document.span.end_byte, 0, &mut out);
405        if !out.ends_with('\n') {
406            out.push('\n');
407        }
408        out
409    }
410
411    fn emit_body(&mut self, body: &ObjectBody, indent: usize, out: &mut String) {
412        let mut entries = body_entries(body);
413        entries.sort_by_key(BodyEntry::start_byte);
414        for entry in entries {
415            self.emit_comments_before(entry.start_byte(), indent, out);
416            match entry {
417                BodyEntry::Spread(spread) => {
418                    self.write_indent(out, indent);
419                    out.push_str("...");
420                    out.push_str(&format_substitution(&spread.path));
421                    self.emit_inline_comment(spread.span, out);
422                    out.push('\n');
423                }
424                BodyEntry::Include {
425                    path, path_span, ..
426                } => {
427                    self.write_indent(out, indent);
428                    out.push_str("include ");
429                    out.push_str(&format_string_literal(path));
430                    self.emit_inline_comment(*path_span, out);
431                    out.push('\n');
432                }
433                BodyEntry::Field(field) => {
434                    self.emit_field(field, indent, out);
435                }
436            }
437        }
438        self.emit_comments_before(body.span.end_byte, indent, out);
439    }
440
441    fn emit_field(&mut self, field: &crate::ast::Field, indent: usize, out: &mut String) {
442        self.write_indent(out, indent);
443        out.push_str(&format_path(&field.path));
444        match &field.value {
445            AstValue::Object { body, .. } => {
446                if field_uses_equals(self.source, field) {
447                    out.push_str(" = {");
448                } else {
449                    out.push_str(" {");
450                }
451                self.emit_inline_comment(field_header_span(self.source, field), out);
452                out.push('\n');
453                self.emit_body(body, indent + self.options.indent, out);
454                self.write_indent(out, indent);
455                out.push('}');
456                self.emit_inline_comment(field.span, out);
457                out.push('\n');
458            }
459            value => {
460                out.push_str(" = ");
461                self.emit_value(value, indent, out);
462                self.emit_inline_comment(field.span, out);
463                out.push('\n');
464            }
465        }
466    }
467
468    fn emit_value(&mut self, value: &AstValue, indent: usize, out: &mut String) {
469        match value {
470            AstValue::Object { body, .. } => {
471                out.push('{');
472                out.push('\n');
473                self.emit_body(body, indent + self.options.indent, out);
474                self.write_indent(out, indent);
475                out.push('}');
476            }
477            AstValue::Array { items, span } => self.emit_array(items, *span, indent, out),
478            AstValue::String { span, .. }
479            | AstValue::Number { span, .. }
480            | AstValue::Bool { span, .. }
481            | AstValue::Null { span } => out.push_str(self.span_text(*span).trim()),
482            AstValue::Substitution { path, .. } => out.push_str(&format_substitution(path)),
483        }
484    }
485
486    fn emit_array(&mut self, items: &[ArrayItem], span: Span, indent: usize, out: &mut String) {
487        if items.is_empty() {
488            out.push_str("[]");
489            return;
490        }
491        out.push('[');
492        out.push('\n');
493        for (index, item) in items.iter().enumerate() {
494            self.emit_comments_before(array_item_start(item), indent + self.options.indent, out);
495            self.write_indent(out, indent + self.options.indent);
496            match item {
497                ArrayItem::Value(value) => {
498                    self.emit_value(value, indent + self.options.indent, out)
499                }
500                ArrayItem::Spread { path, .. } => {
501                    out.push_str("...");
502                    out.push_str(&format_substitution(path));
503                }
504            }
505            if index + 1 != items.len() {
506                out.push(',');
507            }
508            self.emit_inline_comment(array_item_span(item), out);
509            out.push('\n');
510        }
511        self.emit_comments_before(span.end_byte, indent + self.options.indent, out);
512        self.write_indent(out, indent);
513        out.push(']');
514    }
515
516    fn emit_comments_before(&mut self, byte: usize, indent: usize, out: &mut String) {
517        let mut index = 0;
518        while index < self.comments.len() {
519            if self.comments[index].emitted || self.comments[index].span.start_byte >= byte {
520                index += 1;
521                continue;
522            }
523            self.write_indent(out, indent);
524            out.push_str(self.comments[index].text.trim());
525            out.push('\n');
526            self.comments[index].emitted = true;
527            index += 1;
528        }
529    }
530
531    fn emit_inline_comment(&mut self, span: Span, out: &mut String) {
532        let line = self
533            .line_index
534            .source_position(self.source, span.start_byte)
535            .line;
536        if let Some(comment) = self.comments.iter_mut().find(|comment| {
537            !comment.emitted
538                && comment.line == line
539                && comment.span.start_byte >= span.start_byte
540                && comment.span.start_byte >= span.end_byte
541        }) {
542            out.push(' ');
543            out.push_str(comment.text.trim());
544            comment.emitted = true;
545        }
546    }
547
548    fn span_text(&self, span: Span) -> &str {
549        self.source
550            .get(span.start_byte..span.end_byte)
551            .unwrap_or_default()
552    }
553
554    fn write_indent(&self, out: &mut String, indent: usize) {
555        for _ in 0..indent {
556            out.push(' ');
557        }
558    }
559}
560
561enum BodyEntry<'a> {
562    Spread(&'a crate::ast::ObjectSpread),
563    Include {
564        path: &'a str,
565        path_span: &'a Span,
566        span: &'a Span,
567    },
568    Field(&'a crate::ast::Field),
569}
570
571impl BodyEntry<'_> {
572    fn start_byte(&self) -> usize {
573        match self {
574            BodyEntry::Spread(spread) => spread.span.start_byte,
575            BodyEntry::Include { span, .. } => span.start_byte,
576            BodyEntry::Field(field) => field.span.start_byte,
577        }
578    }
579}
580
581fn body_entries(body: &ObjectBody) -> Vec<BodyEntry<'_>> {
582    let mut entries = Vec::with_capacity(body.spreads.len() + body.members.len());
583    entries.extend(body.spreads.iter().map(BodyEntry::Spread));
584    for member in &body.members {
585        match member {
586            LocalMember::Include {
587                path,
588                path_span,
589                span,
590                ..
591            } => entries.push(BodyEntry::Include {
592                path,
593                path_span,
594                span,
595            }),
596            LocalMember::Field(field) => entries.push(BodyEntry::Field(field)),
597        }
598    }
599    entries
600}
601
602fn field_uses_equals(source: &str, field: &crate::ast::Field) -> bool {
603    source
604        .get(field.path_span.end_byte..field.value_start_byte())
605        .is_some_and(|text| text.contains('='))
606}
607
608fn field_header_span(source: &str, field: &crate::ast::Field) -> Span {
609    let end_byte = source
610        .get(field.path_span.end_byte..field.span.end_byte)
611        .and_then(|text| text.find('{'))
612        .map(|offset| field.path_span.end_byte + offset + 1)
613        .unwrap_or(field.path_span.end_byte);
614    Span::new(field.span.start_byte, end_byte)
615}
616
617trait AstValueExt {
618    fn start_byte(&self) -> usize;
619}
620
621impl AstValueExt for AstValue {
622    fn start_byte(&self) -> usize {
623        match self {
624            AstValue::Object { span, .. }
625            | AstValue::Array { span, .. }
626            | AstValue::String { span, .. }
627            | AstValue::Number { span, .. }
628            | AstValue::Bool { span, .. }
629            | AstValue::Null { span }
630            | AstValue::Substitution { span, .. } => span.start_byte,
631        }
632    }
633}
634
635trait FieldExt {
636    fn value_start_byte(&self) -> usize;
637}
638
639impl FieldExt for crate::ast::Field {
640    fn value_start_byte(&self) -> usize {
641        self.value.start_byte()
642    }
643}
644
645fn array_item_start(item: &ArrayItem) -> usize {
646    array_item_span(item).start_byte
647}
648
649fn array_item_span(item: &ArrayItem) -> Span {
650    match item {
651        ArrayItem::Value(value) => match value {
652            AstValue::Object { span, .. }
653            | AstValue::Array { span, .. }
654            | AstValue::String { span, .. }
655            | AstValue::Number { span, .. }
656            | AstValue::Bool { span, .. }
657            | AstValue::Null { span }
658            | AstValue::Substitution { span, .. } => *span,
659        },
660        ArrayItem::Spread { span, .. } => *span,
661    }
662}
663
664fn format_substitution(path: &SconPath) -> String {
665    format!("${{{}}}", format_path(path))
666}
667
668fn format_path(path: &SconPath) -> String {
669    path.iter()
670        .map(|segment| {
671            if is_unquoted_key(segment) {
672                segment.clone()
673            } else {
674                format_string_literal(segment)
675            }
676        })
677        .collect::<Vec<_>>()
678        .join(".")
679}
680
681fn is_unquoted_key(segment: &str) -> bool {
682    if matches!(segment, "include" | "true" | "false" | "null") {
683        return false;
684    }
685    let mut chars = segment.chars();
686    let Some(first) = chars.next() else {
687        return false;
688    };
689    (first.is_ascii_alphabetic() || first == '_')
690        && chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '-')
691}
692
693fn format_string_literal(text: &str) -> String {
694    let mut out = String::from("\"");
695    let mut chars = text.chars().peekable();
696    while let Some(ch) = chars.next() {
697        match ch {
698            '"' => out.push_str("\\\""),
699            '\\' => out.push_str("\\\\"),
700            '\n' => out.push_str("\\n"),
701            '\r' => out.push_str("\\r"),
702            '\t' => out.push_str("\\t"),
703            '$' if chars.peek() == Some(&'{') => out.push_str("\\$"),
704            ch if ch.is_control() => {
705                use std::fmt::Write;
706                let _ = write!(out, "\\u{:04x}", ch as u32);
707            }
708            ch => out.push(ch),
709        }
710    }
711    out.push('"');
712    out
713}
714
715fn collect_symbols(
716    body: &ObjectBody,
717    source: &str,
718    line_index: &LineIndex,
719    file: Option<&PathBuf>,
720    parent_path: &[String],
721) -> Vec<Symbol> {
722    let mut symbols = Vec::new();
723    for member in &body.members {
724        let LocalMember::Field(field) = member else {
725            continue;
726        };
727        let mut path = parent_path.to_vec();
728        path.extend(field.path.iter().cloned());
729        symbols.push(Symbol {
730            path: path.clone(),
731            file: file.cloned(),
732            range: line_index.source_range(source, field.path_span),
733        });
734        if let AstValue::Object { body, .. } = &field.value {
735            symbols.extend(collect_symbols(body, source, line_index, file, &path));
736        }
737    }
738    symbols
739}
740
741struct SemanticCollector<'a> {
742    source: &'a str,
743    line_index: &'a LineIndex,
744    file: Option<&'a PathBuf>,
745    completed_definitions: HashMap<Vec<String>, Definition>,
746    definitions: Vec<Definition>,
747    references: Vec<Reference>,
748    includes: Vec<IncludeReference>,
749}
750
751impl<'a> SemanticCollector<'a> {
752    fn new(source: &'a str, line_index: &'a LineIndex, file: Option<&'a PathBuf>) -> Self {
753        Self {
754            source,
755            line_index,
756            file,
757            completed_definitions: HashMap::new(),
758            definitions: Vec::new(),
759            references: Vec::new(),
760            includes: Vec::new(),
761        }
762    }
763
764    fn collect_body(&mut self, body: &ObjectBody, parent_path: &[String]) {
765        for spread in &body.spreads {
766            self.push_reference(&spread.path, ReferenceKind::ObjectSpread, spread.path_span);
767        }
768
769        for member in &body.members {
770            match member {
771                LocalMember::Include {
772                    path,
773                    path_span,
774                    loc,
775                    ..
776                } => {
777                    self.includes.push(IncludeReference {
778                        path: path.clone(),
779                        file: self.file.cloned(),
780                        range: self.source_range(*path_span),
781                        resolved_path: resolve_include_path(self.file, path, loc).ok(),
782                    });
783                }
784                LocalMember::Field(field) => {
785                    let full_path = join_paths(parent_path, &field.path);
786                    match &field.value {
787                        AstValue::Object { body, .. } => {
788                            self.push_definition(full_path.clone(), field.path_span);
789                            self.collect_body(body, &full_path);
790                        }
791                        value => {
792                            self.collect_value(value);
793                            self.push_definition(full_path, field.path_span);
794                        }
795                    }
796                }
797            }
798        }
799    }
800
801    fn collect_value(&mut self, value: &AstValue) {
802        match value {
803            AstValue::Object { body, .. } => self.collect_body(body, &[]),
804            AstValue::Array { items, .. } => {
805                for item in items {
806                    match item {
807                        ArrayItem::Value(value) => self.collect_value(value),
808                        ArrayItem::Spread {
809                            path, path_span, ..
810                        } => self.push_reference(path, ReferenceKind::ArraySpread, *path_span),
811                    }
812                }
813            }
814            AstValue::String { value, .. } => {
815                if let crate::ast::StringValue::Parts(parts) = value {
816                    for part in parts {
817                        if let StringPart::Interpolation {
818                            path, path_span, ..
819                        } = part
820                        {
821                            self.push_reference(path, ReferenceKind::Interpolation, *path_span);
822                        }
823                    }
824                }
825            }
826            AstValue::Substitution {
827                path, path_span, ..
828            } => self.push_reference(path, ReferenceKind::Substitution, *path_span),
829            AstValue::Number { .. } | AstValue::Bool { .. } | AstValue::Null { .. } => {}
830        }
831    }
832
833    fn push_definition(&mut self, path: Vec<String>, span: Span) {
834        let definition = Definition {
835            path: path.clone(),
836            kind: DefinitionKind::Field,
837            file: self.file.cloned(),
838            range: self.source_range(span),
839        };
840        self.completed_definitions.insert(path, definition.clone());
841        self.definitions.push(definition);
842    }
843
844    fn push_reference(&mut self, path: &SconPath, kind: ReferenceKind, span: Span) {
845        let path = path.iter().cloned().collect::<Vec<_>>();
846        let target = self.completed_definitions.get(&path).cloned();
847        self.references.push(Reference {
848            path,
849            kind,
850            file: self.file.cloned(),
851            range: self.source_range(span),
852            target,
853        });
854    }
855
856    fn source_range(&self, span: Span) -> SourceRange {
857        self.line_index.source_range(self.source, span)
858    }
859}
860
861fn join_paths(parent: &[String], child: &SconPath) -> Vec<String> {
862    let mut path = Vec::with_capacity(parent.len() + child.len());
863    path.extend(parent.iter().cloned());
864    path.extend(child.iter().cloned());
865    path
866}
867
868fn resolve_include_path(
869    file: Option<&PathBuf>,
870    include_path: &str,
871    loc: &crate::ast::Location,
872) -> Result<PathBuf> {
873    validate_include_path(include_path, loc.clone())?;
874    let base = file
875        .and_then(|path| path.parent())
876        .unwrap_or_else(|| Path::new("."));
877    Ok(base.join(include_path))
878}
879
880struct AnalysisLoader<'a> {
881    include_root: PathBuf,
882    options: LoadOptions,
883    store: &'a dyn SourceStore,
884    cache: HashMap<PathBuf, Document>,
885    stack: Vec<PathBuf>,
886    seen: HashSet<PathBuf>,
887}
888
889impl<'a> AnalysisLoader<'a> {
890    fn new(entry: &Path, mut options: LoadOptions, store: &'a dyn SourceStore) -> Self {
891        let entry = normalize_path(entry);
892        let default_root = entry
893            .parent()
894            .map(Path::to_path_buf)
895            .unwrap_or_else(|| PathBuf::from("."));
896        let include_root = options.include_root.take().unwrap_or(default_root);
897        Self {
898            include_root: normalize_path(&include_root),
899            options,
900            store,
901            cache: HashMap::new(),
902            stack: Vec::new(),
903            seen: HashSet::new(),
904        }
905    }
906
907    fn load_canonical(&mut self, path: PathBuf) -> Result<Document> {
908        if let Some(doc) = self.cache.get(&path) {
909            return Ok(doc.clone());
910        }
911        if self.stack.contains(&path) {
912            return Err(Error::new(
913                ErrorCode::IncludeCycle,
914                format!("include cycle: {}", path.display()),
915            ));
916        }
917        if self.stack.len() >= self.options.limits.max_include_depth {
918            return Err(Error::new(
919                ErrorCode::ResourceLimitExceeded,
920                "maximum include depth exceeded",
921            ));
922        }
923        self.stack.push(path.clone());
924        self.seen.insert(path.clone());
925        if self.seen.len() > self.options.limits.max_include_files {
926            return Err(Error::new(
927                ErrorCode::ResourceLimitExceeded,
928                "maximum include file count exceeded",
929            ));
930        }
931        let source = self
932            .store
933            .read_source(&path)
934            .map_err(|err| {
935                Error::new(
936                    ErrorCode::IncludeNotFound,
937                    format!("failed to read include file: {err}"),
938                )
939            })?
940            .ok_or_else(|| {
941                Error::new(
942                    ErrorCode::IncludeNotFound,
943                    format!("include file not found: {}", path.display()),
944                )
945            })?;
946        if source.len() > self.options.limits.max_file_size {
947            return Err(Error::new(
948                ErrorCode::ResourceLimitExceeded,
949                "maximum file size exceeded",
950            ));
951        }
952        let doc = crate::parser::parse_str(&source, Some(path.clone())).map_err(|err| Error {
953            code: if err.code == ErrorCode::InvalidRootType {
954                ErrorCode::IncludeRootTypeError
955            } else {
956                ErrorCode::IncludeParseError
957            },
958            ..err
959        })?;
960        self.stack.pop();
961        self.cache.insert(path, doc.clone());
962        Ok(doc)
963    }
964
965    fn resolve_include(
966        &self,
967        including_file: Option<&Path>,
968        include_path: &str,
969        loc: crate::ast::Location,
970    ) -> Result<PathBuf> {
971        validate_include_path(include_path, loc.clone())?;
972        let base = including_file
973            .and_then(Path::parent)
974            .unwrap_or(&self.include_root);
975        let candidate = normalize_path(&base.join(include_path));
976        if !candidate.starts_with(&self.include_root) {
977            return Err(Error::new(
978                ErrorCode::IncludePathDenied,
979                "include path escapes include root",
980            )
981            .at(loc));
982        }
983        Ok(candidate)
984    }
985}
986
987impl IncludeLoader for AnalysisLoader<'_> {
988    fn load_include(
989        &mut self,
990        including_file: Option<&Path>,
991        path: &str,
992        loc: crate::ast::Location,
993    ) -> Result<Document> {
994        let resolved = self.resolve_include(including_file, path, loc.clone())?;
995        self.load_canonical(resolved)
996            .map_err(|err| attach_include_location(err, loc))
997    }
998}
999
1000fn attach_include_location(err: Error, loc: crate::ast::Location) -> Error {
1001    if err.span.is_none() && err.file.is_none() {
1002        err.at(loc)
1003    } else {
1004        err
1005    }
1006}
1007
1008fn validate_include_path(include_path: &str, loc: crate::ast::Location) -> Result<()> {
1009    if include_path.contains("://")
1010        || include_path.starts_with("classpath:")
1011        || include_path.contains('*')
1012        || include_path.starts_with('~')
1013        || include_path.starts_with('$')
1014        || include_path.starts_with('/')
1015        || looks_like_windows_absolute(include_path)
1016    {
1017        return Err(Error::new(ErrorCode::InvalidIncludePath, "invalid include path").at(loc));
1018    }
1019    Ok(())
1020}
1021
1022fn normalize_path(path: &Path) -> PathBuf {
1023    path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
1024}
1025
1026fn looks_like_windows_absolute(path: &str) -> bool {
1027    let bytes = path.as_bytes();
1028    bytes.len() >= 3 && bytes[1] == b':' && bytes[2] == b'/'
1029}
1030
1031fn parse_path_query(path: &str) -> Result<Vec<String>> {
1032    let mut parts = Vec::new();
1033    let mut current = String::new();
1034    let mut chars = path.chars().peekable();
1035    while let Some(ch) = chars.next() {
1036        match ch {
1037            '.' if !current.is_empty() => {
1038                parts.push(std::mem::take(&mut current));
1039            }
1040            '"' if current.is_empty() => {
1041                while let Some(ch) = chars.next() {
1042                    match ch {
1043                        '"' => break,
1044                        '\\' => {
1045                            let Some(escaped) = chars.next() else {
1046                                return Err(Error::new(
1047                                    ErrorCode::InvalidEscape,
1048                                    "unterminated quoted path escape",
1049                                ));
1050                            };
1051                            current.push(escaped);
1052                        }
1053                        ch => current.push(ch),
1054                    }
1055                }
1056            }
1057            ch => current.push(ch),
1058        }
1059    }
1060    if !current.is_empty() {
1061        parts.push(current);
1062    }
1063    if parts.is_empty() {
1064        return Err(Error::new(ErrorCode::UnexpectedToken, "empty path"));
1065    }
1066    Ok(parts)
1067}