garden-lang-parser 0.19.0

A live programming language.
Documentation
use std::{path::PathBuf, rc::Rc};

use serde::{Deserialize, Serialize};

use crate::lex::Token;

/// A position is a range in source code. It is a span between
/// `start_offset` and `end_offset` in `path`.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
pub struct Position {
    /// The start of this position, relative to the start of the
    /// file. Measured in bytes.
    pub start_offset: usize,
    /// The end of this position, relative to the start of the
    /// file. Measured in bytes.
    pub end_offset: usize,
    // TODO: Use LineNumber instead, finding a way to serialize it.
    pub line_number: usize,
    pub end_line_number: usize,
    /// The column of the start of this position. Zero indexed.
    pub column: usize,
    pub end_column: usize,
    pub path: Rc<PathBuf>,
}

impl std::fmt::Debug for Position {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if std::env::var("VERBOSE").is_ok() {
            f.debug_struct("Position")
                .field("start_offset", &self.start_offset)
                .field("end_offset", &self.end_offset)
                .field("line_number", &self.line_number)
                .field("end_line_number", &self.end_line_number)
                .field("path", &self.path)
                .finish()
        } else {
            f.write_str("Position { ... }")
        }
    }
}

impl Position {
    pub fn todo(path: Rc<PathBuf>) -> Self {
        Self {
            start_offset: 0,
            end_offset: 0,
            line_number: 0,
            column: 0,
            end_column: 0,
            end_line_number: 0,
            path,
        }
    }

    /// Return the merged position of `first` and `second`.
    pub fn merge(first: &Self, second: &Self) -> Self {
        Self {
            start_offset: first.start_offset,
            end_offset: std::cmp::max(first.end_offset, second.end_offset),
            line_number: first.line_number,
            end_line_number: std::cmp::max(first.end_line_number, second.end_line_number),
            column: first.column,
            end_column: if first.end_offset > second.end_offset {
                first.end_column
            } else {
                second.end_column
            },
            path: first.path.clone(),
        }
    }

    /// Merge the position of this token (including doc comments) with
    /// the second position.
    pub fn merge_token(first: &Token, second: &Self) -> Self {
        let mut first_pos = first.position.clone();
        if let Some((comment_pos, _)) = first.preceding_comments.first() {
            first_pos = comment_pos.clone();
        }

        Self::merge(&first_pos, second)
    }

    /// Format this position as a human-friendly string, e.g. "foo.gdn:123".
    pub fn as_ide_string(&self) -> String {
        format!(
            "{}:{}",
            self.path.display(),
            // 1-indexed line number
            self.line_number + 1,
        )
    }

    pub fn contains_offset(&self, offset: usize) -> bool {
        self.start_offset <= offset && offset <= self.end_offset
    }
}