#parser #combinators #ll #language #combine

combine-language

Extra parser combinators, useful for parsing programming languages

14 releases (5 stable)

3.0.2 Mar 21, 2019
3.0.1 Aug 23, 2018
2.0.0 Oct 20, 2016
2.0.0-beta Jun 6, 2016
0.5.0 Jul 17, 2015

#3 in #ll

Download history 15/week @ 2019-12-01 195/week @ 2019-12-08 17/week @ 2019-12-22 2/week @ 2019-12-29 1/week @ 2020-01-05 98/week @ 2020-01-12 46/week @ 2020-01-19 2/week @ 2020-01-26 1/week @ 2020-02-02 16/week @ 2020-02-09 23/week @ 2020-02-16 100/week @ 2020-02-23 5/week @ 2020-03-01 33/week @ 2020-03-08 19/week @ 2020-03-15

175 downloads per month

MIT license

43KB
908 lines

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, Parser};
use combine::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

Dependencies

~1MB
~18K SLoC