blade-syntax 0.1.0

Lossless parser for Laravel Blade templates
Documentation
//! Every kind of token and node in a Blade syntax tree.

/// Token kinds come first, then node kinds. The `u16` value is what rowan stores.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u16)]
#[allow(non_camel_case_types)]
pub enum SyntaxKind {
    // Tokens
    /// Anything Blade doesn't treat specially (HTML, CSS, JS, plain text).
    TEXT,
    /// Spaces or tabs between a directive name and its arguments, or any run of
    /// whitespace inside an HTML tag.
    WHITESPACE,
    /// `{{`
    ECHO_OPEN,
    /// `}}`
    ECHO_CLOSE,
    /// `{!!`
    RAW_ECHO_OPEN,
    /// `!!}`
    RAW_ECHO_CLOSE,
    /// Embedded PHP, kept opaque: echo contents, `@php` bodies, `<?php ?>` bodies.
    PHP,
    /// A whole Blade comment, `{{-- ... --}}`.
    COMMENT,
    /// Output Blade prints literally: `@{{ ... }}`, `@{!! ... !!}`, `@@name`.
    ESCAPED,
    /// A directive name including its `@`, e.g. `@if`.
    DIRECTIVE_NAME,
    /// Directive arguments including the parentheses, e.g. `($user)`.
    DIRECTIVE_ARGS,
    /// `<?php` or `<?=`
    PHP_OPEN,
    /// `?>`
    PHP_CLOSE,
    /// `<` starting a start tag.
    TAG_OPEN,
    /// `</` starting an end tag.
    END_TAG_OPEN,
    /// A tag name, e.g. `div`, `x-slot:title`, `livewire:counter`.
    TAG_NAME,
    /// `>` ending a tag.
    TAG_CLOSE,
    /// `/>` ending a self-closing tag.
    TAG_SELF_CLOSE,
    /// An attribute name, e.g. `class`, `:value`, `wire:model.live`, `@click`.
    ATTR_NAME,
    /// `=` between an attribute name and its value.
    EQ,
    /// `"` or `'` around an attribute value.
    QUOTE,
    /// `<!--`
    HTML_COMMENT_OPEN,
    /// `-->`
    HTML_COMMENT_CLOSE,
    /// A whole `<!DOCTYPE ...>`.
    DOCTYPE,

    // Nodes
    ROOT,
    ECHO,
    RAW_ECHO,
    DIRECTIVE,
    BLOCK,
    BODY,
    VERBATIM,
    PHP_BLOCK,
    PHP_TAG,
    /// A start tag, `<div class="a">` or `<br/>`. Not a whole element: Blade
    /// directives may open and close elements in different branches.
    TAG,
    /// An end tag, `</div>`.
    END_TAG,
    /// `name`, `name=value` or `name="value"` inside a tag.
    ATTRIBUTE,
    /// An attribute value, with its quotes if it has them.
    ATTR_VALUE,
    ERROR,
}

use SyntaxKind::*;

const ALL: [SyntaxKind; 38] = [
    TEXT,
    WHITESPACE,
    ECHO_OPEN,
    ECHO_CLOSE,
    RAW_ECHO_OPEN,
    RAW_ECHO_CLOSE,
    PHP,
    COMMENT,
    ESCAPED,
    DIRECTIVE_NAME,
    DIRECTIVE_ARGS,
    PHP_OPEN,
    PHP_CLOSE,
    TAG_OPEN,
    END_TAG_OPEN,
    TAG_NAME,
    TAG_CLOSE,
    TAG_SELF_CLOSE,
    ATTR_NAME,
    EQ,
    QUOTE,
    HTML_COMMENT_OPEN,
    HTML_COMMENT_CLOSE,
    DOCTYPE,
    ROOT,
    ECHO,
    RAW_ECHO,
    DIRECTIVE,
    BLOCK,
    BODY,
    VERBATIM,
    PHP_BLOCK,
    PHP_TAG,
    TAG,
    END_TAG,
    ATTRIBUTE,
    ATTR_VALUE,
    ERROR,
];

impl From<SyntaxKind> for rowan::SyntaxKind {
    fn from(kind: SyntaxKind) -> Self {
        Self(kind as u16)
    }
}

/// Marker type that ties rowan's generic tree to [`SyntaxKind`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum BladeLanguage {}

impl rowan::Language for BladeLanguage {
    type Kind = SyntaxKind;

    fn kind_from_raw(raw: rowan::SyntaxKind) -> SyntaxKind {
        ALL[usize::from(raw.0)]
    }

    fn kind_to_raw(kind: SyntaxKind) -> rowan::SyntaxKind {
        kind.into()
    }
}

pub type SyntaxNode = rowan::SyntaxNode<BladeLanguage>;
pub type SyntaxToken = rowan::SyntaxToken<BladeLanguage>;

#[cfg(test)]
mod tests {
    use super::*;
    use rowan::Language;

    #[test]
    fn every_kind_survives_the_raw_round_trip() {
        for (i, kind) in ALL.iter().enumerate() {
            assert_eq!(*kind as usize, i);
            assert_eq!(
                BladeLanguage::kind_from_raw(BladeLanguage::kind_to_raw(*kind)),
                *kind
            );
        }
        assert_eq!(ALL.len(), ERROR as usize + 1);
    }
}