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 crate::interning::{Interned, Interner};
use crate::latin_1::Latin1Str;
use crate::tokens::{TokenKind, Trivia};
use std::fmt::Debug;
use std::io::{self, Write};
use std::sync::RwLock;

static STR_INTERNER: RwLock<Interner<Latin1Str>> = RwLock::new(Interner::new());

/// A source-code token.
#[derive(Clone, Eq, PartialEq)]
pub struct Token {
    pub(crate) leading_trivia: Trivia,
    kind: TokenKind,
    text: Interned<Latin1Str>,
}

impl Debug for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Token")
            .field("leading_trivia", &self.leading_trivia)
            .field("kind", &self.kind)
            .field("text", &self.text())
            .finish()
    }
}

impl Token {
    pub fn new(kind: TokenKind, text: impl AsRef<Latin1Str>, leading_trivia: Trivia) -> Token {
        Token {
            leading_trivia,
            kind,
            text: Interned::get(&STR_INTERNER, text.as_ref()),
        }
    }

    pub(crate) fn eof(leading_trivia: Trivia) -> Token {
        Token::new(TokenKind::Eof, b"", leading_trivia)
    }

    #[cfg(test)]
    pub fn simple(kind: TokenKind, text: impl AsRef<Latin1Str>) -> Token {
        Token::new(kind, text, Trivia::default())
    }

    pub fn kind(&self) -> TokenKind {
        self.kind
    }

    pub fn leading_trivia(&self) -> &Trivia {
        &self.leading_trivia
    }

    pub fn set_leading_trivia(&mut self, trivia: Trivia) {
        self.leading_trivia = trivia;
    }

    pub fn text(&self) -> &Latin1Str {
        self.text.value(&STR_INTERNER)
    }

    /// The length of the main content of this token in bytes without any trivia
    pub fn text_len(&self) -> usize {
        self.text().len()
    }

    /// The length of this token including trivia
    pub fn byte_len(&self) -> usize {
        self.leading_trivia.byte_len() + self.text_len()
    }

    pub fn write_to(&self, writer: &mut impl Write) -> io::Result<()> {
        self.leading_trivia().write_to(writer)?;
        writer.write_all(self.text().as_bytes())?;
        Ok(())
    }
}