[][src]Crate syn_rsx

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

    Support for unquoted text is planned (currently requires Rust nightly)

    <div>"String literal"</div>
  • Node names separated by dash or colon

    <tag-name attribute-key="value" />
    <tag:name attribute:key="value" />
  • Node names as mod style path

    <tag::path attribute::path="value" />
  • Node names with reserved keywords

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

    <div key=some::value() />
  • Braced blocks are parsed as arbitrary Rust code

    <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

    This example is not tested
    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.

Modules

punctuation

Custom syn punctuations

Structs

Node

Node in the tree

Parser

RSX Parser

ParserConfig

Configures the Parser behavior

Enums

NodeName

Name of the node

NodeType

Type of the node

Functions

parse

Parse the given proc-macro::TokenStream into a Node tree

parse2

Parse the given proc-macro2::TokenStream into a Node tree

parse2_with_config

Parse the given proc-macro2::TokenStream into a Node tree with custom ParserConfig

parse_with_config

Parse the given proc-macro::TokenStream into a Node tree with custom ParserConfig