combine-language

Crates.iocombine-language
lib.rscombine-language
version4.0.0
sourcesrc
created_at2015-07-17 14:32:56.372325
updated_at2020-12-14 18:01:56.787638
descriptionExtra parser combinators, useful for parsing programming languages.
homepage
repositoryhttps://github.com/Marwes/combine-language
max_upload_size
id2625
size44,905
Markus Westerlind (Marwes)

documentation

https://marwes.github.io/combine-language/combine_language/index.html

README

combine-language

Build Status Docs v2 Docs Gitter

This a crate providing an easy way of constructing parsers which can easily parse various programming languages. It has much of the same API as Text.Parsec.Token but are otherwise a bit different to fit in to the ownership model of rust. The crate is an extension of the combine crate.

Example

extern crate combine;
extern crate combine_language;
use combine::{satisfy, EasyParser, Parser};
use combine::parser::char::{alpha_num, letter, string};
use combine_language::{Identifier, LanguageEnv, LanguageDef};
fn main() {
    let env = LanguageEnv::new(LanguageDef {
        ident: Identifier {
            start: letter(),
            rest: alpha_num(),
            reserved: ["if", "then", "else", "let", "in", "type"].iter()
                                                                 .map(|x| (*x).into())
                                                                 .collect(),
        },
        op: Identifier {
            start: satisfy(|c| "+-*/".chars().any(|x| x == c)),
            rest: satisfy(|c| "+-*/".chars().any(|x| x == c)),
            reserved: ["+", "-", "*", "/"].iter().map(|x| (*x).into()).collect()
        },
        comment_start: string("/*").map(|_| ()),
        comment_end: string("*/").map(|_| ()),
        comment_line: string("//").map(|_| ()),
    });
    let id = env.identifier();//An identifier parser
    let integer = env.integer();//An integer parser
    let result = (id, integer).easy_parse("this /* Skips comments */ 42");
    assert_eq!(result, Ok(((String::from("this"), 42), "")));
}

Links

combine

Commit count: 80

cargo fmt