GithubHelp home page GithubHelp logo

ryman / syn-rsx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stoically/syn-rsx

0.0 1.0 0.0 231 KB

syn-powered parser for JSX-like TokenStreams

Home Page: https://crates.io/crates/syn-rsx

License: MIT License

Rust 100.00%

syn-rsx's Introduction

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 std::convert::TryFrom;

use eyre::bail;
use quote::quote;
use syn_rsx::{parse2, Node, NodeAttribute, NodeElement, NodeText};

// Create HTML `TokenStream`.
let tokens = quote! { <hello world>"hi"</hello> };

// Parse the tokens into a tree of `Node`s.
let nodes = parse2(tokens)?;

// Extract some specific nodes from the tree.
let Node::Element(element) = &nodes[0] else { bail!("element") };
let Node::Attribute(attribute) = &element.attributes[0] else { bail!("attribute") };
let Node::Text(text) = &element.children[0] else { bail!("text") };

// Work with the nodes.
assert_eq!(element.name.to_string(), "hello");
assert_eq!(attribute.key.to_string(), "world");
assert_eq!(String::try_from(&text.value)?, "hi");

You might want to check out the html-to-string-macro example as well.

Features

  • Not opinionated

    Every tag or attribute name is valid

    <hello world />
  • Text nodes

    Support for unquoted text is planned.

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

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

    <input type="submit" />
  • 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"; } />
  • Attribute values can be any valid syn expression without requiring braces

    <div key=some::value() />
  • 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> };
      |                        ^^^
  • Possibility to get the span for a whole node

    This can be used to improve error reporting, e.g.

    error: Invalid element
    --> examples/src/main.rs:14:13
       |
    14 | /             <div>
    15 | |                 "invalid node for some consumer specific reason"
    16 | |             </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

syn-rsx's People

Contributors

stoically avatar gbj avatar danheuck avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.