map

Macro map 

Source
macro_rules! map {
    ($($parser:expr => $value:expr),*) => { ... };
}
Expand description

A convenience macro for mapping multiple parsers to specific values.

This macro creates a new parser that tries each provided parser in sequence. If a parser succeeds, it returns the corresponding value. This is a concise alternative to chaining multiple or calls with map.

§Syntax

map!(parser1 => value1, parser2 => value2, ...)

§Example

use dlexer::lex::symbol;
use dlexer::map;
use dlexer::parsec::*;

#[derive(Debug, PartialEq)]
enum Keyword {
    Let,
    If,
    Else,
}

let keyword_parser = map!(
    symbol("let") => Keyword::Let,
    symbol("if") => Keyword::If,
    symbol("else") => Keyword::Else
);

assert_eq!(keyword_parser.test("if").unwrap(), Keyword::If);
assert!(keyword_parser.test("other").is_err());