rils_frontend 0.4.0

Static analysis and semantic frontend for the Rils scripting language
Documentation
use std::collections::HashMap;

use rils_host::HostContract;

use crate::{
    FrontendError, SourceId,
    analysis::{DocumentAnalysis, ExternalModuleExport},
    ast::Program,
};

pub fn analyze_with_host(
    source: &str,
    host: &HostContract,
) -> Result<DocumentAnalysis, FrontendError> {
    let tokens = crate::lexer::lex(source).map_err(FrontendError::Lex)?;
    let program = crate::parser::parse(tokens).map_err(FrontendError::Parse)?;
    Ok(analyze_module_with_host(
        &program,
        SourceId::UNKNOWN,
        &[],
        host,
        &HashMap::new(),
    ))
}

pub fn analyze_with_host_and_source_id_and_external_exports(
    source: &str,
    source_id: SourceId,
    host: &HostContract,
    external_exports: &HashMap<String, Vec<ExternalModuleExport>>,
) -> Result<DocumentAnalysis, FrontendError> {
    let tokens = crate::lexer::lex_with_source_id(source, source_id).map_err(FrontendError::Lex)?;
    let program = crate::parser::parse(tokens).map_err(FrontendError::Parse)?;
    Ok(
        analyze_program_with_host_and_source_id_and_external_exports(
            &program,
            source_id,
            host,
            external_exports,
        ),
    )
}

pub fn analyze_program_with_host_and_source_id_and_external_exports(
    program: &Program,
    source_id: SourceId,
    host: &HostContract,
    external_exports: &HashMap<String, Vec<ExternalModuleExport>>,
) -> DocumentAnalysis {
    analyze_module_with_host(program, source_id, &[], host, external_exports)
}

/// Analyze one source in its project namespace using the shared export table.
pub fn analyze_module_with_host(
    program: &Program,
    source_id: SourceId,
    module_path: &[String],
    host: &HostContract,
    external_exports: &HashMap<String, Vec<ExternalModuleExport>>,
) -> DocumentAnalysis {
    analyze_module_with_context(
        program,
        source_id,
        module_path,
        host,
        external_exports,
        None,
    )
}

/// Analyze a document or recovered edit using the owning project's complete declarations.
pub fn analyze_module_with_host_and_declarations(
    program: &Program,
    source_id: SourceId,
    module_path: &[String],
    host: &HostContract,
    external_exports: &crate::exports::ExportTable,
    declarations: &crate::semantic::DeclarationTypeResolver,
) -> DocumentAnalysis {
    analyze_module_with_context(
        program,
        source_id,
        module_path,
        host,
        external_exports,
        Some(declarations),
    )
}

fn analyze_module_with_context(
    program: &Program,
    source_id: SourceId,
    module_path: &[String],
    host: &HostContract,
    external_exports: &crate::exports::ExportTable,
    declarations: Option<&crate::semantic::DeclarationTypeResolver>,
) -> DocumentAnalysis {
    let signatures = host.signatures();
    let host_types = host
        .types()
        .map(|declaration| declaration.name.clone())
        .collect();
    crate::analysis::analyze_program_in_module_with_external_exports_and_host_types(
        program,
        source_id,
        &signatures,
        &host_types,
        external_exports,
        module_path,
        crate::analysis::ModuleAnalysisContext {
            host_contract: Some(host),
            declaration_types: declarations,
        },
    )
}