blade-syntax 0.1.0

Lossless parser for Laravel Blade templates
Documentation
use blade_syntax::{debug::sexp, parse};

fn tree(t: &str) -> String {
    sexp(&parse(t).syntax())
}

fn errors(t: &str) -> Vec<String> {
    parse(t)
        .errors()
        .iter()
        .map(|e| e.message.clone())
        .collect()
}

#[test]
fn parses_echo() {
    assert_eq!(tree("a{{ $x }}"), r#"(ROOT "a" (ECHO "{{" " $x " "}}"))"#);
    assert!(errors("a{{ $x }}").is_empty());
}

#[test]
fn parses_directive_with_args() {
    assert_eq!(
        tree("@include('x')"),
        r#"(ROOT (DIRECTIVE "@include" "('x')"))"#
    );
}

#[test]
fn parses_verbatim() {
    assert_eq!(
        tree("@verbatim{{ x }}@endverbatim"),
        r#"(ROOT (VERBATIM (DIRECTIVE "@verbatim") "{{ x }}" (DIRECTIVE "@endverbatim")))"#
    );
}

#[test]
fn reports_unclosed_echo() {
    assert_eq!(errors("{{ $x"), ["unclosed echo: expected }}"]);
}

#[test]
fn reports_unclosed_args() {
    assert_eq!(errors("@include($a"), ["unclosed ( in directive arguments"]);
}

#[test]
fn round_trips_non_ascii_crlf_and_bom() {
    let t = "\u{feff}héllo\r\n{{ $ü }}\r\n👋 @if(\"ü\")\r\n";
    assert_eq!(parse(t).syntax().to_string(), t);
}

#[test]
fn debug_tree_is_indented() {
    let root = parse("{{ $x }}").syntax();
    assert_eq!(
        blade_syntax::debug::tree(&root),
        "ROOT@0..8\n  ECHO@0..8\n    ECHO_OPEN@0..2 \"{{\"\n    PHP@2..6 \" $x \"\n    ECHO_CLOSE@6..8 \"}}\"\n"
    );
}

#[test]
fn triple_echo_is_an_echo() {
    assert_eq!(tree("{{{ $x }}}"), r#"(ROOT (ECHO "{{{" " $x " "}}}"))"#);
    assert!(errors("{{{ $x }}}").is_empty());
}