Coder Social home page Coder Social logo

comprs's Introduction

A WIP for a Rust implementation of parser combinators. With this library, one can build up a complex parser from primitive parsers such as "get one token" (the One parser), or compose parsers together such as "if this parser fails, try that parser" (the Or combinator), etc. The mental model is a binary tree of execution nodes through which a sequence of input tokens flows.

The main goal of this library is to supports memoization and curtailment as described in [Frost2007]. This allows a memoized parser to achieve asymptotically best performance, and support ambiguous grammars.

The seccondary goal (WIP) is to experiment with declaratively building parsers.

// expression := expression "+" integer |
//                              integer
// integer    := [0-9]*
#[derive(Clone, PartialEq, Debug)]
enum Exp {
    Plus(Box<Exp>, Box<Exp>),
    // Minus(Box<Exp>, Box<Exp>),
    Number(i32),
}
// let number = many()

static PLUS_EXP_ID: usize = 0;

fn parse_number(input: &str, ctx: ParseContext) -> ParseResult<Exp> {
    // integer    := [0-9]*
    map(predicate(|c| c.is_digit(10)), |s: String| {
        // does not account for numbers starting with 0
        Exp::Number(s.parse::<i32>().unwrap())
    })
    .parse(input, ctx)
}
fn parse_exp(input: &str, ctx: ParseContext) -> ParseResult<Exp> {
    // expression := expression "+" integer |
    //                              integer
    or_rec(
        PLUS_EXP_ID,
        map(
            and(
                // expression "+" integer
                map(and(parse_exp, terminal("+")), |(e, _)| e), // expression "+"
                parse_number,
            ),
            |(e, number)| Exp::Plus(Box::new(e), Box::new(number)),
        ),
        parse_number,
    )
    .parse(input, ctx)
}

assert_eq!(
    Ok(("", Exp::Number(234))),
    parse_exp("234", new_parse_context())
);
assert_eq!(
    Ok((
        "",
        Exp::Plus(
            Box::new(Exp::Plus(
                Box::new(Exp::Number(1)),
                Box::new(Exp::Number(2))
            )),
            Box::new(Exp::Number(3)),
        )
    )),
    parse_exp("1+2+3", new_parse_context())
);

References

Frost2007

Frost R.A., Hafiz R., Callaghan P. (2007) "Parser Combinators for Ambiguous Left-Recursive Grammars". In: Hudak P., Warren D.S. (eds) "Practical Aspects of Declarative Languages". PADL 2008. Lecture Notes in Computer Science, vol 4902. Springer, Berlin, Heidelberg

comprs's People

Watchers

Adrian Tofan 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.