leo-span 4.0.1

Span handling for the Leo programming language
Documentation
// Copyright (C) 2019-2026 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

//! Builds the symbol strings into `Symbol` values.

use std::{
    env,
    fmt,
    fs,
    io::{BufWriter, Read, Write},
    path::{Path, PathBuf},
    process::ExitCode,
};

const SYMBOLS_FILE: &str = "symbols.txt";
const SYMBOLS_GENERATED_FILE: &str = "symbols_generated.rs";
const PRE_DEFINED_HEADER: &str = "// @generated by build.rs. Do not edit.\nconst PRE_DEFINED: &[&str] = &[\n";
const SYM_HEADER: &str = "];\n\n#[allow(non_upper_case_globals)]\npub mod sym {\n";
const SYM_FOOTER: &str = "}\n";

struct SymbolLine<'a> {
    ident: &'a str,
    literal: Option<&'a str>,
}

type BuildResult<T> = Result<T, String>;

fn main() -> ExitCode {
    match run() {
        Ok(()) => ExitCode::SUCCESS,
        Err(err) => {
            eprintln!("cargo:warning={err}");
            ExitCode::FAILURE
        }
    }
}

fn run() -> BuildResult<()> {
    let manifest_dir = manifest_dir()?;
    let input_path = manifest_dir.join(SYMBOLS_FILE);
    println!("cargo:rerun-if-changed={}", input_path.display());

    let input = read_symbols(&input_path)?;
    let symbols = parse_symbols(&input, &input_path)?;
    if symbols.is_empty() {
        return Err(format!("no symbols found in {}", input_path.display()));
    }

    let out_dir = out_dir()?;
    let out_path = out_dir.join(SYMBOLS_GENERATED_FILE);
    let output = fs::File::create(&out_path).map_err(|source| io_error("create", &out_path, source))?;
    let mut output = BufWriter::new(output);

    write_output(&mut output, PRE_DEFINED_HEADER, &out_path)?;
    emit_pre_defined(&symbols, &mut output, &out_path)?;

    write_output(&mut output, SYM_HEADER, &out_path)?;
    emit_sym_module(&symbols, &mut output, &out_path)?;

    write_output(&mut output, SYM_FOOTER, &out_path)?;
    output.flush().map_err(|source| io_error("write", &out_path, source))?;
    Ok(())
}

fn manifest_dir() -> BuildResult<PathBuf> {
    env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).map_err(|source| missing_env_var("CARGO_MANIFEST_DIR", source))
}

fn out_dir() -> BuildResult<PathBuf> {
    env::var("OUT_DIR").map(PathBuf::from).map_err(|source| missing_env_var("OUT_DIR", source))
}

fn read_symbols(path: &Path) -> BuildResult<String> {
    let input_len = fs::metadata(path).map(|meta| meta.len() as usize).unwrap_or(0);
    let mut input = String::with_capacity(input_len);
    let mut file = fs::File::open(path).map_err(|source| io_error("open", path, source))?;
    file.read_to_string(&mut input).map_err(|source| io_error("read", path, source))?;
    Ok(input)
}

fn is_valid_ident(ident: &str) -> bool {
    let mut chars = ident.chars();

    chars.next().is_some_and(|first| {
        (first == '_' || first.is_ascii_alphabetic()) && chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
    })
}

fn parse_symbol_line<'a>(line: &'a str, path: &Path, line_no: usize) -> BuildResult<Option<SymbolLine<'a>>> {
    let trimmed = line.trim();
    if trimmed.is_empty() || trimmed.starts_with("//") {
        return Ok(None);
    }

    let (ident, literal) = match trimmed.split_once(':') {
        Some((left, right)) => {
            let ident = left.trim();
            let literal = right.trim();
            if ident.is_empty() || literal.is_empty() {
                return Err(format!("invalid symbol entry at {}:{line_no}", path.display()));
            }
            if !(literal.starts_with('"') && literal.ends_with('"')) {
                return Err(format!("invalid symbol literal for '{ident}' at {}:{line_no}", path.display()));
            }
            (ident, Some(literal))
        }
        None => (trimmed, None),
    };

    if !is_valid_ident(ident) {
        return Err(format!("invalid symbol identifier '{ident}' at {}:{line_no}", path.display()));
    }

    Ok(Some(SymbolLine { ident, literal }))
}

fn parse_symbols<'a>(input: &'a str, path: &Path) -> BuildResult<Vec<SymbolLine<'a>>> {
    input
        .lines()
        .enumerate()
        .map(|(i, line)| parse_symbol_line(line, path, i + 1))
        .filter_map(Result::transpose)
        .collect()
}

fn missing_env_var(name: &'static str, source: env::VarError) -> String {
    format!("missing environment variable {name}: {source}")
}

fn io_error(action: &'static str, path: &Path, source: std::io::Error) -> String {
    format!("failed to {action} {}: {source}", path.display())
}

fn write_output(output: &mut impl Write, contents: &str, out_path: &Path) -> BuildResult<()> {
    output.write_all(contents.as_bytes()).map_err(|source| io_error("write", out_path, source))
}

fn write_output_fmt(output: &mut impl Write, out_path: &Path, args: fmt::Arguments<'_>) -> BuildResult<()> {
    output.write_fmt(args).map_err(|source| io_error("write", out_path, source))
}

fn emit_pre_defined(symbols: &[SymbolLine<'_>], output: &mut impl Write, out_path: &Path) -> BuildResult<()> {
    for symbol in symbols {
        match symbol.literal {
            Some(literal) => write_output_fmt(output, out_path, format_args!("    {literal},\n"))?,
            None => write_output_fmt(output, out_path, format_args!("    {:?},\n", symbol.ident))?,
        }
    }

    Ok(())
}

fn emit_sym_module(symbols: &[SymbolLine<'_>], output: &mut impl Write, out_path: &Path) -> BuildResult<()> {
    for (index, symbol) in symbols.iter().enumerate() {
        write_output_fmt(
            output,
            out_path,
            format_args!(
                "    pub const {}: crate::symbol::Symbol = crate::symbol::Symbol::new({index});\n",
                symbol.ident
            ),
        )?;
    }

    Ok(())
}