Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the parsing macros #87

Closed
26 tasks done
dtolnay opened this issue Jan 28, 2017 · 1 comment
Closed
26 tasks done

Document the parsing macros #87

dtolnay opened this issue Jan 28, 2017 · 1 comment
Labels

Comments

@dtolnay
Copy link
Owner

dtolnay commented Jan 28, 2017

Before including this in a release, I would like to document all of the parsers with realistic procedural-macro-appropriate examples. @mystor would you be able to help me work through these?

  • alt
  • call
  • cond
  • cond_reduce
  • delimited
  • do_parse
  • epsilon
  • keyword
  • many0
  • map
  • named
  • not
  • opt_vec
  • option
  • peek
  • preceded
  • punct
  • separated_list
  • separated_nonempty_list
  • switch
  • tag
  • take_until
  • terminated
  • terminated_list
  • tuple
  • value

I started you off with two examples of what I have in mind but feel free to invent your own approach if you want.

One or more of something separated by some separator.

  • Syntax: separated_nonempty_list!(SEPARATOR, THING)
  • Output: Vec<THING>
extern crate syn;
#[macro_use] extern crate synom;

use syn::Ty;
use syn::parse::ty;

// One or more Rust types separated by commas.
named!(comma_separated_types -> Vec<Ty>,
    separated_nonempty_list!(
        punct!(","),
        ty));

fn main() {
    let input = "&str, Map<K, V>, String";

    let parsed = comma_separated_types(input).expect("comma-separated types");

    assert_eq!(parsed.len(), 3);
    println!("{:?}", parsed);
}

Value surrounded by a pair of delimiters.

  • Syntax: delimited!(OPEN, THING, CLOSE)
  • Output: THING
extern crate syn;
#[macro_use] extern crate synom;

use syn::Expr;
use syn::parse::expr;

// An expression surrounded by [[ ... ]].
named!(double_bracket_expr -> Expr,
    delimited!(
        punct!("[["),
        expr,
        punct!("]]")));

fn main() {
    let input = "[[ 1 + 1 ]]";

    let parsed = double_bracket_expr(input).expect("double bracket expr");

    println!("{:?}", parsed);
}
@dtolnay
Copy link
Owner Author

dtolnay commented Feb 8, 2017

Fixed in #91 and #93.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant