vhdl_syntax 0.2.0

VHDL parser for lossless concrete syntax trees
Documentation
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c)  2024, Lukas Scheller lukasscheller@icloud.com

use std::io::{self, Write};

use crate::tokens::comment::Comment;

/// Single trivia pieces that can be combined to form [Trivia](crate::tokens::Trivia) tokens.
#[derive(Clone, Eq, PartialEq, Debug)]
// ANCHOR: trivia-piece
pub enum TriviaPiece {
    /// Horizontal tab '\t' characters
    HorizontalTabs(usize),
    /// Vertical tab '\v' characters
    VerticalTabs(usize),
    /// Carriage return '\r' characters
    CarriageReturns(usize),
    /// Carriage return + line feed ("\r\n") pairs
    CarriageReturnLineFeeds(usize),
    /// newline '\n' characters
    LineFeeds(usize),
    /// form feed '\f' characters
    FormFeeds(usize),
    /// A line comment starting with a '--'
    LineComment(Comment),
    /// A block comment starting with a '/*' and ending with a '*/'
    BlockComment(Comment),
    /// Space ' ' characters
    Spaces(usize),
    /// Non breaking space characters
    NonBreakingSpaces(usize),
}
// ANCHOR_END: trivia-piece

impl TriviaPiece {
    /// Returns the length of this trivia piece.
    pub fn byte_len(&self) -> usize {
        use TriviaPiece::*;
        match self {
            HorizontalTabs(n) | VerticalTabs(n) | CarriageReturns(n) | LineFeeds(n)
            | FormFeeds(n) | Spaces(n) | NonBreakingSpaces(n) => *n,
            CarriageReturnLineFeeds(n) => *n * 2,
            LineComment(str) | BlockComment(str) => str.byte_len(),
        }
    }

    /// Returns `true` when this trivia piece represents a whitespace, newline or tab
    pub fn is_whitespace(&self) -> bool {
        self.is_newline() || self.is_space_or_tab()
    }

    /// Returns if this trivia represents a newline.
    pub fn is_newline(&self) -> bool {
        use TriviaPiece::*;
        matches!(
            self,
            CarriageReturns(_)
                | LineFeeds(_)
                | FormFeeds(_)
                | CarriageReturnLineFeeds(_)
                | VerticalTabs(_)
        )
    }

    /// Returns if this piece is a space or tab symbol, excluding vertical tabs
    pub fn is_space_or_tab(&self) -> bool {
        use TriviaPiece::*;
        matches!(self, Spaces(_) | HorizontalTabs(_) | NonBreakingSpaces(_))
    }

    /// Returns if this trivia piece is a block or line comment.
    pub fn is_comment(&self) -> bool {
        use TriviaPiece::*;
        matches!(self, BlockComment(_) | LineComment(_))
    }

    pub fn write_to(&self, writer: &mut impl Write) -> io::Result<()> {
        fn write_repeated(writer: &mut impl Write, el: &[u8], count: usize) -> io::Result<()> {
            for _ in 0..count {
                writer.write_all(el)?;
            }
            Ok(())
        }
        use TriviaPiece::*;
        match self {
            HorizontalTabs(n) => write_repeated(writer, b"\t", *n),
            VerticalTabs(n) => write_repeated(writer, &[0x0Bu8], *n),
            CarriageReturns(n) => write_repeated(writer, b"\r", *n),
            CarriageReturnLineFeeds(n) => write_repeated(writer, b"\r\n", *n),
            LineFeeds(n) => write_repeated(writer, b"\n", *n),
            FormFeeds(n) => write_repeated(writer, &[0x0Cu8], *n),
            LineComment(comment) | BlockComment(comment) => writer.write_all(comment.as_bytes()),
            Spaces(n) => write_repeated(writer, b" ", *n),
            NonBreakingSpaces(n) => write_repeated(writer, &[0xA0u8], *n),
        }
    }
}