#syn #jsx #rsx #html #macro

syn-rsx

syn-powered parser for JSX-like TokenStreams

18 releases (7 breaking)

0.8.0 Feb 17, 2021
0.7.3 Oct 30, 2020
0.6.1 Jun 6, 2020

#56 in Procedural macros

Download history 91/week @ 2021-06-26 206/week @ 2021-07-03 315/week @ 2021-07-10 356/week @ 2021-07-17 263/week @ 2021-07-24 351/week @ 2021-07-31 356/week @ 2021-08-07 529/week @ 2021-08-14 352/week @ 2021-08-21 291/week @ 2021-08-28 157/week @ 2021-09-04 100/week @ 2021-09-11 120/week @ 2021-09-18 99/week @ 2021-09-25 121/week @ 2021-10-02 123/week @ 2021-10-09

938 downloads per month
Used in 15 crates (6 directly)

MIT license

36KB
636 lines

syn-rsx

crates.io page docs.rs page codecov build license: MIT

syn-powered parser for JSX-like TokenStreams, aka RSX. The parsed result is a nested Node structure, similar to the browser DOM, where node name and value are syn expressions to support building proc macros.

use quote::quote;
use syn_rsx::parse2;

let tokens = quote! { <hello world>"hi"</hello> };

let nodes = parse2(tokens).unwrap();
assert_eq!(nodes[0].name_as_string().unwrap(), "hello");
assert_eq!(nodes[0].attributes[0].name_as_string().unwrap(), "world");
assert_eq!(nodes[0].children[0].value_as_string().unwrap(), "hi");

Features

  • Not opinionated

    Every tag or attribute name is valid

    <hello world />
    
  • Text nodes

    <div>"String literal"</div>
    

    Support for unquoted text is planned

  • Node names separated by dash, colon or double colon

    <tag-name attribute-key="value" />
    <tag:name attribute:key="value" />
    <tag::name attribute::key="value" />
    
  • Node names with reserved keywords

    <input type="submit" />
    
  • Attribute values can be any valid syn expression without requiring braces

    <div key=some::value() />
    
  • Doctypes, Comments and Fragments

    <!DOCTYPE html>
    <!-- "comment" -->
    <></>
    
  • Braced blocks are parsed as arbitrary Rust code

    <{ let block = "in node name position"; } />
    <div>{ let block = "in node position"; }</div>
    <div { let block = "in attribute position"; } />
    <div key={ let block = "in attribute value position"; } />
    
  • Helpful error reporting out of the box

    error: open tag has no corresponding close tag and is not self-closing
    --> examples/html-to-string-macro/tests/lib.rs:5:24
      |
    5 |     html_to_string! { <div> };
      |                        ^^^
    
  • Customization

    A ParserConfig to customize parsing behavior is available, so if you have slightly different requirements for parsing and it's not yet customizable feel free to open an issue or pull request to extend the configuration.

    One highlight with regards to customization is the transform_block configuration, which takes a closure that receives raw block content as ParseStream and lets you optionally convert it to a TokenStream. That makes it possible to have custom syntax in blocks. More details in #9

Dependencies

~310–730KB
~18K SLoC