rhaiper 0.2.0

Minifier for Rhai scripts, with Source Map v3 output
Documentation
//! An [`InputStream`] over a `&str` that tracks byte offsets.
//!
//! Rhai's own `MultiInputsStream` reads through `Peekable<Chars>` and keeps un-got characters in a
//! side buffer, so it cannot report where in the source it currently is. Byte offsets are what let
//! us slice verbatim token text and build source mappings, so we supply our own stream instead.

use rhai::{InputStream, Position};

/// A `&str` input stream that exposes its current byte offset.
///
/// The whole implementation is a byte cursor with no pushback buffer: every `unget` in rhai's
/// tokenizer returns a character it had just consumed, so ungetting is exactly a cursor rewind.
pub struct StrStream<'a> {
    src: &'a str,
    cursor: usize,
}

impl<'a> StrStream<'a> {
    pub fn new(src: &'a str) -> Self {
        Self { src, cursor: 0 }
    }

    /// Byte offset of the next character to be read.
    pub fn byte_offset(&self) -> usize {
        self.cursor
    }

    pub fn src(&self) -> &'a str {
        self.src
    }

    fn remainder(&self) -> &'a str {
        &self.src[self.cursor..]
    }
}

impl InputStream for StrStream<'_> {
    fn unget(&mut self, ch: char) {
        let width = ch.len_utf8();
        debug_assert!(
            self.src[..self.cursor].ends_with(ch),
            "unget({ch:?}) does not match the character before the cursor at {}",
            self.cursor
        );
        self.cursor -= width;
    }

    fn get_next(&mut self) -> Option<char> {
        let ch = self.remainder().chars().next()?;
        self.cursor += ch.len_utf8();
        Some(ch)
    }

    fn peek_next(&mut self) -> Option<char> {
        self.remainder().chars().next()
    }
}

/// Converts a rhai [`Position`] into a byte offset, and back.
///
/// Rhai counts columns in **characters**, 1-based, where `0` means beginning-of-line. Both
/// directions are needed: forward to locate AST nodes in the token table, and backward to emit
/// Source Map v3 columns (which are 0-based UTF-16 code units).
pub struct LineIndex {
    /// Byte offset of the start of each line, indexed by 0-based line number.
    starts: Vec<usize>,
}

impl LineIndex {
    pub fn new(src: &str) -> Self {
        let mut starts = vec![0];
        starts.extend(src.match_indices('\n').map(|(i, _)| i + 1));
        Self { starts }
    }

    /// Byte offset of the 1-based `line`, or `None` if the line is out of range.
    pub fn line_start(&self, line: usize) -> Option<usize> {
        line.checked_sub(1)
            .and_then(|i| self.starts.get(i))
            .copied()
    }

    /// Converts a 0-based character column to a 0-based UTF-16 column.
    ///
    /// Rhai counts columns in characters; Source Map v3 counts them in UTF-16 code units. The two
    /// agree except on lines holding astral-plane characters, where each counts as one character but
    /// two UTF-16 units.
    pub fn utf16_col(&self, text: &str, line: u32, char_col: u32) -> u32 {
        let Some(start) = self.line_start(line as usize + 1) else {
            return char_col;
        };
        text[start..]
            .chars()
            .take_while(|&c| c != '\n')
            .take(char_col as usize)
            .map(|c| c.len_utf16() as u32)
            .sum()
    }

    /// Converts a 0-based UTF-16 column back to a 0-based character column.
    pub fn char_col(&self, text: &str, line: u32, utf16_col: u32) -> u32 {
        let Some(start) = self.line_start(line as usize + 1) else {
            return utf16_col;
        };
        let mut remaining = utf16_col;
        let mut chars = 0;
        for ch in text[start..].chars().take_while(|&c| c != '\n') {
            let width = ch.len_utf16() as u32;
            if remaining < width {
                break;
            }
            remaining -= width;
            chars += 1;
        }
        chars
    }

    /// 0-based line and character column of a byte offset.
    ///
    /// The reverse of [`byte_offset_of`], and preferred when a byte offset is already known: it is
    /// total, whereas rhai reports [`Position::NONE`] for some tokens.
    ///
    /// [`byte_offset_of`]: Self::byte_offset_of
    pub fn line_col_of(&self, src: &str, offset: usize) -> (u32, u32) {
        let line = self.starts.partition_point(|&start| start <= offset) - 1;
        let col = src[self.starts[line]..offset].chars().count();
        (line as u32, col as u32)
    }

    /// Byte offset of a rhai [`Position`] within `src`.
    ///
    /// Returns `None` for [`Position::NONE`] and for positions outside the source, which the
    /// parser does produce for synthesized AST nodes.
    pub fn byte_offset_of(&self, src: &str, pos: Position) -> Option<usize> {
        let (line, col) = (pos.line()?, pos.position()?);
        let start = self.line_start(line)?;
        let line_text = &src[start..];
        // `col` is 1-based in characters; step over `col - 1` of them.
        let mut offset = start;
        for ch in line_text.chars().take(col - 1) {
            if ch == '\n' {
                return None;
            }
            offset += ch.len_utf8();
        }
        Some(offset)
    }
}